def makefile(infile,outfilename): rfile = hdf.File(infile, 'r') heights = rfile['z_coords'].value[1:] tempk = np.zeros_like(heights) press = np.zeros_like(heights) vapor = np.zeros_like(heights) for i in range(len(heights)): tempk[i] = np.mean(rfile['tempk'].value[0,i+1,:,:]) press[i] = np.mean(rfile['press'].value[0,i+1,:,:]) vapor[i] = np.mean(rfile['vapor'].value[0,i+1,:,:]) rho = (press*100.)/ (287.*tempk) relhum = rfile['relhum'].value[0,1:,0,0] u = rfile['u'].value[0,1:,0,0] v = rfile['v'].value[0,1:,0,0] data = {'T':tempk, 'RH':relhum, 'p':press*100.} dewpt = atmos.calculate('Td', **data) sat = atmos.calculate('rvs', **data) * 1000. dz = np.zeros_like(heights) dz[0:-1] = np.diff(heights) dz[-1] = dz[-2] vsat = sat * rho * dz vact = vapor * rho * dz lows = np.where(press > 850.) mids = np.logical_and(press < 850, press > 500) highs = np.where(press < 500) lowsat = 100. * (np.sum(vact[lows]) / np.sum(vsat[lows])) midsat = 100. * (np.sum(vact[mids]) / np.sum(vsat[mids])) highsat = 100. * (np.sum(vact[highs]) / np.sum(vsat[highs])) print outfilename print lowsat, midsat, highsat print ' ' outfile = Dataset(outfilename, "w", format="NETCDF4") height = outfile.createDimension("height", len(heights)) dim = outfile.createVariable('height',"f4",('height')) outfile.variables['height'][:]=heights a = outfile.createVariable('temp',"f4",('height')) outfile.variables['temp'][:]=tempk-273.15 aa = outfile.createVariable('press',"f4",('height')) outfile.variables['press'][:]=press a = outfile.createVariable('dewpoint',"f4",('height')) outfile.variables['dewpoint'][:]=dewpt-273.15 a = outfile.createVariable('v',"f4",('height')) outfile.variables['v'][:]=v a = outfile.createVariable('u',"f4",('height')) outfile.variables['u'][:]=u outfile.close()
def get_metcro2d_rh(self, lay=None): import atmos data = { 'T': self.dset['TEMP2'][:].compute().values, 'rv': self.dset['Q2'][:].compute().values, 'p': self.dset['PRSFC'][:].compute().values } if lay is None: a = atmos.calculate('RH', **data)[:, :, :, :].squeeze() else: atmos.calculate('RH', **data)[:, lay, :, :].squeeze() return a
def get_air_density(t): """ Returns air density given the temperature :param t: :return: """ return atmos.calculate('rho', Tv=t + 273.15, p=101325)
def dMarkFreezRain_(pr, ps, tas, t925, t850, t700, q925, q850, q700, pr_min=1.8e-5, t_cold=273.24, t_melt=272.51, rh_melt=89., h_cold=6900): import atmos aa = _rl(pr) > pr_min bb = _rl(tas) <= t_cold ta = np.vstack((_rl(t925), _rl(t850), _rl(t700))) qa = np.vstack((_rl(q925), _rl(q850), _rl(q700))) prl = np.asarray([92500, 85000, 70000]).reshape((3, 1)) rh = atmos.calculate('RH', qv=qa, T=ta, p=prl) cc = ta > t_melt dd = rh >= rh_melt ee = (_rl(ps) - h_cold - prl) >= 0 ff = np.any(np.logical_and(cc, dd, ee), axis=0) return np.logical_and(aa, bb, ff)
def mf2rh(P, T, mf): mix2mass = (18 / (0.8 * 28 + 0.2 * 32)) W = np.copy(mf) W[W < 0] = 0 W[:, P < 101325 * np.exp(-3)] = 0 RH = atmos.calculate('RH', T=T, p=P, rt=W * mix2mass) RH[(RH < 0) | (W == 0)] = 0 return RH
def get_abs_humidity(sense): """Get the absolute humidity, handles unit conversions(why does sense hat not use SI units?)""" # Get relative humidity relh = sense.get_humidity() # Get pressure, and convert from mB to Pa pressure = sense.get_pressure() * 100 # Get temperature, and convert from degrees Celsius to kelvin temp = sense.get_temperature() + 273.15 # Get absolute humidity abshumidity = atmos.calculate("AH", RH=relh, T=temp, p=pressure) # Why is the SI unit the KILOgram? return abshumidity * 1000
T = nc.variables['T'][:].squeeze() nc.close() # buscando indices de niveles superior e inferior us = np.nanmean( (u[:,zs:zi+1,:,:].copy())**2, axis=0) del u vs = np.nanmean( (v[:,zs:zi+1,:,:].copy())**2, axis=0) del v ms = np.sqrt(us + vs) qs = np.nanmean( q[:,zs:zi+1,:,:].copy(), axis=0) del q ts = np.nanmean( T[:,zs:zi+1,:,:].copy(), axis=0) del T # calculando rho rho = atmos.calculate('rho',p = zl*100.0, Tv = ts) # calculando el wind speed ws.append ( (np.nansum( rho * ms, axis=0)) / (np.nansum(rho, axis=0) ) ) # calculando Presion Ps.append ( (np.nansum( rho * ms * zl, axis=0 )) / (np.nansum(rho * ms, axis=0) ) ) # calculando latitud ls_top = np.nansum ( rho * ms, axis=0) * y ls_top = np.nansum ( ls_top[ys:yi,:], axis = 0 ) ls_inf = np.nansum ( rho * ms, axis=0) ls_inf = np.nansum ( ls_inf[ys:yi,:], axis = 0 ) ls.append( ls_top/ls_inf ) t.append(f[i][7:-3])
def calculateAbsoluteHumidity(rel_humidity, temperature): temp_kelvin = temperature + 273.15 absolute_humidity = calculate('AH', RH=rel_humidity, p=1e5, T=temp_kelvin, debug=True)[0] # convert from kg/m^3 to g/m^3 absolute_humidity = absolute_humidity * 1000 return absolute_humidity
from matplotlib import gridspec import matplotlib.image as mpimg filename = 'KWAJEX_Soundings_11Aug1999/sounding_11aug1999_10mb_control.txt' var = np.loadtxt(filename, dtype=float) p = var[:,0] T = var[:,1] rv = var[:,2]*1000. u = var[:,3] v = var[:,4] data = {'T':T+273.15, 'rv':rv/1000., 'p':p*100.} Td = atmos.calculate('Td', **data) -273.15 Tv = atmos.calculate('Tv', **data) data = {'Tv':Tv, 'rv':rv/1000., 'p':p*100.} rho = atmos.calculate('rho', **data) dpdz = rho * 9.8 heights = np.zeros_like(p) heights[0] = 10.0 for i in range(1,len(heights)): heights[i] = ((((p[i-1] - p[i])*100.)) / dpdz[i-1]) + heights[i-1] p=p*units.hPa45 T=T*units.degC Td=Td*units.degC fig = plt.figure(figsize=(9, 9))45
'ccmap': "viridis_r", 'llevels': np.arange(970, 1040, 4), 'llevels2': [1000], 'time_index': tindex, 'times': times, 'skip': skip, 'mapscale': mapscale, 'mapparms': mapparms, 'fontsize': fontsize } if model == 'SMN_WRF': cffield = data['TMP_GDS3_HTGL'] cffield.values = 273.15 + atmos.calculate( 'thetae', T=data['TMP_GDS3_HTGL'].values, p=data['PRES_GDS3_SFC'].values, qv=data['SPF_H_GDS3_HTGL'].values) cffield.attrs['long_name'] = '2 m equivalent potential temperature' cffield.attrs['units'] = 'K' lfield = data['PRMSL_GDS3_MSL'] / 100. lfield2 = topo ufld = data['U_GRD_GDS3_HTGL'] * 1.94 vfld = data['V_GRD_GDS3_HTGL'] * 1.94 else: cffield = getvar(files, params['cfield'], timeidx=params['time_index'], units='K') cffield = cffield.isel(bottom_top=0) lfield = getvar(files, 'slp', timeidx=params['time_index'], units='hPa')
def rho_fr_t_q_p_(t, q, p): import atmos return atmos.calculate('rho', T=_rl(t), qv=_rl(q), p=_rl(p))