def get_profiles_from_HYCOM(N,folder_hycom,prefix,lon_track,lat_track,\ lon_hycom,lat_hycom,var): afiles = sorted(glob.glob(os.path.join(folder_hycom, prefix + '*.a'))) file = afiles[N] #Reading time stamp year = int(file.split('/')[-1].split('.')[1][0:4]) month = int(file.split('/')[-1].split('.')[1][4:6]) day = int(file.split('/')[-1].split('.')[1][6:8]) hour = int(file.split('/')[-1].split('.')[1][8:10]) dt = int(file.split('/')[-1].split('.')[3][1:]) timestamp_hycom = mdates.date2num(datetime(year, month, day, hour)) + dt / 24 time_hycom = mdates.num2date(timestamp_hycom) # Interpolating lat_track and lon_track into HYCOM grid oklon = np.round( np.interp(lon_track + 360, lon_hycom[0, :], np.arange(len(lon_hycom[0, :])))).astype(int) oklat = np.round( np.interp(lat_track, lat_hycom[:, 0], np.arange(len(lat_hycom[:, 0])))).astype(int) # Reading 3D variable from binary file var_hyc = readBinz(file[:-2], '3z', var) var_hycom = var_hyc[oklat, oklon, :].T time_hycom = np.asarray(time_hycom) return var_hycom, time_hycom
def get_glider_transect_from_HYCOM(folder_hycom, prefix, nz, lon_hycom, lat_hycom, var, timestamp_glider, lon_glider, lat_glider): afiles = sorted(glob.glob(os.path.join(folder_hycom, prefix + '*.a'))) target_var_hycom = np.empty((nz, len(afiles))) target_var_hycom[:] = np.nan time_hycom = [] for x, file in enumerate(afiles): print(x) #lines=[line.rstrip() for line in open(file[:-2]+'.b')] #Reading time stamp year = int(file.split('/')[-1].split('.')[1][0:4]) month = int(file.split('/')[-1].split('.')[1][4:6]) day = int(file.split('/')[-1].split('.')[1][6:8]) hour = int(file.split('/')[-1].split('.')[1][8:10]) dt = int(file.split('/')[-1].split('.')[3][1:]) timestamp_hycom = mdates.date2num(datetime(year, month, day, hour)) + dt / 24 time_hycom.append(mdates.num2date(timestamp_hycom)) # Interpolating latg and longlider into HYCOM grid sublon_hycom = np.interp(timestamp_hycom, timestamp_glider, lon_glider) sublat_hycom = np.interp(timestamp_hycom, timestamp_glider, lat_glider) oklon_hycom = np.int( np.round( np.interp(sublon_hycom, lon_hycom[0, :], np.arange(len(lon_hycom[0, :]))))) oklat_hycom = np.int( np.round( np.interp(sublat_hycom, lat_hycom[:, 0], np.arange(len(lat_hycom[:, 0]))))) # Reading 3D variable from binary file var_hycom = readBinz(file[:-2], '3z', var) #ts=readBin(afile,'archive','temp') target_var_hycom[:, x] = var_hycom[oklat_hycom, oklon_hycom, :] time_hycom = np.asarray(time_hycom) #timestamp_hycom = mdates.date2num(time_hycom) return target_var_hycom, time_hycom
dt = int(file.split('.')[3][1:]) timestamp_HMON_HYCOM = date2num(datetime(year, month, day, hour)) + dt / 24 time_HMON_HYCOM.append(num2date(timestamp_HMON_HYCOM)) # Interpolating latg and longlider into RTOFS grid #sublonHMON_HYCOM = np.interp(timestamp_HMON_HYCOM,timestampg,target_long) #sublatHMON_HYCOM = np.interp(timestamp_HMON_HYCOM,timestampg,target_latg) oklonHMON_HYCOM = np.int( np.round(np.interp(target_lonG, hlon[0, :], np.arange(len(hlon[0, :]))))) oklatHMON_HYCOM = np.int( np.round(np.interp(target_latG, hlat[:, 0], np.arange(len(hlat[:, 0]))))) # Reading 3D variable from binary file temp_HMON_HYCOM = readBinz(file[:-2], '3z', var_name) #ts=readBin(afile,'archive','temp') target_temp_HMON_HYCOM[x, :] = temp_HMON_HYCOM[oklatHMON_HYCOM, oklonHMON_HYCOM, :] time_HMON_HYCOM = np.asarray(time_HMON_HYCOM) timestamp_HMON_HYCOM = date2num(time_HMON_HYCOM) #%% Reading POM grid files pom_grid = xr.open_dataset(ncfolder + grid_file) lonc = np.asarray(pom_grid['east_e'][:]) latc = np.asarray(pom_grid['north_e'][:]) #lonu, latu = pom_grid['east_u'][:], pom_grid['north_u'][:] #lonv, latv = pom_grid['east_v'][:], pom_grid['north_v'][:]
dt = int(file.split('.')[3][1:]) timestamp_RTOFS = date2num(datetime(year, month, day, hour)) + dt / 24 time_RTOFS.append(num2date(timestamp_RTOFS)) # Interpolating latg and longlider into RTOFS grid sublonRTOFS = np.interp(timestamp_RTOFS, timestampg, target_long) sublatRTOFS = np.interp(timestamp_RTOFS, timestampg, target_latg) oklonRTOFS = np.int( np.round(np.interp(sublonRTOFS, hlon[0, :], np.arange(len(hlon[0, :]))))) oklatRTOFS = np.int( np.round(np.interp(sublatRTOFS, hlat[:, 0], np.arange(len(hlat[:, 0]))))) # Reading 3D variable from binary file temp_RTOFS = readBinz(file[:-2], '3z', var_name) #ts=readBin(afile,'archive','temp') target_temp_RTOFS[x, :] = temp_RTOFS[oklatRTOFS, oklonRTOFS, :] # Extracting list of variables #count=0 #for line in lines: # count+=1 # if line[0:5] == 'field': # break #lines=lines[count:] #vars=[line.split()[0] for line in lines] time_RTOFS = np.asarray(time_RTOFS) timestamp_RTOFS = date2num(time_RTOFS)
def HMON_HYCOM_fields(cycle,storm_id,lon_forec_track,lat_forec_track,lon_lim,lat_lim,temp_lim,salt_lim,temp200_lim,salt200_lim,tempb_lim,tempt_lim,folder_fig): #%% import numpy as np import matplotlib.pyplot as plt from datetime import datetime, timedelta import os import os.path import glob import cmocean from matplotlib.dates import date2num, num2date import xarray as xr import sys sys.path.append('/home/aristizabal/NCEP_scripts/') from utils4HYCOM import readBinz,readgrids #from utils4HYCOM import readdepth, readVar #from utils4HYCOM2 import readBinz # Increase fontsize of labels globally plt.rc('xtick',labelsize=14) plt.rc('ytick',labelsize=14) plt.rc('legend',fontsize=14) #%% Bathymetry file bath_file = '/home/aristizabal/bathymetry_files/GEBCO_2014_2D_-100.0_0.0_-10.0_70.0.nc' #%% Reading bathymetry data ncbath = xr.open_dataset(bath_file) bath_lat = ncbath.variables['lat'][:] bath_lon = ncbath.variables['lon'][:] bath_elev = ncbath.variables['elevation'][:] oklatbath = np.logical_and(bath_lat >= lat_lim[0],bath_lat <= lat_lim[-1]) oklonbath = np.logical_and(bath_lon >= lon_lim[0],bath_lon <= lon_lim[-1]) bath_latsub = bath_lat[oklatbath] bath_lonsub = bath_lon[oklonbath] bath_elevs = bath_elev[oklatbath,:] bath_elevsub = bath_elevs[:,oklonbath] #%% folder and file names ti = datetime.today() - timedelta(1) folder_hmon_hycom = '/home/aristizabal/HMON_HYCOM_' + storm_id + '_' + str(ti.year) + '/' + 'HMON_HYCOM_' + storm_id + '_' + cycle + '/' #%% Reading RTOFS grid grid_file = sorted(glob.glob(os.path.join(folder_hmon_hycom,'*regional.grid.*')))[0][:-2] #%% Reading RTOFS grid print('Retrieving coordinates from RTOFS') # Reading lat and lon #lines_grid = [line.rstrip() for line in open(grid_file+'.b')] lon_hycom = np.array(readgrids(grid_file,'plon:',[0])) lat_hycom = np.array(readgrids(grid_file,'plat:',[0])) #depth_HMON_HYCOM = np.asarray(readdepth(HMON_HYCOM_depth,'depth')) # Reading depths afiles = sorted(glob.glob(os.path.join(folder_hmon_hycom,'*hat10_3z'+'*.a'))) lines=[line.rstrip() for line in open(afiles[0][:-2]+'.b')] z = [] for line in lines[6:]: if line.split()[2]=='temp': #print(line.split()[1]) z.append(float(line.split()[1])) depth_HYCOM = np.asarray(z) #%% time_HYCOM = [] for x, file in enumerate(afiles): print(x) #lines=[line.rstrip() for line in open(file[:-2]+'.b')] #Reading time stamp year = int(file.split('/')[-1].split('.')[1][0:4]) month = int(file.split('/')[-1].split('.')[1][4:6]) day = int(file.split('/')[-1].split('.')[1][6:8]) hour = int(file.split('/')[-1].split('.')[1][8:10]) dt = int(file.split('/')[-1].split('.')[-2][1:]) timestamp_HYCOM = date2num(datetime(year,month,day,hour)) + dt/24 time_HYCOM.append(num2date(timestamp_HYCOM)) # Reading 3D variable from binary file oktime = 0 # first file temp_HMON_HYCOM = readBinz(afiles[oktime][:-2],'3z','temp') salt_HMON_HYCOM = readBinz(afiles[oktime][:-2],'3z','salinity') uvel_HMON_HYCOM = readBinz(afiles[oktime][:-2],'3z','u-veloc.') vvel_HMON_HYCOM = readBinz(afiles[oktime][:-2],'3z','v-veloc.') #%% oklon_HYCOM = np.where(np.logical_and(lon_hycom[0,:] >= lon_lim[0]+360,lon_hycom[0,:] <= lon_lim[1]+360))[0] oklat_HYCOM = np.where(np.logical_and(lat_hycom[:,0] >= lat_lim[0],lat_hycom[:,0] <= lat_lim[1]))[0] lon_HYCOM =lon_hycom[0,oklon_HYCOM]-360 lat_HYCOM =lat_hycom[oklat_HYCOM,0] sst_HYCOM = temp_HMON_HYCOM[oklat_HYCOM,:,0][:,oklon_HYCOM] sss_HYCOM = salt_HMON_HYCOM[oklat_HYCOM,:,0][:,oklon_HYCOM] su_HYCOM = uvel_HMON_HYCOM[oklat_HYCOM,:,0][:,oklon_HYCOM] sv_HYCOM = vvel_HMON_HYCOM[oklat_HYCOM,:,0][:,oklon_HYCOM] #okdepth200 = np.where(depth_HYCOM >= 200)[0][0] #temp200_HYCOM = temp_HMON_HYCOM[oklat_HYCOM,:,okdepth200][:,oklon_HYCOM] #salt200_HYCOM = salt_HMON_HYCOM[oklat_HYCOM,:,okdepth200][:,oklon_HYCOM] #%% SST kw = dict(levels = np.arange(temp_lim[0],temp_lim[1],0.5)) plt.figure() plt.contour(bath_lonsub,bath_latsub,bath_elevsub,levels=[0],colors='k') plt.contourf(bath_lonsub,bath_latsub,bath_elevsub,levels=[0,10000],colors='papayawhip',alpha=0.5) plt.contourf(lon_HYCOM,lat_HYCOM,sst_HYCOM,cmap=cmocean.cm.thermal,**kw) plt.plot(lon_forec_track,lat_forec_track,'.-',color='grey') cbar = plt.colorbar() cbar.ax.tick_params(labelsize=14) cbar.ax.set_ylabel('($^\circ$C)',fontsize=14,labelpad=15) plt.axis('scaled') plt.xlim(lon_lim[0],lon_lim[1]) plt.ylim(lat_lim[0],lat_lim[1]) plt.title('HMON-HYCOM SST ' + 'Storm ' + storm_id + ' Cycle ' + cycle + '\n on '+str(time_HYCOM[oktime])[0:13],fontsize=16) file_name = folder_fig + 'HMON_HYCOM_SST_' + cycle plt.savefig(file_name,bbox_inches = 'tight',pad_inches = 0.1) #%% SSS kw = dict(levels = np.arange(salt_lim[0],salt_lim[1],0.5)) plt.figure() plt.contour(bath_lonsub,bath_latsub,bath_elevsub,levels=[0],colors='k') plt.contourf(bath_lonsub,bath_latsub,bath_elevsub,levels=[0,10000],colors='papayawhip',alpha=0.5) plt.contourf(lon_HYCOM,lat_HYCOM,sss_HYCOM,cmap=cmocean.cm.haline,**kw) plt.plot(lon_forec_track,lat_forec_track,'.-',color='grey') cbar = plt.colorbar() cbar.ax.tick_params(labelsize=14) plt.axis('scaled') plt.xlim(lon_lim[0],lon_lim[1]) plt.ylim(lat_lim[0],lat_lim[1]) plt.title('HMON-HYCOM SSS ' + 'Storm ' + storm_id + ' Cycle ' + cycle + '\n on '+str(time_HYCOM[oktime])[0:13],fontsize=16) file_name = folder_fig + 'HMON_HYCOM_SSS_' + cycle plt.savefig(file_name,bbox_inches = 'tight',pad_inches = 0.1) #%% SST and velocity vectors kw = dict(levels = np.arange(temp_lim[0],temp_lim[1],0.5)) plt.figure() plt.contour(bath_lonsub,bath_latsub,bath_elevsub,levels=[0],colors='k') plt.contourf(bath_lonsub,bath_latsub,bath_elevsub,levels=[0,10000],colors='papayawhip',alpha=0.5) plt.contourf(lon_HYCOM,lat_HYCOM,sst_HYCOM,cmap=cmocean.cm.thermal,**kw) cbar = plt.colorbar() cbar.ax.tick_params(labelsize=14) cbar.ax.set_ylabel('($^\circ$C)',fontsize=14,labelpad=15) plt.axis('scaled') plt.xlim(lon_lim[0],lon_lim[1]) plt.ylim(lat_lim[0],lat_lim[1]) plt.title('HMON-HYCOM SST ' + 'Storm ' + storm_id + ' Cycle ' + cycle + '\n on '+str(time_HYCOM[oktime])[0:13],fontsize=16) q = plt.quiver(lon_HYCOM[::10], lat_HYCOM[::10],su_HYCOM[::10,::10],sv_HYCOM[::10,::10]) #plt.quiverkey(q,np.min(lon_HYCOM)-5,np.max(lat_HYCOM)-5,1,"1 m/s",coordinates='data',color='k',fontproperties={'size': 14}) file_name = folder_fig + 'HMON_HYCOM_SST_UV_' + cycle plt.savefig(file_name,bbox_inches = 'tight',pad_inches = 0.1) #%% Figure temp transect along storm path lat_forec_tracku, ind = np.unique(lat_forec_track,return_index=True) lon_forec_tracku = lon_forec_track[ind] lon_forec_track_interp = np.interp(lat_hycom[:,0],lat_forec_tracku,lon_forec_tracku,left=np.nan,right=np.nan) lat_forec_track_interp = np.copy(lat_hycom[:,0]) lat_forec_track_interp[np.isnan(lon_forec_track_interp)] = np.nan lon_forec_track_int = lon_forec_track_interp[np.isfinite(lon_forec_track_interp)] lat_forec_track_int = lat_forec_track_interp[np.isfinite(lat_forec_track_interp)] oklon = np.round(np.interp(lon_forec_track_int,lon_hycom[0,:]-360,np.arange(len(lon_hycom[0,:])))).astype(int) oklat = np.round(np.interp(lat_forec_track_int,lat_hycom[:,0],np.arange(len(lat_hycom[:,0])))).astype(int) trans_temp_HYCOM = temp_HMON_HYCOM[oklat,oklon,:] kw = dict(levels = np.arange(tempt_lim[0],tempt_lim[1],1)) plt.figure() plt.contourf(lat_hycom[oklat,0],-depth_HYCOM,trans_temp_HYCOM.T,cmap=cmocean.cm.thermal,**kw) cbar = plt.colorbar() cbar.ax.tick_params(labelsize=16) plt.contour(lat_hycom[oklat,0],-depth_HYCOM,trans_temp_HYCOM.T,[26],color='k') cbar.ax.set_ylabel('($^\circ$C)',fontsize=14) cbar.ax.tick_params(labelsize=14) plt.ylabel('Depth (m)',fontsize=14) plt.xlabel('Latitude ($^o$)',fontsize=14) plt.title('HMON-HYCOM Temperature ' + 'Storm ' + storm_id + ' Cycle ' + cycle + '\n along Forecasted Storm Track',fontsize=16) plt.ylim([-300,0]) file = folder_fig + 'HMON_HYCOM_temp_along_forecasted_track_' + cycle plt.savefig(file,bbox_inches = 'tight',pad_inches = 0.1)
plt.axis('scaled') plt.ylim([ylim[0],ylim[1]]) plt.xlim([xlim[0],xlim[1]]) #%% Surface temperature HYCOM experimental folder_hycom =folder_hycom_exp prefix = prefix_hycom N = 0 var = 'temp' afiles = sorted(glob.glob(os.path.join(folder_hycom,prefix+'*.a'))) file = afiles[N] # Reading 3D variable from binary file var_hyc = readBinz(file[:-2],'3z',var) temp_HYCOM = var_hyc[:,:,0] #Reading time stamp year = int(file.split('/')[-1].split('.')[1][0:4]) month = int(file.split('/')[-1].split('.')[1][4:6]) day = int(file.split('/')[-1].split('.')[1][6:8]) hour = int(file.split('/')[-1].split('.')[1][8:10]) dt = int(file.split('/')[-1].split('.')[3][1:]) timestamp_hycom = mdates.date2num(datetime(year,month,day,hour)) + dt/24 time_hycom = mdates.num2date(timestamp_hycom) kw = dict(levels = np.linspace(28,30,41)) #fig,ax = plt.subplots(figsize=(5,5)) plt.figure() plt.contourf(lon_hycom[0,:]-360,lat_hycom[:,0],temp_HYCOM,cmap=cmocean.cm.thermal,**kw)
time_HMON_HYCOM.append(num2date(timestamp_HMON_HYCOM)) # Interpolating latg and longlider into RTOFS grid sublonHMON_HYCOM = np.interp(timestamp_HMON_HYCOM, timestampg, long + 360) sublatHMON_HYCOM = np.interp(timestamp_HMON_HYCOM, timestampg, latg) oklonHMON_HYCOM = np.int( np.round( np.interp(sublonHMON_HYCOM, lon_hycom[0, :], np.arange(len(lon_hycom[0, :]))))) oklatHMON_HYCOM = np.int( np.round( np.interp(sublatHMON_HYCOM, lat_hycom[:, 0], np.arange(len(lat_hycom[:, 0]))))) # Reading 3D variable from binary file temp_HMON_HYCOM = readBinz(file[:-2], '3z', 'temp') #ts=readBin(afile,'archive','temp') target_temp_HMON_HYCOM[x, :] = temp_HMON_HYCOM[oklatHMON_HYCOM, oklonHMON_HYCOM, :] # Extracting list of variables #count=0 #for line in lines: # count+=1 # if line[0:5] == 'field': # break #lines=lines[count:] #vars=[line.split()[0] for line in lines] time_HMON_HYCOM = np.asarray(time_HMON_HYCOM)
#%% N = 0 var = 'temp' afiles = sorted( glob.glob(os.path.join(folder_RTOFS_DA, prefix_RTOFS_DA + '*.a'))) file = afiles[N] lines = [line.rstrip() for line in open(file[:-2] + '.b')] time_stamp = lines[-1].split()[2] hycom_days = lines[-1].split()[3] tzero = datetime(1901, 1, 1, 0, 0) time_RTOFS_DA = tzero + timedelta(float(hycom_days) - 1) # Reading 3D variable from binary file var_rtofs = readBinz(file[:-2], '3z', var) #%% oklon = np.where( np.logical_and(lon_RTOFS_DA[0, :] >= lon_lim[0] + 360, lon_RTOFS_DA[0, :] <= lon_lim[1] + 360))[0] oklat = np.where( np.logical_and(lat_RTOFS_DA[:, 0] >= lat_lim[0], lat_RTOFS_DA[:, 0] <= lat_lim[1]))[0] temp_RTOFS_DA = np.asarray(var_rtofs[oklat, :, 0][:, oklon]) m = Basemap(projection='merc', llcrnrlat=15, urcrnrlat=32.5,
time_HMON_HYCOM.append(num2date(timestamp_HMON_HYCOM)) # Interpolating latg and longlider into RTOFS grid sublonHMON_HYCOM = np.interp(timestamp_HMON_HYCOM, timestampg, target_long) sublatHMON_HYCOM = np.interp(timestamp_HMON_HYCOM, timestampg, target_latg) oklonHMON_HYCOM = np.int( np.round( np.interp(sublonHMON_HYCOM, hlon[0, :], np.arange(len(hlon[0, :]))))) oklatHMON_HYCOM = np.int( np.round( np.interp(sublatHMON_HYCOM, hlat[:, 0], np.arange(len(hlat[:, 0]))))) # Reading 3D variable from binary file temp_HMON_HYCOM = readBinz(file[:-2], '3z', var_name) #ts=readBin(afile,'archive','temp') target_temp_HMON_HYCOM[x, :] = temp_HMON_HYCOM[oklatHMON_HYCOM, oklonHMON_HYCOM, :] # Extracting list of variables #count=0 #for line in lines: # count+=1 # if line[0:5] == 'field': # break #lines=lines[count:] #vars=[line.split()[0] for line in lines] time_HMON_HYCOM = np.asarray(time_HMON_HYCOM)
# Read HYCOM exp file = afiles[n] #Reading time stamp year = int(file.split('/')[-1].split('.')[1][0:4]) month = int(file.split('/')[-1].split('.')[1][4:6]) day = int(file.split('/')[-1].split('.')[1][6:8]) hour = int(file.split('/')[-1].split('.')[1][8:10]) dt = int(file.split('/')[-1].split('.')[3][1:]) timestamp_hycom = mdates.date2num(datetime(year, month, day, hour)) + dt / 24 time_hycom_exp.append(datetime(year, month, day, hour)) # Reading 3D variable from binary file temp_hycom = np.asarray(readBinz(file[:-2], '3z', 'temp')) temp_hycom[temp_hycom > 100] = np.nan salt_hycom = np.asarray(readBinz(file[:-2], '3z', 'salinity')) salt_hycom[salt_hycom > 100] = np.nan oklon_hycom = np.where( np.logical_and(lon_hycom[0, :] - 360 > xlim[0], lon_hycom[0, :] - 360 < xlim[1]))[0] oklat_hycom = np.where( np.logical_and(lat_hycom[:, 0] > ylim[0], lat_hycom[:, 0] < ylim[1]))[0] meshlon_lat_hycom = np.meshgrid(lon_hycom[0, oklon_hycom], lat_hycom[oklat_hycom, 0]) lat_lon_matrix_hycom = np.stack(
time_HYCOM = [] for x, file in enumerate(afiles): print(x) lines = [line.rstrip() for line in open(file[:-2] + '.b')] #Reading time stamp year = int(file.split('.')[1][0:4]) month = int(file.split('.')[1][4:6]) day = int(file.split('.')[1][6:8]) hour = int(file.split('.')[1][8:10]) dt = int(file.split('.')[3][1:]) timestamp_HYCOM = date2num(datetime(year, month, day, hour)) + dt / 24 time_HYCOM.append(num2date(timestamp_HYCOM)) # Reading 3D variable from binary file temp_HYCOM = readBinz(file[:-2], '3z', 'temp') sst_HYCOM[x, :, :] = temp_HYCOM[botm:top, left:right, 0] u_HYCOM = readBinz(file[:-2], '3z', 'u-veloc.') su_HYCOM[x, :, :] = u_HYCOM[botm:top, left:right, 0] v_HYCOM = readBinz(file[:-2], '3z', 'v-veloc.') sv_HYCOM[x, :, :] = v_HYCOM[botm:top, left:right, 0] time_HYCOM = np.asarray(time_HYCOM) timestamp_HYCOM = date2num(time_HYCOM) #%% Reading bathymetry data ncbath = xr.open_dataset(bath_file) bath_lat = ncbath.variables['lat'][:]