def get_atm_variables(mus, muv, phi, height, coeffs): (ah2o, bh2o, ao3, tau) = coeffs # From GetAtmVariables tau_step = np.linspace(TAUSTEP4SPHALB, MAXNUMSPHALBVALUES * TAUSTEP4SPHALB, MAXNUMSPHALBVALUES) sphalb0 = csalbr(tau_step) air_mass = 1.0 / mus + 1 / muv air_mass = air_mass.where(air_mass <= MAXAIRMASS, -1.0) taur = tau * xu.exp(-height / SCALEHEIGHT) rhoray, trdown, trup = chand(phi, muv, mus, taur) sphalb = sphalb0[np.int32(taur / TAUSTEP4SPHALB + 0.5)] Ttotrayu = ((2 / 3. + muv) + (2 / 3. - muv) * trup) / (4 / 3. + taur) Ttotrayd = ((2 / 3. + mus) + (2 / 3. - mus) * trdown) / (4 / 3. + taur) tO3 = 1.0 tO2 = 1.0 tH2O = 1.0 if ao3 != 0: tO3 = xu.exp(-air_mass * UO3 * ao3) if bh2o != 0: if bUseV171: tH2O = xu.exp(-xu.exp(ah2o + bh2o * xu.log(air_mass * UH2O))) else: tH2O = xu.exp(-(ah2o * ((air_mass * UH2O)**bh2o))) #t02 = exp(-m * aO2) TtotraytH2O = Ttotrayu * Ttotrayd * tH2O tOG = tO3 * tO2 return sphalb, rhoray, TtotraytH2O, tOG
def ds_arithmetics(ds: DatasetLike.TYPE, op: str, monitor: Monitor = Monitor.NONE) -> xr.Dataset: """ Do arithmetic operations on the given dataset by providing a list of arithmetic operations and the corresponding constant. The operations will be applied to the dataset in the order in which they appear in the list. For example: 'log,+5,-2,/3,*2' Currently supported arithmetic operations: log,log10,log2,log1p,exp,+,-,/,* where: log - natural logarithm log10 - base 10 logarithm log2 - base 2 logarithm log1p - log(1+x) exp - the exponential The operations will be applied element-wise to all arrays of the dataset. :param ds: The dataset to which to apply arithmetic operations :param op: A comma separated list of arithmetic operations to apply :param monitor: a progress monitor. :return: The dataset with given arithmetic operations applied """ ds = DatasetLike.convert(ds) retset = ds with monitor.starting('Calculate result', total_work=len(op.split(','))): for item in op.split(','): with monitor.child(1).observing("Calculate"): item = item.strip() if item[0] == '+': retset = retset + float(item[1:]) elif item[0] == '-': retset = retset - float(item[1:]) elif item[0] == '*': retset = retset * float(item[1:]) elif item[0] == '/': retset = retset / float(item[1:]) elif item[:] == 'log': retset = xu.log(retset) elif item[:] == 'log10': retset = xu.log10(retset) elif item[:] == 'log2': retset = xu.log2(retset) elif item[:] == 'log1p': retset = xu.log1p(retset) elif item[:] == 'exp': retset = xu.exp(retset) else: raise ValueError('Arithmetic operation {} not' ' implemented.'.format(item[0])) return retset
def ds_arithmetics(ds: xr.Dataset, op: str) -> xr.Dataset: """ Do arithmetic operations on the given dataset by providing a list of arithmetic operations and the corresponding constant. The operations will be applied to the dataset in the order in which they appear in the list. For example: 'log,+5,-2,/3,*2' Currently supported arithmetic operations: log,log10,log2,log1p,exp,+,-,/,* where: log - natural logarithm log10 - base 10 logarithm log2 - base 2 logarithm log1p - log(1+x) exp - the exponential The operations will be applied element-wise to all arrays of the dataset. :param ds: The dataset to which to apply arithmetic operations :param op: A comma separated list of arithmetic operations to apply :return: The dataset with given arithmetic operations applied """ retset = ds for item in op.split(','): item = item.strip() if item[0] == '+': retset = retset + float(item[1:]) elif item[0] == '-': retset = retset - float(item[1:]) elif item[0] == '*': retset = retset * float(item[1:]) elif item[0] == '/': retset = retset / float(item[1:]) elif item[:] == 'log': retset = xu.log(retset) elif item[:] == 'log10': retset = xu.log10(retset) elif item[:] == 'log2': retset = xu.log2(retset) elif item[:] == 'log1p': retset = xu.log1p(retset) elif item[:] == 'exp': retset = xu.exp(retset) else: raise ValueError('Arithmetic operation {} not' ' implemented.'.format(item[0])) return retset
swe_mask_align, pr_full = xray.align(swe_mask, pr, join='inner', copy=False) del pr gc.collect() direc = '/raid/gergel' pdur_file = 'pduration_mod.nc' # this is the one that I regridded to 6 x 6 km using cdo remapcon and /raid9/gergel/agg_snowpack/keepextracopies/grid_info_mine pdur_full = xray.open_dataset(os.path.join( direc, pdur_file)) ## pdur beta parameter for John's transform from Matt Jolly ''' for j in np.arange(len(pr_full.lat)): for k in np.arange(len(pr_full.lon)): if np.isnan(pr_full['precipitation'].values[0,j,k]) == False: lon_ind = np.argmin(np.abs(pdur_full.lon - pr_full.lon[k])) lat_ind = np.argmin(np.abs(pdur_full.lat - pr_full.lat[j])) beta = np.float(pdur_full.isel_points(lon=[lon_ind],lat=[lat_ind])['pdur']) #u = pr_arr[:,j,k] # u = np.round(24 * (1 - (np.exp(-beta*pr_full['precipitation'].values[:,j,k])))) pr_full['precipitation'].values[:,j,k] = np.round(24 * (1 - (np.exp(-beta*pr_full['precipitation'].values[:,j,k])))) ''' pr_full['precipitation'].values = np.round(24 * ( 1 - (ufuncs.exp(-pdur_full['pdur'].values * pr_full['precipitation'].values)))) direc = '/raid/gergel/pptdur' filename = '%s_%s.nc' % (model, scenario) pr_full.to_netcdf(os.path.join(direc, filename)) print('saved dataset to netcdf as %s' % os.path.join(direc, filename))
def global_XCO2(datetime): execfile('../../rpnenv/bin/activate_this.py', dict(__file__='../../rpnenv/bin/activate_this.py')) import fstd2nc # get the ACOS pressure coefficients and acceleration due to gravity at set pressure levels acos_plevs = np.load('../earth_data/ACOS_plevs.npy') grav_plevs, grav = np.load('../earth_data/ACOS_grav.npy') # get the model files that will be read from model_fname = datetime.strftime(_rpn_format) model_fname_prev = (datetime - dt.timedelta(days=1)).strftime(_rpn_format) model = fstd2nc.Buffer(model_fname, vars='CO2,HU,P0', diag_as_model_level=True).to_xarray() try: # open files for the day of the observations and the final time step of the previous day mprev = fstd2nc.Buffer(model_fname_prev, vars='CO2,HU,P0', diag_as_model_level=True).to_xarray() model = xarray.concat([ mprev.sel(time=datetime.strftime('%Y-%m-%dT00:00:00.000000000')), model ], 'time').transpose('time', 'level', 'lat', 'lon') except: # First day - no previous day to get data from, so the first 2 hours will have to be extrapolated print("Error due to first day of run ignored") pass model.load() model_interp = model.interp(time=datetime, kwargs={'fill_value': None}) print(model_interp) logp = model_interp.a + model_interp.b * log(model_interp.P0 / 1000.) p = exp(logp) / 100. p = p.transpose('level', 'lat', 'lon').values psurf = model_interp.P0.values acos_p = psurf[np.newaxis, ...] * acos_plevs[:, np.newaxis, np.newaxis] CO2 = model_interp.CO2.values HU = model_interp.CO2.values print("interpolating to ACOS algorithm pressure levels") u = np.zeros(acos_p.shape) q = np.zeros(acos_p.shape) g = np.zeros(acos_p.shape) for i in range(p.shape[1]): for j in range(p.shape[2]): u[:, i, j] = np.interp(acos_p[:, i, j], p[:, i, j], CO2[:, i, j]) q[:, i, j] = np.interp(acos_p[:, i, j], p[:, i, j], HU[:, i, j]) g[:, i, j] = np.interp(acos_p[:, i, j], grav_plevs, grav) c = (1. - q) / mdry / g cavg = 0.5 * (c[:-1, ...] + c[1:, ...]) delp = acos_p[:-1, ...] - acos_p[1:, ...] hprime = cavg * delp / np.sum(cavg * delp, axis=0)[np.newaxis, ...] f = 0.5 * np.ones(acos_p.shape) f[-1, ...] = (psurf - acos_p[-2, ...]) / (acos_p[-1, ...] - acos_p[-2, ...]) h = np.zeros(acos_p.shape) h[0, ...] = (1. - f[0, ...]) * hprime[0, ...] h[1:-2, ...] = f[:-3, ...] * hprime[:-2, ...] + ( 1. - f[1:-2, ...]) * hprime[1:-1, ...] h[-2, ...] = f[-3, ...] * hprime[-2, ...] + ( 1 - f[-2, ...] * f[-1, ...]) * hprime[-1, ...] h[-1, ...] = f[-1, ...] * f[-2, ...] * hprime[-1, ...] profile = 0.0024141666666666665 * u xco2 = np.sum(h * profile, axis=0) lats = model_interp.lat.values lons = model_interp.lon.values lons[lons > 180.] -= 360. reorder = np.argsort(lons) xco2 = xco2[:, reorder] lons = lons[reorder] return xco2, lats, lons
def wanninkhof92(T=None, S=None, P=None, u_mean=None, u_var=None, xCO2=None, pCO2_sw=None, iceFrac=None, scale_factor=0.27): ''' Calculate air-sea CO2 flux following Wanninkhof 1992 Inputs ============ T = Temperature [degC] S = Salinity [parts per thousand] P = Atmospheric pressure at 10m [atm] u_mean = mean wind speed [m/s] u_var = variance of wind speed m/s averaged monthly [m/s] xcO2 = atmoapheric mixing ratio of CO2 [ppm] pCO2_sw = CO2 partial pressure of seawter [uatm] scale_factor = gas transfer scale factor (default=0.27) Output ============ F_co2 = air-sea co2 flux [molC/m2/yr] References ============ Weiss (1974) [for solubility of CO2] Carbon dioxide in water and seawater: the solubility of a non-ideal gas Weiss and Price (1980) [for saturation vapor pressure, not used here] Nitrous oxide solubility in water and seawater Dickson et al (2007) [for saturation vapor pressure] Guide to best practices for Ocean CO2 measurements, Chapter 5 section 3.2 Wanninkhof (1992) [for gas transfer and Schmidt number] Relationship between wind speed and gas exchange over the ocean Sweeney et al. (2007) [for gas transfer scale factor] Constraining global air‐sea gas exchange for CO2 with recent bomb 14C measurements Sarmiento and Gruber (2007) [for overview and discussion of CO2 flux] Ocean biogeochmiecal dynsmics, Ch3 see panel 3.2.1 Notes ============ this code is optimized to work with Xarray ## Notes on partial pressure moist air P_h20/P = saturation vapor pressure (Weiss and Price 1980) Water vapor is generally assumed to be at saturation in the vicinity of the air-sea interfae. p_moist = X_dry (P - P_h20) = x_dry * P (1-P_h20/P) = P_dry (1-P_h20/P) ## Notes on U2 Need to add wind speed variance if using Wanninkhof (1992) If we don't, then we are underestimating the gas-transfer velocity U2 = (U_mean)^2 + (U_prime)^2, (U_prime)^2 is the standard deviation See Sarmiento and Gruber (2007) for a discussion of this ## Test values # T = 298.15 # S = 35 ''' # ================================================================== # 0. Conversions # ================================================================== # Convert from Celsius to kelvin T_kelvin = T + 273.15 # ================================================================== # 1. partial pressure of CO2 in moist air # Units : uatm # ================================================================== # Weiss and Price (1980) formula ## P_sat_vapor = xu.exp(24.4543 - 67.4509*(100/T_kelvin) - 4.8489*xu.log(T_kelvin/100) - 0.000544*S) # Following Dickson et al (2007) Chapter 5 section 3.2 a1 = -7.85951783 a2 = 1.84408259 a3 = -11.7866497 a4 = 22.6807411 a5 = -15.9618719 a6 = 1.80122502 Tc = 647.096 pc = (22.064 * 10**6) / 101325 g = (1 - (T_kelvin / Tc)) # pure water saturation vapor pressure, Wagner and Pruß, 2002 p_h20 = pc * xu.exp( (Tc / T_kelvin) * (a1 * g + a2 * g**(1.5) + a3 * g**(3) + a4 * g** (3.5) + a5 * g**(4) + a6 * g**(7.5))) # total molality MT = (31.998 * S) / (1000 - 1.005 * S) # osmotic coefficient at 25C by Millero (1974) b1 = 0.90799 b2 = -0.08992 b3 = 0.18458 b4 = -0.07395 b5 = -0.00221 phi = b1 + b2 * (0.5 * MT) + b3 * (0.5 * MT)**2 + b4 * ( 0.5 * MT)**3 + b5 * (0.5 * MT)**4 # vapor pressure above sea-water, Dickson et al. (2007), Chapter 5, section 3.2 P_sat_vapor = p_h20 * xu.exp(-0.018 * phi * MT) # Partial pressure of CO2 in moist air, Dickson et al. (2007), SOP 4 section 8.3 P_moist = (P * xCO2) * (1 - P_sat_vapor) # ================================================================== # 2. Solubility of CO2 # Notes : T in degC, S in PSU, # 1.0E-6 converts to correct to muatm # Units : to mol.kg^{-1}uatm^{-1} # Reference : Weiss (1974) # ================================================================== S_co2 = xu.exp( 9345.17 / T_kelvin - 60.2409 + \ 23.3585 * xu.log( T_kelvin / 100 ) + \ S * ( 0.023517 - 0.00023656 * T_kelvin + \ 0.0047036 * ( T_kelvin / 100 )**2 )) *1.0E-6 # ================================================================== # 3. Gas transfer velocity # Units : cm/hr # Reference : Wanninkhof (1992) # Sweeney et al. (2007) # per.comm with P. Landschutzer Feb. 19 2019 # ================================================================== # Dimensionless Schmidt number (Sc) # References Wanninkhof 1992 A = 2073.1 B = 125.62 C = 3.6276 D = 0.043219 # Schmidt number # units : dimensionless Sc = A - B * T + C * T**2 - D * T**3 # Gas transfer velocity # References : Wanninkhof 1992, # Sweeney et al. 2007 scale factor k_w = scale_factor * (Sc / 660)**(-0.5) * (u_mean**2 + u_var) # ================================================ # 4. air-sea CO2 exchange # Units : mol/m2/yr # Reference : Wanninkhof (1992) # ================================================ # Convert from cm*mol/hr/kg to mol/m2/yr conversion = (1000 / 100) * 365 * 24 # Air-sea CO2 flux F_co2 = k_w * S_co2 * (1 - iceFrac) * (P_moist - pCO2_sw) * conversion return F_co2
def exp_precip(data): data.loc['tp'] = data.loc['tp'].where(data.loc['tp'] > -19, np.nan) # all -20 get nan data.loc['tp'] = xu.exp(data.loc['tp']) # all nan stay nan data.loc['tp'] = data.loc['tp'].where(~np.isnan(data.loc['tp']), 0) # all nan get zero precip return data