def air_density(): Rd = 287. # [J K-1 kg-1] # density values do not vary significantly Tv = tm.virtual_temperature(C=stemp, mixing_ratio=smixr / 1000.) + 273.15 air_density1 = (spress * 100.) / (Rd * Tv) Tv = tm.virtual_temperature(C=mtemp, mixing_ratio=smixr / 1000.) + 273.15 air_density2 = (mpress * 100.) / (Rd * Tv)
def air_density(): Rd = 287. # [J K-1 kg-1] # density values do not vary significantly Tv = tm.virtual_temperature(C=stemp, mixing_ratio=smixr/1000.)+273.15 air_density1 = (spress*100.)/(Rd*Tv) Tv = tm.virtual_temperature(C=mtemp, mixing_ratio=smixr/1000.)+273.15 air_density2 = (mpress*100.)/(Rd*Tv)
def get_meteo(self,start_time, end_time): start = self.df.index.searchsorted(start_time) end = self.df.index.searchsorted(end_time) # print meteo.columns.tolist() cols=[0,1,2,3,6,7,8,9,13,14,15] meteo=self.df.ix[start:end,cols].copy() ''' pressure ''' pres = meteo.apres.values ''' add relative humidity ''' temp = meteo.atemp.values dewp = meteo.dewp.values relh = thermo.relative_humidity(C=temp,Dewp=dewp) # [%] meteo.loc[:,'relh'] = pd.Series(relh,index=meteo.index) ''' mixing ratio ''' satmixr=thermo.sat_mix_ratio(C=temp,hPa=pres) mixr=relh*satmixr/100 ''' add theta ''' theta = thermo.theta2(C=temp,hPa=pres,mixing_ratio=mixr) meteo.loc[:,'theta'] = pd.Series(theta,index=meteo.index) ''' add thetav ''' thetav = thermo.virtual_temperature(theta=theta,mixing_ratio=mixr) meteo.loc[:,'thetav'] = pd.Series(thetav,index=meteo.index) ''' add thetae ''' thetaeq = thermo.theta_equiv2(C=temp,hPa=pres, mixing_ratio=mixr,relh=relh) meteo.loc[:,'thetaeq'] = pd.Series(thetaeq,index=meteo.index) return meteo
def parse_surface_met(file_met, index_field=None, name_field=None, locelevation=None): from scipy import stats ''' Process surface meteorological files with .met extension Output dataframe assumes: - Each file is a 24h period observation - Frequency of observation is constant ''' casename = file_met.split('/')[-2] station_id = file_met.split('/')[-1][:3] usr_case = str(int(casename[-2:])) if usr_case in ['1', '2']: index_field = {'bby': [3, 4, 10, 5, 6, 11, 13], 'czc': [3, 4, 10, 5, 6, 11, 13]} elif usr_case in ['3', '4', '5', '6', '7']: index_field = {'bby': [3, 5, 8, 10, 12, 17, 26], 'czc': [3, 4, 5, 6, 8, 13, 22]} else: index_field = {'bby': [3, 4, 5, 6, 8, 13, 15], 'czc': [3, 4, 5, 6, 8, 13, 15], 'frs': [3, 4, 5, 6, 8, 13, 15]} elev = {'bby': 15, 'czc': 462, 'frs': 45} name_field = ['press', 'temp', 'rh', 'wspd', 'wdir', 'precip', 'mixr'] index_field = index_field[station_id] locelevation = elev[station_id] ''' read the csv file ''' raw_dframe = pd.read_csv(file_met, header=None) ''' parse date columns into a single date col ''' dates_col = [0, 1, 2] raw_dates = raw_dframe.ix[:, dates_col] raw_dates.columns = ['Y', 'j', 'HHMM'] raw_dates['HHMM'] = raw_dates['HHMM'].apply(lambda x: '{0:0>4}'.format(x)) raw_dates = raw_dates.apply( lambda x: '%s %s %s' % (x['Y'], x['j'], x['HHMM']), axis=1) dates_fmt = '%Y %j %H%M' dates = raw_dates.apply(lambda x: datetime.strptime(x, dates_fmt)) ''' make meteo df, assign datetime index, and name columns ''' meteo = raw_dframe.ix[:, index_field] meteo.index = dates meteo.columns = name_field ''' create a dataframe with regular and continuous 24h period time index (this works as a QC for time gaps) ''' nano = 1000000000 time_diff = np.diff(meteo.index) sample_freq = (stats.mode(time_diff)[0][0]/nano).astype(int) # [seconds] fidx = meteo.index[0] # (start date for dataframe) fidx_str = pd.to_datetime( fidx.year*10000 + fidx.month*100 + fidx.day, format='%Y%m%d') periods = (24*60*60)/sample_freq ts = pd.date_range(fidx_str, periods=periods, freq=str(sample_freq)+'s') nanarray = np.empty((periods, 1)) nanarray[:] = np.nan df = pd.DataFrame(nanarray, index=ts) meteo = df.join(meteo, how='outer') meteo.drop(0, axis=1, inplace=True) ''' make field with hourly acum precip ''' hour = pd.TimeGrouper('H') preciph = meteo.precip.groupby(hour).sum() meteo = meteo.join(preciph, how='outer', rsuffix='h') ''' add thermodynamics ''' theta = tm.theta1(C=meteo.temp, hPa=meteo.press) thetaeq = tm.theta_equiv1(C=meteo.temp, hPa=meteo.press) meteo.loc[:, 'theta'] = pd.Series(theta, index=meteo.index) meteo.loc[:, 'thetaeq'] = pd.Series(thetaeq, index=meteo.index) ''' add sea level pressure ''' Tv = tm.virtual_temperature(C=meteo.temp, mixing_ratio=meteo.mixr/1000.) slp = tm.sea_level_press(K=Tv+273.15, Pa=meteo.press*100, m=locelevation) meteo.loc[:, 'sea_levp'] = slp ''' assign metadata (prototype, not really used) ''' units = {'press': 'mb', 'temp': 'C', 'rh': '%', 'wspd': 'm s-1', 'wdir': 'deg', 'precip': 'mm', 'mixr': 'g kg-1'} agl = {'press': 'NaN', 'temp': '10 m', 'rh': '10 m', 'wspd': 'NaN', 'wdir': 'NaN', 'precip': 'NaN', 'mixr': 'NaN'} for n in name_field: meteo[n].units = units[n] meteo[n].agl = agl[n] meteo[n].nan = -9999.999 meteo[n].sampling_freq = '2 minute' return meteo
def parse_surface_met(file_met, index_field=None, name_field=None, locelevation=None): from scipy import stats ''' Process surface meteorological files with .met extension Output dataframe assumes: - Each file is a 24h period observation - Frequency of observation is constant ''' casename = file_met.split('/')[-2] station_id = file_met.split('/')[-1][:3] usr_case = str(int(casename[-2:])) if usr_case in ['1', '2']: index_field = { 'bby': [3, 4, 10, 5, 6, 11, 13], 'czc': [3, 4, 10, 5, 6, 11, 13] } elif usr_case in ['3', '4', '5', '6', '7']: index_field = { 'bby': [3, 5, 8, 10, 12, 17, 26], 'czc': [3, 4, 5, 6, 8, 13, 22] } else: index_field = { 'bby': [3, 4, 5, 6, 8, 13, 15], 'czc': [3, 4, 5, 6, 8, 13, 15], 'frs': [3, 4, 5, 6, 8, 13, 15] } elev = {'bby': 15, 'czc': 462, 'frs': 45} name_field = ['press', 'temp', 'rh', 'wspd', 'wdir', 'precip', 'mixr'] index_field = index_field[station_id] locelevation = elev[station_id] ''' read the csv file ''' raw_dframe = pd.read_csv(file_met, header=None) ''' parse date columns into a single date col ''' dates_col = [0, 1, 2] raw_dates = raw_dframe.ix[:, dates_col] raw_dates.columns = ['Y', 'j', 'HHMM'] raw_dates['HHMM'] = raw_dates['HHMM'].apply(lambda x: '{0:0>4}'.format(x)) raw_dates = raw_dates.apply(lambda x: '%s %s %s' % (x['Y'], x['j'], x['HHMM']), axis=1) dates_fmt = '%Y %j %H%M' dates = raw_dates.apply(lambda x: datetime.strptime(x, dates_fmt)) ''' make meteo df, assign datetime index, and name columns ''' meteo = raw_dframe.ix[:, index_field] meteo.index = dates meteo.columns = name_field ''' create a dataframe with regular and continuous 24h period time index (this works as a QC for time gaps) ''' nano = 1000000000 time_diff = np.diff(meteo.index) sample_freq = (stats.mode(time_diff)[0][0] / nano).astype(int) # [seconds] fidx = meteo.index[0] # (start date for dataframe) fidx_str = pd.to_datetime(fidx.year * 10000 + fidx.month * 100 + fidx.day, format='%Y%m%d') periods = (24 * 60 * 60) / sample_freq ts = pd.date_range(fidx_str, periods=periods, freq=str(sample_freq) + 's') nanarray = np.empty((periods, 1)) nanarray[:] = np.nan df = pd.DataFrame(nanarray, index=ts) meteo = df.join(meteo, how='outer') meteo.drop(0, axis=1, inplace=True) ''' make field with hourly acum precip ''' hour = pd.TimeGrouper('H') preciph = meteo.precip.groupby(hour).sum() meteo = meteo.join(preciph, how='outer', rsuffix='h') ''' add thermodynamics ''' theta = tm.theta1(C=meteo.temp, hPa=meteo.press) thetaeq = tm.theta_equiv1(C=meteo.temp, hPa=meteo.press) meteo.loc[:, 'theta'] = pd.Series(theta, index=meteo.index) meteo.loc[:, 'thetaeq'] = pd.Series(thetaeq, index=meteo.index) ''' add sea level pressure ''' Tv = tm.virtual_temperature(C=meteo.temp, mixing_ratio=meteo.mixr / 1000.) slp = tm.sea_level_press(K=Tv + 273.15, Pa=meteo.press * 100, m=locelevation) meteo.loc[:, 'sea_levp'] = slp ''' assign metadata (prototype, not really used) ''' units = { 'press': 'mb', 'temp': 'C', 'rh': '%', 'wspd': 'm s-1', 'wdir': 'deg', 'precip': 'mm', 'mixr': 'g kg-1' } agl = { 'press': 'NaN', 'temp': '10 m', 'rh': '10 m', 'wspd': 'NaN', 'wdir': 'NaN', 'precip': 'NaN', 'mixr': 'NaN' } for n in name_field: meteo[n].units = units[n] meteo[n].agl = agl[n] meteo[n].nan = -9999.999 meteo[n].sampling_freq = '2 minute' return meteo