def get_field(self): gridxy = self.get_gridxy(with_xy_bounds=True) coords_stacked = gridxy.get_value_stacked() exact = self.get_exact_field_value(coords_stacked[1], coords_stacked[0]) exact = Variable(name='exact1', value=exact, dimensions=gridxy.dimensions) dimension_map = {'x': {'variable': 'x', 'bounds': 'xbounds'}, 'y': {'variable': 'y', 'bounds': 'ybounds'}} field1 = Field.from_variable_collection(gridxy.parent, dimension_map=dimension_map) field1.add_variable(exact) field1.set_name('exact1') field1.append_to_tags(TagName.DATA_VARIABLES, 'exact1') return field1
def get_field(self): gridxy = self.get_gridxy(with_xy_bounds=True) coords_stacked = gridxy.get_value_stacked() exact = self.get_exact_field_value(coords_stacked[1], coords_stacked[0]) exact = Variable(name='exact1', value=exact, dimensions=gridxy.dimensions) dimension_map = {'x': {'variable': 'x', 'bounds': 'xbounds'}, 'y': {'variable': 'y', 'bounds': 'ybounds'}} field1 = Field.from_variable_collection(gridxy.parent, dimension_map=dimension_map) field1.add_variable(exact) field1.set_name('exact1') field1.append_to_tags(TagName.DATA_VARIABLES, 'exact1') return field1
def get_field(self, *args, **kwargs): vc = kwargs.pop('vc', None) format_time = kwargs.pop(KeywordArgument.FORMAT_TIME, True) if KeywordArgument.GRID_ABSTRACTION in kwargs: grid_abstraction = kwargs.pop(KeywordArgument.GRID_ABSTRACTION) else: grid_abstraction = constants.UNINITIALIZED if vc is None: # Get the raw variable collection from source. new_kwargs = kwargs.copy() new_kwargs['source_name'] = None vc = self.get_variable_collection(*args, **new_kwargs) # Get the appropriate metadata for the collection. group_metadata = self.get_group_metadata(vc.group, self.metadata_source) # Always pull the dimension map from the request dataset. This allows it to be overloaded. dimension_map = self.get_group_metadata(vc.group, self.rd.dimension_map) # Modify the coordinate system variable. If it is overloaded on the request dataset, then the variable # collection needs to be updated to hold the variable and any alternative coordinate systems needs to be # removed. to_remove = None to_add = None crs = self.get_crs(group_metadata) if self.rd._has_assigned_coordinate_system: to_add = self.rd._crs if crs is not None: to_remove = crs.name else: if self.rd._crs is not None and self.rd._crs != 'auto': to_add = self.rd._crs if crs is not None: to_remove = crs.name elif crs is not None: to_add = crs if to_remove is not None: vc.pop(to_remove, None) if to_add is not None: vc.add_variable(to_add, force=True) # Overload the dimension map with the CRS. if to_add is not None: # dimension_map[DimensionMapKey.CRS][DimensionMapKey.VARIABLE] = to_add.name dimension_map.set_crs(to_add.name) # Remove the mask variable if present in the raw dimension map and the source dimension map is set to None. if self.rd.dimension_map.get_spatial_mask() is None and self.dimension_map_raw.get_spatial_mask() is not None: vc.pop(self.dimension_map_raw.get_spatial_mask()) # Convert the raw variable collection to a field. kwargs['dimension_map'] = dimension_map kwargs[KeywordArgument.FORMAT_TIME] = format_time if grid_abstraction != constants.UNINITIALIZED: kwargs[KeywordArgument.GRID_ABSTRACTION] = grid_abstraction field = Field.from_variable_collection(vc, *args, **kwargs) # If this is a source grid for regridding, ensure the flag is updated. field.regrid_source = self.rd.regrid_source # Update the assigned coordinate system flag. field._has_assigned_coordinate_system = self.rd._has_assigned_coordinate_system # Apply any requested subsets. if self.rd.time_range is not None: field = field.time.get_between(*self.rd.time_range).parent if self.rd.time_region is not None: field = field.time.get_time_region(self.rd.time_region).parent if self.rd.time_subset_func is not None: field = field.time.get_subset_by_function(self.rd.time_subset_func).parent if self.rd.level_range is not None: field = field.level.get_between(*self.rd.level_range).parent # These variables have all the dimensions needed for a data classification. Use overloaded values from the # request dataset if they are provided. try: data_variable_names = list(get_variable_names(self.rd.rename_variable)) except NoDataVariablesFound: # It is okay to have no data variables in a field. data_variable_names = [] pass for dvn in data_variable_names: field.append_to_tags(TagName.DATA_VARIABLES, dvn, create=True) # Load child fields. for child in list(field.children.values()): kwargs['vc'] = child field.children[child.name] = self.get_field(*args, **kwargs) return field
def create_field(self, *args, **kwargs): """ Create a field object. In general, this should not be overloaded by subclasses. :keyword bool format_time: ``(=True)`` If ``False``, do not convert numeric times to Python date objects. :keyword str grid_abstraction: ``(='auto')`` If provided, use this grid abstraction. :keyword raw_field: ``(=None)`` If provided, modify this field instead. :type raw_field: None | :class:`~ocgis.Field` :param kwargs: Additional keyword arguments to :meth:`~ocgis.driver.base.AbstractDriver.create_raw_field`. :return: :class:`ocgis.Field` """ kwargs = kwargs.copy() raw_field = kwargs.pop('raw_field', None) format_time = kwargs.pop(KeywordArgument.FORMAT_TIME, True) grid_abstraction = kwargs.pop(KeywordArgument.GRID_ABSTRACTION, self.rd.grid_abstraction) grid_is_isomorphic = kwargs.pop('grid_is_isomorphic', self.rd.grid_is_isomorphic) if raw_field is None: # Get the raw variable collection from source. new_kwargs = kwargs.copy() new_kwargs['source_name'] = None raw_field = self.create_raw_field(*args, **new_kwargs) # Get the appropriate metadata for the collection. group_metadata = self.get_group_metadata(raw_field.group, self.metadata_source) # Always pull the dimension map from the request dataset. This allows it to be overloaded. dimension_map = self.get_group_metadata(raw_field.group, self.rd.dimension_map) # Modify the coordinate system variable. If it is overloaded on the request dataset, then the variable # collection needs to be updated to hold the variable and any alternative coordinate systems needs to be # removed. to_remove = None to_add = None crs = self.get_crs(group_metadata) if self.rd._has_assigned_coordinate_system: to_add = self.rd._crs if crs is not None: to_remove = crs.name else: if self.rd._crs is not None and self.rd._crs != 'auto': to_add = self.rd._crs if crs is not None: to_remove = crs.name elif crs is not None: to_add = crs if to_remove is not None: raw_field.pop(to_remove, None) if to_add is not None: raw_field.add_variable(to_add, force=True) # Overload the dimension map with the CRS. if to_add is not None: # dimension_map[DimensionMapKey.CRS][DimensionMapKey.VARIABLE] = to_add.name dimension_map.set_crs(to_add.name) # Remove the mask variable if present in the raw dimension map and the source dimension map is set to None. if self.rd.dimension_map.get_spatial_mask( ) is None and self.dimension_map_raw.get_spatial_mask() is not None: raw_field.pop(self.dimension_map_raw.get_spatial_mask()) # Convert the raw variable collection to a field. # TODO: Identify a way to remove this code block; field should be appropriately initialized; format_time and grid_abstraction are part of a dimension map. kwargs[KeywordArgument.DIMENSION_MAP] = dimension_map kwargs[KeywordArgument.FORMAT_TIME] = format_time if grid_abstraction != 'auto': kwargs[KeywordArgument.GRID_ABSTRACTION] = grid_abstraction if grid_is_isomorphic != 'auto': kwargs['grid_is_isomorphic'] = grid_is_isomorphic field = Field.from_variable_collection(raw_field, *args, **kwargs) # If this is a source grid for regridding, ensure the flag is updated. field.regrid_source = self.rd.regrid_source # Update the assigned coordinate system flag. field._has_assigned_coordinate_system = self.rd._has_assigned_coordinate_system # Apply any requested subsets. if self.rd.time_range is not None: field = field.time.get_between(*self.rd.time_range).parent if self.rd.time_region is not None: field = field.time.get_time_region(self.rd.time_region).parent if self.rd.time_subset_func is not None: field = field.time.get_subset_by_function( self.rd.time_subset_func).parent if self.rd.level_range is not None: field = field.level.get_between(*self.rd.level_range).parent # These variables have all the dimensions needed for a data classification. Use overloaded values from the # request dataset if they are provided. try: data_variable_names = list( get_variable_names(self.rd.rename_variable)) except NoDataVariablesFound: # It is okay to have no data variables in a field. data_variable_names = [] pass for dvn in data_variable_names: field.append_to_tags(TagName.DATA_VARIABLES, dvn, create=True) # Load child fields. for child in list(field.children.values()): kwargs['raw_field'] = child field.children[child.name] = self.create_field(*args, **kwargs) return field
def create_field(self, *args, **kwargs): """ Create a field object. In general, this should not be overloaded by subclasses. :keyword bool format_time: ``(=True)`` If ``False``, do not convert numeric times to Python date objects. :keyword str grid_abstraction: ``(='auto')`` If provided, use this grid abstraction. :keyword raw_field: ``(=None)`` If provided, modify this field instead. :type raw_field: None | :class:`~ocgis.Field` :param kwargs: Additional keyword arguments to :meth:`~ocgis.driver.base.AbstractDriver.create_raw_field`. :return: :class:`ocgis.Field` """ kwargs = kwargs.copy() raw_field = kwargs.pop('raw_field', None) format_time = kwargs.pop(KeywordArgument.FORMAT_TIME, True) grid_abstraction = kwargs.pop(KeywordArgument.GRID_ABSTRACTION, self.rd.grid_abstraction) grid_is_isomorphic = kwargs.pop('grid_is_isomorphic', self.rd.grid_is_isomorphic) if raw_field is None: # Get the raw variable collection from source. new_kwargs = kwargs.copy() new_kwargs['source_name'] = None raw_field = self.create_raw_field(*args, **new_kwargs) # Get the appropriate metadata for the collection. group_metadata = self.get_group_metadata(raw_field.group, self.metadata_source) # Always pull the dimension map from the request dataset. This allows it to be overloaded. dimension_map = self.get_group_metadata(raw_field.group, self.rd.dimension_map) # Modify the coordinate system variable. If it is overloaded on the request dataset, then the variable # collection needs to be updated to hold the variable and any alternative coordinate systems needs to be # removed. to_remove = None to_add = None crs = self.get_crs(group_metadata) if self.rd._has_assigned_coordinate_system: to_add = self.rd._crs if crs is not None: to_remove = crs.name else: if self.rd._crs is not None and self.rd._crs != 'auto': to_add = self.rd._crs if crs is not None: to_remove = crs.name elif crs is not None: to_add = crs if to_remove is not None: raw_field.pop(to_remove, None) if to_add is not None: raw_field.add_variable(to_add, force=True) # Overload the dimension map with the CRS. if to_add is not None: # dimension_map[DimensionMapKey.CRS][DimensionMapKey.VARIABLE] = to_add.name dimension_map.set_crs(to_add.name) # Remove the mask variable if present in the raw dimension map and the source dimension map is set to None. if self.rd.dimension_map.get_spatial_mask() is None and self.dimension_map_raw.get_spatial_mask() is not None: raw_field.pop(self.dimension_map_raw.get_spatial_mask()) # Convert the raw variable collection to a field. # TODO: Identify a way to remove this code block; field should be appropriately initialized; format_time and grid_abstraction are part of a dimension map. kwargs[KeywordArgument.DIMENSION_MAP] = dimension_map kwargs[KeywordArgument.FORMAT_TIME] = format_time if grid_abstraction != 'auto': kwargs[KeywordArgument.GRID_ABSTRACTION] = grid_abstraction if grid_is_isomorphic != 'auto': kwargs['grid_is_isomorphic'] = grid_is_isomorphic field = Field.from_variable_collection(raw_field, *args, **kwargs) # If this is a source grid for regridding, ensure the flag is updated. field.regrid_source = self.rd.regrid_source # Update the assigned coordinate system flag. field._has_assigned_coordinate_system = self.rd._has_assigned_coordinate_system # Apply any requested subsets. if self.rd.time_range is not None: field = field.time.get_between(*self.rd.time_range).parent if self.rd.time_region is not None: field = field.time.get_time_region(self.rd.time_region).parent if self.rd.time_subset_func is not None: field = field.time.get_subset_by_function(self.rd.time_subset_func).parent if self.rd.level_range is not None: field = field.level.get_between(*self.rd.level_range).parent # These variables have all the dimensions needed for a data classification. Use overloaded values from the # request dataset if they are provided. try: data_variable_names = list(get_variable_names(self.rd.rename_variable)) except NoDataVariablesFound: # It is okay to have no data variables in a field. data_variable_names = [] pass for dvn in data_variable_names: field.append_to_tags(TagName.DATA_VARIABLES, dvn, create=True) # Load child fields. for child in list(field.children.values()): kwargs['raw_field'] = child field.children[child.name] = self.create_field(*args, **kwargs) return field