def get_angles(self, vis): from pyorbital.astronomy import get_alt_az, sun_zenith_angle from pyorbital.orbital import get_observer_look lons, lats = vis.attrs['area'].get_lonlats_dask(chunks=vis.data.chunks) suna = get_alt_az(vis.attrs['start_time'], lons, lats)[1] suna = xu.rad2deg(suna) sunz = sun_zenith_angle(vis.attrs['start_time'], lons, lats) sata, satel = get_observer_look(vis.attrs['satellite_longitude'], vis.attrs['satellite_latitude'], vis.attrs['satellite_altitude'], vis.attrs['start_time'], lons, lats, 0) satz = 90 - satel return sata, satz, suna, sunz
def get_angles(self, vis): from pyorbital.astronomy import get_alt_az, sun_zenith_angle from pyorbital.orbital import get_observer_look lons, lats = vis.attrs['area'].get_lonlats_dask( chunks=vis.data.chunks) sunalt, suna = get_alt_az(vis.attrs['start_time'], lons, lats) suna = xu.rad2deg(suna) sunz = sun_zenith_angle(vis.attrs['start_time'], lons, lats) sata, satel = get_observer_look( vis.attrs['satellite_longitude'], vis.attrs['satellite_latitude'], vis.attrs['satellite_altitude'], vis.attrs['start_time'], lons, lats, 0) satz = 90 - satel return sata, satz, suna, sunz
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')) ))
def xyz2angle(x, y, z): """Convert cartesian to azimuth and zenith.""" azi = xu.rad2deg(xu.arctan2(x, y)) zen = 90 - xu.rad2deg(xu.arctan2(z, xu.sqrt(x**2 + y**2))) return azi, zen
def xyz2lonlat(x, y, z): """Convert cartesian to lon lat.""" lon = xu.rad2deg(xu.arctan2(y, x)) lat = xu.rad2deg(xu.arctan2(z, xu.sqrt(x**2 + y**2))) return lon, lat
#print('fuuuuuuuuuuuuuuucl', tmp[vv]) ##i=i+1 #print('shape', d['rain'].shape, 'i', i) #print(i-1, rainmm[0]) #rainmm[i]=d['rain'][0,0].values - rainmm[i-1] #-rainmm[i-1] #tmp['rain']=rainmm[i] #else: #print('new dataset alert!!!!!! must disregard current rain rate value') #rainmm[i]=d['rain'][0,0].values #tmp[vv]=np.nan #else: #tmp['bc'] =d['bc'][0,0,0].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) #print(tmp.shape) tmp = pd.DataFrame(tmp, index=index).sort_index(axis=1) #tmp.drop_duplicates(subset ="dayofyear", keep = 'last', inplace = True) out = pd.concat((out, tmp)) #print(out) #out.drop_duplicates(subset ="dayofyear", keep = 'last', inplace = True) out.to_csv(op_fname) print('done ', op_fname) #if __name__ == '__main__': #main('./config.json')
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'))))
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-mh'] mh = config['coords']['Galway'] out = pd.DataFrame() f_paths = sorted(g.glob(os.path.join(path_in, 'wrfout_d01*'))) 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] idx = { 'x': np.asscalar(abs(d.XLONG[0, :] - mh['lon']).argmin()), 'y': np.asscalar(abs(d.XLAT[:, 0] - mh['lat']).argmin()) } 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.values tmp['qgraup'] = d.isel(bottom_top=0).QGRAUP.values tmp['qicd'] = d.isel(bottom_top=0).QICE.values tmp['qrain'] = d.isel(bottom_top=0).QRAIN.values tmp['qsnow'] = d.isel(bottom_top=0).QSNOW.values 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.values tmp = pd.DataFrame(tmp, index=index).sort_index(axis=1) out = pd.concat((out, tmp)) print('done {}'.format(f_path)) # 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, '{}_galway.csv'.format(index0.strftime('%Y%m%d%H%M'))))