def test_system_grouped_dimension_map(self): initial_data = { DMK.X: { DMK.VARIABLE: 'longitude' }, DMK.GROUPS: { 'nested': { DMK.Y: { DMK.VARIABLE: 'latitude' }, DMK.GROUPS: { 'nested_in_nested': { DMK.CRS: { DMK.VARIABLE: 'coord_sys' } } } } } } dmap = DimensionMap.from_dict(initial_data) nested = dmap.get_group('nested') self.assertEqual(nested.get_variable(DMK.Y), 'latitude') self.assertIsNone(dmap.get_variable(DMK.Y)) self.assertEqual( dmap.get_group([None, 'nested', 'nested_in_nested']).get_crs(), 'coord_sys')
def create_dimension_map(self, group_metadata): ret = {DMK.GEOM: {DMK.VARIABLE: VariableName.GEOMETRY_VARIABLE, DMK.DIMENSION: DimensionName.GEOMETRY_DIMENSION}} ret = DimensionMap.from_dict(ret) crs = self.get_crs(group_metadata) if crs is not None: ret.set_crs(crs) return ret
def get_dimension_map(self, group_metadata, strict=False): # Get dimension variable metadata. This involves checking for the presence of any bounds variables. variables = group_metadata['variables'] dimensions = group_metadata['dimensions'] axes = { 'realization': 'R', 'time': 'T', 'level': 'Z', 'x': 'X', 'y': 'Y' } check_bounds = list(axes.keys()) check_bounds.pop(check_bounds.index('realization')) # Get the main entry for each axis. for k, v in list(axes.items()): axes[k] = get_dimension_map_entry(v, variables, dimensions, strict=strict) # Attempt to find bounds for each entry (ignoring realizations). for k in check_bounds: if axes[k] is not None: keys = ['bounds'] if k == 'time': keys += ['climatology'] bounds_var = get_by_key_list( variables[axes[k]['variable']]['attrs'], keys) if bounds_var is not None: if bounds_var not in variables: msg = 'Bounds listed for variable "{0}" but the destination bounds variable "{1}" does not exist.'. \ format(axes[k]['variable'], bounds_var) ocgis_lh(msg, logger='nc.driver', level=logging.WARNING) bounds_var = None axes[k]['bounds'] = bounds_var # Create the template dimension map dictionary. ret = {k: v for k, v in list(axes.items()) if v is not None} # Check for coordinate system variables. This will check every variable. crs_name = get_coordinate_system_variable_name(self, group_metadata) if crs_name is not None: ret[DimensionMapKey.CRS] = {DimensionMapKey.VARIABLE: crs_name} ret = DimensionMap.from_dict(ret) # Check for a spatial mask. for varname, var in group_metadata['variables'].items(): if 'ocgis_role' in var.get('attrs', {}): if var['attrs']['ocgis_role'] == 'spatial_mask': ret.set_spatial_mask(varname, attrs=var['attrs']) return ret
def test_system_grouped_dimension_map(self): initial_data = {DMK.X: {DMK.VARIABLE: 'longitude'}, DMK.GROUPS: {'nested': {DMK.Y: {DMK.VARIABLE: 'latitude'}, DMK.GROUPS: { 'nested_in_nested': {DMK.CRS: {DMK.VARIABLE: 'coord_sys'}}}}}} dmap = DimensionMap.from_dict(initial_data) nested = dmap.get_group('nested') self.assertEqual(nested.get_variable(DMK.Y), 'latitude') self.assertIsNone(dmap.get_variable(DMK.Y)) self.assertEqual(dmap.get_group([None, 'nested', 'nested_in_nested']).get_crs(), 'coord_sys')
def create_dimension_map(self, group_metadata): ret = { DMK.GEOM: { DMK.VARIABLE: VariableName.GEOMETRY_VARIABLE, DMK.DIMENSION: DimensionName.GEOMETRY_DIMENSION } } ret = DimensionMap.from_dict(ret) crs = self.get_crs(group_metadata) if crs is not None: ret.set_crs(crs) return ret
def test_get_data_variable_names(self): driver = self.get_drivernetcdf() dvars = driver.get_data_variable_names(driver.rd.metadata, driver.rd.dimension_map) self.assertEqual(len(dvars), 0) # Test a found variable. dimension_map = {'time': {'variable': 'the_time', DimensionMapKey.DIMENSION: ['tt', 'ttt']}, 'x': {'variable': 'xx', DimensionMapKey.DIMENSION: ['xx', 'xxx']}, 'y': {'variable': 'yy', DimensionMapKey.DIMENSION: ['yy', 'yyy']}} metadata = {'variables': {'tas': {'dimensions': ('xx', 'ttt', 'yyy')}, 'pr': {'dimensions': ('foo',)}}} dimension_map = DimensionMap.from_dict(dimension_map) dvars = driver.get_data_variable_names(metadata, dimension_map) self.assertEqual(dvars, ('tas',)) # Test request dataset uses the dimensioned variables. driver = self.get_drivernetcdf() with self.assertRaises(NoDataVariablesFound): assert driver.rd.variable
def test_get_data_variable_names(self): driver = self.get_drivernetcdf() dvars = driver.get_data_variable_names(driver.rd.metadata, driver.rd.dimension_map) self.assertEqual(len(dvars), 0) # Test a found variable. dimension_map = { 'time': { 'variable': 'the_time', DimensionMapKey.DIMENSION: ['tt', 'ttt'] }, 'x': { 'variable': 'xx', DimensionMapKey.DIMENSION: ['xx', 'xxx'] }, 'y': { 'variable': 'yy', DimensionMapKey.DIMENSION: ['yy', 'yyy'] } } metadata = { 'variables': { 'tas': { 'dimensions': ('xx', 'ttt', 'yyy') }, 'pr': { 'dimensions': ('foo', ) } } } dimension_map = DimensionMap.from_dict(dimension_map) dvars = driver.get_data_variable_names(metadata, dimension_map) self.assertEqual(dvars, ('tas', )) # Test request dataset uses the dimensioned variables. driver = self.get_drivernetcdf() with self.assertRaises(NoDataVariablesFound): assert driver.rd.variable
def __init__(self, uri=None, variable=None, units=None, time_range=None, time_region=None, time_subset_func=None, level_range=None, conform_units_to=None, crs='auto', t_units=None, t_calendar=None, t_conform_units_to=None, grid_abstraction='auto', grid_is_isomorphic='auto', dimension_map=None, field_name=None, driver=None, regrid_source=True, regrid_destination=False, metadata=None, format_time=True, opened=None, uid=None, rename_variable=None, predicate=None, rotated_pole_priority=False, driver_kwargs=None): self._is_init = True self._field_name = field_name self._level_range = None self._time_range = None self._time_region = None self._time_subset_func = None self._driver_kwargs = driver_kwargs if rename_variable is not None: rename_variable = get_tuple(rename_variable) self._rename_variable = rename_variable self.rotated_pole_priority = rotated_pole_priority self.predicate = predicate if dimension_map is not None and isinstance(dimension_map, dict): dimension_map = DimensionMap.from_dict(dimension_map) self._dimension_map = dimension_map self._metadata = deepcopy(metadata) self._uri = None self.uid = uid # This is an "open" file-like object that may be passed in-place of file location parameters. self._opened = opened if opened is not None and driver is None: msg = 'If "opened" is not None, then a "driver" must be provided.' ocgis_lh(logger='request', exc=RequestValidationError('driver', msg)) # Field creation options. self.format_time = format_time self.grid_abstraction = grid_abstraction self.grid_is_isomorphic = grid_is_isomorphic # Flag used for regridding to determine if the coordinate system was assigned during initialization. self._has_assigned_coordinate_system = False if crs == 'auto' else True if uri is None: # Fields may be created from pure metadata. if metadata is not None: # The default OCGIS driver is NetCDF. if driver is None: driver = DriverKey.NETCDF_CF elif opened is None: ocgis_lh(logger='request', exc=RequestValidationError('uri', 'Cannot be None')) else: self._uri = get_uri(uri) if driver is None: klass = get_autodiscovered_driver(uri) else: klass = get_driver(driver) self._driver = klass(self) if variable is not None: variable = get_tuple(variable) self._variable = variable self.time_range = time_range self.time_region = time_region self.time_subset_func = time_subset_func self.level_range = level_range self._crs = deepcopy(crs) self.regrid_source = regrid_source self.regrid_destination = regrid_destination self.units = units self.conform_units_to = conform_units_to self._is_init = False self._validate_time_subset_() # Update metadata for time variable. tvar = self.dimension_map.get_variable(DMK.TIME) if tvar is not None: m = self.metadata['variables'][tvar] if t_units is not None: m['attrs']['units'] = t_units if t_calendar is not None: m['attrs']['calendar'] = t_calendar if t_conform_units_to is not None: from ocgis.util.units import get_units_object t_calendar = m['attrs'].get( 'calendar', constants.DEFAULT_TEMPORAL_CALENDAR) t_conform_units_to = get_units_object(t_conform_units_to, calendar=t_calendar) m['conform_units_to'] = t_conform_units_to
def __init__(self, uri=None, variable=None, units=None, time_range=None, time_region=None, time_subset_func=None, level_range=None, conform_units_to=None, crs='auto', t_units=None, t_calendar=None, t_conform_units_to=None, grid_abstraction='auto', grid_is_isomorphic='auto', dimension_map=None, field_name=None, driver=None, regrid_source=True, regrid_destination=False, metadata=None, format_time=True, opened=None, uid=None, rename_variable=None, predicate=None, rotated_pole_priority=False, driver_kwargs=None, decomp_type=DecompositionType.OCGIS): self._is_init = True self._field_name = field_name self._level_range = None self._time_range = None self._time_region = None self._time_subset_func = None self._driver_kwargs = driver_kwargs self._decomp_type = decomp_type if rename_variable is not None: rename_variable = get_tuple(rename_variable) self._rename_variable = rename_variable self.rotated_pole_priority = rotated_pole_priority self.predicate = predicate if dimension_map is not None and isinstance(dimension_map, dict): dimension_map = DimensionMap.from_dict(dimension_map) self._dimension_map = dimension_map self._metadata = deepcopy(metadata) self._uri = None self.uid = uid # This is an "open" file-like object that may be passed in-place of file location parameters. self._opened = opened if opened is not None and driver is None: msg = 'If "opened" is not None, then a "driver" must be provided.' ocgis_lh(logger='request', exc=RequestValidationError('driver', msg)) # Field creation options. self.format_time = format_time self.grid_abstraction = grid_abstraction self.grid_is_isomorphic = grid_is_isomorphic # Flag used for regridding to determine if the coordinate system was assigned during initialization. self._has_assigned_coordinate_system = False if crs == 'auto' else True if uri is None: # Fields may be created from pure metadata. if metadata is not None: # The default OCGIS driver is NetCDF. if driver is None: driver = DriverKey.NETCDF_CF elif opened is None: ocgis_lh(logger='request', exc=RequestValidationError('uri', 'Cannot be None')) else: self._uri = get_uri(uri) if driver is None: klass = get_autodiscovered_driver(uri) else: klass = get_driver(driver) self._driver = klass(self) if variable is not None: variable = get_tuple(variable) self._variable = variable self.time_range = time_range self.time_region = time_region self.time_subset_func = time_subset_func self.level_range = level_range self._crs = deepcopy(crs) self.regrid_source = regrid_source self.regrid_destination = regrid_destination self.units = units self.conform_units_to = conform_units_to self._is_init = False self._validate_time_subset_() # Update metadata for time variable. tvar = self.dimension_map.get_variable(DMK.TIME) if tvar is not None: m = self.metadata['variables'][tvar] if t_units is not None: m['attrs']['units'] = t_units if t_calendar is not None: m['attrs']['calendar'] = t_calendar if t_conform_units_to is not None: from ocgis.util.units import get_units_object t_calendar = m['attrs'].get('calendar', constants.DEFAULT_TEMPORAL_CALENDAR) t_conform_units_to = get_units_object(t_conform_units_to, calendar=t_calendar) m['conform_units_to'] = t_conform_units_to