def test_Treshold(self): field = self.get_field(with_value=True,month_count=2) grouping = ['month'] tgd = field.temporal.get_grouping(grouping) dv = Threshold(field=field,parms={'threshold':0.5,'operation':'gte'},tgd=tgd) ret = dv.execute() self.assertEqual(ret['threshold'].value.shape,(2,2,2,3,4)) self.assertNumpyAllClose(ret['threshold'].value[1,1,1,0,:], np.ma.array([13,16,15,12],mask=False,fill_value=1e20))
def test_calculate(self): rd = self.test_data.get_rd('cancm4_tasmax_2011') field = rd.get() field = field[:,:,:,0:10,0:10] for calc_grouping in [['month'],['month','year']]: tgd = field.temporal.get_grouping(calc_grouping) itg = IcclimSU(field=field,tgd=tgd) ret_icclim = itg.execute() threshold = Threshold(field=field,tgd=tgd,parms={'threshold':298.15,'operation':'gt'}) ret_ocgis = threshold.execute() self.assertNumpyAll(ret_icclim['icclim_SU'].value,ret_ocgis['threshold'].value)
def test_calculate(self): rd = self.test_data.get_rd('cancm4_tasmax_2011') field = rd.get() field = field.get_field_slice({'y': slice(0, 10), 'x': slice(0, 10)}) for calc_grouping in [['month'], ['month', 'year']]: tgd = field.temporal.get_grouping(calc_grouping) itg = IcclimSU(field=field, tgd=tgd) ret_icclim = itg.execute() threshold = Threshold(field=field, tgd=tgd, parms={'threshold': 298.15, 'operation': 'gt'}) ret_ocgis = threshold.execute() self.assertNumpyAll(ret_icclim['icclim_SU'].get_value(), ret_ocgis['threshold'].get_value())
def test_calculation_iteration_two_calculations(self): field = self.get_field(with_value=True, month_count=2) field.variables.add_variable(Variable(value=field.variables['tmax'].value + 5, name='tmin', alias='tmin')) field.temporal.name_uid = 'tid' field.level.name_uid = 'lid' field.spatial.geom.name_uid = 'gid' grouping = ['month'] tgd = field.temporal.get_grouping(grouping) mu = Mean(field=field, tgd=tgd, alias='my_mean', dtype=np.float64, add_parents=True) ret = mu.execute() thresh = Threshold(field=field, vc=ret, tgd=tgd, alias='a_treshold', add_parents=True, parms={'operation': 'gte', 'threshold': 0.5}) ret = thresh.execute() kwds = copy(field.__dict__) kwds.pop('_raw') kwds.pop('_variables') kwds.pop('_should_regrid') kwds.pop('_has_assigned_coordinate_system') kwds.pop('_attrs') kwds['name'] = kwds.pop('_name') kwds['temporal'] = tgd kwds['variables'] = ret cfield = DerivedField(**kwds) cfield.temporal.name_uid = 'tid' cfield.temporal.name_value = 'time' cfield.spatial.name_uid = 'gid' sc = ShpCabinet() meta = sc.get_meta('state_boundaries') sp = SpatialCollection(meta=meta, key='state_boundaries', headers=constants.HEADERS_CALC) for row in sc.iter_geoms('state_boundaries', as_spatial_dimension=True): sp.add_field(cfield, ugeom=row) cids = set() for ii, row in enumerate(sp.get_iter_dict(melted=True)): cids.update([row[1]['cid']]) if ii == 0: self.assertEqual(row[0].bounds, (-100.5, 39.5, -99.5, 40.5)) self.assertDictEqual(row[1], {'lid': 1, 'ugid': 1, 'vid': 1, 'cid': 1, 'did': 1, 'year': 2000, 'time': datetime.datetime(2000, 1, 16, 0, 0), 'calc_alias': 'my_mean_tmax', 'value': 0.44808476666433006, 'month': 1, 'alias': 'tmax', 'variable': 'tmax', 'gid': 1, 'calc_key': 'mean', 'tid': 1, 'level': 50, 'day': 16}) self.assertEqual(len(row), 2) self.assertEqual(len(row[1]), len(constants.HEADERS_CALC)) self.assertEqual(ii + 1, 2 * 2 * 2 * 3 * 4 * 51 * 4) self.assertEqual(len(cids), 4)
def test_execute(self): field = self.get_field(with_value=True, month_count=2) grouping = ['month'] tgd = field.temporal.get_grouping(grouping) dv = Threshold(field=field, parms={ 'threshold': 0.5, 'operation': 'gte' }, tgd=tgd) ret = dv.execute() self.assertEqual(ret['threshold'].get_value().shape, (2, 2, 2, 3, 4)) self.assertNumpyAllClose( ret['threshold'].get_masked_value()[1, 1, 1, 0, :], np.ma.array([13, 16, 15, 12], mask=False))