Source code for pelagos_py.steps.quality_control.position_on_land_qc

# This file is part of pelagos_py.
#
# Copyright 2025-2026 National Oceanography Centre and The Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""QC test that identifies glider positions not located on land and flags accordingly."""

#### Mandatory imports ####
from pelagos_py.steps.base_qc import BaseQC, register_qc

#### Custom imports ####
import matplotlib.pyplot as plt
import shapely as sh
import numpy as np
import xarray as xr
import matplotlib
from pelagos_py.utils import fig_spec


@register_qc
[docs] class position_on_land_qc(BaseQC): """ Target Variable: LATITUDE, LONGITUDE Flag Number: 4 (bad data) Variables Flagged: LATITUDE, LONGITUDE Flags positions on land, using the Natural Earth 1:110m land polygons. """ qc_name = "position on land qc" parameter_schema = {} required_variables = ["LATITUDE", "LONGITUDE"] qc_outputs = ["LATITUDE_QC", "LONGITUDE_QC"]
[docs] def return_qc(self): lon, lat = self.data["LONGITUDE"].values, self.data["LATITUDE"].values on_land = sh.contains_xy(fig_spec.land_polygons("110m"), lon, lat) qc = np.where( np.isnan(lon) | np.isnan(lat), 9, np.where(on_land, 4, self.pass_flag) ) # The same flags go on LATITUDE as well. self.flags = xr.Dataset( data_vars={ "LONGITUDE_QC": ("N_MEASUREMENTS", qc), "LATITUDE_QC": ("N_MEASUREMENTS", qc.copy()), }, coords={"N_MEASUREMENTS": self.data["N_MEASUREMENTS"]}, ) return self.flags
[docs] def plot_diagnostics(self): matplotlib.use("tkagg") lon, lat = self.data["LONGITUDE"].values, self.data["LATITUDE"].values flags = self.flags["LATITUDE_QC"].values bad = flags == 4 if not (np.isfinite(lon) & np.isfinite(lat)).any(): self.log_warn("No positions to plot.") return # left: regional context; right: zoomed on flagged positions, else on the track views = [("Region", fig_spec.map_extent(lon, lat, pad=2.0))] if bad.any(): views.append( ("Flagged positions", fig_spec.map_extent(lon[bad], lat[bad], pad=0.3)) ) else: views.append(("Track", fig_spec.map_extent(lon, lat, pad=0.05))) fig, axes = fig_spec.new_fig(1, 2) for ax, (title, extent) in zip(axes[0], views): fig_spec.flag_points(ax, lon, lat, flags) fig_spec.style_axes( ax, title=title, xlabel=fig_spec.axis_label( "LONGITUDE", self.data["LONGITUDE"].attrs.get("units") ), ylabel=fig_spec.axis_label( "LATITUDE", self.data["LATITUDE"].attrs.get("units") ), ) fig_spec.coastlines(ax, extent) fig_spec.legend(axes[0][1], title="Flags") fig_spec.finish(fig, suptitle="Position On Land Test") plt.show(block=True)