def test_DimensionCoordinate_convert_reference_time(self): d = cf.DimensionCoordinate() d.set_data( cf.Data([1, 2, 3], 'months since 2004-1-1', calendar='gregorian')) self.assertTrue((d.array == [1., 2, 3]).all()) e = d.copy() self.assertIsNone( e.convert_reference_time(calendar_months=True, inplace=True)) f = d.convert_reference_time(calendar_months=True) for x in (e, f): self.assertTrue((x.array == [31., 60., 91.]).all()) self.assertTrue((x.datetime_array == [ cf.dt('2004-02-01 00:00:00', calendar='gregorian'), cf.dt('2004-03-01 00:00:00', calendar='gregorian'), cf.dt('2004-04-01 00:00:00', calendar='gregorian') ]).all()) self.assertTrue((d.array == [1., 2, 3]).all()) d = cf.DimensionCoordinate() d.set_data( cf.Data([1, 2, 3], 'months since 2004-1-1', calendar='360_day')) e = d.copy() self.assertIsNone( e.convert_reference_time(calendar_months=True, inplace=True)) f = d.convert_reference_time(calendar_months=True) for x in (e, f): self.assertTrue((x.array == [30., 60., 90.]).all()) self.assertTrue((x.datetime_array == [ cf.dt('2004-02-01 00:00:00', calendar='360_day'), cf.dt('2004-03-01 00:00:00', calendar='360_day'), cf.dt('2004-04-01 00:00:00', calendar='360_day') ]).all()) self.assertTrue((d.array == [1., 2, 3]).all()) d = cf.DimensionCoordinate() d.set_data( cf.Data([1, 2, 3], 'months since 2004-1-1', calendar='noleap')) e = d.copy() self.assertIsNone( e.convert_reference_time(calendar_months=True, inplace=True)) f = d.convert_reference_time(calendar_months=True) for x in (e, f): self.assertTrue((x.array == [31., 59., 90.]).all()) self.assertTrue((x.datetime_array == [ cf.dt('2004-02-01 00:00:00', calendar='noleap'), cf.dt('2004-03-01 00:00:00', calendar='noleap'), cf.dt('2004-04-01 00:00:00', calendar='noleap') ]).all()) self.assertTrue((d.array == [1., 2, 3]).all())
def test_DimensionCoordinate_convert_reference_time(self): d = cf.DimensionCoordinate() d.set_data( cf.Data([1, 2, 3], "months since 2004-1-1", calendar="gregorian")) self.assertTrue((d.array == [1.0, 2, 3]).all()) e = d.copy() self.assertIsNone( e.convert_reference_time(calendar_months=True, inplace=True)) f = d.convert_reference_time(calendar_months=True) for x in (e, f): self.assertTrue((x.array == [31.0, 60.0, 91.0]).all()) self.assertTrue((x.datetime_array == [ cf.dt("2004-02-01 00:00:00", calendar="gregorian"), cf.dt("2004-03-01 00:00:00", calendar="gregorian"), cf.dt("2004-04-01 00:00:00", calendar="gregorian"), ]).all()) self.assertTrue((d.array == [1.0, 2, 3]).all()) d = cf.DimensionCoordinate() d.set_data( cf.Data([1, 2, 3], "months since 2004-1-1", calendar="360_day")) e = d.copy() self.assertIsNone( e.convert_reference_time(calendar_months=True, inplace=True)) f = d.convert_reference_time(calendar_months=True) for x in (e, f): self.assertTrue((x.array == [30.0, 60.0, 90.0]).all()) self.assertTrue((x.datetime_array == [ cf.dt("2004-02-01 00:00:00", calendar="360_day"), cf.dt("2004-03-01 00:00:00", calendar="360_day"), cf.dt("2004-04-01 00:00:00", calendar="360_day"), ]).all()) self.assertTrue((d.array == [1.0, 2, 3]).all()) d = cf.DimensionCoordinate() d.set_data( cf.Data([1, 2, 3], "months since 2004-1-1", calendar="noleap")) e = d.copy() self.assertIsNone( e.convert_reference_time(calendar_months=True, inplace=True)) f = d.convert_reference_time(calendar_months=True) for x in (e, f): self.assertTrue((x.array == [31.0, 59.0, 90.0]).all()) self.assertTrue((x.datetime_array == [ cf.dt("2004-02-01 00:00:00", calendar="noleap"), cf.dt("2004-03-01 00:00:00", calendar="noleap"), cf.dt("2004-04-01 00:00:00", calendar="noleap"), ]).all()) self.assertTrue((d.array == [1.0, 2, 3]).all())
def test_Datetime_rt2dt(self): self.assertEqual( cf.cfdatetime.rt2dt(1, Units('days since 2004-2-28')), numpy.array(cf.dt(2004, 2, 29, calendar='standard'), dtype='O')) self.assertTrue((cf.cfdatetime.rt2dt( [1, 3], Units('days since 2004-2-28')) == numpy.array([ datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2) ])).all()) a = numpy.array([ cf.dt(2004, 2, 29, calendar=None), cf.dt(2004, 3, 2, calendar='gregorian') ], dtype='O') b = cf.cfdatetime.rt2dt([1, 3], Units('days since 2004-2-28')) self.assertTrue((a == b).all())
def test_Datetime_dt2rt(self): units = Units('days since 2004-2-28') self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), numpy.array(1.0)) self.assertTrue((cf.cfdatetime.dt2rt( [datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2)], None, units) == numpy.array([1., 3.])).all()) units = Units('days since 2004-2-28', '360_day') self.assertTrue((cf.cfdatetime.dt2rt( [cf.dt(2004, 2, 29), cf.dt(2004, 3, 1)], None, units) == numpy.array([1., 3.])).all()) units = Units('seconds since 2004-2-28') self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), numpy.array(86400.0))
def test_Datetime(self): d = cf.dt(2003) d = cf.dt(2003, 2) d = cf.dt(2003, 2, 30, calendar='360_day') d = cf.dt(2003, 2, 30, 0, 0, calendar='360_day') d = cf.dt(2003, 2, 30, 0, 0, 0, calendar='360_day') d = cf.dt(2003, 4, 5, 12, 30, 15) d = cf.dt(2003, month=4, day=5, hour=12, minute=30, second=15)
def test_Datetime_dt_vector(self): for v in (2000, [2000], [[2000]], "2000-01-1", ["2000-01-1"]): x = cf.dt_vector(v) self.assertIsInstance(x, np.ndarray) self.assertEqual(x[0], cf.dt(2000, 1, 1)) for v in ([2000, 2001], [[2000], [2001]]): x = cf.dt_vector(v) self.assertIsInstance(x, np.ndarray) self.assertEqual( x.tolist(), [cf.dt(2000, 1, 1), cf.dt(2001, 1, 1)]) for v in ([[2000, 1], [2001, 2]], ["2000-01-1", "2001-02-1"]): x = cf.dt_vector(v) self.assertIsInstance(x, np.ndarray) self.assertEqual( x.tolist(), [cf.dt(2000, 1, 1), cf.dt(2001, 2, 1)])
def test_Datetime_rt2dt(self): for a in (1, np.array(1), np.ma.array(1)): self.assertEqual( cf.cfdatetime.rt2dt(a, Units("days since 2004-2-28")), np.array(cf.dt(2004, 2, 29, calendar="standard"), dtype="O"), ) for a in (np.ma.array(1, mask=True), np.ma.array([1], mask=True)): b = cf.cfdatetime.rt2dt(a, Units("days since 2004-2-28")) self.assertIsInstance(b, np.ndarray) self.assertEqual(b.mask, True) self.assertTrue( (cf.cfdatetime.rt2dt([1, 3], Units("days since 2004-2-28")) == np.array([ datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2), ])).all()) a = np.array( [ cf.dt(2004, 2, 29, calendar=None), cf.dt(2004, 3, 2, calendar="gregorian"), ], dtype="O", ) b = cf.cfdatetime.rt2dt([1, 3], Units("days since 2004-2-28")) self.assertTrue((a == b).all()) for a in ( np.ma.array(3), np.ma.array([3]), ): b = cf.cfdatetime.rt2dt(a, Units("days since 1970-01-01")) self.assertEqual(b, cf.dt(1970, 1, 4, calendar="gregorian")) for a in ( np.ma.array(3, mask=True), np.ma.array([3], mask=True), ): b = cf.cfdatetime.rt2dt(a, Units("days since 1970-01-01")) self.assertEqual(b.mask, True)
def test_Datetime_dt2rt(self): units = Units("days since 2004-2-28") self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), np.array(1.0), ) self.assertTrue((cf.cfdatetime.dt2rt( [ datetime.datetime(2004, 2, 29), datetime.datetime(2004, 3, 2), ], None, units, ) == np.array([1.0, 3.0])).all()) units = Units("days since 2004-2-28", "360_day") self.assertTrue((cf.cfdatetime.dt2rt( [cf.dt(2004, 2, 29), cf.dt(2004, 3, 1)], None, units) == np.array([1.0, 3.0])).all()) units = Units("seconds since 2004-2-28") self.assertEqual( cf.cfdatetime.dt2rt(datetime.datetime(2004, 2, 29), None, units), np.array(86400.0), )
def test_Datetime_Data(self): d = cf.Data([1, 2, 3], "days since 2004-02-28") self.assertTrue((d < cf.dt(2005, 2, 28)).all()) with self.assertRaises(Exception): d < cf.dt(2005, 2, 29) with self.assertRaises(Exception): d < cf.dt(2005, 2, 29, calendar="360_day") d = cf.Data([1, 2, 3], "days since 2004-02-28", calendar="360_day") self.assertTrue((d < cf.dt(2005, 2, 28)).all()) self.assertTrue((d < cf.dt(2005, 2, 29)).all()) self.assertTrue((d < cf.dt(2005, 2, 30)).all()) with self.assertRaises(Exception): d < cf.dt(2005, 2, 31) with self.assertRaises(Exception): d < cf.dt(2005, 2, 29, calendar="noleap")
def test_Query_datetime1(self): d = cf.Data([[1., 5.], [6, 2]], 'days since 2000-12-29 21:00:00', calendar='standard') message = 'Diff ='+str((d-cf.Data(cf.dt( '2001-01-03 21:00:00', calendar='standard'))).array) self.assertTrue( (d == cf.eq(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, True], [False, False]]), verbose=2), message ) self.assertTrue( (d == cf.ne(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, False], [True, True]])), message ) self.assertTrue( (d == cf.ge(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, True], [True, False]])), message ) self.assertTrue( (d == cf.gt(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, False], [True, False]])), message ) self.assertTrue( (d == cf.le(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, True], [False, True]])), message ) self.assertTrue( (d == cf.lt(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, False], [False, True]])), message ) self.assertTrue( (d == cf.wi(cf.dt('2000-12-31 21:00:00'), cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, True], [False, True]])), message ) self.assertTrue( (d == cf.wo(cf.dt('2000-12-31 21:00:00'), cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, False], [True, False]])), message ) self.assertTrue( (d == cf.set([cf.dt('2000-12-31 21:00:00'), cf.dt('2001-01-03 21:00:00')])).equals( cf.Data([[False, True], [False, True]])), message ) _ = cf.seasons() [cf.seasons(n, start) for n in [1, 2, 3, 4, 6, 12] for start in range(1, 13)] with self.assertRaises(Exception): cf.seasons(13) with self.assertRaises(Exception): cf.seasons(start=8.456) _ = cf.mam() _ = cf.djf() _ = cf.jja() _ = cf.son()
def test_TimeDuration_bounds(self): for direction in (True, False): for x, y in zip( [ cf.Y().bounds(cf.dt(1984, 1, 1), direction=direction), cf.Y().bounds(cf.dt(1984, 12, 1), direction=direction), cf.Y().bounds(cf.dt(1984, 12, 3), direction=direction), cf.Y(month=9).bounds( cf.dt(1984, 1, 1), direction=direction ), cf.Y(month=9).bounds( cf.dt(1984, 3, 3), direction=direction ), cf.Y(month=9).bounds( cf.dt(1984, 9, 20), direction=direction ), cf.Y(month=9, day=13).bounds( cf.dt(1984, 12, 12), direction=direction ), ], [ ( cf.dt("1984-01-01", calendar="standard"), cf.dt("1985-01-01", calendar="standard"), ), ( cf.dt("1984-01-01", calendar="standard"), cf.dt("1985-01-01", calendar="standard"), ), ( cf.dt("1984-01-01", calendar="standard"), cf.dt("1985-01-01", calendar="standard"), ), ( cf.dt("1983-09-01", calendar="standard"), cf.dt("1984-09-01", calendar="standard"), ), ( cf.dt("1983-09-01", calendar="standard"), cf.dt("1984-09-01", calendar="standard"), ), ( cf.dt("1984-09-01", calendar="standard"), cf.dt("1985-09-01", calendar="standard"), ), ( cf.dt("1984-09-13", calendar="standard"), cf.dt("1985-09-13", calendar="standard"), ), ], ): if direction is False: y = y[::-1] self.assertEqual(x, y, f"{x}!={y} direction={direction}") for x, y in zip( [ cf.M().bounds(cf.dt(1984, 1, 1), direction=direction), cf.M().bounds(cf.dt(1984, 12, 1), direction=direction), cf.M().bounds(cf.dt(1984, 12, 3), direction=direction), cf.M(day=15).bounds( cf.dt(1984, 12, 1), direction=direction ), cf.M(day=15).bounds( cf.dt(1984, 12, 3), direction=direction ), cf.M(day=15).bounds( cf.dt(1984, 12, 15), direction=direction ), cf.M(day=15).bounds( cf.dt(1984, 12, 20), direction=direction ), ], [ ( cf.dt("1984-01-01", calendar="standard"), cf.dt("1984-02-01", calendar="standard"), ), ( cf.dt("1984-12-01", calendar="standard"), cf.dt("1985-01-01", calendar="standard"), ), ( cf.dt("1984-12-01", calendar="standard"), cf.dt("1985-01-01", calendar="standard"), ), ( cf.dt("1984-11-15", calendar="standard"), cf.dt("1984-12-15", calendar="standard"), ), ( cf.dt("1984-11-15", calendar="standard"), cf.dt("1984-12-15", calendar="standard"), ), ( cf.dt("1984-12-15", calendar="standard"), cf.dt("1985-01-15", calendar="standard"), ), ( cf.dt("1984-12-15", calendar="standard"), cf.dt("1985-01-15", calendar="standard"), ), ], ): if direction is False: y = y[::-1] self.assertEqual(x, y, f"{x}!={y}") for x, y in zip( [ cf.D().bounds(cf.dt(1984, 1, 1), direction=direction), cf.D().bounds(cf.dt(1984, 12, 3), direction=direction), cf.D(hour=15).bounds( cf.dt(1984, 12, 1), direction=direction ), cf.D(hour=15).bounds( cf.dt(1984, 12, 1, 12), direction=direction ), cf.D(hour=15).bounds( cf.dt(1984, 12, 1, 15), direction=direction ), cf.D(hour=15).bounds( cf.dt(1984, 12, 1, 20), direction=direction ), ], [ ( cf.dt("1984-01-01", calendar="gregorian"), cf.dt("1984-01-02", calendar="gregorian"), ), ( cf.dt("1984-12-03", calendar="gregorian"), cf.dt("1984-12-04", calendar="gregorian"), ), ( cf.dt("1984-11-30 15:00", calendar="gregorian"), cf.dt("1984-12-01 15:00", calendar="gregorian"), ), ( cf.dt("1984-11-30 15:00", calendar="gregorian"), cf.dt("1984-12-01 15:00", calendar="gregorian"), ), ( cf.dt("1984-12-01 15:00", calendar="gregorian"), cf.dt("1984-12-02 15:00", calendar="gregorian"), ), ( cf.dt("1984-12-01 15:00", calendar="gregorian"), cf.dt("1984-12-02 15:00", calendar="gregorian"), ), ], ): if direction is False: y = y[::-1] self.assertEqual(x, y, f"{x}!={y}")
def test_Datetime(self): cf.dt(2003) cf.dt(2003, 2) cf.dt(2003, 2, 30, calendar="360_day") cf.dt(2003, 2, 30, 0, 0, calendar="360_day") cf.dt(2003, 2, 30, 0, 0, 0, calendar="360_day") cf.dt(2003, 4, 5, 12, 30, 15) cf.dt(2003, month=4, day=5, hour=12, minute=30, second=15)
def test_TimeDuration_arithmetic(self): self.assertEqual(cf.M() + cf.dt(2000, 1, 1), cf.dt(2000, 2, 1)) self.assertEqual(cf.M() * 8, cf.M(8)) self.assertEqual(cf.M() * 8.5, cf.M(8.5)) self.assertEqual(cf.dt(2000, 1, 1) + cf.M(), cf.dt(2000, 2, 1)) self.assertEqual(cf.dt(2000, 1, 1) - cf.M(), cf.dt(1999, 12, 1)) self.assertEqual( cf.M() + datetime.datetime(2000, 1, 1), cf.dt(2000, 2, 1, calendar="gregorian"), ) self.assertEqual( datetime.datetime(2000, 1, 1) + cf.M(), cf.dt(2000, 2, 1, calendar="gregorian"), ) self.assertEqual( datetime.datetime(2000, 1, 1) - cf.M(), cf.dt(1999, 12, 1, calendar="gregorian"), ) d = cf.dt(2000, 1, 1) d += cf.M() self.assertEqual(d, cf.dt(2000, 2, 1)) d -= cf.M() self.assertEqual(d, cf.dt(2000, 1, 1)) d = datetime.datetime(2000, 1, 1) d += cf.M() self.assertEqual(d, cf.dt(2000, 2, 1, calendar="gregorian")) d -= cf.M() self.assertEqual(d, cf.dt(2000, 1, 1, calendar="gregorian")) self.assertEqual(cf.M() * 8, cf.M(8)) self.assertEqual(cf.M() * 8.5, cf.M(8.5)) self.assertEqual(cf.M() / 2.0, cf.M(0.5)) self.assertEqual(cf.M(8) / 3, cf.M(8 / 3)) self.assertEqual(cf.M(8) // 3, cf.M(2)) # Test arithmetic involving Data as well as datetimes: da = cf.Data([2], units="days since 2000-01-01") dt = cf.TimeDuration(14, "day") t0 = da + dt t1 = dt + da self.assertEqual( t0, cf.dt(2000, 1, 17, calendar="gregorian"), ) self.assertEqual(t0, t1) t2 = dt - da t3 = da - dt self.assertEqual( t2, cf.dt(1999, 12, 20, calendar="gregorian"), ) self.assertEqual(t2, t3)
# patch dimensions x = cf.DimensionCoordinate(data=cf.Data(lon, units='degrees_east'), properties={'standard_name': 'longitude'}) x.axis = 'X' y = cf.DimensionCoordinate(data=cf.Data(lat, units='degrees_north'), properties={'standard_name': 'latitude'}) y.axis = 'Y' z = cf.DimensionCoordinate(data=cf.Data(height, units='m'), properties={'standard_name': 'height'}) z.axis = 'Z' # create list of all datetimes in cf format wrf_datetime_list = [] for index, item in enumerate(wrflist): wrf_datetime = cf.dt( datetime.datetime.strptime( str(getvar(wrflist[index], "Times", meta=False)), '%Y-%m-%dT%H:00:00.000000000')) wrf_datetime_list.extend([wrf_datetime]) # create dimension out of the datetimes t = cf.DimensionCoordinate(data=cf.Data(wrf_datetime_list, units='hours since ' + str(wrf_datetime.year) + '-1-1'), properties={'standard_name': 'time'}) t.axis = 'T' # define all the variables that I want to extract from wrfchem # additional variables that I want that cause issues = pres, rh, rh_2m # *** the issue before was that this list was going over two lines and was breaking *** # *** the issue before was that this list was going over 72 characters and breaking *** variables = ['PSFC','PBLH','RAINC','RAINNC','U10','V10','T','T2', \
def test_FieldList_select_by_construct(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.read([self.filename, self.filename2]) g = f.select_by_construct() self.assertEqual(len(g), 2) g = f.select_by_construct('latitude') self.assertEqual(len(g), 2) g = f.select_by_construct('latitude', 'longitude') self.assertEqual(len(g), 2) g = f.select_by_construct('latitude', 'time') self.assertEqual(len(g), 1) g = f.select_by_construct('latitude', 'time', OR=False) self.assertEqual(len(g), 1) g = f.select_by_construct('latitude', 'time', OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(0)) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01'))) self.assertEqual(len(g), 1) g = f.select_by_construct(longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct('longitude', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct('latitude', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct('time', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct('time', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=False) self.assertEqual(len(g), 1) g = f.select_by_construct('time', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=False) self.assertEqual(len(g), 1) g = f.select_by_construct('qwerty') self.assertEqual(len(g), 0) g = f.select_by_construct('qwerty', 'latitude') self.assertEqual(len(g), 0) g = f.select_by_construct('qwerty', 'latitude', OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct('qwerty', 'time', 'longitude') self.assertEqual(len(g), 0) g = f.select_by_construct('qwerty', 'time', 'longitude', OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(70)) self.assertEqual(len(g), 1) g = f.select_by_construct(longitude=cf.gt(0), time=cf.le(cf.dt('1999-12-01'))) self.assertEqual(len(g), 0)
def test_FieldList_select_by_construct(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return x = self.x.copy() x.del_construct('time') f = cf.FieldList(x) f.extend(self.f2.copy()) g = f.select_by_construct() self.assertEqual(len(g), 2) g = f.select_by_construct('latitude') self.assertEqual(len(g), 2) g = f.select_by_construct('latitude', 'longitude') self.assertEqual(len(g), 2) g = f.select_by_construct('latitude', 'time') self.assertEqual(len(g), 1) g = f.select_by_construct('latitude', 'time', OR=False) self.assertEqual(len(g), 1) g = f.select_by_construct('latitude', 'time', OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(0)) self.assertEqual(len(g), 2) g = f.select_by_construct( longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01'))) self.assertEqual(len(g), 1) g = f.select_by_construct( longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct( 'longitude', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True ) self.assertEqual(len(g), 2) g = f.select_by_construct( 'latitude', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True ) self.assertEqual(len(g), 2) g = f.select_by_construct( 'time', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=True ) self.assertEqual(len(g), 2) g = f.select_by_construct( 'time', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=False ) self.assertEqual(len(g), 1) g = f.select_by_construct( 'time', longitude=cf.gt(0), time=cf.le(cf.dt('2008-12-01')), OR=False ) self.assertEqual(len(g), 1) g = f.select_by_construct('qwerty') self.assertEqual(len(g), 0) g = f.select_by_construct('qwerty', 'latitude') self.assertEqual(len(g), 0) g = f.select_by_construct('qwerty', 'latitude', OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct('qwerty', 'time', 'longitude') self.assertEqual(len(g), 0) g = f.select_by_construct('qwerty', 'time', 'longitude', OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(7.6)) self.assertEqual(len(g), 1) g = f.select_by_construct( longitude=cf.gt(0), time=cf.le(cf.dt('1999-12-01'))) self.assertEqual(len(g), 0)
def test_FieldList_select_by_construct(self): x = self.x.copy() x.del_construct("time") f = cf.FieldList(x) f.extend(self.f2.copy()) g = f.select_by_construct() self.assertEqual(len(g), 2) g = f.select_by_construct("latitude") self.assertEqual(len(g), 2) g = f.select_by_construct("latitude", "longitude") self.assertEqual(len(g), 2) g = f.select_by_construct("latitude", "time") self.assertEqual(len(g), 1) g = f.select_by_construct("latitude", "time", OR=False) self.assertEqual(len(g), 1) g = f.select_by_construct("latitude", "time", OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(0)) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01"))) self.assertEqual(len(g), 1) g = f.select_by_construct(longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01")), OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct( "longitude", longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01")), OR=True, ) self.assertEqual(len(g), 2) g = f.select_by_construct( "latitude", longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01")), OR=True, ) self.assertEqual(len(g), 2) g = f.select_by_construct( "time", longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01")), OR=True, ) self.assertEqual(len(g), 2) g = f.select_by_construct( "time", longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01")), OR=False, ) self.assertEqual(len(g), 1) g = f.select_by_construct( "time", longitude=cf.gt(0), time=cf.le(cf.dt("2008-12-01")), OR=False, ) self.assertEqual(len(g), 1) g = f.select_by_construct("qwerty") self.assertEqual(len(g), 0) g = f.select_by_construct("qwerty", "latitude") self.assertEqual(len(g), 0) g = f.select_by_construct("qwerty", "latitude", OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct("qwerty", "time", "longitude") self.assertEqual(len(g), 0) g = f.select_by_construct("qwerty", "time", "longitude", OR=True) self.assertEqual(len(g), 2) g = f.select_by_construct(longitude=cf.gt(7.6)) self.assertEqual(len(g), 1) g = f.select_by_construct(longitude=cf.gt(0), time=cf.le(cf.dt("1999-12-01"))) self.assertEqual(len(g), 0)
def test_TimeDuration_interval(self): self.assertTrue(cf.M().interval(cf.dt(1999, 12)) == ( cf.dt('1999-12-01 00:00:00', calendar=None), cf.dt('2000-01-01 00:00:00', calendar=None))) self.assertTrue( cf.Y(2).interval(cf.dt(2000, 2), end=True) == ( cf.dt('1998-02-01 00:00:00', calendar=None), cf.dt('2000-02-01 00:00:00', calendar=None))) self.assertTrue( cf.D(30).interval(cf.dt(1983, 12, 1, 6)) == ( cf.dt('1983-12-01 06:00:00', calendar=None), cf.dt('1983-12-31 06:00:00', calendar=None))) self.assertTrue( cf.D(30).interval(cf.dt(1983, 12, 1, 6), end=True) == ( cf.dt('1983-11-01 06:00:00', calendar=None), cf.dt('1983-12-01 06:00:00', calendar=None))) self.assertTrue( cf.D(0).interval(cf.dt(1984, 2, 3)) == ( cf.dt('1984-02-03 00:00:00', calendar=None), cf.dt('1984-02-03 00:00:00', calendar=None))) self.assertTrue( cf.D(5, hour=6).interval(cf.dt(2004, 3, 2), end=True) == ( cf.dt('2004-02-26 00:00:00', calendar=None), cf.dt('2004-03-02 00:00:00', calendar=None))) self.assertTrue( cf.D(5, hour=6).interval( cf.dt(2004, 3, 2, calendar='noleap'), end=True) == ( cf.dt('2004-02-25 00:00:00', calendar='noleap'), cf.dt('2004-03-02 00:00:00', calendar='noleap'))) self.assertTrue( cf.D(5, hour=6).interval( cf.dt(2004, 3, 2, calendar='360_day'), end=True) == ( cf.dt('2004-02-27 00:00:00', calendar='360_day'), cf.dt('2004-03-02 00:00:00', calendar='360_day'))) self.assertTrue( cf.h(19897.5).interval(cf.dt(1984, 2, 3, 0)) == ( cf.dt('1984-02-03 00:00:00', calendar=None), cf.dt('1986-05-12 01:30:00', calendar=None))) self.assertTrue( cf.h(19897.6).interval(cf.dt(1984, 2, 3, 0), end=True) == ( cf.dt('1981-10-26 22:24:00', calendar=None), cf.dt('1984-02-03 00:00:00', calendar=None)))
def test_Query_dteq_dtne_dtge_dtgt_dtle_dtlt(self): d = cf.Data([[1., 5.], [6, 2]], 'days since 2000-12-29 21:00:00') message = 'Diff =' + str((d-cf.Data( cf.dt('2001-01-03 21:00:00', calendar='standard'))).array) self.assertTrue( (d == cf.eq(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, True], [False, False]])), message ) self.assertTrue( (d == cf.ne(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, False], [True, True]])), message ) self.assertTrue( (d == cf.ge(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, True], [True, False]])), message ) self.assertTrue( (d == cf.gt(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[False, False], [True, False]])), message ) self.assertTrue( (d == cf.le(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, True], [False, True]])), message ) self.assertTrue( (d == cf.lt(cf.dt('2001-01-03 21:00:00'))).equals( cf.Data([[True, False], [False, True]])), message ) self.assertTrue( (d == cf.eq(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [False, False]])), message ) self.assertTrue( (d == cf.ne(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [True, True]])), message ) self.assertTrue( (d == cf.ge(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [True, False]])), message ) self.assertTrue( (d == cf.gt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, False], [True, False]])), message ) self.assertTrue( (d == cf.le(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, True], [False, True]])), message ) self.assertTrue( (d == cf.lt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [False, True]])), message ) d = cf.dt(2002, 6, 16) self.assertEqual(cf.eq(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.eq(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.eq(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.eq(cf.dt('2001-1-1')) & cf.eq(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ge(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.ge(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.ge(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.ge(cf.dt('2001-1-1')) & cf.ge(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.gt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.gt(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.gt(cf.dt('2100-1-1')), d) self.assertEqual( cf.gt(cf.dt('2001-1-1')) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ne(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.ne(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.ne(cf.dt('2100-1-1')), d) self.assertEqual( cf.ne(cf.dt('2001-1-1')) & cf.ne(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.le(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.le(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.le(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.le(cf.dt('2001-1-1')) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.lt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.lt(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.lt(cf.dt('2100-1-1')), d) self.assertNotEqual( cf.lt(cf.dt('2001-1-1')) & cf.lt(cf.dt(2010, 12, 31)), d)
def test_TimeDuration_arithmetic(self): self.assertEqual(cf.M() + cf.dt(2000, 1, 1), cf.dt(2000, 2, 1)) self.assertEqual(cf.M() * 8, cf.M(8)) self.assertEqual(cf.M() * 8.5, cf.M(8.5)) self.assertEqual(cf.dt(2000, 1, 1) + cf.M(), cf.dt(2000, 2, 1)) self.assertEqual(cf.dt(2000, 1, 1) - cf.M(), cf.dt(1999, 12, 1)) self.assertEqual( cf.M() + datetime.datetime(2000, 1, 1), cf.dt(2000, 2, 1, calendar="gregorian"), ) self.assertEqual( datetime.datetime(2000, 1, 1) + cf.M(), cf.dt(2000, 2, 1, calendar="gregorian"), ) self.assertEqual( datetime.datetime(2000, 1, 1) - cf.M(), cf.dt(1999, 12, 1, calendar="gregorian"), ) d = cf.dt(2000, 1, 1) d += cf.M() self.assertEqual(d, cf.dt(2000, 2, 1)) d -= cf.M() self.assertEqual(d, cf.dt(2000, 1, 1)) d = datetime.datetime(2000, 1, 1) d += cf.M() self.assertEqual(d, cf.dt(2000, 2, 1, calendar="gregorian")) d -= cf.M() self.assertEqual(d, cf.dt(2000, 1, 1, calendar="gregorian")) self.assertEqual(cf.M() * 8, cf.M(8)) self.assertEqual(cf.M() * 8.5, cf.M(8.5)) self.assertEqual(cf.M() / 2.0, cf.M(0.5)) self.assertEqual(cf.M(8) / 3, cf.M(8 / 3)) self.assertEqual(cf.M(8) // 3, cf.M(2))
def test_Query_dteq_dtne_dtge_dtgt_dtle_dtlt(self): d = cf.Data([[1.0, 5.0], [6, 2]], "days since 2000-12-29 21:00:00") message = "Diff =" + str( (d - cf.Data(cf.dt("2001-01-03 21:00:00", calendar="standard"))).array) self.assertTrue( (d == cf.eq(cf.dt("2001-01-03 21:00:00"))).equals( cf.Data([[False, True], [False, False]])), message, ) self.assertTrue( (d == cf.ne(cf.dt("2001-01-03 21:00:00"))).equals( cf.Data([[True, False], [True, True]])), message, ) self.assertTrue( (d == cf.ge(cf.dt("2001-01-03 21:00:00"))).equals( cf.Data([[False, True], [True, False]])), message, ) self.assertTrue( (d == cf.gt(cf.dt("2001-01-03 21:00:00"))).equals( cf.Data([[False, False], [True, False]])), message, ) self.assertTrue( (d == cf.le(cf.dt("2001-01-03 21:00:00"))).equals( cf.Data([[True, True], [False, True]])), message, ) self.assertTrue( (d == cf.lt(cf.dt("2001-01-03 21:00:00"))).equals( cf.Data([[True, False], [False, True]])), message, ) self.assertTrue( (d == cf.eq(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [False, False]])), message, ) self.assertTrue( (d == cf.ne(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [True, True]])), message, ) self.assertTrue( (d == cf.ge(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, True], [True, False]])), message, ) self.assertTrue( (d == cf.gt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[False, False], [True, False]])), message, ) self.assertTrue( (d == cf.le(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, True], [False, True]])), message, ) self.assertTrue( (d == cf.lt(cf.dt(2001, 1, 3, 21, 0, 0))).equals( cf.Data([[True, False], [False, True]])), message, ) d = cf.dt(2002, 6, 16) self.assertEqual(cf.eq(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.eq(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.eq(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.eq(cf.dt("2001-1-1")) & cf.eq(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ge(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.ge(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.ge(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.ge(cf.dt("2001-1-1")) & cf.ge(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.gt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.gt(cf.dt(2002, 6, 16)), d) self.assertNotEqual(cf.gt(cf.dt("2100-1-1")), d) self.assertEqual( cf.gt(cf.dt("2001-1-1")) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertEqual(cf.ne(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.ne(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.ne(cf.dt("2100-1-1")), d) self.assertEqual( cf.ne(cf.dt("2001-1-1")) & cf.ne(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.le(cf.dt(1990, 1, 1)), d) self.assertEqual(cf.le(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.le(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.le(cf.dt("2001-1-1")) & cf.le(cf.dt(2010, 12, 31)), d) d = cf.dt(2002, 6, 16) self.assertNotEqual(cf.lt(cf.dt(1990, 1, 1)), d) self.assertNotEqual(cf.lt(cf.dt(2002, 6, 16)), d) self.assertEqual(cf.lt(cf.dt("2100-1-1")), d) self.assertNotEqual( cf.lt(cf.dt("2001-1-1")) & cf.lt(cf.dt(2010, 12, 31)), d)
def test_TimeDuration_bounds(self): for direction in (True, False): for x, y in zip([ cf.Y().bounds(cf.dt(1984, 1, 1), direction=direction), cf.Y().bounds(cf.dt(1984, 12, 1), direction=direction), cf.Y().bounds(cf.dt(1984, 12, 3), direction=direction), cf.Y(month=9).bounds(cf.dt(1984, 1, 1), direction=direction), cf.Y(month=9).bounds(cf.dt(1984, 3, 3), direction=direction), cf.Y(month=9).bounds(cf.dt(1984, 9, 20), direction=direction), cf.Y(month=9, day=13).bounds(cf.dt(1984, 12, 12), direction=direction), ], [ (cf.dt('1984-01-01', calendar='standard'), cf.dt('1985-01-01', calendar='standard')), (cf.dt('1984-01-01', calendar='standard'), cf.dt('1985-01-01', calendar='standard')), (cf.dt('1984-01-01', calendar='standard'), cf.dt('1985-01-01', calendar='standard')), (cf.dt('1983-09-01', calendar='standard'), cf.dt('1984-09-01', calendar='standard')), (cf.dt('1983-09-01', calendar='standard'), cf.dt('1984-09-01', calendar='standard')), (cf.dt('1984-09-01', calendar='standard'), cf.dt('1985-09-01', calendar='standard')), (cf.dt('1984-09-13', calendar='standard'), cf.dt('1985-09-13', calendar='standard')), ]): if direction is False: y = y[::-1] self.assertTrue(x == y, "{}!={} direction={}".format(x, y, direction)) for x, y in zip([ cf.M().bounds(cf.dt(1984, 1, 1), direction=direction), cf.M().bounds(cf.dt(1984, 12, 1), direction=direction), cf.M().bounds(cf.dt(1984, 12, 3), direction=direction), cf.M(day=15).bounds(cf.dt(1984, 12, 1), direction=direction), cf.M(day=15).bounds(cf.dt(1984, 12, 3), direction=direction), cf.M(day=15).bounds(cf.dt(1984, 12, 15), direction=direction), cf.M(day=15).bounds(cf.dt(1984, 12, 20), direction=direction), ], [ (cf.dt('1984-01-01', calendar='standard'), cf.dt('1984-02-01', calendar='standard')), (cf.dt('1984-12-01', calendar='standard'), cf.dt('1985-01-01', calendar='standard')), (cf.dt('1984-12-01', calendar='standard'), cf.dt('1985-01-01', calendar='standard')), (cf.dt('1984-11-15', calendar='standard'), cf.dt('1984-12-15', calendar='standard')), (cf.dt('1984-11-15', calendar='standard'), cf.dt('1984-12-15', calendar='standard')), (cf.dt('1984-12-15', calendar='standard'), cf.dt('1985-01-15', calendar='standard')), (cf.dt('1984-12-15', calendar='standard'), cf.dt('1985-01-15', calendar='standard')), ]): if direction is False: y = y[::-1] self.assertTrue(x == y, "{}!={}".format(x, y)) for x, y in zip([ cf.D().bounds(cf.dt(1984, 1, 1), direction=direction), cf.D().bounds(cf.dt(1984, 12, 3), direction=direction), cf.D(hour=15).bounds(cf.dt(1984, 12, 1), direction=direction), cf.D(hour=15).bounds(cf.dt(1984, 12, 1, 12), direction=direction), cf.D(hour=15).bounds(cf.dt(1984, 12, 1, 15), direction=direction), cf.D(hour=15).bounds(cf.dt(1984, 12, 1, 20), direction=direction), ], [ (cf.dt('1984-01-01', calendar='gregorian'), cf.dt('1984-01-02', calendar='gregorian')), (cf.dt('1984-12-03', calendar='gregorian'), cf.dt('1984-12-04', calendar='gregorian')), (cf.dt('1984-11-30 15:00', calendar='gregorian'), cf.dt('1984-12-01 15:00', calendar='gregorian')), (cf.dt('1984-11-30 15:00', calendar='gregorian'), cf.dt('1984-12-01 15:00', calendar='gregorian')), (cf.dt('1984-12-01 15:00', calendar='gregorian'), cf.dt('1984-12-02 15:00', calendar='gregorian')), (cf.dt('1984-12-01 15:00', calendar='gregorian'), cf.dt('1984-12-02 15:00', calendar='gregorian')), ]): if direction is False: y = y[::-1] self.assertTrue(x == y, "{}!={}".format(x, y))
def test_TimeDuration_arithmetic(self): self.assertTrue(cf.M() + cf.dt(2000, 1, 1) == cf.dt(2000, 2, 1)) self.assertTrue(cf.M() * 8 == cf.M(8)) self.assertTrue(cf.M() * 8.5 == cf.M(8.5)) self.assertTrue(cf.dt(2000, 1, 1) + cf.M() == cf.dt(2000, 2, 1)) self.assertTrue(cf.dt(2000, 1, 1) - cf.M() == cf.dt(1999, 12, 1)) self.assertTrue(cf.M() + datetime.datetime(2000, 1, 1) == cf.dt(2000, 2, 1)) self.assertTrue( datetime.datetime(2000, 1, 1) + cf.M() == cf.dt(2000, 2, 1)) self.assertTrue( datetime.datetime(2000, 1, 1) - cf.M() == cf.dt(1999, 12, 1)) d = cf.dt(2000, 1, 1) d += cf.M() self.assertTrue(d == cf.dt(2000, 2, 1)) d -= cf.M() self.assertTrue(d == cf.dt(2000, 1, 1)) d = datetime.datetime(2000, 1, 1) d += cf.M() self.assertTrue(d == cf.dt(2000, 2, 1)) d -= cf.M() self.assertTrue(d == cf.dt(2000, 1, 1)) self.assertTrue(cf.M() * 8 == cf.M(8)) self.assertTrue(cf.M() * 8.5 == cf.M(8.5)) self.assertTrue(cf.M() / 2.0 == cf.M(0.5)) self.assertTrue(cf.M(8) / 3 == cf.M(8 / 3)) self.assertTrue(cf.M(8) // 3 == cf.M(2))
t.size print(t.domain_axes) t t.data.shape t.get_data_axes() data = t.del_data() t.has_data() t.set_data(data) t.data d = cf.Data([1, 2, 3], units='days since 2004-2-28') print(d.array) print(d.datetime_array) e = cf.Data([1, 2, 3], units='days since 2004-2-28', calendar='360_day') print(d.array) print(d.datetime_array) date_time = cf.dt(2004, 2, 29) date_time d = cf.Data(date_time, calendar='gregorian') print(d.array) d.datetime_array date_times = cf.dt_vector(['2004-02-29', '2004-02-30', '2004-03-01'], calendar='360_day') print(date_times) e = cf.Data(date_times) print(e.array) print(e.datetime_array) d = cf.Data(['2004-02-29', '2004-02-30', '2004-03-01'], calendar='360_day') d.Units print(d.array) print(d.datetime_array) e = cf.Data(['2004-02-29', '2004-03-01', '2004-03-02'], dt=True)
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)