def test_relative_vorticity_distance(self): x_min = 0.0 x_max = 100.0 dx = 1.0 x_1d = numpy.arange(x_min, x_max, dx) size = x_1d.size data_1d = x_1d * 2.0 + 1.0 data_2d = numpy.broadcast_to(data_1d[numpy.newaxis, :], (size, size)) dim_x = cf.DimensionCoordinate( data=cf.Data(x_1d, "m"), properties={"axis": "X"} ) dim_y = cf.DimensionCoordinate( data=cf.Data(x_1d, "m"), properties={"axis": "Y"} ) u = cf.Field() X = u.set_construct(cf.DomainAxis(size=dim_x.data.size)) Y = u.set_construct(cf.DomainAxis(size=dim_y.data.size)) u.set_construct(dim_x, axes=[X]) u.set_construct(dim_y, axes=[Y]) u.set_data(cf.Data(data_2d, "m/s"), axes=("Y", "X")) v = cf.Field() v.set_construct(cf.DomainAxis(size=dim_x.data.size)) v.set_construct(cf.DomainAxis(size=dim_y.data.size)) v.set_construct(dim_x, axes=[X]) v.set_construct(dim_y, axes=[Y]) v.set_data(cf.Data(data_2d, "m/s"), axes=("X", "Y")) rv = cf.relative_vorticity(u, v, one_sided_at_boundary=True) self.assertTrue((rv.array == 0.0).all())
def test_relative_vorticity_distance(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: returncf x_min = 0.0 x_max = 100.0 dx = 1.0 x_1d = numpy.arange(x_min, x_max, dx) size = x_1d.size data_1d = x_1d * 2.0 + 1.0 data_2d = numpy.broadcast_to(data_1d[numpy.newaxis, :], (size, size)) dim_x = cf.DimensionCoordinate(data=cf.Data(x_1d, 'm'), properties={'axis': 'X'}) dim_y = cf.DimensionCoordinate(data=cf.Data(x_1d, 'm'), properties={'axis': 'Y'}) u = cf.Field() X = u.set_construct(cf.DomainAxis(size=dim_x.data.size)) Y = u.set_construct(cf.DomainAxis(size=dim_y.data.size)) u.set_construct(dim_x, axes=[X]) u.set_construct(dim_y, axes=[Y]) u.set_data(cf.Data(data_2d, 'm/s'), axes=('Y', 'X')) v = cf.Field() v.set_construct(cf.DomainAxis(size=dim_x.data.size)) v.set_construct(cf.DomainAxis(size=dim_y.data.size)) v.set_construct(dim_x, axes=[X]) v.set_construct(dim_y, axes=[Y]) v.set_data(cf.Data(data_2d, 'm/s'), axes=('X', 'Y')) rv = cf.relative_vorticity(u, v, one_sided_at_boundary=True) self.assertTrue((rv.array == 0.0).all())
def test_relative_vorticity_latlong(self): lat_min = -90.0 lat_max = 90.0 dlat = 1.0 lat_1d = numpy.arange(lat_min, lat_max, dlat) lat_size = lat_1d.size lon_min = 0.0 lon_max = 359.0 dlon = 1.0 lon_1d = numpy.arange(lon_min, lon_max, dlon) lon_size = lon_1d.size u_1d = lat_1d * 2.0 + 1.0 u_2d = numpy.broadcast_to(u_1d[numpy.newaxis, :], (lon_size, lat_size)) v_1d = lon_1d * 2.0 + 1.0 v_2d = numpy.broadcast_to(v_1d[:, numpy.newaxis], (lon_size, lat_size)) v_2d = v_2d * numpy.cos(lat_1d * numpy.pi / 180.0)[numpy.newaxis, :] rv_array = ( u_2d / cf.Data(6371229.0, "meters") * numpy.tan(lat_1d * numpy.pi / 180.0)[numpy.newaxis, :] ) dim_x = cf.DimensionCoordinate( data=cf.Data(lon_1d, "degrees_east"), properties={"axis": "X"} ) dim_y = cf.DimensionCoordinate( data=cf.Data(lat_1d, "degrees_north"), properties={"axis": "Y"} ) u = cf.Field() u.set_construct(cf.DomainAxis(size=lon_1d.size)) u.set_construct(cf.DomainAxis(size=lat_1d.size)) u.set_construct(dim_x) u.set_construct(dim_y) u.set_data(cf.Data(u_2d, "m/s"), axes=("X", "Y")) u.cyclic("X", period=360.0) v = cf.Field() v.set_construct(cf.DomainAxis(size=lon_1d.size)) v.set_construct(cf.DomainAxis(size=lat_1d.size)) v.set_construct(dim_x) v.set_construct(dim_y) v.set_data(cf.Data(v_2d, "m/s"), axes=("X", "Y")) v.cyclic("X", period=360.0) rv = cf.relative_vorticity(u, v, wrap=False) self.assertTrue(numpy.allclose(rv.array, rv_array))
def test_relative_vorticity_latlong(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return lat_min = -90.0 lat_max = 90.0 dlat = 1.0 lat_1d = numpy.arange(lat_min, lat_max, dlat) lat_size = lat_1d.size lon_min = 0.0 lon_max = 359.0 dlon = 1.0 lon_1d = numpy.arange(lon_min, lon_max, dlon) lon_size = lon_1d.size u_1d = lat_1d * 2.0 + 1.0 u_2d = numpy.broadcast_to(lat_1d[numpy.newaxis, :], (lon_size, lat_size)) v_1d = lon_1d * 2.0 + 1.0 v_2d = numpy.broadcast_to(lon_1d[:, numpy.newaxis], (lon_size, lat_size)) v_2d = v_2d * numpy.cos(lat_1d * numpy.pi / 180.0)[numpy.newaxis, :] rv_array = (u_2d / cf.Data(6371229.0, 'meters') * numpy.tan(lat_1d * numpy.pi / 180.0)[numpy.newaxis, :]) dim_x = cf.DimensionCoordinate(data=cf.Data(lon_1d, 'degrees_east'), properties={'axis': 'X'}) dim_y = cf.DimensionCoordinate(data=cf.Data(lat_1d, 'degrees_north'), properties={'axis': 'Y'}) u = cf.Field() u.set_construct(cf.DomainAxis(size=lon_1d.size)) u.set_construct(cf.DomainAxis(size=lat_1d.size)) u.set_construct(dim_x) u.set_construct(dim_y) u.set_data(cf.Data(u_2d, 'm/s'), axes=('X', 'Y')) u.cyclic('X', period=360.0) v = cf.Field() v.set_construct(cf.DomainAxis(size=lon_1d.size)) v.set_construct(cf.DomainAxis(size=lat_1d.size)) v.set_construct(dim_x) v.set_construct(dim_y) v.set_data(cf.Data(v_2d, 'm/s'), axes=('X', 'Y')) v.cyclic('X', period=360.0) rv = cf.relative_vorticity(u, v, wrap=True) self.assertTrue(numpy.allclose(rv.array, rv_array))
def save_datasets(self, datasets, filename, **kwargs): """Save all datasets to one or more files) """ fields = [] shapes = {} for dataset in datasets: if dataset.shape in shapes: domain = shapes[dataset.shape] else: lines, pixels = dataset.shape # Create a grid_latitude dimension coordinate line_coord = cf.DimensionCoordinate( data=cf.Data(np.arange(lines), '1')) pixel_coord = cf.DimensionCoordinate( data=cf.Data(np.arange(pixels), '1')) domain = cf.Domain(dim={ 'lines': line_coord, 'pixels': pixel_coord }, ) shapes[dataset.shape] = domain data = cf.Data(dataset, dataset.info['units']) properties = {'standard_name': dataset.info['standard_name']} fields.append( cf.Field(properties=properties, data=data, axes=['lines', 'pixels'], domain=domain)) cf.write(fields, filename, fmt='NETCDF4')
def wrap_netcdf(year_o, yearpart_o, var_o, standard_name_o, units_o): var_shape = var_o.shape # Define Coordinaties start_date = (datetime.datetime(year_o, 1, 1) + datetime.timedelta(yearpart_o)).strftime('%Y-%m-%d') if var_shape[0] == 1: dim0 = cf.DimensionCoordinate(properties={'standard_name': 'time'}, data=cf.Data( 0., cf.Units('days since ' + start_date, calendar='standard'))) elif var_shape[0] == 240: nc_time = (86400.0 / count_time / divt) * np.arange(count_time * divt) dim0 = cf.DimensionCoordinate(properties={'standard_name': 'time'}, data=cf.Data( nc_time, cf.Units('seconds since ' + start_date + ' 0:3:0', calendar='standard'))) elif var_shape[0] == 241: nc_time = (86400.0 / count_time / divt) * np.arange(count_time * divt + 1) dim0 = cf.DimensionCoordinate(properties={'standard_name': 'time'}, data=cf.Data( nc_time, cf.Units('seconds since ' + start_date + ' 0:0:0', calendar='standard'))) dim1 = cf.DimensionCoordinate(data=cf.Data(latitude, 'degrees_north'), properties={'standard_name': 'latitude'}) dim2 = cf.DimensionCoordinate(data=cf.Data(longitude, 'degrees_east'), properties={'standard_name': 'longitude'}) # Define cf.Field then insert variable and coordinates f = cf.Field(properties={'standard_name': standard_name_o}) f.insert_dim(dim0) f.insert_dim(dim1) f.insert_dim(dim2) data = cf.Data(var_o, units_o) f.insert_data(data) return f
def test_GATHERING_create(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # Define the gathered values gathered_array = numpy.array([[280, 282.5, 281], [279, 278, 277.5]], dtype='float32') # Define the list array values list_array = [1, 4, 5] # Initialise the list variable list_variable = cf.List(data=cf.Data(list_array)) # Initialise the gathered array object array = cf.GatheredArray(compressed_array=cf.Data(gathered_array), compressed_dimension=1, shape=(2, 3, 2), size=12, ndim=3, list_variable=list_variable) # Create the field construct with the domain axes and the # gathered array tas = cf.Field(properties={ 'standard_name': 'air_temperature', 'units': 'K' }) # Create the domain axis constructs for the uncompressed array T = tas.set_construct(cf.DomainAxis(2)) Y = tas.set_construct(cf.DomainAxis(3)) X = tas.set_construct(cf.DomainAxis(2)) uncompressed_array = numpy.ma.masked_array(data=[[[1, 280.0], [1, 1], [282.5, 281.0]], [[1, 279.0], [1, 1], [278.0, 277.5]]], mask=[[[True, False], [True, True], [False, False]], [[True, False], [True, True], [False, False]]], fill_value=1e+20, dtype='float32') for chunksize in (1000000, ): cf.chunksize(chunksize) message = 'chunksize=' + str(chunksize) # Set the data for the field tas.set_data(cf.Data(array), axes=[T, Y, X]) self.assertTrue((tas.data.array == uncompressed_array).all(), message) self.assertEqual(tas.data.get_compression_type(), 'gathered', message) self.assertTrue( (tas.data.compressed_array == numpy.array( [[280., 282.5, 281.], [279., 278., 277.5]], dtype='float32')).all(), message) self.assertTrue( (tas.data.get_list().data.array == numpy.array([1, 4, 5])).all(), message)
def create(dat): """creates cf-python Field object :param dict dat: calpost_reader generated dict of data :return: cf.Field """ v = cf.Field( properties={ 'standard_name': 'mass_concentration_of_methane_in_air', 'units': 'kg m-3', }) v.set_data(dat['v'] / 1000) v.nc_set_variable('methane') if len(dat['v'].shape) == 4: nt, nz, ny, nx = dat['v'].shape has_z = True print(nt, nz, ny, nx) else: nt, ny, nx = dat['v'].shape has_z = False print('a') domain_axisT = cf.DomainAxis(nt) domain_axisT.nc_set_unlimited(True) domain_axisY = cf.DomainAxis(ny) domain_axisX = cf.DomainAxis(nx) domain_axisT.nc_set_dimension('time') domain_axisY.nc_set_dimension('y') domain_axisX.nc_set_dimension('x') axisT = v.set_construct(domain_axisT) axisY = v.set_construct(domain_axisY) axisX = v.set_construct(domain_axisX) if has_z: domain_axisZ = cf.DomainAxis(nz) domain_axisZ.nc_set_dimension('z') axisZ = v.set_construct(domain_axisZ) print(type(domain_axisY)) print(dir(domain_axisY)) print(domain_axisY.identity()) print('b') x = dat['x'] y = dat['y'] dimX = cf.DimensionCoordinate(data=x * 1000, properties={ 'standard_name': 'projection_x_coordinate', 'units': 'meters' }) dimY = cf.DimensionCoordinate(data=y * 1000, properties={ 'standard_name': 'projection_y_coordinate', 'units': 'meters' }) dimT = cf.DimensionCoordinate(data=dat['ts'], ) dimT.nc_set_variable('time') dimY.nc_set_variable('y') dimX.nc_set_variable('x') if has_z: z = dat['z'] dimZ = cf.DimensionCoordinate(data=z, properties={ 'standard_name': 'height', 'units': 'meters' }) dimZ.nc_set_variable('z') print('c') dim_t = v.set_construct(dimT, axes=domain_axisT.identity()) dim_y = v.set_construct(dimY, axes=domain_axisY.identity()) dim_x = v.set_construct(dimX, axes=domain_axisX.identity()) if has_z: v.set_construct(dimZ, axes=domain_axisZ.identity()) print('d') if has_z: v.set_data_axes([axisT, axisZ, axisY, axisX]) else: v.set_data_axes([axisT, axisY, axisX]) datum = cf.Datum(parameters={'earth_radius': 637000.0}) coordinate_conversion_h = cf.CoordinateConversion( parameters={ 'grid_mapping_name': 'lambert_conformal_conic', 'standard_parallel': (38.5, 38.5), 'longitude_of_central_meridian': -97.5, 'latitude_of_projection_origin': 38.5, }) horizontal_crs = cf.CoordinateReference( datum=datum, coordinate_conversion=coordinate_conversion_h, coordinates=[dim_x, dim_y]) v.set_construct(horizontal_crs) return v
def test_DSG_contiguous(self): f = cf.read(self.contiguous, verbose=0) self.assertEqual(len(f), 2) # Select the specific humidity field q = [ g for g in f if g.get_property("standard_name") == "specific_humidity" ][0] self.assertTrue(q._equals(q.data.array.mask, self.a.mask)) self.assertTrue( q._equals(self.a, q.data.array), "\nself.a=\n" + str(self.a) + "\nq.array=\n" + str(q.array), ) cf.write(f, tmpfile, verbose=0) g = cf.read(tmpfile) self.assertEqual(len(g), len(f)) for i in range(len(f)): self.assertTrue(g[i].equals(f[i], verbose=2)) # ------------------------------------------------------------ # Test creation # ------------------------------------------------------------ # Define the ragged array values ragged_array = numpy.array( [280, 282.5, 281, 279, 278, 279.5], dtype="float32" ) # Define the count array values count_array = [2, 4] # Create the count variable count_variable = cf.Count(data=cf.Data(count_array)) count_variable.set_property( "long_name", "number of obs for this timeseries" ) # Create the contiguous ragged array object array = cf.RaggedContiguousArray( compressed_array=cf.Data(ragged_array), shape=(2, 4), size=8, ndim=2, count_variable=count_variable, ) # Create the field construct with the domain axes and the ragged # array tas = cf.Field() tas.set_properties( { "standard_name": "air_temperature", "units": "K", "featureType": "timeSeries", } ) # Create the domain axis constructs for the uncompressed array X = tas.set_construct(cf.DomainAxis(4)) Y = tas.set_construct(cf.DomainAxis(2)) # Set the data for the field tas.set_data(cf.Data(array), axes=[Y, X]) cf.write(tas, tmpfile)
'na_a03','na_a04','ca_a01','ca_a02','ca_a03','ca_a04','hysw_a01',\ 'hysw_a02','hysw_a03','hysw_a04','cl_a01','cl_a02','cl_a03','cl_a04',\ 'co3_a01','co3_a02','co3_a03','co3_a04','oin_a01','oin_a02','oin_a03',\ 'oin_a04'] # for all variables fl = cf.FieldList() for index, item in enumerate(variables): # create dynamic variable name name = "variable" exec(name + " = str(item)") print(variable) # concatenate variable over time, and extract cat = getvar(wrflist, variable, timeidx=ALL_TIMES, method="cat") # create cf field field = cf.Field(data=cf.Data(cat), properties={'standard_name': variable}) # add dimension coordinates to cube - depending if the variable has a height dimension if len(field.axes()) == 4: field.insert_dim(t, 'dim0') field.insert_dim(z, 'dim1') field.insert_dim(y, 'dim2') field.insert_dim(x, 'dim3') if len(field.axes()) == 3: field.insert_dim(t, 'dim0') field.insert_dim(y, 'dim1') field.insert_dim(x, 'dim2') if len(field.axes()) == 2: field.insert_dim(y, 'dim0') field.insert_dim(x, 'dim1')
def test_GATHERING_create(self): # Define the gathered values gathered_array = numpy.array( [[280, 282.5, 281], [279, 278, 277.5]], dtype="float32" ) # Define the list array values list_array = [1, 4, 5] # Initialise the list variable list_variable = cf.List(data=cf.Data(list_array)) # Initialise the gathered array object array = cf.GatheredArray( compressed_array=cf.Data(gathered_array), compressed_dimension=1, shape=(2, 3, 2), size=12, ndim=3, list_variable=list_variable, ) # Create the field construct with the domain axes and the # gathered array tas = cf.Field( properties={"standard_name": "air_temperature", "units": "K"} ) # Create the domain axis constructs for the uncompressed array T = tas.set_construct(cf.DomainAxis(2)) Y = tas.set_construct(cf.DomainAxis(3)) X = tas.set_construct(cf.DomainAxis(2)) uncompressed_array = numpy.ma.masked_array( data=[ [[1, 280.0], [1, 1], [282.5, 281.0]], [[1, 279.0], [1, 1], [278.0, 277.5]], ], mask=[ [[True, False], [True, True], [False, False]], [[True, False], [True, True], [False, False]], ], fill_value=1e20, dtype="float32", ) for chunksize in (1000000,): cf.chunksize(chunksize) message = "chunksize=" + str(chunksize) # Set the data for the field tas.set_data(cf.Data(array), axes=[T, Y, X]) self.assertTrue( (tas.data.array == uncompressed_array).all(), message ) self.assertEqual( tas.data.get_compression_type(), "gathered", message ) self.assertTrue( ( tas.data.compressed_array == numpy.array( [[280.0, 282.5, 281.0], [279.0, 278.0, 277.5]], dtype="float32", ) ).all(), message, ) self.assertTrue( ( tas.data.get_list().data.array == numpy.array([1, 4, 5]) ).all(), message, )
def test_DSG_contiguous(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.read(self.contiguous, verbose=0) self.assertEqual(len(f), 2) # Select the specific humidity field q = [g for g in f if g.get_property('standard_name') == 'specific_humidity'][0] self.assertTrue(q._equals(q.data.array.mask, self.a.mask)) self.assertTrue(q._equals(self.a, q.data.array), '\nself.a=\n'+str(self.a)+'\nq.array=\n'+str(q.array)) cf.write(f, tmpfile, verbose=0) g = cf.read(tmpfile) self.assertEqual(len(g), len(f)) for i in range(len(f)): self.assertTrue(g[i].equals(f[i], verbose=2)) # ------------------------------------------------------------ # Test creation # ------------------------------------------------------------ # Define the ragged array values ragged_array = numpy.array([280, 282.5, 281, 279, 278, 279.5], dtype='float32') # Define the count array values count_array = [2, 4] # Create the count variable count_variable = cf.Count(data=cf.Data(count_array)) count_variable.set_property( 'long_name', 'number of obs for this timeseries') # Create the contiguous ragged array object array = cf.RaggedContiguousArray( compressed_array=cf.Data(ragged_array), shape=(2, 4), size=8, ndim=2, count_variable=count_variable) # Create the field construct with the domain axes and the ragged # array tas = cf.Field() tas.set_properties({'standard_name': 'air_temperature', 'units': 'K', 'featureType': 'timeSeries'}) # Create the domain axis constructs for the uncompressed array X = tas.set_construct(cf.DomainAxis(4)) Y = tas.set_construct(cf.DomainAxis(2)) # Set the data for the field tas.set_data(cf.Data(array), axes=[Y, X]) cf.write(tas, tmpfile)
print(u.array) print(t.where(u, x=-t, y=-99).array) print(t.where(cf.gt(0.5), x=cf.masked, construct='grid_latitude').array) print("\n**Field creation**\n") print("\n**Stage 1:** The field construct is created without metadata\n") print("\n**Stage 2:** Metadata constructs are created independently.\n") print("\n**Stage 3:** The metadata constructs are inserted into the field\n") p = cf.Field(properties={'standard_name': 'precipitation_flux'}) p dc = cf.DimensionCoordinate(properties={'long_name': 'Longitude'}, data=cf.Data([0, 1, 2.])) dc fa = cf.FieldAncillary( properties={'standard_name': 'precipitation_flux status_flag'}, data=cf.Data(numpy.array([0, 0, 2], dtype='int8'))) fa p = cf.Field() p p.set_property('standard_name', 'precipitation_flux') p dc = cf.DimensionCoordinate() dc dc.set_property('long_name', 'Longitude')
def test_create_field(self): # Dimension coordinates dim1 = cf.DimensionCoordinate( data=cf.Data(numpy.arange(10.), 'degrees')) dim1.standard_name = 'grid_latitude' dim0 = cf.DimensionCoordinate( data=cf.Data(numpy.arange(9.) + 20, 'degrees')) dim0.standard_name = 'grid_longitude' dim0.data[-1] += 5 bounds = cf.Data(numpy.array( [dim0.data.array-0.5, dim0.data.array+0.5]).transpose((1, 0))) bounds[-2, 1] = 30 bounds[-1, :] = [30, 36] dim0.set_bounds(cf.Bounds(data=bounds)) dim2 = cf.DimensionCoordinate( data=cf.Data([1.5]), bounds=cf.Bounds(data=cf.Data([[1, 2.]])) ) dim2.standard_name = 'atmosphere_hybrid_height_coordinate' # Auxiliary coordinates ak = cf.DomainAncillary(data=cf.Data([10.], 'm')) ak.id = 'atmosphere_hybrid_height_coordinate_ak' bounds = cf.Bounds(data=cf.Data([[5, 15.]], units=ak.Units)) ak.set_bounds(bounds) bk = cf.DomainAncillary(data=cf.Data([20.])) bk.id = 'atmosphere_hybrid_height_coordinate_bk' bounds = cf.Bounds(data=cf.Data([[14, 26.]])) bk.set_bounds(bounds) aux2 = cf.AuxiliaryCoordinate( data=cf.Data(numpy.arange(-45, 45, dtype='int32').reshape(10, 9), units='degree_N')) aux2.standard_name = 'latitude' aux3 = cf.AuxiliaryCoordinate( data=cf.Data(numpy.arange(60, 150, dtype='int32').reshape(9, 10), units='degreesE')) aux3.standard_name = 'longitude' aux4 = cf.AuxiliaryCoordinate( data=cf.Data(numpy.array( ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa'], dtype='S' )) ) aux4.standard_name = 'greek_letters' aux4[0] = cf.masked # Cell measures msr0 = cf.CellMeasure( data=cf.Data(1+numpy.arange(90.).reshape(9, 10)*1234, 'km 2')) msr0.measure = 'area' # Data data = cf.Data(numpy.arange(90.).reshape(10, 9), 'm s-1') properties = {'standard_name': 'eastward_wind'} f = cf.Field(properties=properties) axisX = f.set_construct(cf.DomainAxis(9)) axisY = f.set_construct(cf.DomainAxis(10)) axisZ = f.set_construct(cf.DomainAxis(1)) f.set_data(data) x = f.set_construct(dim0) y = f.set_construct(dim1, axes=[axisY]) z = f.set_construct(dim2, axes=[axisZ]) lat = f.set_construct(aux2) lon = f.set_construct(aux3, axes=['X', axisY]) f.set_construct(aux4, axes=['Y']) ak = f.set_construct(ak, axes=['Z']) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references coordinate_conversion = cf.CoordinateConversion( parameters={'grid_mapping_name': 'rotated_latitude_longitude', 'grid_north_pole_latitude': 38.0, 'grid_north_pole_longitude': 190.0}) ref0 = cf.CoordinateReference( coordinate_conversion=coordinate_conversion, coordinates=[x, y, lat, lon] ) f.set_construct(msr0, axes=[axisX, 'Y']) f.set_construct(ref0) orog = cf.DomainAncillary() orog.standard_name = 'surface_altitude' orog.set_data(cf.Data(f.array*2, 'm')) orog.transpose([1, 0], inplace=True) orog_key = f.set_construct(orog, axes=['X', axisY]) coordinate_conversion = cf.CoordinateConversion( parameters={ 'standard_name': 'atmosphere_hybrid_height_coordinate' }, domain_ancillaries={ 'orog': orog_key, 'a': ak, 'b': bk } ) ref1 = cf.CoordinateReference( coordinate_conversion=coordinate_conversion, coordinates=[z]) f.set_construct(ref1) # Field ancillary variables g = cf.FieldAncillary() g.set_data(f.data) g.transpose([1, 0], inplace=True) g.standard_name = 'ancillary0' g *= 0.01 f.set_construct(g) g = cf.FieldAncillary() g.set_data(f.data) g.standard_name = 'ancillary1' g *= 0.01 f.set_construct(g) g = cf.FieldAncillary() g.set_data(f[0, :].data) g.squeeze(inplace=True) g.standard_name = 'ancillary2' g *= 0.001 f.set_construct(g) g = cf.FieldAncillary() g.set_data(f[:, 0].data) g.squeeze(inplace=True) g.standard_name = 'ancillary3' g *= 0.001 f.set_construct(g) f.flag_values = [1, 2, 4] f.flag_meanings = ['a', 'bb', 'ccc'] for cm in cf.CellMethod.create( 'grid_longitude: mean grid_latitude: max'): f.set_construct(cm) # Write the file, and read it in cf.write(f, self.filename, verbose=0, string=True) g = cf.read(self.filename, squeeze=True, verbose=0)[0] self.assertTrue(g.equals(f, verbose=0), "Field not equal to itself read back in") x = g.dump(display=False) x = f.dump(display=False)
def _formula_terms(standard_name): """Return a field construct with a vertical CRS, its computed non- parametric coordinates, and the computed standard name.""" # field: air_temperature field = cf.Field() field.set_properties({"standard_name": "air_temperature", "units": "K"}) data = cf.Data([0, 1, 2], units="K", dtype="f8") # domain_axis: Z c = cf.DomainAxis() c.set_size(3) c.nc_set_dimension("z") axisZ = field.set_construct(c, key="domainaxis1", copy=False) field.set_data(data) # coordinate_reference: coordref = cf.CoordinateReference() coordref.coordinate_conversion.set_parameter( "standard_name", standard_name ) aux = cf.AuxiliaryCoordinate() aux.long_name = "Computed from parametric {} vertical coordinates".format( standard_name ) if standard_name == "atmosphere_ln_pressure_coordinate": computed_standard_name = "air_pressure" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([700, 500, 300], "hPa", dtype="f8") aux.set_data(data) bounds = cf.Bounds() data = cf.Data([[800, 600], [600, 400], [400, 200]], "hPa", dtype="f8") bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: p0 p0 = cf.DomainAncillary() p0.standard_name = ( "reference_air_pressure_for_atmosphere_vertical_coordinate" ) data = cf.Data(1000.0, units="hPa", dtype="f8") p0.set_data(data) p0_key = field.set_construct(p0, axes=(), copy=False) # domain_ancillary: Z lev = cf.DomainAncillary() lev.standard_name = standard_name data = -(aux.data / p0.data).log() lev.set_data(data) bounds = cf.Bounds() data = -(aux.bounds.data / p0.data).log() bounds.set_data(data) lev.set_bounds(bounds) lev_key = field.set_construct(lev, axes=axisZ, copy=False) # dimension_coordinate: Z levc = cf.DimensionCoordinate(source=lev) levc_key = field.set_construct(levc, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({levc_key}) coordref.coordinate_conversion.set_domain_ancillaries( {"p0": p0_key, "lev": lev_key} ) field.set_construct(coordref) elif standard_name == "atmosphere_sigma_coordinate": computed_standard_name = "air_pressure" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([700, 500, 300], "hPa", dtype="f8") aux.set_data(data) b = cf.Bounds() data = cf.Data([[800, 600], [600, 400], [400, 200]], "hPa", dtype="f8") b.set_data(data) aux.set_bounds(b) # domain_ancillary: ps ps = cf.DomainAncillary() ps.standard_name = "surface_air_pressure" data = cf.Data(1000, units="hPa", dtype="f8") ps.set_data(data) ps_key = field.set_construct(ps, axes=(), copy=False) # domain_ancillary: ptop ptop = cf.DomainAncillary() ptop.standard_name = "air_pressure_at_top_of_atmosphere_model" data = cf.Data(10, units="hPa", dtype="f8") ptop.set_data(data) ptop_key = field.set_construct(ptop, axes=(), copy=False) # domain_ancillary: sigma sigma = cf.DomainAncillary() sigma.standard_name = standard_name data = cf.Data([0.6969697, 0.49494949, 0.29292929]) sigma.set_data(data) b = cf.Bounds() data = cf.Data( [ [0.7979798, 0.5959596], [0.5959596, 0.39393939], [0.39393939, 0.19191919], ] ) b.set_data(data) sigma.set_bounds(b) sigma_key = field.set_construct(sigma, axes=axisZ, copy=False) # dimension_coordinate: sigma sigmac = cf.DimensionCoordinate(source=sigma) sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sigmac_key}) coordref.coordinate_conversion.set_domain_ancillaries( {"ptop": ptop_key, "ps": ps_key, "sigma": sigma_key} ) field.set_construct(coordref) elif standard_name == "atmosphere_hybrid_sigma_pressure_coordinate": computed_standard_name = "air_pressure" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([700, 500, 300], "hPa", dtype="f8") aux.set_data(data) bounds = cf.Bounds() data = cf.Data([[800, 600], [600, 400], [400, 200]], "hPa", dtype="f8") bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: ps ps = cf.DomainAncillary() ps.standard_name = "surface_air_pressure" data = cf.Data(1000, units="hPa", dtype="f8") ps.set_data(data) ps_key = field.set_construct(ps, axes=(), copy=False) # domain_ancillary: p0 p0 = cf.DomainAncillary() data = cf.Data(1000, units="hPa", dtype="f8") p0.set_data(data) p0_key = field.set_construct(p0, axes=(), copy=False) # domain_ancillary: a a = cf.DomainAncillary() data = cf.Data([0.6, 0.3, 0], dtype="f8") a.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.75, 0.45], [0.45, 0.15], [0.15, 0]]) bounds.set_data(data) a.set_bounds(bounds) a_key = field.set_construct(a, axes=axisZ, copy=False) # domain_ancillary: b b = cf.DomainAncillary() data = cf.Data([0.1, 0.2, 0.3], dtype="f8") b.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.05, 0.15], [0.15, 0.25], [0.25, 0.2]]) bounds.set_data(data) b.set_bounds(bounds) b_key = field.set_construct(b, axes=axisZ, copy=False) # dimension_coordinate: sigma sigma = cf.DimensionCoordinate() sigma.standard_name = standard_name data = cf.Data([0.6969697, 0.49494949, 0.29292929]) sigma.set_data(data) bounds = cf.Bounds() data = cf.Data( [ [0.7979798, 0.5959596], [0.5959596, 0.39393939], [0.39393939, 0.19191919], ] ) bounds.set_data(data) sigma.set_bounds(bounds) sigma_key = field.set_construct(sigma, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sigma_key}) coordref.coordinate_conversion.set_domain_ancillaries( {"p0": p0_key, "a": a_key, "b": b_key, "ps": ps_key} ) field.set_construct(coordref) elif standard_name == "atmosphere_sleve_coordinate": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([100, 200, 300], "m", dtype="f8") aux.set_data(data) bounds = cf.Bounds() data = cf.Data([[50, 150], [150, 250], [250, 350]], "m", dtype="f8") bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: ztop ztop = cf.DomainAncillary() ztop.standard_name = "altitude_at_top_of_atmosphere_model" data = cf.Data(1000, units="m", dtype="f8") ztop.set_data(data) ztop_key = field.set_construct(ztop, axes=(), copy=False) # domain_ancillary: zsurf1 zsurf1 = cf.DomainAncillary() data = cf.Data(90, units="m", dtype="f8") zsurf1.set_data(data) zsurf1_key = field.set_construct(zsurf1, axes=(), copy=False) # domain_ancillary: zsurf2 zsurf2 = cf.DomainAncillary() data = cf.Data(0.1, units="m", dtype="f8") zsurf2.set_data(data) zsurf2_key = field.set_construct(zsurf2, axes=(), copy=False) # domain_ancillary: b1 b1 = cf.DomainAncillary() data = cf.Data([0.05, 0.04, 0.03], dtype="f8") b1.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.055, 0.045], [0.045, 0.035], [0.035, 0.025]]) bounds.set_data(data) b1.set_bounds(bounds) b1_key = field.set_construct(b1, axes=axisZ, copy=False) # domain_ancillary: b2 b2 = cf.DomainAncillary() data = cf.Data([0.5, 0.4, 0.3]) b2.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.55, 0.45], [0.45, 0.35], [0.35, 0.25]]) bounds.set_data(data) b2.set_bounds(bounds) b2_key = field.set_construct(b2, axes=axisZ, copy=False) # domain_ancillary: a a = cf.DomainAncillary() data = cf.Data([0.09545, 0.19636, 0.29727]) a.set_data(data) bounds = cf.Bounds() data = cf.Data( [[0.044995, 0.145905], [0.145905, 0.246815], [0.246815, 0.347725]] ) bounds.set_data(data) a.set_bounds(bounds) a_key = field.set_construct(a, axes=axisZ, copy=False) # coordinate_reference: coordref.coordinate_conversion.set_domain_ancillaries( { "zsurf1": zsurf1_key, "a": a_key, "b1": b1_key, "b2": b2_key, "zsurf2": zsurf2_key, "ztop": ztop_key, } ) field.set_construct(coordref) elif standard_name == "ocean_sigma_coordinate": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([10, 20, 30], "m", dtype="f8") aux.set_data(data) bounds = cf.Bounds() data = cf.Data([[5, 15], [15, 25], [25, 35]], "m", dtype="f8") bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: depth depth = cf.DomainAncillary() depth.standard_name = "sea_floor_depth_below_geoid" data = cf.Data(-1000.0, units="m") depth.set_data(data) depth_key = field.set_construct(depth, axes=(), copy=False) # domain_ancillary: eta eta = cf.DomainAncillary() eta.standard_name = "sea_surface_height_above_geoid" data = cf.Data(100.0, units="m") eta.set_data(data) eta_key = field.set_construct(eta, axes=(), copy=False) # domain_ancillary: sigma sigma = cf.DomainAncillary() sigma.standard_name = standard_name data = cf.Data([0.1, 0.08888888888888889, 0.07777777777777778]) sigma.set_data(data) bounds = cf.Bounds() data = cf.Data( [ [0.10555556, 0.09444444], [0.09444444, 0.08333333], [0.08333333, 0.07222222], ] ) bounds.set_data(data) sigma.set_bounds(bounds) sigma_key = field.set_construct(sigma, axes=axisZ, copy=False) # dimension_coordinate: sigma sigmac = cf.DimensionCoordinate(source=sigma) sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sigmac_key}) coordref.coordinate_conversion.set_domain_ancillaries( {"depth": depth_key, "eta": eta_key, "sigma": sigma_key} ) field.set_construct(coordref) elif standard_name == "ocean_s_coordinate": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([15.01701191, 31.86034296, 40.31150319], units="m") aux.set_data(data) bounds = cf.Bounds() data = cf.Data( [ [15.01701191, 23.42877638], [23.42877638, 31.86034296], [31.86034296, 40.31150319], ], units="m", ) bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: depth depth = cf.DomainAncillary() depth.standard_name = "sea_floor_depth_below_geoid" data = cf.Data(-1000.0, units="m") depth.set_data(data) depth_key = field.set_construct(depth, axes=(), copy=False) # domain_ancillary: eta eta = cf.DomainAncillary() eta.standard_name = "sea_surface_height_above_geoid" data = cf.Data(100.0, units="m") eta.set_data(data) eta_key = field.set_construct(eta, axes=(), copy=False) # domain_ancillary: depth_c depth_c = cf.DomainAncillary() data = cf.Data(10.0, units="m") depth_c.set_data(data) depth_c_key = field.set_construct(depth_c, axes=(), copy=False) # domain_ancillary: a a = cf.DomainAncillary() data = cf.Data(0.5) a.set_data(data) a_key = field.set_construct(a, axes=(), copy=False) # domain_ancillary: b b = cf.DomainAncillary() data = cf.Data(0.75) b.set_data(data) b_key = field.set_construct(b, axes=(), copy=False) # domain_ancillary: s s = cf.DomainAncillary() s.standard_name = standard_name data = cf.Data([0.1, 0.08, 0.07]) s.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]]) bounds.set_data(data) s.set_bounds(bounds) s_key = field.set_construct(s, axes=axisZ, copy=False) # dimension_coordinate: s sc = cf.DimensionCoordinate(source=s) sc_key = field.set_construct(sc, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sc_key}) coordref.coordinate_conversion.set_domain_ancillaries( { "depth": depth_key, "eta": eta_key, "depth_c": depth_c_key, "a": a_key, "b": b_key, "s": s_key, } ) field.set_construct(coordref) elif standard_name == "ocean_s_coordinate_g1": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([555.4, 464.32, 373.33], units="m") aux.set_data(data) bounds = cf.Bounds() data = cf.Data( [[600.85, 509.86], [509.86, 418.87], [418.87, 327.88]], units="m" ) bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: depth depth = cf.DomainAncillary() depth.standard_name = "sea_floor_depth_below_geoid" data = cf.Data(-1000.0, units="m") depth.set_data(data) depth_key = field.set_construct(depth, axes=(), copy=False) # domain_ancillary: eta eta = cf.DomainAncillary() eta.standard_name = "sea_surface_height_above_geoid" data = cf.Data(100.0, units="m") eta.set_data(data) eta_key = field.set_construct(eta, axes=(), copy=False) # domain_ancillary: depth_c depth_c = cf.DomainAncillary() data = cf.Data(10.0, units="m") depth_c.set_data(data) depth_c_key = field.set_construct(depth_c, axes=(), copy=False) # domain_ancillary: C C = cf.DomainAncillary() data = cf.Data([-0.5, -0.4, -0.3]) C.set_data(data) bounds = cf.Bounds() data = cf.Data([[-0.55, -0.45], [-0.45, -0.35], [-0.35, -0.25]]) bounds.set_data(data) C.set_bounds(bounds) C_key = field.set_construct(C, axes=axisZ, copy=False) # domain_ancillary: s s = cf.DomainAncillary() s.standard_name = standard_name data = cf.Data([0.1, 0.08, 0.07]) s.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]]) bounds.set_data(data) s.set_bounds(bounds) s_key = field.set_construct(s, axes=axisZ, copy=False) # dimension_coordinate: s sc = cf.DimensionCoordinate(source=s) sc_key = field.set_construct(sc, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sc_key}) coordref.coordinate_conversion.set_domain_ancillaries( { "depth": depth_key, "eta": eta_key, "depth_c": depth_c_key, "C": C_key, "s": s_key, } ) field.set_construct(coordref) elif standard_name == "ocean_s_coordinate_g2": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([555.45454545, 464.36363636, 373.36363636], units="m") aux.set_data(data) bounds = cf.Bounds() data = cf.Data( [ [600.90909091, 509.90909091], [509.90909091, 418.90909091], [418.90909091, 327.90909091], ], units="m", ) bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: depth depth = cf.DomainAncillary() depth.standard_name = "sea_floor_depth_below_geoid" data = cf.Data(-1000.0, units="m") depth.set_data(data) depth_key = field.set_construct(depth, axes=(), copy=False) # domain_ancillary: eta eta = cf.DomainAncillary() eta.standard_name = "sea_surface_height_above_geoid" data = cf.Data(100.0, units="m") eta.set_data(data) eta_key = field.set_construct(eta, axes=(), copy=False) # domain_ancillary: depth_c depth_c = cf.DomainAncillary() data = cf.Data(10.0, units="m") depth_c.set_data(data) depth_c_key = field.set_construct(depth_c, axes=(), copy=False) # domain_ancillary: C C = cf.DomainAncillary() data = cf.Data([-0.5, -0.4, -0.3]) C.set_data(data) bounds = cf.Bounds() data = cf.Data([[-0.55, -0.45], [-0.45, -0.35], [-0.35, -0.25]]) bounds.set_data(data) C.set_bounds(bounds) C_key = field.set_construct(C, axes=axisZ, copy=False) # domain_ancillary: s s = cf.DomainAncillary() s.standard_name = standard_name data = cf.Data([0.1, 0.08, 0.07]) s.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]]) bounds.set_data(data) s.set_bounds(bounds) s_key = field.set_construct(s, axes=axisZ, copy=False) # dimension_coordinate: s sc = cf.DimensionCoordinate(source=s) sc_key = field.set_construct(sc, axes=axisZ, copy=False) # coordinat # coordinate_reference: coordref.set_coordinates({sc_key}) coordref.coordinate_conversion.set_domain_ancillaries( { "depth": depth_key, "eta": eta_key, "depth_c": depth_c_key, "C": C_key, "s": s_key, } ) field.set_construct(coordref) elif standard_name == "ocean_sigma_z_coordinate": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data([10.0, 30.0, 40.0], "m", dtype="f8") aux.set_data(data) bounds = cf.Bounds() data = cf.Data( [[10.0, 19.0], [25.0, 35.0], [35.0, 45.0]], "m", dtype="f8" ) bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: depth depth = cf.DomainAncillary() depth.standard_name = "sea_floor_depth_below_geoid" data = cf.Data(-1000.0, units="m") depth.set_data(data) depth_key = field.set_construct(depth, axes=(), copy=False) # domain_ancillary: eta eta = cf.DomainAncillary() eta.standard_name = "sea_surface_height_above_geoid" data = cf.Data(100.0, units="m") eta.set_data(data) eta_key = field.set_construct(eta, axes=(), copy=False) # domain_ancillary: depth_c depth_c = cf.DomainAncillary() data = cf.Data(10.0, units="m") depth_c.set_data(data) depth_c_key = field.set_construct(depth_c, axes=(), copy=False) # domain_ancillary: nsigma nsigma = cf.DomainAncillary() data = cf.Data(1) nsigma.set_data(data) nsigma_key = field.set_construct(nsigma, axes=(), copy=False) # domain_ancillary: zlev zlev = cf.DomainAncillary() zlev.standard_name = "altitude" data = cf.Data([20, 30, 40], units="m", dtype="f8") zlev.set_data(data) bounds = cf.Bounds() data = cf.Data([[15, 25], [25, 35], [35, 45]], units="m", dtype="f8") bounds.set_data(data) zlev.set_bounds(bounds) zlev_key = field.set_construct(zlev, axes=axisZ, copy=False) # domain_ancillary: sigma sigma = cf.DomainAncillary() sigma.standard_name = standard_name data = cf.Data([0.1, 0.08, 0.07]) sigma.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]]) bounds.set_data(data) sigma.set_bounds(bounds) sigma_key = field.set_construct(sigma, axes=axisZ, copy=False) # dimension_coordinate: sigma sigmac = cf.DimensionCoordinate(source=sigma) sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sigmac_key}) coordref.coordinate_conversion.set_domain_ancillaries( { "depth": depth_key, "eta": eta_key, "depth_c": depth_c_key, "nsigma": nsigma_key, "zlev": zlev_key, "sigma": sigma_key, } ) field.set_construct(coordref) elif standard_name == "ocean_double_sigma_coordinate": computed_standard_name = "altitude" # Computed vertical corodinates aux.standard_name = computed_standard_name data = cf.Data( [0.15000000000000002, 0.12, 932.895], units="m", dtype="f8" ) aux.set_data(data) bounds = cf.Bounds() data = cf.Data( [ [1.50000e-01, 1.35000e-01], [1.35000e-01, 1.20000e-01], [9.22880e02, 9.32895e02], ], units="m", dtype="f8", ) bounds.set_data(data) aux.set_bounds(bounds) # domain_ancillary: depth depth = cf.DomainAncillary() depth.standard_name = "sea_floor_depth_below_geoid" data = cf.Data(-1000.0, units="m") depth.set_data(data) depth_key = field.set_construct(depth, axes=(), copy=False) # domain_ancillary: z1 z1 = cf.DomainAncillary() data = cf.Data(2, units="m") z1.set_data(data) z1_key = field.set_construct(z1, axes=(), copy=False) # domain_ancillary: z2 z2 = cf.DomainAncillary() data = cf.Data(1.5, units="m") z2.set_data(data) z2_key = field.set_construct(z2, axes=(), copy=False) # domain_ancillary: a a = cf.DomainAncillary() data = cf.Data(2.5, units="m") a.set_data(data) a_key = field.set_construct(a, axes=(), copy=False) # domain_ancillary: href href = cf.DomainAncillary() data = cf.Data(10.5, units="m") href.set_data(data) href_key = field.set_construct(href, axes=(), copy=False) # domain_ancillary: k_c k_c = cf.DomainAncillary() data = cf.Data(1) k_c.set_data(data) k_c_key = field.set_construct(k_c, axes=(), copy=False) # dimension_coordinate: sigma sigma = cf.DomainAncillary() sigma.standard_name = standard_name data = cf.Data([0.1, 0.08, 0.07]) sigma.set_data(data) bounds = cf.Bounds() data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]]) bounds.set_data(data) sigma.set_bounds(bounds) sigma_key = field.set_construct(sigma, axes=axisZ, copy=False) # dimension_coordinate: sigma sigmac = cf.DimensionCoordinate(source=sigma) sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False) # coordinate_reference: coordref.set_coordinates({sigmac_key}) coordref.coordinate_conversion.set_domain_ancillaries( { "depth": depth_key, "a": a_key, "k_c": k_c_key, "z1": z1_key, "z2": z2_key, "href": href_key, "sigma": sigma_key, } ) field.set_construct(coordref) else: raise ValueError( "Bad standard name: {}, " "not an element of FormulaTerms.standard_names".format( standard_name ) ) return (field, aux, computed_standard_name)
def test_create_field(self): # Dimension coordinates dim1 = cf.DimensionCoordinate( data=cf.Data(numpy.arange(10.0), "degrees")) dim1.standard_name = "grid_latitude" dim0 = cf.DimensionCoordinate( data=cf.Data(numpy.arange(9.0) + 20, "degrees")) dim0.standard_name = "grid_longitude" dim0.data[-1] += 5 bounds = cf.Data( numpy.array([dim0.data.array - 0.5, dim0.data.array + 0.5]).transpose((1, 0))) bounds[-2, 1] = 30 bounds[-1, :] = [30, 36] dim0.set_bounds(cf.Bounds(data=bounds)) dim2 = cf.DimensionCoordinate( data=cf.Data([1.5]), bounds=cf.Bounds(data=cf.Data([[1, 2.0]]))) dim2.standard_name = "atmosphere_hybrid_height_coordinate" # Auxiliary coordinates ak = cf.DomainAncillary(data=cf.Data([10.0], "m")) ak.id = "atmosphere_hybrid_height_coordinate_ak" bounds = cf.Bounds(data=cf.Data([[5, 15.0]], units=ak.Units)) ak.set_bounds(bounds) bk = cf.DomainAncillary(data=cf.Data([20.0])) bk.id = "atmosphere_hybrid_height_coordinate_bk" bounds = cf.Bounds(data=cf.Data([[14, 26.0]])) bk.set_bounds(bounds) aux2 = cf.AuxiliaryCoordinate(data=cf.Data( numpy.arange(-45, 45, dtype="int32").reshape(10, 9), units="degree_N", )) aux2.standard_name = "latitude" aux3 = cf.AuxiliaryCoordinate(data=cf.Data( numpy.arange(60, 150, dtype="int32").reshape(9, 10), units="degreesE", )) aux3.standard_name = "longitude" aux4 = cf.AuxiliaryCoordinate(data=cf.Data( numpy.array( [ "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", ], dtype="S", ))) aux4.standard_name = "greek_letters" aux4[0] = cf.masked # Cell measures msr0 = cf.CellMeasure( data=cf.Data(1 + numpy.arange(90.0).reshape(9, 10) * 1234, "km 2")) msr0.measure = "area" # Data data = cf.Data(numpy.arange(90.0).reshape(10, 9), "m s-1") properties = {"standard_name": "eastward_wind"} f = cf.Field(properties=properties) axisX = f.set_construct(cf.DomainAxis(9)) axisY = f.set_construct(cf.DomainAxis(10)) axisZ = f.set_construct(cf.DomainAxis(1)) f.set_data(data) x = f.set_construct(dim0) y = f.set_construct(dim1, axes=[axisY]) z = f.set_construct(dim2, axes=[axisZ]) lat = f.set_construct(aux2) lon = f.set_construct(aux3, axes=["X", axisY]) f.set_construct(aux4, axes=["Y"]) ak = f.set_construct(ak, axes=["Z"]) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references coordinate_conversion = cf.CoordinateConversion( parameters={ "grid_mapping_name": "rotated_latitude_longitude", "grid_north_pole_latitude": 38.0, "grid_north_pole_longitude": 190.0, }) ref0 = cf.CoordinateReference( coordinate_conversion=coordinate_conversion, coordinates=[x, y, lat, lon], ) f.set_construct(msr0, axes=[axisX, "Y"]) f.set_construct(ref0) orog = cf.DomainAncillary() orog.standard_name = "surface_altitude" orog.set_data(cf.Data(f.array * 2, "m")) orog.transpose([1, 0], inplace=True) orog_key = f.set_construct(orog, axes=["X", axisY]) coordinate_conversion = cf.CoordinateConversion( parameters={ "standard_name": "atmosphere_hybrid_height_coordinate" }, domain_ancillaries={ "orog": orog_key, "a": ak, "b": bk }, ) ref1 = cf.CoordinateReference( coordinate_conversion=coordinate_conversion, coordinates=[z]) f.set_construct(ref1) # Field ancillary variables g = cf.FieldAncillary() g.set_data(f.data) g.transpose([1, 0], inplace=True) g.standard_name = "ancillary0" g *= 0.01 f.set_construct(g) g = cf.FieldAncillary() g.set_data(f.data) g.standard_name = "ancillary1" g *= 0.01 f.set_construct(g) g = cf.FieldAncillary() g.set_data(f[0, :].data) g.squeeze(inplace=True) g.standard_name = "ancillary2" g *= 0.001 f.set_construct(g) g = cf.FieldAncillary() g.set_data(f[:, 0].data) g.squeeze(inplace=True) g.standard_name = "ancillary3" g *= 0.001 f.set_construct(g) f.flag_values = [1, 2, 4] f.flag_meanings = ["a", "bb", "ccc"] for cm in cf.CellMethod.create( "grid_longitude: mean grid_latitude: max"): f.set_construct(cm) # Write the file, and read it in cf.write(f, self.filename, verbose=0, string=True) g = cf.read(self.filename, squeeze=True, verbose=0)[0] self.assertTrue(g.equals(f, verbose=0), "Field not equal to itself read back in") x = g.dump(display=False) x = f.dump(display=False)
u = t.where(cf.lt(273.15), x=cf.masked) print(u.array) u = t.where(cf.lt(273.15), x=0, y=1) print(u.array) print(t.where(u, x=-t, y=-99).array) print(t.where(cf.gt(0.5), x=cf.masked, construct='grid_latitude').array) print("\n**Field creation**\n") print("\n**Stage 1:** The field construct is created without metadata\n") print("\n**Stage 2:** Metadata constructs are created independently.\n") print("\n**Stage 3:** The metadata constructs are inserted into the field\n") p = cf.Field(properties={'standard_name': 'precipitation_flux'}) p dc = cf.DimensionCoordinate(properties={'long_name': 'Longitude'}, data=cf.Data([0, 1, 2.])) dc fa = cf.FieldAncillary( properties={'standard_name': 'precipitation_flux status_flag'}, data=cf.Data(numpy.array([0, 0, 2], dtype='int8'))) fa p = cf.Field() p p.set_property('standard_name', 'precipitation_flux') p dc = cf.DimensionCoordinate() dc dc.set_property('long_name', 'Longitude')
def save_datasets(self, datasets, filename, **kwargs): """Save all datasets to one or more files.""" LOG.info('Saving datasets to NetCDF4/CF.') fields = [] shapes = {} for dataset in datasets: if dataset.shape in shapes: domain = shapes[dataset.shape] else: lines, pixels = dataset.shape area = dataset.info.get('area') add_time = False try: # Create a longitude auxiliary coordinate lat = cf.AuxiliaryCoordinate( data=cf.Data(area.lats, 'degrees_north')) lat.standard_name = 'latitude' # Create a latitude auxiliary coordinate lon = cf.AuxiliaryCoordinate( data=cf.Data(area.lons, 'degrees_east')) lon.standard_name = 'longitude' aux = [lat, lon] add_time = True except AttributeError: LOG.info('No longitude and latitude data to save.') aux = None try: grid_mapping = create_grid_mapping(area) units = area.proj_dict.get('units', 'm') line_coord = cf.DimensionCoordinate( data=cf.Data(area.proj_y_coords, units)) line_coord.standard_name = "projection_y_coordinate" pixel_coord = cf.DimensionCoordinate( data=cf.Data(area.proj_x_coords, units)) pixel_coord.standard_name = "projection_x_coordinate" add_time = True except (AttributeError, NotImplementedError): LOG.info('No grid mapping to save.') grid_mapping = None line_coord = cf.DimensionCoordinate( data=cf.Data(np.arange(lines), '1')) line_coord.standard_name = "line" pixel_coord = cf.DimensionCoordinate( data=cf.Data(np.arange(pixels), '1')) pixel_coord.standard_name = "pixel" start_time = cf.dt(dataset.info['start_time']) end_time = cf.dt(dataset.info['end_time']) middle_time = cf.dt((dataset.info['end_time'] - dataset.info['start_time']) / 2 + dataset.info['start_time']) # import ipdb # ipdb.set_trace() if add_time: info = dataset.info dataset = dataset[np.newaxis, :, :] dataset.info = info bounds = cf.CoordinateBounds( data=cf.Data([start_time, end_time], cf.Units('days since 1970-1-1'))) time_coord = cf.DimensionCoordinate( properties=dict(standard_name='time'), data=cf.Data(middle_time, cf.Units('days since 1970-1-1')), bounds=bounds) coords = [time_coord, line_coord, pixel_coord] else: coords = [line_coord, pixel_coord] domain = cf.Domain(dim=coords, aux=aux, ref=grid_mapping) shapes[dataset.shape] = domain data = cf.Data(dataset, dataset.info.get('units', 'm')) # import ipdb # ipdb.set_trace() wanted_keys = ['standard_name', 'long_name'] properties = { k: dataset.info[k] for k in set(wanted_keys) & set(dataset.info.keys()) } new_field = cf.Field(properties=properties, data=data, domain=domain) new_field._FillValue = dataset.fill_value try: new_field.valid_range = dataset.info['valid_range'] except KeyError: new_field.valid_range = new_field.min(), new_field.max() new_field.Conventions = 'CF-1.7' fields.append(new_field) fields[0].history = ("Created by pytroll/satpy on " + str(datetime.utcnow())) flist = cf.FieldList(fields) cf.write(flist, filename, fmt='NETCDF4', compress=6)