def test_format(self): self.assertEqual(PolygonLike.format(None), '') coords = [(10.4, 20.2), (30.8, 20.2), (30.8, 40.8), (10.4, 40.8)] pol = PolygonLike.convert(coords) self.assertEqual( PolygonLike.format(pol), 'POLYGON ((10.4 20.2, 30.8 20.2, 30.8 40.8, 10.4 40.8, 10.4 20.2))' )
def data_frame_subset(gdf: gpd.GeoDataFrame, region_op: bool = 'intersects', region: PolygonLike.TYPE = None, var_names: VarNamesLike.TYPE = None) -> gpd.GeoDataFrame: """ Create a GeoDataFrame subset from given variables (data frame columns) and/or region. :param gdf: A GeoDataFrame. :param region_op: The geometric operation to be performed if *region* is given. :param region: A region polygon used to filter rows. :param var_names: The variables (columns) to select. :return: A GeoDataFrame subset. """ region = PolygonLike.convert(region) var_names = VarNamesLike.convert(var_names) if not var_names and not region: return gdf if var_names: if 'geometry' not in var_names: var_names = ['geometry'] + var_names gdf = gdf[var_names] if region and region_op: geom_str = PolygonLike.format(region) gdf = data_frame_query(gdf, f'@{region_op}("{geom_str}")') return gdf
def data_frame_subset(gdf: gpd.GeoDataFrame, region_op: bool = 'intersects', region: PolygonLike.TYPE = None, var_names: VarNamesLike.TYPE = None) -> gpd.GeoDataFrame: """ Create a GeoDataFrame subset from given variables (data frame columns) and/or region. :param gdf: A GeoDataFrame. :param region_op: The geometric operation to be performed if *region* is given. :param region: A region polygon used to filter rows. :param var_names: The variables (columns) to select. :return: A GeoDataFrame subset. """ region = PolygonLike.convert(region) var_names = VarNamesLike.convert(var_names) if not var_names and not region: return gdf if var_names: if 'geometry' not in var_names: var_names = ['geometry'] + var_names gdf = gdf[var_names] if region and region_op: geom_str = PolygonLike.format(region) gdf = data_frame_query(gdf, f'@{region_op}("{geom_str}")') return gdf
def to_json_dict(self): """ Return a JSON-serializable dictionary representation of this object. :return: A JSON-serializable dictionary """ config = OrderedDict({ 'name': self._name, 'meta_data': { 'temporal_coverage': TimeRangeLike.format(self._temporal_coverage) if self._temporal_coverage else None, 'spatial_coverage': PolygonLike.format(self._spatial_coverage) if self._spatial_coverage else None, 'variables': VarNamesLike.format(self._variables) if self._variables else None, 'reference_type': self._reference_type, 'reference_name': self._reference_name }, 'files': [[item[0], item[1][0], item[1][1]] if item[1] else [item[0]] for item in self._files.items()] }) return config
def generate_title(cls, title: str, time_range: Optional[TimeRange] = None, region: Optional[shapely.geometry.Polygon] = None, var_names: Optional[VarNames] = None) -> str: if time_range: title += " [TimeRange:{}]".format(TimeRangeLike.format(time_range)) if region: title += " [Region:{}]".format(PolygonLike.format(region)) if var_names: title += " [Variables:{}]".format(VarNamesLike.format(var_names)) return title
def generate_uuid(cls, ref_id: str, time_range: Optional[TimeRange] = None, region: Optional[shapely.geometry.Polygon] = None, var_names: Optional[VarNames] = None) -> str: if time_range: ref_id += TimeRangeLike.format(time_range) if region: ref_id += PolygonLike.format(region) if var_names: ref_id += VarNamesLike.format(var_names) return str(uuid.uuid3(_NAMESPACE, ref_id))
def generate_title(cls, title: str, time_range: Optional[TimeRange] = None, region: Optional[shapely.geometry.Polygon] = None, var_names: Optional[VarNames] = None) -> str: if time_range: title += " [TimeRange:{}]".format(TimeRangeLike.format(time_range)) if region: title += " [Region:{}]".format(PolygonLike.format(region)) if var_names: title += " [Variables:{}]".format(VarNamesLike.format(var_names)) return title
def generate_uuid(cls, ref_id: str, time_range: Optional[TimeRange] = None, region: Optional[shapely.geometry.Polygon] = None, var_names: Optional[VarNames] = None) -> str: if time_range: ref_id += TimeRangeLike.format(time_range) if region: ref_id += PolygonLike.format(region) if var_names: ref_id += VarNamesLike.format(var_names) return str(uuid.uuid3(_NAMESPACE, ref_id))
def enso(ds: xr.Dataset, var: VarName.TYPE, file: str, region: str = 'n34', custom_region: PolygonLike.TYPE = None, threshold: float = None, monitor: Monitor = Monitor.NONE) -> pd.DataFrame: """ Calculate ENSO index, which is defined as a five month running mean of anomalies of monthly means of SST data in the given region. :param ds: A monthly SST dataset :param file: Path to the reference data file e.g. a climatology. A suitable reference dataset can be generated using the long_term_average operation :param var: Dataset variable to use for index calculation :param region: Region for index calculation, the default is Nino3.4 :param custom_region: If 'custom' is chosen as the 'region', this parameter has to be provided to set the desired region. :param threshold: If given, boolean El Nino/La Nina timeseries will be calculated and added to the output dataset, according to the given threshold. Where anomaly larger than then positive value of the threshold indicates El Nino and anomaly smaller than the negative of the given threshold indicates La Nina. :param monitor: a progress monitor. :return: A dataset that contains the index timeseries. """ regions = { 'N1+2': '-90, -10, -80, 0', 'N3': '-150, -5, -90, 5', 'N3.4': '-170, -5, -120, 5', 'N4': '160, -5, -150, 5', 'custom': custom_region } converted_region = PolygonLike.convert(regions[region]) if not converted_region: raise ValidationError( 'No region has been provided to ENSO index calculation') name = 'ENSO ' + region + ' Index' if 'custom' == region: name = 'ENSO Index over ' + PolygonLike.format(converted_region) return _generic_index_calculation(ds, var, converted_region, 5, file, name, threshold, monitor)
def enso(ds: xr.Dataset, var: VarName.TYPE, file: str, region: str = 'n34', custom_region: PolygonLike.TYPE = None, threshold: float = None, monitor: Monitor = Monitor.NONE) -> pd.DataFrame: """ Calculate ENSO index, which is defined as a five month running mean of anomalies of monthly means of SST data in the given region. :param ds: A monthly SST dataset :param file: Path to the reference data file e.g. a climatology. A suitable reference dataset can be generated using the long_term_average operation :param var: Dataset variable to use for index calculation :param region: Region for index calculation, the default is Nino3.4 :param custom_region: If 'custom' is chosen as the 'region', this parameter has to be provided to set the desired region. :param threshold: If given, boolean El Nino/La Nina timeseries will be calculated and added to the output dataset, according to the given threshold. Where anomaly larger than then positive value of the threshold indicates El Nino and anomaly smaller than the negative of the given threshold indicates La Nina. :param monitor: a progress monitor. :return: A dataset that contains the index timeseries. """ regions = {'N1+2': '-90, -10, -80, 0', 'N3': '-150, -5, -90, 5', 'N3.4': '-170, -5, -120, 5', 'N4': '160, -5, -150, 5', 'custom': custom_region} converted_region = PolygonLike.convert(regions[region]) if not converted_region: raise ValidationError('No region has been provided to ENSO index calculation') name = 'ENSO ' + region + ' Index' if 'custom' == region: name = 'ENSO Index over ' + PolygonLike.format(converted_region) return _generic_index_calculation(ds, var, converted_region, 5, file, name, threshold, monitor)
def test_format(self): coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)] pol = PolygonLike.convert(coords) self.assertTrue( 'POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))' == PolygonLike.format(pol))
def test_format(self): self.assertEqual(PolygonLike.format(None), '') coords = [(10.4, 20.2), (30.8, 20.2), (30.8, 40.8), (10.4, 40.8)] pol = PolygonLike.convert(coords) self.assertEqual(PolygonLike.format(pol), 'POLYGON ((10.4 20.2, 30.8 20.2, 30.8 40.8, 10.4 40.8, 10.4 20.2))')