def test_recursively_compute_variable_multi_level(load_variable_data_loader): one_level = Var( name='one_level', variables=(condensation_rain, condensation_rain), func=lambda x, y: x + y) multi_level = Var( name='multi_level', variables=(one_level, condensation_rain), func=lambda x, y: x + y) result = load_variable_data_loader.recursively_compute_variable( multi_level, DatetimeNoLeap(5, 1, 1), DatetimeNoLeap(5, 12, 31), intvl_in='monthly') filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf', '00050101.precip_monthly.nc') expected = 3. * _open_ds_catch_warnings(filepath)['condensation_rain'] np.testing.assert_array_equal(result.values, expected.values)
def test_recursively_compute_grid_attr_multi_level(load_variable_data_loader): one_level = Var(name='one_level', variables=(bk, ), func=lambda x: 2 * x) multi_level = Var(name='multi_level', variables=(one_level, bk), func=lambda x, y: x + y) result = load_variable_data_loader.recursively_compute_variable( multi_level, DatetimeNoLeap(5, 1, 1), DatetimeNoLeap(5, 12, 31), model=example_model, intvl_in='monthly') filepath = os.path.join( os.path.split(ROOT_PATH)[0], 'netcdf', '00060101.sphum_monthly.nc') expected = 3 * xr.open_dataset(filepath)['bk'] np.testing.assert_array_equal(result.values, expected.values)
def test_recursively_compute_grid_attr_error(load_variable_data_loader): # Should fail because zsurf is not provided to the example_model object zsurf = Var(name=ZSURF_STR, def_time=False, def_vert=False, def_lon=True, def_lat=True) with pytest.raises(AttributeError): load_variable_data_loader.recursively_compute_variable( zsurf, DatetimeNoLeap(5, 1, 1), DatetimeNoLeap(5, 12, 31), model=example_model, intvl_in='monthly')
def recursive_test_params(): basic_params = { 'proj': example_proj, 'model': example_model, 'run': example_run, 'var': condensation_rain, 'date_range': (datetime.datetime(4, 1, 1), datetime.datetime(6, 12, 31)), 'intvl_in': 'monthly', 'dtype_in_time': 'ts' } recursive_params = basic_params.copy() recursive_condensation_rain = Var(name='recursive_condensation_rain', variables=(precip, convection_rain), func=lambda x, y: x - y, def_time=True) recursive_params['var'] = recursive_condensation_rain yield (basic_params, recursive_params) _clean_test_direcs()
precip_largescale, precip_convective : xarray.DataArrays Precipitation from grid-scale condensation and from convective parameterization, respectively. Returns ------- xarray.DataArray """ total = total_precip(precip_largescale, precip_convective) # Mask using xarray's `where` method to prevent divide-by-zero. return precip_convective / total.where(total) precip_largescale = Var( name='precip_largescale', alt_names=('condensation_rain', ), def_time=True, description='Precipitation generated via grid-scale condensation', ) precip_convective = Var( name='precip_convective', alt_names=('convection_rain', ), def_time=True, description='Precipitation generated by convective parameterization', ) precip_total = Var( name='precip_total', def_time=True, func=total_precip, variables=(precip_largescale, precip_convective),
runs=[example_run], load_grid_data=True, grid_attrs={ LAND_MASK_STR: 'custom_land_mask', LON_STR: 'custom_lon' }) example_proj = Proj('example_proj', direc_out=os.path.join(os.path.dirname(__file__), 'test-files'), tar_direc_out=os.path.join(os.path.dirname(__file__), 'test-tar-files'), models=[example_model]) var_not_time_defined = Var( name='var_no_time_def', def_time=False, ) condensation_rain = Var( name='condensation_rain', alt_names=('prec_ls', ), def_time=True, description=('condensation rain'), ) convection_rain = Var( name='convection_rain', alt_names=('prec_conv', ), def_time=True, description=('convection rain'), )
'00060101.sphum_monthly.nc'), os.path.join( os.path.split(ROOT_PATH)[0], 'netcdf', 'im.landmask.nc')), ), runs=[example_run]) example_proj = Proj('example_proj', direc_out=os.path.join(os.path.dirname(__file__), 'test-files'), tar_direc_out=os.path.join(os.path.dirname(__file__), 'test-tar-files'), models=[example_model]) condensation_rain = Var( name='condensation_rain', alt_names=('prec_ls', ), def_time=True, description=('condensation rain'), ) convection_rain = Var( name='convection_rain', alt_names=('prec_conv', ), def_time=True, description=('convection rain'), ) precip = Var(name='total_precipitation', def_time=True, description=('total precipitation rate'), func=total_precipitation, variables=(convection_rain, condensation_rain))
base = GFDLDataLoader( data_direc=rootdir, data_dur=5, data_start_date=datetime(1980, 1, 1), data_end_date=datetime(1999, 12, 31), ) example_run = Run( name='relative humidity examination', description='An examination of relative humidity 1980 => 2000', data_loader=base) example_model = Model(name='example_model', runs=[example_run], grid_file_paths=(os.path.join(rootdir, 'atmos', 'ts', 'monthly', 'zg_A1.199501-199912.nc'))) example_proj = Proj('example_proj', direc_out=output_dir, models=[example_model]) hur = Var(name='hur', def_time=True, description='Relative humidity') globe = Region(name='globe', description='Entire globe', west_bound=0, east_bound=360, south_bound=-90, north_bound=90, do_land_mask=False)