def test_system_rotated_pole_spherical_subsetting(self): """Test rotated pole coordinates are left alone during a subset (no mask applied).""" def _run_mask_test_(target): for vn in ['rlat', 'rlon']: self.assertIsNone(target[vn].get_mask()) rd = RequestDataset(metadata=self.fixture_rotated_spherical_metadata) field = rd.create_field() _run_mask_test_(field) for ctr, vn in enumerate(['lat', 'lon', 'rlat', 'rlon']): var = field[vn] var.get_value()[:] = np.arange(var.size).reshape(var.shape) + (ctr * 10) path = self.get_temporary_file_path('foo.nc') field.write(path) new_field = RequestDataset(path).create_field() _run_mask_test_(new_field) subset_geom = box(*new_field.grid.extent) subset_field = new_field.grid.get_intersects(subset_geom, optimized_bbox_subset=True).parent _run_mask_test_(subset_field) path2 = self.get_temporary_file_path('foo2.nc') subset_field.write(path2) in_subset_field = RequestDataset(path2).create_field() _run_mask_test_(in_subset_field)
def test_init_metadata_only(self): metadata = {'variables': {'foo': {}}} rd = RequestDataset(metadata=metadata) self.assertEqual(rd.driver.key, DriverKey.NETCDF_CF) self.assertIsNone(rd.uri) self.assertEqual(rd.metadata, metadata) field = rd.create_field() self.assertIn('foo', field.keys())
def test_redistribute_by_src_idx(self): if vm.size != 4: raise SkipTest('vm.size != 4') dist = OcgDist() dim1 = dist.create_dimension('dim1', 5 * vm.size, dist=True) dim2 = dist.create_dimension('dim2', 2, dist=False) dist.update_dimension_bounds() rank_value = np.arange(5) + (10 * (vm.rank + 1)) var1 = Variable(name='dvar1', value=rank_value, dimensions=dim1) var2 = Variable(name='dvar2', dimensions=[dim1, dim2]) var1.parent.add_variable(var2) path = self.get_temporary_file_path('out.nc') var1.parent.write(path) desired_idx = np.array([1, 7, 9, 10, 14]) vdesired_value = variable_gather(var1) if vm.rank == 0: desired_value = vdesired_value.get_value()[desired_idx] desired_idx_ranks = {0: slice(1, 2), 1: [2, 4], 2: [0, 4]} rd = RequestDataset(path) rd.metadata['dimensions'][dim1.name]['dist'] = True field = rd.create_field() indvar = field[var1.name] field[var2.name].load() try: rank_slice = desired_idx_ranks[vm.rank] except KeyError: sub = Variable(is_empty=True) else: sub = indvar[rank_slice] self.barrier_print(sub.is_empty) redistribute_by_src_idx(indvar, dim1.name, sub.dimensions_dict.get(dim1.name)) with vm.scoped_by_emptyable('gather for test', indvar): if vm.is_null: self.assertIn(vm.rank_global, [2, 3]) else: self.assertIn(vm.rank_global, [0, 1]) for v in [indvar, indvar.parent[var2.name]]: self.assertIsNone(v._value) self.assertIsNone(v._mask) self.assertIsNone(v._is_empty) self.assertFalse(v._has_initialized_value) self.rank_print(indvar) actual_value = variable_gather(indvar) if vm.rank == 0: actual_value = actual_value.get_value() self.assertNumpyAll(actual_value, desired_value)
from ocgis import RequestDataset, OcgOperations from ocgis.contrib.library_icclim import IcclimTG90p ######################################################################################################################## # Compute a custom percentile basis using ICCLIM. # Path to CF climate dataset. This examples uses the same file for indice and percentile basis calculation. in_file = '/path/to/cf_data.nc' # Subset the input dataset to return the desired base period for the percentile basis. variable = 'tas' years = range(2001, 2003) # A custom date range may be required for your data time_region = {'year': years} rd = RequestDataset(uri=in_file, variable=variable) field = rd.create_field() field = field.time.get_time_region(time_region).parent # Calculate the percentile basis. The data values must be a three-dimensional array. arr = field[variable].get_masked_value().squeeze() # This is the field data to use for the calculation dt_arr = field.temporal.value_datetime # This is an array of datetime objects. percentile = 90 window_width = 5 t_calendar, t_units = field.time.calendar, field.time.units # ICCLIM requires calendar and units for the calculation percentile_dict = IcclimTG90p.get_percentile_dict(arr, dt_arr, percentile, window_width, t_calendar, t_units) ######################################################################################################################## # Calculate indice using custom percentile basis. # Depending on the size of the data, this computation may take some time... calc = [{'func': 'icclim_TG90p', 'name': 'TG90p', 'kwds': {'percentile_dict': percentile_dict}}] calc_grouping = 'month'
from ocgis import RequestDataset, OcgOperations from ocgis.contrib.library_icclim import IcclimTG90p ######################################################################################################################## # Compute a custom percentile basis using ICCLIM. # Path to CF climate dataset. This examples uses the same file for indice and percentile basis calculation. in_file = '/path/to/cf_data.nc' # Subset the input dataset to return the desired base period for the percentile basis. variable = 'tas' years = range(2001, 2003) # A custom date range may be required for your data time_region = {'year': years} rd = RequestDataset(uri=in_file, variable=variable) field = rd.create_field() field = field.time.get_time_region(time_region).parent # Calculate the percentile basis. The data values must be a three-dimensional array. arr = field[variable].get_masked_value().squeeze( ) # This is the field data to use for the calculation dt_arr = field.temporal.value_datetime # This is an array of datetime objects. percentile = 90 window_width = 5 t_calendar, t_units = field.time.calendar, field.time.units # ICCLIM requires calendar and units for the calculation percentile_dict = IcclimTG90p.get_percentile_dict(arr, dt_arr, percentile, window_width, t_calendar, t_units) ######################################################################################################################## # Calculate indice using custom percentile basis.