def plot_zetares(time1, zeta1, time2, zeta2,show=True, tidecon=None): """ Plot timeseries and residual. """ ittide=True try: import ttide except ImportError: print("ttide is not installed, please install tide.") ittide=False f,ax=plt.subplots(2,1,sharex=True) ax[0].plot(time1, zeta1,'r',lw=2) ax[0].plot(time2, zeta2,'b',lw=.5) if ittide: if tidecon is None: out1=ttide.t_tide(zeta1,stime=time1[0],dt=24*(time1[1]-time1[0])) out2=ttide.t_tide(zeta2,stime=time2[0],dt=24*(time2[1]-time2[0])) else: out1=ttide.t_tide(zeta1,stime=time1[0],dt=24*(time1[1]-time1[0]),constitnames=tidecon) out2=ttide.t_tide(zeta2,stime=time2[0],dt=24*(time2[1]-time2[0]),constitnames=tidecon) ax[1].plot(time1, zeta1-out1(time1),'r',lw=2) ax[1].plot(time2, zeta2-out2(time2),'b',lw=.5) if show==True: f.show() return f,ax,out1,out2 else: return f,ax,out1,out2
def run_ttide(time,zeta,lat,constitnames=None): if constitnames is None: out=ttide.t_tide(zeta,stime=time[0],dt=np.diff(time)[0]*24,synth=-1,out_style=None,lat=lat) else: out=ttide.t_tide(zeta,stime=time[0],dt=np.diff(time)[0]*24,synth=-1,out_style=None,lat=lat,constitnames=constitnames) return out
def process_point(i): try: e = interp(nhours,hours,ncv['elev'][:,i]) tides = tt.t_tide(e,dt=dthours,out_style=None) except: tides=None return tides
def process_timeseries_parallel(elev): try: e = interp(nhours,hours,elev) tides = tt.t_tide(e,dt=dthours,out_style=None) except: tides=None return tides
def process_parallel(i): try: nnc = netCDF4.Dataset('schout_2.nc') e = interp(nhours,hours,nnc.variables['elev'][:,i]) nnc.close() tides = tt.t_tide(e,dt=dthours,out_style=None) except: tides=None return tides
def process_chunk_parallel(elev): tides=[] for i in range(elev.shape[1]): try: e = interp(nhours,hours,elev[:,i]) tides.append(tt.t_tide(e,dt=dthours,out_style=None)) except: tides.append(None) return tides
def run_ttide(time, zeta, lat, constitnames=None): if constitnames is None: out = ttide.t_tide(zeta, stime=time[0], dt=np.diff(time)[0] * 24, synth=-1, out_style=None, lat=lat) else: out = ttide.t_tide(zeta, stime=time[0], dt=np.diff(time)[0] * 24, synth=-1, out_style=None, lat=lat, constitnames=constitnames) return out
def ttide_filt(utest, vtest): #Subtract TIDES if sum(~isnan(utest)) < 3: ufilt = utest vfilt = vtest else: tfit_u = tt.t_tide(utest + 1j * vtest, dt=1) tidepart = tfit_u(arange(0, len(utest) / 2, 0.5)) ufilt = utest - tidepart.real vfilt = vtest - tidepart.imag return ufilt, vfilt
def station_ttide(zeta_da, grid, lat_t, lon_t, stime, constit_list): zeta_flat = zeta_da.stack(etaxi=('eta_rho', 'xi_rho')) grid_flat = grid.stack(etaxi=('eta_rho', 'xi_rho')) lat_s = grid_flat.lat_rho.values[grid_flat.mask_rho.values == True] lon_s = grid_flat.lon_rho.values[grid_flat.mask_rho.values == True] zeta_s = zeta_flat.values[:, grid_flat.mask_rho.values == True] etaxi_s = zeta_flat.etaxi.values[grid_flat.mask_rho.values == True] points = np.column_stack((lat_s, lon_s)) tree = KDTree(points) target = np.column_stack((lat_t, lon_t)) dist, ind = tree.query(target) #dist=dist*10.0 tmp = {} tmp['roms_signal'] = zeta_s[:, ind].squeeze() tmp['roms_ind'], tmp['dist_to ATG'] = ind, dist lat_r = lat_s[ind] lon_r = lon_s[ind] eta_rho, xi_rho = np.fromstring(str(etaxi_s[ind])[2:-2], sep=', ', dtype=int) #print('atg lat(lon): %.2f,%.2f'%(lat_t,lon_t)) #print('roms lat(lon): %.2f,%.2f'%(lat_r,lon_r)) dist = haversine(lon_t, lat_t, lon_r, lat_r) try: tmp['t_tide'] = tt.t_tide(tmp['roms_signal'], dt=1, stime=stime, lat=lat_r, out_style=None) except TypeError: for const in constit_list: tmp[const] = [np.nan, np.nan, np.nan, np.nan] return eta_rho, xi_rho, dist, tmp for const in constit_list: tide_con_ind = list(tmp['t_tide']['nameu']).index( str.encode(const + ' ')) tmp[const] = tmp['t_tide']['tidecon'][tide_con_ind] #print(eta_rho,xi_rho) return eta_rho, xi_rho, dist, tmp
def ttideAnalyse(self): '''T Tide Analysis processing''' input_dict1 = self.inputDict1() input_dict2 = self.inputDict2() ad = input_dict1['depth'] at = input_dict1['time'] latitude = input_dict2['latitude'] time_diff = input_dict1['interval'] / 60 time_num = date2num(at.to_pydatetime()) coef = t_tide(ad, dt=time_diff, stime=time_num[0], lat=latitude, synth=0) return coef
def grid_ttide(da, grid_ds, stime, constit_list, res=50): ana_list = ['amp', 'amp_err', 'phase', 'phase_err'] print('setting up the new fields ', ana_list, ' for ', constit_list) dummy = np.empty((da.eta_rho.size, da.xi_rho.size)) dummy[:, :] = np.nan for const in constit_list: for ana in ana_list: #print(const+'_'+ana) grid_ds[const + '_' + ana] = (('eta_rho', 'xi_rho'), dummy.copy()) print("applying t_tide to every ", res, "th cell ...") xi_values = np.linspace(da.xi_rho[0].values, da.xi_rho.size - 1, res, dtype=int, endpoint=True) eta_values = np.linspace(da.eta_rho[0].values, da.eta_rho.size - 1, res, dtype=int, endpoint=True) for xi in log_progress(xi_values, name='xi'): for eta in eta_values: da_sl = da.isel(eta_rho=eta, xi_rho=xi) grd_sl = grid_ds.isel(eta_rho=eta, xi_rho=xi) if da_sl.isnull().values.any(): for const in constit_list: for ana in ana_list: grid_ds[const + '_' + ana][eta, xi] = np.NaN else: signal = da_sl.values latitude = grd_sl.lat_rho.values try: ttide_out = tt.t_tide(signal, stime=stime, lat=latitude, out_style=None) tt_ind = {} for const in constit_list: tt_ind[const] = list(ttide_out['nameu']).index( str.encode(const + ' ')) for ana, tt_ana in zip( ana_list, ttide_out['tidecon'][tt_ind[const]]): grid_ds[const + '_' + ana][eta, xi] = tt_ana except TypeError: for const in constit_list: for ana in ana_list: grid_ds[const + '_' + ana][eta, xi] = np.NaN print('interpolating intermediate cells and mask land') for con in constit_list: for ana in ana_list: grid_ds[con + '_' + ana].values = NDinterp(grid_ds[con + '_' + ana].values) grid_ds[con + '_' + ana] = grid_ds[con + '_' + ana].where( grid_ds.mask_rho, 0.0) return grid_ds
from aux_funcs import * ############################################################################# ################### Start with AQD data, then add ADCP and interp############ ############################################################################# import ttide as tt moorname = 'CF1' aqdlist = glob.glob(datadir + 'OSNAP2016recovery/AQD_Data_CF/OS_OSNAP-' + moorname + '*.nc') dat = Dataset(aqdlist[0], 'r') utest = array([float(tt) for tt in dat.variables['UCUR'][:].flatten()]) vtest = array([float(tt) for tt in dat.variables['VCUR'][:].flatten()]) tfit_u = tt.t_tide(utest + 1j * vtest, dt=0.5) tidepart = tfit_u(arange(0, len(utest) / 2, 0.5)) ufilt = utest - tidepart.real vfilt = vtest - tidepart.imag figure(figsize=(15, 5)) plot(utest) plot(tidepart.real) figure(figsize=(15, 5)) plot(vtest) plot(tidepart.imag) mean(vtest) mean(vfilt) # def extrapsurf(afield,datevec,prsvec,pdall): # panpiv=pd.DataFrame(index=datevec, # columns=prsvec)
'/home/mif001/scratch/sjh_hr_v3/test_bfric2/{}/output/'.format(name), singlename=grid + '_0001.nc') print('done load') savepath = '{}/{}_{}/ttide/{}/'.format(datapath, grid, datatype, name) if not os.path.exists(savepath): os.makedirs(savepath) dt = np.diff(data['time'])[0] * 24 out = pymp.shared.array((len(data['nodell'][:, 0]), 35, 4)) with pymp.Parallel(48) as p: for j in p.range(len(data['nodell'][:, 0])): print(j) out[j, ] = t_tide(data['zeta'][starttime:endtime, j], stime=data['time'][starttime], lat=data['lat'][j], synth=-1, out_style=None, dt=.25)['tidecon'] outall = t_tide(data['zeta'][starttime:endtime, j], stime=data['time'][starttime], lat=data['lat'][j], synth=-1, out_style=None, dt=.25) np.save( '{}{}_{}_{}_ttide_grid_tidecon_el_all_pymp.npy'.format( savepath, name, starttime, endtime), out) np.save( '{}{}_{}_{}_ttide_grid_singleout_el_all_pymp.npy'.format(
datetime.datetime(2012, 1, 1, 0, 0, 0) + datetime.timedelta(day) for day in days ]), constituents=cons) for c in mod_tide.model: # if c['constituent'].name in ['M2','M4','S2']: print(' model %s: %0.2f m' % (c['constituent'].name, c[1])) if use_ttide: print('%s' % name) ostart, ostop = abs(ncdays - days[0]).argmin(), abs(ncdays - days[-1]).argmin() try: obs_tide = tt.t_tide(ncelev[ostart:ostop], dt=obsdt, out_style=None) e = interp(nhours, hours, elevs[name]) mod_tide = tt.t_tide(e, dt=nmodeldt, out_style=None) m2idx = list(mod_tide['nameu']).index(b'M2 ') m4idx = list(mod_tide['nameu']).index(b'M4 ') s2idx = list(mod_tide['nameu']).index(b'S2 ') modtext = 'modelled M2: %0.3f, S2: %0.3f' % ( mod_tide['tidecon'][m2idx][0], mod_tide['tidecon'][s2idx][0]) obstext = 'observed M2: %0.3f, S2: %0.3f' % ( obs_tide['tidecon'][m2idx][0], obs_tide['tidecon'][s2idx][0]) print(modtext) print(obstext) except:
return asarray(nvmask) inum = len(nc.dimensions['nSCHISM_hgrid_node']) m2_amp = zeros((inum, )) m2_pha = zeros((inum, )) m4_amp = zeros((inum, )) m4_pha = zeros((inum, )) ut = netcdftime(ncv['time'].units) hours = ncv['time'][:] / 3600. hours = hours - hours[0] dthours = hours[1] - hours[0] for i in range(inum): tides = tt.t_tide(ncv['elev'][:, i], dt=dthours, out_style=None) m2idx = tides['nameu'].index(b'M2 ') m4idx = tides['nameu'].index(b'M4 ') m2_amp[i] = tides['tidevcon'][m2idx][0] m4_pha[i] = tides['tidevcon'][m2idx][2] m4_amp[i] = tides['tidevcon'][m4idx][0] m2_pha[i] = tides['tidevcon'][m4idx][2] if False: figure() tripcolor(x, y, nv, m2_amp, cmap=cm.jet, rasterized=True) proj.drawcoastlines() proj.fillcontinents((0.9, 0.9, 0.8)) cb = colorbar() cb.ax.set_title('M2 [m]\n', size=10.) savefig('m2_amp.jpg' % (varname, varname, tidx), dpi=600)
oz=ipt.interp1d(obs['rtime'],obs['rzeta'],timeshift) mz=ipt.interp1d(mod['rtime'],mod['rzeta'],timeshift) nidx=np.isnan(oz) mz[nidx]=np.nan oz=oz-np.nanmean(oz) mz=mz-np.nanmean(mz) r1z=residual_stats(mz[~nidx],oz[~nidx]) print('T_tide zeta obs') oout=ttide.t_tide(oz,dt=60.0/3600.0,stime=timeshift[0],lat=adcp['lat'],out_style=None) osnr=0 clist=oout['nameu'][oout['snr']>2] nsnr=len(clist) while(osnr!=nsnr): #print('looping') oout2=ttide.t_tide(oz,dt=60.0/3600.0,stime=timeshift[0],lat=adcp['lat'],constitnames=clist,out_style=None) osnr=nsnr clist=oout2['nameu'][oout2['snr']>2] nsnr=len(clist) print('T_tide zeta mod') mout=ttide.t_tide(mz,dt=60.0/3600.0,stime=timeshift[0],lat=model['lat'],out_style=None) osnr=0 clist=mout['nameu'][mout['snr']>2]
name5 = 'sjh_lr_v1_year_coare3_wu_mixing' data1 = loadnc( '/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1_sub/tg/' + name1, tgname + '_fvcom.nc', False) data2 = loadnc( '/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1_sub/tg/' + name2, tgname + '_fvcom.nc', False) data4 = loadnc( '/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1/tg/' + name4, tgname + '_fvcom.nc', False) data5 = loadnc( '/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1/tg/' + name5, tgname + '_fvcom.nc', False) obs = loadnc('/mnt/drive_1/obs/east/all', tgname + '.nc', False) tt1 = ttide.t_tide(data1['zeta'], lat=data1['lat'], dt=1) tt2 = ttide.t_tide(data2['zeta'], lat=data2['lat'], dt=1) tt4 = ttide.t_tide(data4['zeta'], lat=data4['lat'], dt=1) tt5 = ttide.t_tide(data5['zeta'], lat=data5['lat'], dt=1) t1, d1, d2 = interp_clean_common(data2['time'], data2['zeta'], obs['time'], obs['zeta'], 500, -500) time = np.arange(t1[0], t1[-1] + 1 / 24.0, 1 / 24.0) testz = ipt.interp1d(t1, d2, time) tt3 = ttide.t_tide(testz, lat=obs['lat'], dt=1) FFT1 = sp.fft(tt1['xres']) FFT2 = sp.fft(tt2['xres']) FFT3 = sp.fft(tt3['xres']) FFT4 = sp.fft(tt4['xres'])
from datetime import datetime import numpy as np from ttide import t_tide from scipy.interpolate import interp1d """ 功能:处理验潮站数据,并画图 """ # %% 数据准备 gauge_path = r'E:\gauge_data\h632a93.dat' # 376表示厦门,632表示坎门 gauge_data = ReadData(gauge_path, types='gauge') gauge_position = (121.169, 28.053) # 厦门地理坐标(118.04, 24.27),坎门的为(121.169, 28.053) # %% 调和分析 elev_anomaly = gauge_data.data.elev / 1000 - 3.87 # 3.281表示厦门的海平参考面,坎门的为3.87 xout = t_tide(elev_anomaly, stime=gauge_data.data.time[0], lat=gauge_position[1]) gauge_zeta = elev_anomaly - xout(gauge_data.data.time) # %% 筛选数据 select_time = pd.date_range('1993-09-28 00:00:00', '1993-10-12 00:00:00', freq='H') select_time_stamp = date2num(select_time) select_start_indx = np.where(gauge_data.data.time == select_time_stamp[0])[0][0] select_end_indx = np.where(gauge_data.data.time == select_time_stamp[-1])[0][0] select_gauge_zeta = gauge_zeta[select_start_indx:select_end_indx + 1] # %% 验潮站相对于高度计过境时间时的增水和前期最大增水) alti_pass_time = datetime(1993, 10, 8, 8, 32) alti_pass_date = alti_pass_time.strftime('%m-%dT%H:%M') alti_pass_stime = date2num(alti_pass_time) zeta_func = interp1d(select_time_stamp, select_gauge_zeta) gauge_zeta_alti = zeta_func(alti_pass_stime)
dist = np.sqrt((data['x'] - xloc)**2 + (data['y'] - yloc)**2) asort = np.argsort(dist) close = 0 #while np.sum(data['wet_nodes'][:,asort[close]])<len(data['time']): # close+=1 node = asort[close] if dist[node] > 10000: continue zetac = data['zeta'][starttime:endtime, node] timec = data['time'][starttime:endtime] print(np.diff(data['time'])[0] * 24) out = ttide.t_tide(zetac, stime=timec[0] - 4 / 24.0, dt=np.diff(data['time'])[0] * 24, synth=-1, out_style=None, lat=data['lat'][node]) idxw = np.array([], dtype=int) idx = np.array([], dtype=int) for j, name in enumerate(ttide.t_utils.fourpad(wlev[key]['name'])): tidx = np.argwhere(out['nameu'] == name) if len(tidx) > 0: idxw = np.append(idxw, j) idx = np.append(idx, tidx[0]) print('=' * 80) print(wlev[key]['snum']) print(wlev[key]['sname']) print(dist[node])
def Interp_AQD_ADCP(moornum): moorname = 'CF' + str(moornum) if moorname == 'CF8': aqdlist = hstack( (glob.glob(datadir + 'NOC_M1/nocm1_01_2014/nor/*.edt'), glob.glob(datadir + 'NOC_M1/nocm1_02_2015/nor/*.edt'))) else: aqdlist = glob.glob(datadir + 'AQD_Data_CF/OS_OSNAP-' + moorname + '*.nc') figure(figsize=(12, 10)) time = array([]) u = array([]) v = array([]) prs = array([]) meanprs = array([]) date = array([]) for dd in aqdlist: print(dd) if moorname == 'CF8': dat = pd.read_csv(dd, header=12, sep='\s*') prs_tmp = hrly_ave(dat.ix[:, 5], hrcon) date_tmp = unique([ datetime.datetime(int(dat.ix[ii, 0]), int(dat.ix[ii, 1]), int(dat.ix[ii, 2])) for ii in range(len(dat)) ])[:len(prs_tmp)] time_tmp = array( [datetime.datetime.toordinal(adate) for adate in date_tmp]) utest = array(dat.ix[:, 6] / 100) vtest = array(dat.ix[:, 7] / 100) tfit_u = tt.t_tide(utest) tfit_v = tt.t_tide(vtest) ufilt = utest - tfit_u(arange(len(utest))) vfilt = vtest - tfit_v(arange(len(vtest))) u_tmp = hrly_ave(ufilt, hrcon) v_tmp = hrly_ave(vfilt, hrcon) mprs_tmp = mean(dat.ix[:, 5]) * ones(len(date_tmp)) else: dat = Dataset(dd, 'r') utest = array( [float(tt) for tt in dat.variables['UCUR'][:].flatten()]) vtest = array( [float(tt) for tt in dat.variables['VCUR'][:].flatten()]) tfit_u = tt.t_tide(utest + 1j * vtest, dt=0.5) tidepart = tfit_u(arange(0, len(utest) / 2, 0.5)) ufilt = utest - tidepart.real vfilt = vtest - tidepart.imag u_tmp = hrly_ave(ufilt, hrcon * 2) v_tmp = hrly_ave(vfilt, hrcon * 2) prs_tmp = hrly_ave(list(dat.variables['PRES'][:].flatten()), hrcon * 2) time_tmp = list(dat.variables['TIME'][:])[::hrcon * 2][:len(prs_tmp)] mprs_tmp = nanmean(prs_tmp) * ones(len(time_tmp)) date_tmp = array([ datetime.datetime(1950, 1, 1) + datetime.timedelta(days=int(tt)) for tt in time_tmp ]) u_tmp = u_tmp[:len(date_tmp)] v_tmp = v_tmp[:len(date_tmp)] prs_tmp = prs_tmp[:len(date_tmp)] figure(figsize=(14, 6)) subplot(211) title(str(int(nanmean(prs_tmp)))) plot(utest) plot(tidepart.real) subplot(212) plot(vtest) plot(tidepart.imag) prs = hstack((prs, prs_tmp)) u = hstack((u, u_tmp)) v = hstack((v, v_tmp)) meanprs = hstack((meanprs, mprs_tmp)) date = hstack((date, date_tmp)) time = hstack((time, time_tmp)) ##### Choose time and pressure bins #units are db pstep = 2 ##### Now add ADCP data lat = 60 ua = array([]) va = array([]) prsa = array([]) meanprs = array([]) datea = array([]) if moorname == 'CF8': adcplist = hstack( (glob.glob(datadir + 'NOC_M1/nocm1_01_2014/adcp/*.edt'), glob.glob(datadir + 'NOC_M1/nocm1_02_2015/adcp/*.edt'))) for dd in adcplist: dat = pd.read_csv(dd, header=12, sep='\s*') prs_tmp = hrly_ave(gsw.p_from_z(-dat.ix[:, 4], lat), hrcon) datea_tmp = unique([ datetime.datetime(int(dat.ix[ii, 0]), int(dat.ix[ii, 1]), int(dat.ix[ii, 2])) for ii in range(len(dat)) ])[:len(prs_tmp)] utest = array(dat.ix[:, 6] / 100) vtest = array(dat.ix[:, 7] / 100) utest[utest < -90] = NaN vtest[vtest < -90] = NaN tfit_u = tt.t_tide(utest) tfit_v = tt.t_tide(vtest) ufilt = utest - tfit_u(arange(len(utest))) vfilt = vtest - tfit_v(arange(len(vtest))) ua_tmp = hrly_ave(ufilt, hrcon) va_tmp = hrly_ave(vfilt, hrcon) prsa = hstack((prsa, prs_tmp)) ua = hstack((ua, ua_tmp)) va = hstack((va, va_tmp)) datea = hstack((datea, datea_tmp)) plot(datea_tmp, ua_tmp) plot(datea_tmp, va_tmp) else: dat = io.loadmat(datadir + 'ADCP_Data_CF/OSNAP_cf' + str(moornum) + '_Final1_ilebras.mat') shapmat = shape(dat['u'][1:, :]) shapmat1 = int(shape(dat['u'])[1] / 24) + 1 datea_tmp = unique([ datetime.datetime(int(tt[0]), int(tt[1]), int(tt[2])) for tt in dat['time'] ])[:shapmat1] # Note that adcp "depths" from Dan Torres are also in db... prsa_tmp = zeros((shapmat[0], shapmat1)) ua_tmp = zeros((shapmat[0], shapmat1)) va_tmp = zeros((shapmat[0], shapmat1)) for ii in range(shapmat[0]): prsa_tmp[ii, :] = hrly_ave(dat['z'][ii + 1, :], hrcon) utest = array(dat['u'][ii + 1, :]) vtest = array(dat['v'][ii + 1, :]) # if sum(~isnan(utest))>len(utest)/2: # # print(utest) # tfit_u = tt.t_tide(utest+1j*vtest) # tidepart=tfit_u(arange(len(utest))) # ufilt=utest-tidepart.real # vfilt=vtest-tidepart.imag # figure(figsize=(14,6)) # subplot(211) # title(str(int(nanmean(prsa_tmp[ii,:])))) # plot(utest) # plot(tidepart.real) # subplot(212) # plot(vtest) # plot(tidepart.imag) # ua_tmp[ii,:]=hrly_ave(utest,hrcon) # va_tmp[ii,:]=hrly_ave(vtest,hrcon) # title(str(int(nanmean(vtest)*100)/100)+'/'+str(int(nanmean(va_tmp[ii,:])*100)/100)) # else: ua_tmp[ii, :] = hrly_ave(utest, hrcon) va_tmp[ii, :] = hrly_ave(vtest, hrcon) ua = ua_tmp.flatten() va = va_tmp.flatten() prsa = prsa_tmp.flatten() datea = tile(datea_tmp, [1, shapmat[0]]).flatten() pdall = pd.DataFrame({ 'pressure': hstack((prsa, prs)), 'u': hstack((ua, u)), 'v': hstack((va, v)), 'date bin': hstack((datea, date)) }) pdall['u'][pdall['u'] < -2] = NaN pdall['v'][pdall['v'] < -2] = NaN if moornum == 4: plim = 40 else: plim = 0 pdall = pdall[pdall['pressure'] >= plim] common_mindate = max(min(datea), min(date)) common_maxdate = min(max(datea), max(date)) # datevec=sort(unique(pdall['date bin'])) prsvec = arange(0, int(max(pdall['pressure'])) + 1, 2) dmin = min(pdall['date bin']) dmax = max(pdall['date bin']) dlen = int(divmod((dmax - dmin).total_seconds(), 60 * 60 * 24)[0]) + 1 datevec = array( [dmin + datetime.timedelta(days=x) for x in range(0, dlen)]) u_interp = fillsurf('u', datevec, prsvec, pdall) v_interp = fillsurf('v', datevec, prsvec, pdall) u_interp[(u_interp < -2.5) | (u_interp > 2)] = nan v_interp[(v_interp < -2.5) | (v_interp > 2)] = nan versname = '1808tidecomp' figure(figsize=(12, 4)) subplot(121) plot(u_interp, prsvec) plot(pdall['u'], pdall['pressure'], 'k.') gca().invert_yaxis() title('u, zonal velocity (m/s)') ylabel('pressure (db)') subplot(122) plot(v_interp, prsvec) plot(pdall['v'], pdall['pressure'], 'k.') gca().invert_yaxis() title('v, meridional velocity (m/s)') savefig('../figures/interpolation/VEL/' + moorname + '_uv_measinterp_' + versname + '.png', bbox_inches='tight') plotcontour(u_interp, cm.RdBu_r, -2, 2, moorname) savefig('../figures/interpolation/VEL/' + moorname + '_ucontour_' + versname + '.png', bbox_inches='tight') plotcontour(v_interp, cm.RdBu_r, -2, 2, moorname) savefig('../figures/interpolation/VEL/' + moorname + '_vcontour_' + versname + '.png', bbox_inches='tight') pickle.dump([u_interp, v_interp], open( '../pickles/VELinterp/' + moorname + '_uvinterp_' + versname + '.pickle', 'wb'))
nvmask.append(False) return asarray(nvmask) inum = len(nc.dimensions['nSCHISM_hgrid_node']) m2_amp = zeros((inum,)) m2_pha = zeros((inum,)) m4_amp = zeros((inum,)) m4_pha = zeros((inum,)) ut = netcdftime(ncv['time'].units) hours = ncv['time'][:]/3600. hours = hours - hours[0] dthours = hours[1]-hours[0] for i in range(inum): tides = tt.t_tide(ncv['elev'][:,i],dt=dthours,out_style=None) m2idx = tides['nameu'].index(b'M2 ') m4idx = tides['nameu'].index(b'M4 ') m2_amp[i] = tides['tidevcon'][m2idx][0] m4_pha[i] = tides['tidevcon'][m2idx][2] m4_amp[i] = tides['tidevcon'][m4idx][0] m2_pha[i] = tides['tidevcon'][m4idx][2] if False: figure() tripcolor(x,y,nv,m2_amp,cmap=cm.jet,rasterized=True) proj.drawcoastlines() proj.fillcontinents((0.9,0.9,0.8)) cb=colorbar() cb.ax.set_title('M2 [m]\n',size=10.) savefig('m2_amp.jpg'%(varname,varname,tidx),dpi=600)
tgzeta=tg65[idx,1]-np.nanmean(tg65[idx,1]) tgtime=tgtime[~np.isnan(tgzeta)] tgzeta=tgzeta[~np.isnan(tgzeta)] mtgzeta=ipt.interp1d(tgtime.flatten(),tgzeta.flatten(),time) n=55468 m2amp=np.empty((5000,)) m2amp_tg=np.empty((5000,)) m2amp_cur=np.empty((5000,)) for i in range(5000): print(i) out=ttide.t_tide(data['zeta'][starttime+i:endtime+i,n],stime=time[starttime+i],dt=np.diff(data['time'])[0]*24,synth=-1,out_style=None,lat=data['lat'][n])#,constitnames=np.array(['M2 '])) idx=np.argwhere(out['nameu']=='M2 ') amp=out['tidecon'][idx,0] m2amp[i]=amp out=ttide.t_tide(mtgzeta[starttime+i:endtime+i],stime=time[starttime+i],dt=np.diff(data['time'])[0]*24,synth=-1,out_style=None,lat=data['lat'][n])#,constitnames=np.array(['M2 '])) idx=np.argwhere(out['nameu']=='M2 ') amp=out['tidecon'][idx,0] m2amp_tg[i]=amp out=ttide.t_tide(data['ua'][starttime+i:endtime+i,n]+1j*data['va'][starttime+1:endtime+1,n],stime=time[starttime+i],dt=np.diff(data['time'])[0]*24,synth=-1,out_style=None,lat=data['lat'][n])#,constitnames=np.array(['M2 '])) idx=np.argwhere(out['nameu']=='M2 ') amp=out['tidecon'][idx,0] m2amp_cur[i]=amp
def __init__(self, filename, time1=datetime.datetime(2000, 1, 1, 00, 0), time2=datetime.datetime(2018, 10, 16, 5, 0)): self.data = {} self.year = {} self.month = {} self.day = {} for s in xlrd.open_workbook(filename=filename).sheet_names(): try: self.tide_sheet = pandas.read_excel(filename, sheetname=s) if 'tide' in self.tide_sheet.columns.values: if 'time' in self.tide_sheet.columns.values: pass else: say_out('Please Check Your Format of the Sheet') except: say_out('Please Check Your sheets name of the File') temp_data = self.tide_sheet t = [] if isinstance(temp_data.time[1], pandas.tslib.Timestamp): t = temp_data.time else: for x in temp_data.time: t.append(dateutil.parser.parse(x)) temp_data['format_time'] = t temp_data['tide_init'] = temp_data.tide / 100 if t[0] + (t[1] - t[0]) * (len(t) - 1) != t[len(t) - 1]: say_out(str(s) + '时间间隔有问题,请检查时间序列') deltatime = t[1] - t[0] num_time = np.linspace(0, len(t) * deltatime.total_seconds() / 3600, len(t)) self.tide_info = tt.t_tide(temp_data['tide_init'].values, dt=deltatime.total_seconds() / 3600) #temp_data.tide = self.tide_info(num_time) temp_data[ 'tide_init'] = temp_data.tide #后续调和导出、根据调和分析对数据进行去噪-需要处理### if len(temp_data) % 2 == 1: temp_data = temp_data.ix[0:(len(temp_data) - 2)] temp_data.tide = process(temp_data.tide) temp_data.index = temp_data['format_time'] temp_data = temp_data.ix[temp_data.format_time <= time2] temp_data = temp_data.ix[temp_data.format_time >= time1] temp_data['if_min'] = np.r_[ True, temp_data.tide.values[1:] < temp_data.tide. values[:-1]] & np.r_[ temp_data.tide.values[:-1] < temp_data.tide.values[1:], True] temp_data['if_max'] = np.r_[ True, temp_data.tide.values[1:] > temp_data.tide. values[:-1]] & np.r_[ temp_data.tide.values[:-1] > temp_data.tide.values[1:], True] temp_data.set_value(temp_data.index.max(), 'if_min', False) temp_data.set_value(temp_data.index.max(), 'if_max', False) temp_data.set_value(temp_data.index.min(), 'if_max', False) temp_data.set_value(temp_data.index.min(), 'if_min', False) temp_data['diff'] = None temp_data['raising_time'] = None temp_data['ebb_time'] = None for x in temp_data.ix[temp_data.if_max == True].format_time: try: temp_data.loc[x, 'raising_time'] = x - temp_data[ (temp_data.if_min == True) & (temp_data.format_time < x)].format_time.max() temp_data.loc[x, 'diff'] = temp_data.loc[ x, 'tide'] - temp_data.loc[temp_data[ (temp_data.if_min == True) & (temp_data.format_time < x)].format_time.max(), 'tide'] except: pass for x in temp_data.ix[temp_data.if_min == True].format_time: try: temp_data.loc[x, 'ebb_time'] = x - temp_data[ (temp_data.if_max == True) & (temp_data.format_time < x)].format_time.max() temp_data.loc[x, 'diff'] = temp_data.loc[temp_data[ (temp_data.if_max == True) & (temp_data.format_time < x)].format_time.max( ), 'tide'] - temp_data.loc[x, 'tide'] except: pass ###############筛选极值 #####根据潮差与涨落潮历时筛选 if temp_data['diff'].max() < 50: temp_data['diff'] = temp_data['diff'] * 100 temp_data.loc[temp_data[temp_data['diff'] < 10].index, 'diff'] = None temp_data.loc[temp_data[temp_data['diff'] < 10].index, 'if_max'] = False temp_data.loc[temp_data[temp_data['diff'] < 10].index, 'if_min'] = False temp_data.loc[temp_data[temp_data['diff'] < 10].index, 'raising_time'] = 0 temp_data.loc[temp_data[temp_data['diff'] < 10].index, 'ebb_time'] = 0 temp_data.loc[temp_data[ temp_data['raising_time'] < datetime.timedelta(hours=2)].index, 'diff'] = None temp_data.loc[temp_data[ temp_data['raising_time'] < datetime.timedelta(hours=2)].index, 'if_max'] = False temp_data.loc[temp_data[ temp_data['raising_time'] < datetime.timedelta(hours=2)].index, 'raising_time'] = 0 temp_data.loc[temp_data[temp_data['ebb_time'] < datetime.timedelta( hours=2)].index, 'diff'] = None temp_data.loc[temp_data[temp_data['ebb_time'] < datetime.timedelta( hours=2)].index, 'if_min'] = False temp_data.loc[temp_data[temp_data['ebb_time'] < datetime.timedelta( hours=2)].index, 'ebb_time'] = 0 ###############根据大小潮个数筛选 switch = temp_data.ix[temp_data.if_min == True].append( temp_data.ix[temp_data.if_max == True]).sort_index().index count_filtertime = 0 while (len(temp_data.ix[temp_data.if_min == True]) != len( temp_data.ix[temp_data.if_max == True])): print('低潮潮位个数' + str(len(temp_data.ix[temp_data.if_min == True]))) print('高潮潮位个数' + str(len(temp_data.ix[temp_data.if_max == True]))) if ((len(temp_data.ix[temp_data.if_min == True]) - len(temp_data.ix[temp_data.if_max == True]) == 1) or (len(temp_data.ix[temp_data.if_min == True]) - len(temp_data.ix[temp_data.if_max == True]) == -1)): print('极值只多一个,筛选结束') break print('进行高低潮筛选————————————————————————————————') for i in range(1, len(switch) - 1): ## 时刻i是最大值时 if (temp_data.loc[switch[i - 1], 'if_max'] and temp_data.loc[switch[i + 1], 'if_max']) or ( temp_data.loc[switch[i - 1], 'if_min'] and temp_data.loc[switch[i + 1], 'if_min']): pass else: if temp_data.loc[switch[i], 'if_max']: # 自身高潮 if temp_data.loc[switch[i - 1], 'if_max']: # 后一个也是高潮 if temp_data.loc[switch[i], 'tide'] > temp_data.loc[ switch[i - 1], 'tide']: # 前面的i大,i-1为假 temp_data.loc[switch[i - 1], 'if_max'] = False temp_data.loc[switch[i - 1], 'diff'] = None temp_data.loc[switch[i - 1], 'raising_time'] = None else: temp_data.loc[switch[i], 'if_max'] = False temp_data.loc[switch[i], 'diff'] = None temp_data.loc[switch[i], 'raising_time'] = None else: # 自身低潮 if temp_data.loc[switch[i - 1], 'if_min']: # 后一个也是低潮 if temp_data.loc[switch[i], 'tide'] < temp_data.loc[ switch[i - 1], 'tide']: # 前面的i较小,i-1为假 temp_data.loc[switch[i - 1], 'if_min'] = False temp_data.loc[switch[i - 1], 'diff'] = False temp_data.loc[switch[i - 1], 'ebb_time'] = False else: temp_data.loc[switch[i], 'if_min'] = False temp_data.loc[switch[i], 'diff'] = False temp_data.loc[switch[i], 'ebb_time'] = False switch = temp_data.ix[temp_data.if_min == True].append( temp_data.ix[temp_data.if_max == True]).sort_index().index count_filtertime += 1 year_tide = [] month_tide = [] day_tide = [] for i, j in temp_data.groupby(temp_data.index.year): # year_tide.append(j) for ii, jj in j.groupby(j.index.month): month_tide.append(jj) for iii, jjj in jj.groupby(jj.index.day): day_tide.append(jjj) self.data[s] = temp_data temp_data2 = temp_data temp_data2['tide'] = temp_data['tide_init'] self.data[s + '原始数据'] = temp_data2 year_tide2 = [] month_tide2 = [] day_tide2 = [] for i, j in temp_data2.groupby(temp_data.index.year): # year_tide2.append(j) for ii, jj in j.groupby(j.index.month): month_tide2.append(jj) for iii, jjj in jj.groupby(jj.index.day): day_tide2.append(jjj) self.year[s] = year_tide self.year[s + '原始数据'] = year_tide2 self.month[s] = month_tide self.month[s + '原始数据'] = month_tide2 self.day[s] = day_tide self.day[s + '原始数据'] = day_tide2 print(s + "站预处理结束")
for idx_lat in range(0, len(lats)): print('Indexes: ', idx_lon, idx_lat) # Read the ssh values ssh_mod = vals[:, idx_lat, idx_lon] # Extract Amp and Pha xin = np.array(ssh_mod) latitudes = lats[idx_lat] tidal_comp = ['M2', 'S2', 'K1', 'O1', 'N2', 'P1', 'Q1', 'K2'] # run ttide script harmanOUT = ttide.t_tide(np.array(xin), dt=1, stime=time, lat=latitudes, constitnames=tidal_comp, out_style=None, outfile=None) tideconout = harmanOUT['tidecon'] # Write values in the arrays M2_Amp[idx_lat, idx_lon] = tideconout[5][0] S2_Amp[idx_lat, idx_lon] = tideconout[6][0] K1_Amp[idx_lat, idx_lon] = tideconout[3][0] O1_Amp[idx_lat, idx_lon] = tideconout[1][0] N2_Amp[idx_lat, idx_lon] = tideconout[4][0] P1_Amp[idx_lat, idx_lon] = tideconout[2][0] Q1_Amp[idx_lat, idx_lon] = tideconout[0][0] K2_Amp[idx_lat, idx_lon] = tideconout[7][0]
tgzeta1=tg65[:,1] tgtime=tgtime[~np.isnan(tgzeta)] tgzeta=tgzeta[~np.isnan(tgzeta)] time=np.arange(tgtime.min(),tgtime.max(),60/(24.0*3600)) mtgzeta=ipt.interp1d(tgtime.flatten(),tgzeta.flatten(),time) num=len(range(0,len(time)-window,np.round(intv/3).astype(int)*24*60)) m2amp=np.empty((num,)) m2phs=np.empty((num,)) m2nans=np.empty((num,)) for j,i in enumerate(range(0,len(time)-window,np.round(intv/3).astype(int)*24*60)): out=ttide.t_tide(mtgzeta[starttime+i:window+i],stime=time[starttime+i],dt=np.diff(time)[0]*24,synth=-1,out_style=None) idx=np.argwhere(out['nameu']=='M2 ') m2amp[j]=out['tidecon'][idx,0] m2phs[j]=out['tidecon'][idx,2] m2nans[j]=np.isnan(tgzeta1[starttime+i:window+i]).sum() f=plt.figure() ax=f.add_axes([.125,.1,.775,.8]) ax.plot(m2amp,'r',lw=2,label='M2 Amp.') #ax.set_xlabel('Time (hours)') ax.set_ylabel('M2 Amp. (m)') f.suptitle('Min: {} Max: {} Range: {}\n Mean: {} STD: {} '.format(m2amp.min(),m2amp.max(),m2amp.max()-m2amp.min(),m2amp.mean(),np.sqrt(np.var(m2amp)))) f.savefig('{}m2amp_{}.png'.format(savepath,intv),dpi=300)
for i,key in enumerate(wlev): xloc,yloc = data['proj'](wlev[key]['lon'],wlev[key]['lat']) dist=np.sqrt((data['x']-xloc)**2+(data['y']-yloc)**2) asort=np.argsort(dist) close=0 #while np.sum(data['wet_nodes'][:,asort[close]])<len(data['time']): # close+=1 node=asort[close] if dist[node]>10000: continue zetac=data['zeta'][starttime:endtime,node] timec=data['time'][starttime:endtime] print(np.diff(data['time'])[0]*24) out=ttide.t_tide(zetac,stime=timec[0]-4/24.0,dt=np.diff(data['time'])[0]*24,synth=-1,out_style=None,lat=data['lat'][node]) idxw=np.array([],dtype=int) idx=np.array([],dtype=int) for j,name in enumerate(ttide.t_utils.fourpad(wlev[key]['name'])): tidx=np.argwhere(out['nameu']==name) if len(tidx)>0: idxw=np.append(idxw,j) idx=np.append(idx,tidx[0]) print('='*80) print(wlev[key]['snum']) print(wlev[key]['sname']) print(dist[node])
num = len( range(0, len(time) - window, np.round(intv / 3).astype(int) * 24 * 60)) m2amp = np.empty((num, )) m2phs = np.empty((num, )) m2nans = np.empty((num, )) for j, i in enumerate( range(0, len(time) - window, np.round(intv / 3).astype(int) * 24 * 60)): out = ttide.t_tide(mtgzeta[starttime + i:window + i], stime=time[starttime + i], dt=np.diff(time)[0] * 24, synth=-1, out_style=None) idx = np.argwhere(out['nameu'] == 'M2 ') m2amp[j] = out['tidecon'][idx, 0] m2phs[j] = out['tidecon'][idx, 2] m2nans[j] = np.isnan(tgzeta1[starttime + i:window + i]).sum() f = plt.figure() ax = f.add_axes([.125, .1, .775, .8]) ax.plot(m2amp, 'r', lw=2, label='M2 Amp.') #ax.set_xlabel('Time (hours)') ax.set_ylabel('M2 Amp. (m)') f.suptitle('Min: {} Max: {} Range: {}\n Mean: {} STD: {} '.format( m2amp.min(), m2amp.max(), m2amp.max() - m2amp.min(), m2amp.mean(), np.sqrt(np.var(m2amp))))
# %% 模式数据处理 model_tide_data = ReadData(model_tide_path, types='fvcom', variables=['zeta']) model_wind_data = ReadData(model_wind_path, types='fvcom', variables=['zeta']) model_zeta = model_wind_data.data.zeta - model_tide_data.data.zeta model_start_time = model_tide_data.data.time[0] model_end_time = model_tide_data.data.time[-1] gauge_indx = model_tide_data.find_nearest_point('zeta', position_site) # 减去模式的平均值(减去的平均值是模式结果的整体平均,非个点) specific_mzeta = model_zeta[:, gauge_indx] - model_zeta.mean() # %% 验潮站数据处理 gauge_data = ReadData(gauge_path, types='gauge') # 做调和分析 elev_anomaly = gauge_data.data.elev / 1000 - 3.87 # 3.281表示厦门的海平参考面,坎门的为3.87 xout = ttide.t_tide(elev_anomaly, stime=gauge_data.data.time[0], lat=position_site[1]) gauge_zeta = elev_anomaly - xout(gauge_data.data.time) # 计算模式时间范围的平均值(为了更好的和模式比较) start_indx = np.where(gauge_data.data.time == model_start_time)[0][0] end_indx = np.where(gauge_data.data.time == model_end_time)[0][0] gauge_mean_zeta = np.mean(gauge_zeta[start_indx:end_indx + 1]) # %% 选择研究的时间范围 select_time = pd.date_range('1993-09-28 00:00:00', '1993-10-12 00:00:00', freq='H') select_time_stamp = date2num(select_time) # 选择时间范围的验潮站数据(减去平均) select_start_indx = np.where( gauge_data.data.time == select_time_stamp[0])[0][0]
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- # __NAME__ = compare_tidal_constituents.py # __AUTHOR__ = 'QIU ZHOU' # __TIME__ = 2019/7/16 20:44 """ 比较模式和验潮站的调和常数 """ from fvcom_tools_packages.read_data import ReadData from ttide import t_tide # %% 数据准备 gauge_path = r'E:\gauge_data\h376a93.dat' # 376表示厦门,632表示坎门 gauge_data = ReadData(gauge_path, types='gauge') gauge_position = (118.04, 24.27) # 厦门地理坐标(118.04, 24.27),坎门的为(121.169, 28.053) model_path = r'E:\fvcom\fvcom_output_file\AtideIB_0001.nc' model_data = ReadData(model_path, types='fvcom', variables=['zeta']) indx = model_data.find_nearest_point('zeta', gauge_position) # %% 调和分析 # 验潮站 elev_anomaly = gauge_data.data.elev / 1000 - 3.281 # 3.281表示厦门的海平参考面,坎门的为3.87 gauge_xout = t_tide(elev_anomaly, stime=gauge_data.data.time[0], lat=gauge_position[1]) # 模式 xiamen_model_elev = model_data.data.zeta[:, indx].flatten() fvcom_xout = t_tide(xiamen_model_elev, stime=model_data.data.time[0], lat=gauge_position[1])
### load the .nc file ##### data = loadnc('/home/mif001/scratch/sjh_hr_v3/test_bfric2/{}/output/'.format(name),singlename=grid + '_0001.nc') print('done load') savepath='{}/{}_{}/ttide/{}/'.format(datapath,grid,datatype,name) if not os.path.exists(savepath): os.makedirs(savepath) dt=np.diff(data['time'])[0]*24 out=pymp.shared.array((len(data['nodell'][:,0]),35,4)) with pymp.Parallel(48) as p: for j in p.range(len(data['nodell'][:,0])): print(j) out[j,]=t_tide(data['zeta'][starttime:endtime,j],stime=data['time'][starttime],lat=data['lat'][j],synth=-1,out_style=None,dt=.25)['tidecon'] outall=t_tide(data['zeta'][starttime:endtime,j],stime=data['time'][starttime],lat=data['lat'][j],synth=-1,out_style=None,dt=.25) np.save('{}{}_{}_{}_ttide_grid_tidecon_el_all_pymp.npy'.format(savepath,name,starttime,endtime),out) np.save('{}{}_{}_{}_ttide_grid_singleout_el_all_pymp.npy'.format(savepath,name,starttime,endtime),outall)
tgname='tg_00365' name1='sjh_lr_v1_sub_fit_N4_test_nest_bfric02_taup' name2='test_1_redo' name4='sjh_lr_v1_year_wd_gotm-my25_bathy20171109_dt30_calib1_jcool0' name5='sjh_lr_v1_year_coare3_wu_mixing' data1=loadnc('/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1_sub/tg/'+name1,tgname+'_fvcom.nc',False) data2=loadnc('/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1_sub/tg/'+name2,tgname+'_fvcom.nc',False) data4=loadnc('/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1/tg/'+name4,tgname+'_fvcom.nc',False) data5=loadnc('/home/moflaher/Desktop/workspace_python/dataout/sjh_lr_v1/tg/'+name5,tgname+'_fvcom.nc',False) obs=loadnc('/mnt/drive_1/obs/east/all',tgname+'.nc',False) tt1=ttide.t_tide(data1['zeta'],lat=data1['lat'],dt=1) tt2=ttide.t_tide(data2['zeta'],lat=data2['lat'],dt=1) tt4=ttide.t_tide(data4['zeta'],lat=data4['lat'],dt=1) tt5=ttide.t_tide(data5['zeta'],lat=data5['lat'],dt=1) t1,d1,d2=interp_clean_common(data2['time'],data2['zeta'],obs['time'],obs['zeta'],500,-500) time=np.arange(t1[0],t1[-1]+1/24.0,1/24.0) testz=ipt.interp1d(t1,d2,time) tt3=ttide.t_tide(testz,lat=obs['lat'],dt=1) FFT1=sp.fft(tt1['xres']) FFT2=sp.fft(tt2['xres']) FFT3=sp.fft(tt3['xres']) FFT4=sp.fft(tt4['xres'])
tgtime = tgtime[~np.isnan(tgzeta)] tgzeta = tgzeta[~np.isnan(tgzeta)] mtgzeta = ipt.interp1d(tgtime.flatten(), tgzeta.flatten(), time) n = 55468 m2amp = np.empty((5000, )) m2amp_tg = np.empty((5000, )) m2amp_cur = np.empty((5000, )) for i in range(5000): print(i) out = ttide.t_tide(data['zeta'][starttime + i:endtime + i, n], stime=time[starttime + i], dt=np.diff(data['time'])[0] * 24, synth=-1, out_style=None, lat=data['lat'][n]) #,constitnames=np.array(['M2 '])) idx = np.argwhere(out['nameu'] == 'M2 ') amp = out['tidecon'][idx, 0] m2amp[i] = amp out = ttide.t_tide(mtgzeta[starttime + i:endtime + i], stime=time[starttime + i], dt=np.diff(data['time'])[0] * 24, synth=-1, out_style=None, lat=data['lat'][n]) #,constitnames=np.array(['M2 '])) idx = np.argwhere(out['nameu'] == 'M2 ') amp = out['tidecon'][idx, 0] m2amp_tg[i] = amp
ax.plot(time1,data2-np.mean(data2),'r',lw=.5,label='{}'.format(name)) ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.legend() ax.set_ylabel('Elevation (m)') f.suptitle('Removed TG means - Obs: {} Model: {}\n Bias: {} Std: {} RMSE: {} RAE: {} Corr: {} Skew: {} Skill: {}'.format(np.mean(data1),np.mean(data2),a[0],a[1],a[2],a[3],a[4],a[5],a[6])) f.savefig(figstr,dpi=600) if tide: time=np.arange(time1[0],time1[-1]+1/24.0,1/24.0) tgm_int=ipt.interp1d(tgm['time'],tgm['zeta'],time) tgonan=tgo['zeta'][idx] tgonan[tgonan>500]=np.nan tgo_int=ipt.interp1d(tgo['time'][idx],tgonan,time) tgm_tcon_pre=ttide.t_tide(tgm_int,stime=time[0],lat=tgm['lat'],dt=(time[1]-time[0])*24.0,out_style=None) tgo_tcon_pre=ttide.t_tide(tgo_int,stime=time[0],lat=tgm['lat'],dt=(time[1]-time[0])*24.0,out_style=None) tgm_tcon=ttide.t_tide(tgm_int,stime=time[0],lat=tgm['lat'],dt=(time[1]-time[0])*24.0,constitnames=tgm_tcon_pre['nameu'][tgm_tcon_pre['snr']>=args.snr],out_style=None) tgo_tcon=ttide.t_tide(tgo_int,stime=time[0],lat=tgm['lat'],dt=(time[1]-time[0])*24.0,constitnames=tgo_tcon_pre['nameu'][tgo_tcon_pre['snr']>=args.snr],out_style=None) f=plt.figure(figsize=(15,5)); ax=f.add_axes([.125,.1,.775,.8]); ax.plot(time[:len(tgo_tcon['xres'])],tgo_tcon['xres']-np.nanmean(tgo_tcon['xres']),'k',label='TG: {:05d}'.format(tgm['tgnumber'][0])) ax.plot(time[:len(tgm_tcon['xres'])],tgm_tcon['xres']-np.nanmean(tgm_tcon['xres']),'r',lw=.5,label='{}'.format(name)) ax.xaxis.set_major_locator(months) ax.xaxis.set_major_formatter(monthsFmt) ax.legend() ax.set_ylabel('Residual Elevation (m)') o,m=remove_common_nan(tgo_tcon['xres']-np.nanmean(tgo_tcon['xres']), tgm_tcon['xres']-np.nanmean(tgm_tcon['xres'])) stats=residual_stats(o,m)
def CalculateM2M4(self, slf, XY, id_wl, id_u, ip_start, ip_end, tf_start, tf_end, pp_dir): # calculate M2 and M4 along the channel using ttide_py M2outfileName = pp_dir + 'M2_along_thalweg.out' M4outfileName = pp_dir + 'M4_along_thalweg.out' # the computation of the M2 and M4 components of the Telemac data might be expensive. # as a result, it is first checked if the M2 and M4 components are not already computed and saved to an outputfile # if this is the case, the M2 and M4 components are obtained from the output file instead of being computed if (os.path.isfile(M2outfileName)): M2outputfile = open(M2outfileName, 'r') M2lineNb = M2outputfile.readline().split('xlength=')[1] M2lineNb = int(M2lineNb.replace(']', '')) M2outputfile.close() if (os.path.isfile(M4outfileName)): M4outputfile = open(M4outfileName, 'r') M4lineNb = M4outputfile.readline().split('xlength=')[1] M4lineNb = int(M4lineNb.replace(']', '')) M4outputfile.close() if (os.path.isfile(M2outfileName)) and ( M2lineNb == ip_end - ip_start + 1) and ( os.path.isfile(M4outfileName)) and (M4lineNb == ip_end - ip_start + 1): print('all outputfiles exist and have the correct number of lines') # here there might be some issues between python 3 and python 2 with open(M2outfileName, 'r') as M2outputfile: M2outputfile.readline() dataMatrix = list(csv.reader(M2outputfile)) dataMatrix = np.array(dataMatrix) dataMatrix = dataMatrix.astype(np.float) #XY=dataMatrix[:, 0:2] M2_thalweg_WL = dataMatrix[:, 2:6] M2_thalweg_U = dataMatrix[:, 6:10] with open(M4outfileName, 'r') as M4outputfile: M4outputfile.readline() dataMatrix = list(csv.reader(M4outputfile)) dataMatrix = np.array(dataMatrix) dataMatrix = dataMatrix.astype(np.float) #XY=dataMatrix[:, 0:2] M4_thalweg_WL = dataMatrix[:, 2:6] M4_thalweg_U = dataMatrix[:, 6:10] # M2outputfile.close() # M4outputfile.close() elif not (os.path.isfile(M2outfileName) or os.path.isfile(M4outfileName)): print( 'not all outputfile exist or have the correct number of lines, M2 and M4 are computed' ) M2_thalweg_WL = np.zeros([ip_end - ip_start + 1, 4]) M4_thalweg_WL = np.zeros([ip_end - ip_start + 1, 4]) M2_thalweg_U = np.zeros([ip_end - ip_start + 1, 4]) M4_thalweg_U = np.zeros([ip_end - ip_start + 1, 4]) # python starts from ip_start-1 to ip_end-1 for iPoint in range(ip_start - 1, ip_end): i = iPoint - (ip_start - 1) #i = iPoint - (ip_start - 1)+1 print('Processing the node', str(i)) # read time series at the desired node slf.readVariablesAtNode(iPoint) timeseries = slf.getVarValuesAtNode() # harmonic analysis using t-tide with results every 10 min tideconout_WL = tt.t_tide( timeseries[:, id_wl][tf_start:tf_end], dt=1.0 / 6.0, constitnames=['M2', 'M4', 'M6', 'M8'], out_style=None) # id_wl for water level tideconout_U = tt.t_tide( timeseries[:, id_u][tf_start:tf_end], dt=1.0 / 6.0, constitnames=['M2', 'M4', 'M6', 'M8'], out_style=None ) # id_u for depth-averaged horizontal velocity # get M2 and M4 from WL id_M2_WL = list(tideconout_WL["nameu"]).index( b'M2 ') # look for M2 constituent id_M4_WL = list(tideconout_WL["nameu"]).index( b'M4 ') # look for M4 constituent M2_thalweg_WL[i, :] = tideconout_WL["tidecon"][ id_M2_WL] # [amp err_amp phase err_phase] M4_thalweg_WL[i, :] = tideconout_WL["tidecon"][ id_M4_WL] # [amp err_amp phase err_phase] # get M2 and M4 from U id_M2_U = list(tideconout_U["nameu"]).index( b'M2 ') # look for M2 constituent id_M4_U = list(tideconout_U["nameu"]).index( b'M4 ') # look for M4 constituent M2_thalweg_U[i, :] = tideconout_U["tidecon"][ id_M2_U] # [amp err_amp phase err_phase] M4_thalweg_U[i, :] = tideconout_U["tidecon"][ id_M4_U] # [amp err_amp phase err_phase] # save the data to text files M2_out = np.concatenate( (XY[ip_start - 1:ip_end, :], M2_thalweg_WL, M2_thalweg_U), axis=1) M4_out = np.concatenate( (XY[ip_start - 1:ip_end, :], M4_thalweg_WL, M4_thalweg_U), axis=1) if not os.path.exists(pp_dir): os.mkdir(pp_dir) np.savetxt( M2outfileName, M2_out, delimiter=',', header= '[X,Y,WL_amp,WL_err_amp,WL_phase,WL_err_phase,U_amp,U_err_amp,U_phase,U_err_phase,xlength=' + str(ip_end - ip_start + 1) + ']') np.savetxt( M4outfileName, M4_out, delimiter=',', header= '[X,Y,WL_amp,WL_err_amp,WL_phase,WL_err_phase,U_amp,U_err_amp,U_phase,U_err_phase,xlength=' + str(ip_end - ip_start + 1) + ']') return (M2_thalweg_WL, M4_thalweg_WL, M2_thalweg_U, M4_thalweg_U)
workpath + stations_mod[stn] + '_mod_Tides8_v31.nc', 'r') xin = fT1_mod.variables['sossheig'][:, 0, 0] * 100.0 # want cm not meters latitudes = fT1_mod.variables['lat'][0] # Set the time if stations_mod[stn] == 'zygi1TG' or stations_mod[ stn] == 'iskeTG' or stations_mod[stn] == 'GinostraTG': time = datetime(2018, 7, 1, 0, 30, 0) else: time = datetime(2017, 7, 1, 0, 30, 0) print('Extracting.. STZ LAT TIME', stations_mod[stn], latitudes, time) # Extract Amp and Pha harmanOUT = ttide.t_tide(np.array(xin), dt=1., stime=time, lat=latitudes, constitnames=tidal_comp, out_style='classic', outfile=workpath + stations_mod[stn] + '_mod_tt.txt') # EMODNET OBS if flag_modorobs == 1: # Loop on TG for stn in range(0, len(stations_obs)): # Open file and get values fT1_obs = NC.Dataset(workpath + stations_obs[stn] + 'TG_obs.nc_h.nc', 'r') xin = fT1_obs.variables["sossheig"][:, 0] * 100.0 # want cm not meters # Read latitude from model file fT1_mod = NC.Dataset( workpath + stations_obs[stn] + 'TG_mod_Tides8_v31.nc', 'r')