def test_convert(self): masked_data = np.ma.masked_array(data=np.arange(2), mask=[1, 0], fill_value=-1) unit = "d" var = Variable(data=masked_data, unit=unit) tar_unit = "s" var.convert(tar_unit) self.assertTrue(np.all(var.data.filled() == [-1, 86400])) self.assertEqual(var.unit, "s") tar_unit = "m" with self.assertRaises(ValueError): var.convert(tar_unit)
def create_Daelta_14C_dataset(mdo, ds, mr, mr_14C): alpha = 1.18e-12 t_conv = lambda t: 2001 + (15 + t) / 365.25 F_Delta_14C = lambda C12, C14: (C14 / C12 / alpha - 1) * 1000 ms = mdo.model_structure ## create 14C dataset data_vars = {} ## save stocks for pool_nr, pool_name in enumerate(ms.pool_names): var_name_ds = pool_name soln = mr.solve() soln_14C = mr_14C.solve() new_var = Variable( data=F_Delta_14C(soln[:, pool_nr], soln_14C[:, pool_nr]), unit=mdo.stock_unit, ) add_variable(ds, data_vars, var_name_ds, new_var) ## save external input fluxes # insert np.nan at time t0 # raise(Exception('check units and values, use acc variants')) Us_monthly = Variable( data=np.concatenate( [ np.nan * np.ones((1, len(ms.pool_names))), mr.acc_gross_external_input_vector(), ], axis=0, ), unit="g/(365.25/12 d)", ) print(mr, Us_monthly) Us_monthly_14C = Variable( data=np.concatenate( [ np.nan * np.ones((1, len(ms.pool_names))), mr_14C.acc_gross_external_input_vector(), ], axis=0, ), unit="g/(365.25/12 d)", ) # convert to daily flux rates us = Us_monthly.convert("g/d") us_14C = Us_monthly_14C.convert("g/d") for pool_nr, pool_name in enumerate(ms.pool_names): var_name_ds = "NPP_to_" + pool_name new_var = Variable( data=F_Delta_14C(us.data[:, pool_nr], us_14C.data[:, pool_nr]), unit=us.unit ) add_variable(ds, data_vars, var_name_ds, new_var) ## save internal fluxes # insert np.nan at time t0 Fs_monthly = Variable( data=np.concatenate( [ np.nan * np.ones((1, len(ms.pool_names), len(ms.pool_names))), mr.acc_gross_internal_flux_matrix(), ], axis=0, ), unit="g/(365.25/12 d)", ) Fs_monthly_14C = Variable( data=np.concatenate( [ np.nan * np.ones((1, len(ms.pool_names), len(ms.pool_names))), mr_14C.acc_gross_internal_flux_matrix(), ], axis=0, ), unit="g/(365.25/12 d)", ) # convert to daily flux rates fs = Fs_monthly.convert("g/d") fs_14C = Fs_monthly_14C.convert("g/d") for pnr_from, pn_from in enumerate(ms.pool_names): for pnr_to, pn_to in enumerate(ms.pool_names): var_name_ds = pn_from + "_to_" + pn_to new_var = Variable( data=F_Delta_14C( fs.data[:, pnr_to, pnr_from], fs_14C.data[:, pnr_to, pnr_from] ), unit=fs.unit, ) add_variable(ds, data_vars, var_name_ds, new_var) ## save external output fluxes # insert np.nan at time t0 Rs_monthly = Variable( data=np.concatenate( [ np.nan * np.ones((1, len(ms.pool_names))), mr.acc_gross_external_output_vector(), ], axis=0, ), unit="g/(365.25/12 d)", ) Rs_monthly_14C = Variable( data=np.concatenate( [ np.nan * np.ones((1, len(ms.pool_names))), mr_14C.acc_gross_external_output_vector(), ], axis=0, ), unit="g/(365.25/12 d)", ) # convert to daily flux rates rs = Rs_monthly.convert("g/d") rs_14C = Rs_monthly_14C.convert("g/d") for pool_nr, pool_name in enumerate(ms.pool_names): var_name_ds = pool_name + "_to_RH" new_var = Variable( data=F_Delta_14C(rs.data[:, pool_nr], rs_14C.data[:, pool_nr]), unit=rs.unit ) add_variable(ds, data_vars, var_name_ds, new_var) ds_Delta_14C = xr.Dataset(data_vars=data_vars, coords=ds.coords, attrs=ds.attrs) return ds_Delta_14C