def main(config_path): config = {} with open(config_path) as f_config: config = json.load(f_config) fmt_date = 'wrfout_d01_{}'.format(config['fmt']['date']) path_in = config['output-wrf-raw'] path_out = config['output-gw'] mh = config['coords']['Galway'] out = pd.DataFrame() f_paths = sorted(g.glob(os.path.join(path_in, 'wrfout_d01*'))) rainmm = {} rainmmt = {} snowmm = {} snowmmt = {} for i, f_path in enumerate(f_paths): index = [pd.Timestamp(dt.datetime.strptime( os.path.basename(f_path), fmt_date ))] if i == 0: index0 = index[0] dayofyear = to_dayofyear(index[0]) tmp = {} d = xa.open_dataset(f_path).sel(Time=0)[VARS] # Code to pick out the defined gridcell diffarraylon=np.asarray(d.XLONG[:]-mh['lon']) diffarraylat=np.asarray(d.XLAT[:]-mh['lat']) diffarrayabs=xau.sqrt(xau.square(diffarraylon)+xau.square(diffarraylat)) #note: the array is stored in array dims [lat, lon] ixlat,ixlon=np.where(diffarrayabs == np.min(diffarrayabs)) idx = { 'x': ixlon, 'y': ixlat } tmp_p, tmp_rh = pressure_rh(d) d = d.isel(west_east=idx['x'], south_north=idx['y']) # windspeed tmp['dayofyear'] = dayofyear # tmp['winddirection_deg'] = np.asscalar(xau.rad2deg( # xau.arctan2(-d.U10, -d.V10) # ).values) tmp['pressure_hPa'] = tmp_p[idx['y'], idx['x']] tmp['qcloud'] = d.isel(bottom_top=0).QCLOUD[0,:].values tmp['qgraup'] = d.isel(bottom_top=0).QGRAUP[0,:].values tmp['qicd'] = d.isel(bottom_top=0).QICE[0,:].values tmp['qrain'] = d.isel(bottom_top=0).QRAIN[0,:].values tmp['qsnow'] = d.isel(bottom_top=0).QSNOW[0,:].values if i == 0: rainmm[i] = np.asscalar((d.RAINNC.values + d.RAINC.values)) rainmmt[i] = np.asscalar((d.RAINNC.values + d.RAINC.values)) snowmm[i] = np.asscalar(d.SNOWNC.values) snowmmt[i] = np.asscalar(d.SNOWNC.values) else: rainmmt[i] = np.asscalar((d.RAINNC.values + d.RAINC.values)) rainmm[i] = np.asscalar(d.RAINNC.values + d.RAINC.values)-rainmmt[i-1] snowmmt[i] = np.asscalar(d.SNOWNC.values) snowmm[i] = np.asscalar(d.SNOWNC.values)-snowmmt[i-1] tmp['rain_mm'] = rainmm[i] tmp['relativehumidity_percent'] = tmp_rh[idx['y'], idx['x']] tmp['temperature2m_C'] = np.asscalar((d.T2 - 273.15).values) tmp['winddirection_deg'] = np.asscalar( 270 - xau.rad2deg(xau.arctan2(d.V10, d.U10)).values) % 360 tmp['windspeed_mPs'] = np.asscalar(xau.sqrt(d.U10**2 + d.V10**2).values) tmp['zcldfra'] = d.isel(bottom_top=0).CLDFRA[0,:].values tmp['znox'] = d.isel(bottom_top=0).no2[0,:].values + d.isel(bottom_top=0).no[0,:].values tmp['zo3'] = d.isel(bottom_top=0).o3[0,:].values*1000.0 tmp['zpm25'] = d.isel(bottom_top=0).PM2_5_DRY[0,:].values tmp['zpm10'] = d.isel(bottom_top=0).PM10[0,:].values tmp['zbc1'] = d.isel(bottom_top=0).BC1[0,:].values tmp['zbc2'] = d.isel(bottom_top=0).BC2[0,:].values tmp['zoc1'] = d.isel(bottom_top=0).OC1[0,:].values tmp['zoc2'] = d.isel(bottom_top=0).OC2[0,:].values tmp['zso2'] = d.isel(bottom_top=0).so2[0,:].values tmp['zpblh'] = np.asscalar(d.PBLH.values) tmp['zuv_index'] = np.sum((d.o3*d.PB/6950.0).values) tmp = pd.DataFrame(tmp, index=index).sort_index(axis=1) out = pd.concat((out, tmp)) print('done {}'.format(f_path)) #'BC1', 'BC2', 'OC1', 'OC2'] # out = out.rename({'pressure_sea_hPa': 'pressure_hPa'}) # (out[['dayofyear', 'pressure_hPa', 'relativehumidity_percent', # 'temperature2m_C', 'winddirection_deg', 'windspeed_mPs']] # .to_dataframe() # .to_csv(os.path.join(path_out, # '{}.csv'.format(index0.strftime('%Y%m%d%H%M'))))) out.to_csv(os.path.join( path_out, '{}.csv'.format(index0.strftime('%Y%m%d%H%M')) ))
E_PM25_ugperm2pers = E_PM_10_ugperm2pers * .75 E_BC_1_ugperm2pers = E_PM_10_ugperm2pers * .13 E_OC_DOM_ugperm2pers = E_PM25_ugperm2pers * .65 E_OC_25_10_ugperm2pers = E_PM_10_ugperm2pers * .65 - E_OC_DOM_ugperm2pers E_OIN_10_ugperm2pers = E_PM_10_ugperm2pers * .22 E_OIN_25_ugperm2pers = E_PM25_ugperm2pers * .22 #link_dest=os.path.join(emis_preproc_dir, 'wrfinput_d01') loc_lon, loc_lat = city_list[ss]['lon'], city_list[ss]['lat'] ## Get the index of dublin grid cell from the wrfinput file as the produced wrfchemi files don't contain xlat, xlon lat = np.asarray(dwrf.XLAT[0, :, :]) longg = np.asarray(dwrf.XLONG[0, :, :]) diffarray_lats = lat[:] - [loc_lat] diffarray_lons = longg[:] - [loc_lon] diffarrayabs = xu.sqrt( xu.square(diffarray_lons) + xu.square(diffarray_lats)) ixlat, ixlon = np.where(diffarrayabs == np.min(diffarrayabs)) idxx = {'x': ixlon, 'y': ixlat} lat_ix = np.argmin((arraylat - loc_lat)**2) lon_ix = np.argmin((arraylon - loc_lon)**2) print(lon_ix, lat_ix) #verified as corresponding with the location idx = {'x': lon_ix, 'y': lat_ix} dub_temp = data.var235[:, lat_ix, lon_ix].values - 273.15 print('Todays temp in ', ss, ' ', dub_temp) ## firstly, apply fix to NO, NO2 ratios ds.variables['E_NO2'][:, 0, :, :] = ds.variables['E_NO'][:, 0, :, :] * 0.22 ds.variables['E_NO'][:, 0, :, :] = ds.variables['E_NO'][:, 0, :, :] * 0.88 if dub_temp < 12.0:
print('di', i, f_path) d = xa.open_dataset(f_path)[VARS] index = [ pd.Timestamp( dt.datetime.strptime(os.path.basename(f_path), 'd01_%Y%m%d%H00.nc')) ] if i == 0: index0 = index[0] dayofyear = to_dayofyear(index[0]) mh = coords[kk] diffarraylon = np.asarray(d['lon'][:] - mh['lon']) diffarraylat = np.asarray(d['lat'][:] - mh['lat']) diffarrayabs = xau.sqrt( xau.square(diffarraylon) + xau.square(diffarraylat)) #note: the array is stored in array dims [lat, lon] ixlat, ixlon = np.where(diffarrayabs == np.min(diffarrayabs)) idx = {'x': ixlon, 'y': ixlat} d = d.isel(x=idx['x'], y=idx['y']) tmp['dayofyear'] = dayofyear for vv in VARS: if d[vv].ndim == 3: tmp[vv] = d[vv][0, 0, 0].values else: tmp[vv] = d[vv][0, 0].values #tmp[vv] = 0.0 # first: put in the rain loop #if vv=='rain': #print('look out! its the', vv, 'vbl')
wrfrundir = os.path.join(work_root_dir, 'WRFV3/run') # link the operational wrfinput file to the emission preprocessor directory. This is necessary to generate the hourly emissions. wrfinfile = os.path.join(wrfrundir, 'wrfinput_d01') link_dest = os.path.join(emis_preproc_dir, 'wrfinput_d01') dub_lon, dub_lat = -6.249910, 53.426448 dwrf = xr.open_dataset(wrfinfile) # Get the index of dublin grid cell from the wrfinput file as the produced wrfchemi files don't contain xlat, xlon lat = np.asarray(dwrf.XLAT[0, :, :]) longg = np.asarray(dwrf.XLONG[0, :, :]) diffarray_lats = lat[:] - [dub_lat] diffarray_lons = longg[:] - [dub_lon] print(diffarray_lats.shape, diffarray_lons.shape) #diffarraylon1=longg-dub_lon diffarrayabs = xu.sqrt(xu.square(diffarray_lons) + xu.square(diffarray_lats)) ixlat, ixlon = np.where(diffarrayabs == np.min(diffarrayabs)) idxx = {'x': ixlon, 'y': ixlat} print(idxx) if os.path.exists(link_dest): os.remove(link_dest) print('removed the input file') os.symlink(wrfinfile, link_dest) #ii='datestr' ii = cdo.showdate(input=os.path.join(work_root_dir, 'WRFV3/run/wrfinput_d01')) i = dt.datetime.strptime(ii[0], '%Y-%m-%d') #i=dt.datetime.strptime('2018-11-02', '%Y-%m-%d') #2. Update the namelist with current date upd_file = os.path.join(emis_preproc_dir, 'namelist')
def main(config_path): config = {} with open(config_path) as f_config: config = json.load(f_config) #fmt_date = 'd01_{}'.format('%Y%m%d%H00.nc') path_in = ('/mnt/raid/wrf-chem/wrfchem_v39_cri/data_back/output-wrf') path_out = '../data/output-mh-archive' mh = config['coords']['Mace Head'] out = pd.DataFrame() f_paths = sorted(g.glob(os.path.join(path_in, 'd01_*'))) rainmm = {} rainmmt = {} snowmm = {} snowmmt = {} ddstr = {} for i, f_path in enumerate(f_paths): index = [ pd.Timestamp( dt.datetime.strptime(os.path.basename(f_path), 'd01_%Y%m%d%H00.nc')) ] print('innnndex', index) print(i, f_path) if i == 0: index0 = index[0] print('1st inx', index0) dayofyear = to_dayofyear(index[0]) tmp = {} #d = xa.open_dataset(f_path).sel(time=0)[VARS] d = xa.open_dataset(f_path)[VARS] # Code to pick out the defined gridcell diffarraylon = np.asarray(d['lon'][:] - mh['lon']) diffarraylat = np.asarray(d['lat'][:] - mh['lat']) diffarrayabs = xau.sqrt( xau.square(diffarraylon) + xau.square(diffarraylat)) #note: the array is stored in array dims [lat, lon] ixlat, ixlon = np.where(diffarrayabs == np.min(diffarrayabs)) idx = {'x': ixlon, 'y': ixlat} #tmp_p, tmp_rh = pressure_rh(d) print(d.dims) d = d.isel(x=idx['x'], y=idx['y']) # windspeed tmp['dayofyear'] = dayofyear for vv in VARS: #print('variable', vv) # first: put in the rain loop if vv == 'rain': print('look out! its the', vv, 'vbl') print(d[vv].shape) cmmd = ('stat -c %y ' + str(f_path)) dstr = str(os.system(cmmd)) ddstr[i] = os.popen(cmmd).read()[:10] if i == 0: print('the first datapoint') rainmm[i] = d['rain'][0, 0].values tmp[vv] = np.nan print(tmp[vv]) else: if ddstr[i - 1] == ddstr[i]: print('same dataset') print(d[vv].shape) rainmm[i] = d['rain'][0, 0] - rainmm[i - 1] tmp[vv] = rainmm[i].values else: print( 'new dataset alert!!!!!! must disregard current rain rate value' ) rainmm[i] = d['rain'][0, 0].values tmp[vv] = np.nan print('fuuuuuuuuuuuuuuucl', tmp[vv]) #print('uuup', ddstr) ## if i=0: then disregard the first value, and subtract all subsequent. if new date (find out by executing stat -c '%y' d01_201909110000.nc), then deal with it the same way etc. else: if d[vv].ndim == 3: #print(d[vv].shape) tmp[vv] = d[vv][0, 0, 0].values else: #print(vv, d[vv].shape) tmp[vv] = d[vv][0, 0].values ## extract windspeed and wind direction #winddirection_deg = np.asscalar( #270 - xau.rad2deg(xau.arctan2(d.V10, d.U10)).values) % 360 tmp['winddirection_deg'] = np.asscalar( 270 - xau.rad2deg(xau.arctan2(d.v10, d.u10)).values) % 360 tmp['windspeed_mPs'] = np.asscalar( xau.sqrt(d.u10**2 + d.v10**2).values) tmp = pd.DataFrame(tmp, index=index).sort_index(axis=1) #print(tmp.keys()) #for kk in tmp.keys(): #print(kk, tmp[kk].values) out = pd.concat((out, tmp)) #print('done {}'.format(f_path)) #'BC1', 'BC2', 'OC1', 'OC2'] # out = out.rename({'pressure_sea_hPa': 'pressure_hPa'}) # (out[['dayofyear', 'pressure_hPa', 'relativehumidity_percent', # 'temperature2m_C', 'winddirection_deg', 'windspeed_mPs']] # .to_dataframe() # .to_csv(os.path.join(path_out, # '{}.csv'.format(index0.strftime('%Y%m%d%H%M'))))) out.to_csv( os.path.join(path_out, '{}.csv'.format(index0.strftime('%Y%m%d%H%M'))))