def test_FieldList_append_extend(self): # Append f = cf.FieldList() f.append(self.x) self.assertEqual(len(f), 1) self.assertIsInstance(f, cf.FieldList) f.append(f[0]) self.assertEqual(len(f), 2) self.assertIsInstance(f, cf.FieldList) f.append(f[0]) self.assertEqual(len(f), 3) # Extend f = cf.FieldList() f.extend([self.x]) self.assertEqual(len(f), 1) self.assertIsInstance(f, cf.FieldList) f.extend(f) self.assertEqual(len(f), 2) self.assertIsInstance(f, cf.FieldList) f.extend(f) self.assertEqual(len(f), 4)
def test_FieldList_append_extend(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # Append f = cf.FieldList() f.append(cf.read(self.filename)[0]) self.assertEqual(len(f), 1) self.assertIsInstance(f, cf.FieldList) f.append(f[0].copy()) self.assertEqual(len(f), 2) self.assertIsInstance(f, cf.FieldList) f.append(f[0].copy()) self.assertEqual(len(f), 3) # Extend f = cf.FieldList() f.extend(cf.read(self.filename)) self.assertEqual(len(f), 1) self.assertIsInstance(f, cf.FieldList) f.extend(f.copy()) self.assertEqual(len(f), 2) self.assertIsInstance(f, cf.FieldList) f.extend(f.copy()) self.assertEqual(len(f), 4)
def test_FieldList__mul__imul__(self): f = cf.FieldList() f = f * 4 self.assertEqual(len(f), 0) self.assertIsInstance(f, cf.FieldList) f = cf.FieldList() f *= 4 self.assertEqual(len(f), 0) self.assertIsInstance(f, cf.FieldList) f = cf.FieldList(self.x) * 4 self.assertEqual(len(f), 4) self.assertIsInstance(f, cf.FieldList) f = cf.FieldList(self.x) f *= 4 self.assertEqual(len(f), 4) self.assertIsInstance(f, cf.FieldList) f = f * 2 self.assertEqual(len(f), 8) self.assertIsInstance(f, cf.FieldList) f *= 3 self.assertEqual(len(f), 24) self.assertIsInstance(f, cf.FieldList)
def test_FieldList__mul__imul__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList() f = f * 4 self.assertEqual(len(f), 0) self.assertIsInstance(f, cf.FieldList) f = cf.FieldList() f *= 4 self.assertEqual(len(f), 0) self.assertIsInstance(f, cf.FieldList) f = cf.read(self.filename) f = f * 4 self.assertEqual(len(f), 4) self.assertIsInstance(f, cf.FieldList) f = cf.read(self.filename) f *= 4 self.assertEqual(len(f), 4) self.assertIsInstance(f, cf.FieldList) f = f * 2 self.assertEqual(len(f), 8) self.assertIsInstance(f, cf.FieldList) f *= 3 self.assertEqual(len(f), 24) self.assertIsInstance(f, cf.FieldList)
def test_FieldList__len__(self): f = cf.FieldList(self.x) self.assertEqual(len(cf.FieldList()), 0) self.assertEqual(len(f), 1) f.append(f[0]) self.assertEqual(len(f), 2) f.extend(f) self.assertEqual(len(f), 4)
def test_FieldList__len__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) self.assertEqual(len(cf.FieldList()), 0) self.assertEqual(len(f), 1) f.append(f[0]) self.assertEqual(len(f), 2) f.extend(f) self.assertEqual(len(f), 4)
def test_FieldList_insert_pop_remove(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # Insert f = cf.FieldList(self.x) g = f[0].copy() f.insert(0, g) self.assertEqual(len(f), 2) self.assertIsInstance(f, cf.FieldList) g = g + 10 f.insert(-1, g) self.assertEqual(len(f), 3) self.assertEqual(f[0].maximum(), (f[1].maximum() - 10)) self.assertIsInstance(f, cf.FieldList) # Pop f = cf.FieldList(self.x) g = f[0] h = f[0] + 10 f.append(h) z = f.pop(0) self.assertIs(z, g) self.assertEqual(len(f), 1) self.assertIsInstance(f, cf.FieldList) z = f.pop(-1) self.assertIs(z, h) self.assertEqual(len(f), 0) self.assertIsInstance(f, cf.FieldList) # Remove f = cf.FieldList(self.x) g = f[0] + 10 f.append(g) self.assertEqual(len(f), 2) f.remove(g) self.assertEqual(len(f), 1) self.assertIsInstance(f, cf.FieldList) with self.assertRaises(Exception): f.remove(f[0]*-99) f.remove(f[0].copy()) self.assertEqual(len(f), 0) self.assertIsInstance(f, cf.FieldList)
def test_FieldList_copy(self): f = cf.FieldList(self.x) f.append(f[0].copy()) g = f.copy() self.assertTrue(f.equals(f, verbose=2)) self.assertTrue(f.equals(g, verbose=2))
def test_FieldList_index(self): f = self.f2[0] a, b, c = [f[0], f[1:456], f[456:]] g = cf.FieldList([a, b, c]) self.assertEqual(g.index(a), 0) self.assertEqual(g.index(a, start=0), 0) self.assertEqual(g.index(a, stop=1), 0) self.assertEqual(g.index(a, stop=-2), 0) self.assertEqual(g.index(a, stop=2), 0) self.assertEqual(g.index(b), 1) self.assertEqual(g.index(b, start=0), 1) self.assertEqual(g.index(b, start=1, stop=2), 1) self.assertEqual(g.index(c), 2) self.assertEqual(g.index(c, start=0), 2) self.assertEqual(g.index(c, start=1), 2) self.assertEqual(g.index(c, start=2), 2) self.assertEqual(g.index(c, start=-1), 2) with self.assertRaises(Exception): g.index(f) with self.assertRaises(Exception): g.index(a, start=1)
def test_FieldList__add__iadd__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) f = f + f.copy() self.assertEqual(len(f), 2) self.assertIsInstance(f, cf.FieldList) f += (f[0].copy(),) self.assertEqual(len(f), 3) f += [f[0].copy()] self.assertEqual(len(f), 4) self.assertIsInstance(f, cf.FieldList) f += list(f[0].copy()) self.assertEqual(len(f), 5) f += f.copy() self.assertEqual(len(f), 10) f = f + f.copy() self.assertEqual(len(f), 20)
def create_empty_array(datapathea, count_time, divt, latitude, longitude, yearpart, years): Sa_track_down_last = np.zeros((1, len(latitude), len(longitude))) Sa_track_down_day = np.zeros((1, len(latitude), len(longitude))) Sa_track_top_last = np.zeros((1, len(latitude), len(longitude))) Sa_track_top_day = np.zeros((1, len(latitude), len(longitude))) # Sa_time_down_last = np.zeros((1, len(latitude), len(longitude))) Sa_time_down_day = np.zeros((1, len(latitude), len(longitude))) Sa_time_top_last = np.zeros((1, len(latitude), len(longitude))) Sa_time_top_day = np.zeros((1, len(latitude), len(longitude))) # Sa_dist_down_last = np.zeros((1, len(latitude), len(longitude))) Sa_dist_down_day = np.zeros((1, len(latitude), len(longitude))) Sa_dist_top_last = np.zeros((1, len(latitude), len(longitude))) Sa_dist_top_day = np.zeros((1, len(latitude), len(longitude))) # Build cf.field here. if yearpart[0] == 365: year_o = years[0] + 1 yearpart_o = 0 else: year_o = years[0] yearpart_o = yearpart[0] + 1 f0l = wrap_netcdf(year_o, yearpart_o, Sa_track_down_last, 'Sa_track_down_last', 'm3') f0d = wrap_netcdf(year_o, yearpart_o, Sa_track_down_day, 'Sa_track_down', 'm3') f1l = wrap_netcdf(year_o, yearpart_o, Sa_track_top_last, 'Sa_track_top_last', 'm3') f1d = wrap_netcdf(year_o, yearpart_o, Sa_track_top_day, 'Sa_track_top', 'm3') # f2l = wrap_netcdf(year_o, yearpart_o, Sa_time_down_last, 'Sa_time_down_last', 's') f2d = wrap_netcdf(year_o, yearpart_o, Sa_time_down_day, 'Sa_time_down', 's') f3l = wrap_netcdf(year_o, yearpart_o, Sa_time_top_last, 'Sa_time_top_last', 's') f3d = wrap_netcdf(year_o, yearpart_o, Sa_time_top_day, 'Sa_time_top', 's') # f4l = wrap_netcdf(year_o, yearpart_o, Sa_dist_down_last, 'Sa_dist_down_last', 'm') f4d = wrap_netcdf(year_o, yearpart_o, Sa_dist_down_day, 'Sa_dist_down', 'm') f5l = wrap_netcdf(year_o, yearpart_o, Sa_dist_top_last, 'Sa_dist_top_last', 'm') f5d = wrap_netcdf(year_o, yearpart_o, Sa_dist_top_day, 'Sa_dist_top', 'm') # Write out netcdf if yearpart[0] == 365: datapathnc = datapathea[0] else: datapathnc = datapathea[1] f_list = cf.FieldList( [f0l, f0d, f1l, f1d, f2l, f2d, f3l, f3d, f4l, f4d, f5l, f5d]) cf.write(f_list, datapathnc, single=True, unlimited='time', compress=3) return
def test_FieldList_index(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.read(self.filename2)[0] a, b, c = [f[0], f[1:456], f[456:]] g = cf.FieldList([a, b, c]) self.assertEqual(g.index(a), 0) self.assertEqual(g.index(a, start=0), 0) self.assertEqual(g.index(a, stop=1), 0) self.assertEqual(g.index(a, stop=-2), 0) self.assertEqual(g.index(a, stop=2), 0) self.assertEqual(g.index(b), 1) self.assertEqual(g.index(b, start=0), 1) self.assertEqual(g.index(b, start=1, stop=2), 1) self.assertEqual(g.index(c), 2) self.assertEqual(g.index(c, start=0), 2) self.assertEqual(g.index(c, start=1), 2) self.assertEqual(g.index(c, start=2), 2) self.assertEqual(g.index(c, start=-1), 2) with self.assertRaises(Exception): _ = g.index(f) with self.assertRaises(Exception): _ = g.index(a, start=1)
def test_FieldList__repr__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) f += f _ = repr(f)
def __set__(self, obj, fields: cf.FieldList): if len(fields) == 0: warnings.warn(f"No data with name {obj.name} exists on the input " 'dataset.') ordered_indexes = np.argsort( [field.construct('time').reference_datetime for field in fields]) fields = cf.FieldList([fields[idx] for idx in ordered_indexes]) obj.__dict__[obj.type] = fields
def test_FieldList_close(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) self.assertIsNone(f.close()) _ = repr(f[0])
def __set__(self, obj, fields: cf.FieldList): if len(fields) == 0: raise ValueError("No data with name 'lat' exists on the input " 'dataset.') ordered_indexes = np.argsort( [field.construct('time').reference_datetime for field in fields]) fields = cf.FieldList([fields[idx] for idx in ordered_indexes]) obj.__dict__['ny_grids'] = fields
def test_FieldList__contains__(self): f = cf.FieldList(self.x) f.append(self.x.copy()) f[1] *= 10 g = self.x * 10 self.assertIn(g, f) self.assertNotIn(34.6, f)
def test_FieldList_select(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) g = f('not this one') self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 0) g = f('air_temperature') self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 1, len(g)) g = f(re.compile('^air')) self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 1, len(g)) f *= 9 f[4] = f[0].copy() f[4].standard_name = 'this one' f[6] = f[0].copy() f[6].standard_name = 'this one' g = f(re.compile('^air')) self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 7, len(g)) g = f('this one') self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 2) # select_by_Units f[1] = f[1].override_units(cf.Units('watt')) f[3] = f[3].override_units(cf.Units('K @ 273.15')) self.assertEqual(len(f.select_by_units()), 9) self.assertEqual(len(f.select_by_units(cf.Units('K'))), 7) self.assertEqual( len(f.select_by_units(cf.Units('K'), exact=False)), 8) self.assertEqual(len(f.select_by_units('K')), 7) self.assertEqual(len(f.select_by_units('K', exact=False)), 8) self.assertEqual(len(f.select_by_units(re.compile('^K @|watt'))), 2) self.assertEqual(len(f.select_by_units('long_name=qwery:asd')), 0) # select_by_ncvar for a in f: a.nc_set_variable('ta') f[1].nc_set_variable('qwerty') f[4].nc_set_variable('ta2') self.assertEqual(len(f.select_by_ncvar()), 9) self.assertEqual(len(f.select_by_ncvar('qwerty')), 1) self.assertEqual(len(f.select_by_ncvar('ta')), 7) self.assertEqual(len(f.select_by_ncvar('ta2')), 1) self.assertEqual(len(f.select_by_ncvar(re.compile('^ta'))), 8)
def test_FieldList__getslice__(self): f = cf.FieldList(self.x) f.append(f[0]) f[0:1] f[1:2] f[:1] f[1:]
def test_unique_domains(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.example_field(0) g = cf.example_field(1) fl = cf.FieldList() self.assertFalse(fl.unique_domains()) fl = cf.FieldList([f]) self.assertEqual(len(fl.unique_domains()), 1) fl = cf.FieldList([f, f.copy()]) self.assertEqual(len(fl.unique_domains()), 1) fl = cf.FieldList([f, f.copy(), g]) self.assertEqual(len(fl.unique_domains()), 2)
def test_FieldList_select(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) g = f("not this one") self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 0) g = f("air_temperature") self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 1, len(g)) g = f(re.compile("^air")) self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 1, len(g)) f *= 9 f[4] = f[0].copy() f[4].standard_name = "this one" f[6] = f[0].copy() f[6].standard_name = "this one" g = f(re.compile("^air")) self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 7, len(g)) g = f("this one") self.assertIsInstance(g, cf.FieldList) self.assertEqual(len(g), 2) # select_by_Units f[1] = f[1].override_units(cf.Units("watt")) f[3] = f[3].override_units(cf.Units("K @ 273.15")) self.assertEqual(len(f.select_by_units()), 9) self.assertEqual(len(f.select_by_units(cf.Units("K"))), 7) self.assertEqual(len(f.select_by_units(cf.Units("K"), exact=False)), 8) self.assertEqual(len(f.select_by_units("K")), 7) self.assertEqual(len(f.select_by_units("K", exact=False)), 8) self.assertEqual(len(f.select_by_units(re.compile("^K @|watt"))), 2) self.assertEqual(len(f.select_by_units("long_name=qwery:asd")), 0) # select_by_ncvar for a in f: a.nc_set_variable("ta") f[1].nc_set_variable("qwerty") f[4].nc_set_variable("ta2") self.assertEqual(len(f.select_by_ncvar()), 9) self.assertEqual(len(f.select_by_ncvar("qwerty")), 1) self.assertEqual(len(f.select_by_ncvar("ta")), 7) self.assertEqual(len(f.select_by_ncvar("ta2")), 1) self.assertEqual(len(f.select_by_ncvar(re.compile("^ta"))), 8)
def test_FieldList_concatenate(self): f = self.f2[0] g = cf.FieldList([f[0], f[1:456], f[456:]]) h = g.concatenate(axis=0) self.assertTrue(f.equals(h, verbose=2)) h = g.concatenate(axis=0, _preserve=False) self.assertTrue(f.equals(h, verbose=2))
def test_FieldList_copy(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) f.append(f[0].copy()) g = f.copy() self.assertTrue(f.equals(f, verbose=2)) self.assertTrue(f.equals(g, verbose=2))
def test_FieldList__contains__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) f.append(self.x.copy()) f[1] *= 10 g = self.x * 10 self.assertIn(g, f) self.assertNotIn(34.6, f)
def test_FieldList__getslice__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) f.append(f[0]) _ = f[0:1] _ = f[1:2] _ = f[:1] _ = f[1:]
def test_FieldList_count(self): f = cf.FieldList(self.x) self.assertEqual(f.count(f[0]), 1) f = f * 7 self.assertEqual(f.count(f[0]), 7) f[3] = f[0] * 99 f[5] = f[0] * 99 self.assertEqual(f.count(f[0]), 5) self.assertEqual(f.count(f[3]), 2)
def test_FieldList_concatenate(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.read(self.filename2)[0] g = cf.FieldList([f[0], f[1:456], f[456:]]) h = g.concatenate(axis=0) self.assertTrue(f.equals(h, verbose=2)) h = g.concatenate(axis=0, _preserve=False) self.assertTrue(f.equals(h, verbose=2))
def test_aggregate_bad_units(self): f = cf.read(self.filename, squeeze=True)[0] g = cf.FieldList(f[0]) g.append(f[1:]) h = cf.aggregate(g) self.assertEqual(len(h), 1) g[0].override_units(cf.Units("apples!"), inplace=True) g[1].override_units(cf.Units("oranges!"), inplace=True) h = cf.aggregate(g) self.assertEqual(len(h), 2)
def test_FieldList_count(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cf.FieldList(self.x) self.assertEqual(f.count(f[0]), 1) f = f * 7 self.assertEqual(f.count(f[0]), 7) f[3] = f[0] * 99 f[5] = f[0] * 99 self.assertEqual(f.count(f[0]), 5) self.assertEqual(f.count(f[3]), 2)
def test_FieldList_reverse(self): f = cf.FieldList(self.x) g = f[0] h = f[0] + 10 f.append(h) self.assertIs(g, f[0]) self.assertIs(h, f[1]) f.reverse() self.assertIsInstance(f, cf.FieldList) self.assertEqual(len(f), 2) self.assertIs(g, f[1]) self.assertIs(h, f[0])