def plot_ship(i): f=plt.figure() ax=f.add_axes([.125,.1,.75,.8]) prettyplot_ll(ax,setregion=region) #plotcoast(ax,filename='pacific_harbour.nc',color='None',fcolor='darkgreen',fill=True) ax.imshow(geotiff, cmap=cmap,vmax=13, extent=extent) ax.plot(locs[:,0],locs[:,1],'r',lw=2) qax=ax.quiver(locs[:,0],locs[:,1],sua,sva,angles='xy',scale_units='xy',scale=100,width=0.003) qax.set_zorder(20) qaxk=ax.quiverkey(qax,.9,.85,0.5, r'0.5 ms') ax.plot(locs[i,0],locs[i,1],'m*',markersize=15) time=times[i] lidx=np.argwhere(mtimes<=time).max() uidx=np.argwhere(mtimes>time).min() for j,idx in enumerate(vidx): lua=ipt.interpEfield_locs(data,'ua',data['uvnodell'][idx,:],lidx,ll=True) lva=ipt.interpEfield_locs(data,'va',data['uvnodell'][idx,:],lidx,ll=True) uua=ipt.interpEfield_locs(data,'ua',data['uvnodell'][idx,:],uidx,ll=True) uva=ipt.interpEfield_locs(data,'va',data['uvnodell'][idx,:],uidx,ll=True) u1 = interp1d(mtimes[[lidx,uidx]], np.array([lua,uua]).flatten()) u_vec[j] = u1(time) v1 = interp1d(mtimes[[lidx,uidx]], np.array([lva,uva]).flatten()) v_vec[j] = v1(time) s_vec=speeder(u_vec,v_vec) scale=((region['region'][1]-region['region'][0])*.0475)/s_vec.max() for idx,u,v in zip(vidx,u_vec,v_vec): x,y=data['uvnodell'][idx,:] dx=scale*u dy=scale*v al=np.sqrt(u**2+v**2) sl=np.sqrt(dx**2+dy**2) cidx=np.argwhere(al>=srange) if len(cidx) > 0: cidx=np.max(cidx) fcolor=crange[cidx] if al>=srange[0]: a=pp.FancyArrow(x,y,dx,dy,width_base=sl*.1,width_top=sl*.2, head_length=sl*.35,head_width=sl*.4, length_includes_head=True, edgecolor='None', facecolor=fcolor) ax.add_artist(a) for label in ax.get_xticklabels()[::2]: label.set_visible(False) f.savefig(savepath + grid + '_shippath_'+"{}".format(i+stime)+'.png',dpi=300) plt.close(f)
print(LON.shape) #proj=gridproj(grid) _, _, proj = lcc(data['lon'], data['lat']) x, y = proj(LON, LAT) length = len(range(starttime, endtime, stride)) ua = np.empty((length, len(x))) va = np.empty((length, len(x))) for i, timein in enumerate(range(starttime, endtime, stride)): print(i) print(timein) ua[i, :] = ipt.interpEfield_locs(data, 'ua', np.array([x, y]).T, timein) va[i, :] = ipt.interpEfield_locs(data, 'va', np.array([x, y]).T, timein) hosts = data['trigrid_finder'].__call__(LON, LAT) host = hosts == -1 host = host.astype(bool) i = 0 u = np.ma.masked_array(ua[i, :], host) v = np.ma.masked_array(va[i, :], host) f = plt.figure() ax = plt.subplot(111) ax.pcolormesh(lon, lat, speeder(u, v).reshape(lon.shape)) f.show()
proj = gridproj(grid) if forward: dt = np.diff(data['time'])[0] * 24 * 60 * 60 direction = 1 else: dt = -np.diff(data['time'])[0] * 24 * 60 * 60 runtime = -1 * runtime direction = -1 x = np.array([val[0] for val in vec]) y = np.array([val[1] for val in vec]) locs[:, 0, 0], locs[:, 0, 1] = proj(x, y) for i, step in enumerate(range(starttime, starttime + runtime, direction)): ua = ipt.interpEfield_locs(data, 'ua', locs[:, i, :], step) va = ipt.interpEfield_locs(data, 'va', locs[:, i, :], step) locs[:, i + 1, 0] = locs[:, i, 0] + (ua * dt) locs[:, i + 1, 1] = locs[:, i, 1] + (va * dt) idx = np.where((locs[:, :, 0] == 0) & (locs[:, :, 1] == 0)) locs[idx] = np.nan f = plt.figure() ax = f.add_axes([.125, .1, .775, .8]) clims = np.percentile(data['h'][nidx], [1, 99]) trip = ax.tripcolor(data['trigridxy'], data['h'], vmin=clims[0], vmax=clims[1]) #prettyplot_ll(ax,setregion=region,cb=trip,cblabel='Depth (m)',grid=True) #plotcoast(ax,color='k',fill=True) #if np.shape(cages)!=(): # lseg_t=LC(tmparray,linewidths = lw,linestyles=ls,color=color)
np.random.seed(10) sspeed=np.round(np.random.rand(len(locs)-1)*4)+1 times=np.append(data['time'][stime],np.cumsum(np.divide(d,sspeed)/(24*60*60))+data['time'][stime]) idx=np.argwhere(((data['time']<=times.max()) & (data['time']>=times.min()))).ravel() if idx.min()!=0: idx=np.append(idx[0]-1,idx) idx=np.append(idx,idx[-1]+1) mtimes=data['time'][idx] sua=np.empty((len(locs),)) sva=np.empty((len(locs),)) for i,time in enumerate(times): lidx=np.argwhere(mtimes<=time).max() lua=ipt.interpEfield_locs(data,'ua',locs[i,:],lidx,ll=True) lva=ipt.interpEfield_locs(data,'va',locs[i,:],lidx,ll=True) uidx=np.argwhere(mtimes>time).min() uua=ipt.interpEfield_locs(data,'ua',locs[i,:],uidx,ll=True) uva=ipt.interpEfield_locs(data,'va',locs[i,:],uidx,ll=True) u1 = interp1d(mtimes[[lidx,uidx]], np.array([lua,uua]).flatten()) sua[i] = u1(time) v1 = interp1d(mtimes[[lidx,uidx]], np.array([lva,uva]).flatten()) sva[i] = v1(time) qax=ax.quiver(locs[:,0],locs[:,1],sua,sva,angles='xy',scale_units='xy',scale=50,width=0.003) qax.set_zorder(20) qaxk=ax.quiverkey(qax,.125,.85,0.5, r'0.5 ms')
proj=gridproj(grid) if forward: dt=np.diff(data['time'])[0]*24*60*60 direction=1 else: dt=-np.diff(data['time'])[0]*24*60*60 runtime=-1*runtime direction=-1 x=np.array([val[0] for val in vec]) y=np.array([val[1] for val in vec]) locs[:,0,0],locs[:,0,1]=proj(x,y) for i,step in enumerate(range(starttime,starttime+runtime,direction)): ua=ipt.interpEfield_locs(data,'ua',locs[:,i,:],step) va=ipt.interpEfield_locs(data,'va',locs[:,i,:],step) locs[:,i+1,0]=locs[:,i,0]+(ua*dt) locs[:,i+1,1]=locs[:,i,1]+(va*dt) idx=np.where((locs[:,:,0]==0)&(locs[:,:,1]==0)) locs[idx]=np.nan f=plt.figure() ax=f.add_axes([.125,.1,.775,.8]) clims=np.percentile(data['h'][nidx],[1,99]) trip=ax.tripcolor(data['trigridxy'],data['h'],vmin=clims[0],vmax=clims[1]) #prettyplot_ll(ax,setregion=region,cb=trip,cblabel='Depth (m)',grid=True) #plotcoast(ax,color='k',fill=True) #if np.shape(cages)!=(): # lseg_t=LC(tmparray,linewidths = lw,linestyles=ls,color=color)
def plot_ship(i): f = plt.figure() ax = f.add_axes([.125, .1, .45, .8]) #prettyplot_ll(ax,setregion=region) #plotcoast(ax,filename='pacific_harbour.nc',color='None',fcolor='darkgreen',fill=True) ax.imshow(geotiff, cmap=cmap, vmax=13, extent=extent) ax.plot(locs[:, 0], locs[:, 1], 'r', lw=2) qax = ax.quiver(locs[:, 0], locs[:, 1], csua * cv, csva * cv, angles='xy', scale_units='xy', scale=100, width=0.003) qax.set_zorder(20) if useKnots: qaxk = ax.quiverkey(qax, .9, .85, 0.5, r'1 knot') else: qaxk = ax.quiverkey(qax, .9, .85, 0.5, r'0.5 ms$^{-1}$') ax.plot(locs[i, 0], locs[i, 1], 'm*', markersize=15) time = times[i] lidx = np.argwhere(mtimes <= time).max() uidx = np.argwhere(mtimes > time).min() for j, idx in enumerate(vidx): lua = ipt.interpEfield_locs(data, 'ua', data['uvnodell'][idx, :], lidx, ll=True) lva = ipt.interpEfield_locs(data, 'va', data['uvnodell'][idx, :], lidx, ll=True) uua = ipt.interpEfield_locs(data, 'ua', data['uvnodell'][idx, :], uidx, ll=True) uva = ipt.interpEfield_locs(data, 'va', data['uvnodell'][idx, :], uidx, ll=True) u1 = interp1d(mtimes[[lidx, uidx]], np.array([lua, uua]).flatten()) u_vec[j] = u1(time) * cv v1 = interp1d(mtimes[[lidx, uidx]], np.array([lva, uva]).flatten()) v_vec[j] = v1(time) * cv s_vec = speeder(u_vec, v_vec) scale = ((region['region'][1] - region['region'][0]) * .0475) / s_vec.max() for idx, u, v in zip(vidx, u_vec, v_vec): x, y = data['uvnodell'][idx, :] dx = scale * u dy = scale * v al = np.sqrt(u**2 + v**2) sl = np.sqrt(dx**2 + dy**2) cidx = np.argwhere(al >= srange) if len(cidx) > 0: cidx = np.max(cidx) fcolor = crange[cidx] if al >= srange[0]: a = pp.FancyArrow(x, y, dx, dy, width_base=sl * .1, width_top=sl * .2, head_length=sl * .35, head_width=sl * .4, length_includes_head=True, edgecolor='None', facecolor=fcolor) ax.add_artist(a) for label in ax.get_xticklabels()[::2]: label.set_visible(False) prettyplot_ll(ax, setregion=region) #plt.draw() #a=ax.get_axes().get_position() #print(a) #print(ax.get_aspect()) #figW, figH = f.get_size_inches() #fa = figH / figW #print(fa) #ax.axis(region['region']) space = .01 plots = 3 axsub = np.empty((plots, ), dtype=object) height = (.450898 - ((plots - 1) * space)) / 3 for j in range(plots): #print([.575+space,.1+(height+space)*i,.325,height]) axsub[j] = f.add_axes([ .575 + space / ax.get_aspect(), .1 + (height + space) * j, .315, height ]) axsub[2].plot(ctime * 24 - ctime.min() * 24, speeder(csua, csva) * cv, 'k') axsub[1].quiver(ctime * 24 - ctime.min() * 24, np.zeros((len(ctime), )), csua, csva, scale=3, width=.0025) axsub[0].quiver(ctime2[:-1] * 24 - ctime2[:-1].min() * 24, np.zeros((len(ctime2[:-1]), )), cdxs, cdys, scale=6, width=.0025) for label in axsub[1].get_xticklabels(): label.set_visible(False) for label in axsub[2].get_xticklabels(): label.set_visible(False) axsub[1].xaxis.set_tick_params(labelbottom='off') axsub[2].xaxis.set_tick_params(labelbottom='off') if useKnots: axsub[2].set_ylabel(r'Speed (knot)', fontsize=8) else: axsub[2].set_ylabel(r'Speed (ms$^{-1}$)', fontsize=8) axsub[1].set_ylabel(r'Cur. Dir.', fontsize=8) axsub[0].set_ylabel(r'Ship Dir.', fontsize=8) axsub[0].set_xlabel('Time (h)') if useKnots: axsub[2].set_ylim([0, 2]) else: axsub[2].set_ylim([0, 1]) axsub[1].set_ylim([-np.pi, np.pi]) for axin in axsub: axin.yaxis.set_tick_params(labelleft='off') axin.yaxis.set_label_position('right') axsub[2].yaxis.set_tick_params(labelright='on') for axin in axsub: axin.axvline(times[i] * 24 - times.min() * 24, color='r') f.savefig(savepath + grid + '_shippath_' + "{:06.6f}".format(time) + '.png', dpi=300) plt.close(f)
times = np.append( data['time'][stime], np.cumsum(np.divide(d, sspeed) / (24 * 60 * 60)) + data['time'][stime]) idx = np.argwhere( ((data['time'] <= times.max()) & (data['time'] >= times.min()))).ravel() if idx.min() != 0: idx = np.append(idx[0] - 1, idx) idx = np.append(idx, idx[-1] + 1) mtimes = data['time'][idx] sua = np.empty((len(locs), )) sva = np.empty((len(locs), )) for i, time in enumerate(times): lidx = np.argwhere(mtimes <= time).max() lua = ipt.interpEfield_locs(data, 'ua', locs[i, :], lidx, ll=True) lva = ipt.interpEfield_locs(data, 'va', locs[i, :], lidx, ll=True) uidx = np.argwhere(mtimes > time).min() uua = ipt.interpEfield_locs(data, 'ua', locs[i, :], uidx, ll=True) uva = ipt.interpEfield_locs(data, 'va', locs[i, :], uidx, ll=True) #u1 = interp1d(mtimes[[lidx,uidx]], np.array([lua,uua]).flatten()) #sua[i] = u1(time) #v1 = interp1d(mtimes[[lidx,uidx]], np.array([lva,uva]).flatten()) #sva[i] = v1(time) sua[i] = ipt.interp1d(mtimes[[lidx, uidx]], np.array([lua, uua]).flatten(), time) sva[i] = ipt.interp1d(mtimes[[lidx, uidx]], np.array([lva, uva]).flatten(), time)
def plot_ship(i): f=plt.figure() ax=f.add_axes([.125,.1,.45,.8]) #prettyplot_ll(ax,setregion=region) #plotcoast(ax,filename='pacific_harbour.nc',color='None',fcolor='darkgreen',fill=True) ax.imshow(geotiff, cmap=cmap,vmax=13, extent=extent) ax.plot(locs[:,0],locs[:,1],'r',lw=2) qax=ax.quiver(locs[:,0],locs[:,1],csua*cv,csva*cv,angles='xy',scale_units='xy',scale=100,width=0.003) qax.set_zorder(20) if useKnots: qaxk=ax.quiverkey(qax,.9,.85,0.5, r'1 knot') else: qaxk=ax.quiverkey(qax,.9,.85,0.5, r'0.5 ms$^{-1}$') ax.plot(locs[i,0],locs[i,1],'m*',markersize=15) time=times[i] lidx=np.argwhere(mtimes<=time).max() uidx=np.argwhere(mtimes>time).min() for j,idx in enumerate(vidx): lua=ipt.interpEfield_locs(data,'ua',data['uvnodell'][idx,:],lidx,ll=True) lva=ipt.interpEfield_locs(data,'va',data['uvnodell'][idx,:],lidx,ll=True) uua=ipt.interpEfield_locs(data,'ua',data['uvnodell'][idx,:],uidx,ll=True) uva=ipt.interpEfield_locs(data,'va',data['uvnodell'][idx,:],uidx,ll=True) u1 = interp1d(mtimes[[lidx,uidx]], np.array([lua,uua]).flatten()) u_vec[j] = u1(time)*cv v1 = interp1d(mtimes[[lidx,uidx]], np.array([lva,uva]).flatten()) v_vec[j] = v1(time)*cv s_vec=speeder(u_vec,v_vec) scale=((region['region'][1]-region['region'][0])*.0475)/s_vec.max() for idx,u,v in zip(vidx,u_vec,v_vec): x,y=data['uvnodell'][idx,:] dx=scale*u dy=scale*v al=np.sqrt(u**2+v**2) sl=np.sqrt(dx**2+dy**2) cidx=np.argwhere(al>=srange) if len(cidx) > 0: cidx=np.max(cidx) fcolor=crange[cidx] if al>=srange[0]: a=pp.FancyArrow(x,y,dx,dy,width_base=sl*.1,width_top=sl*.2, head_length=sl*.35,head_width=sl*.4, length_includes_head=True, edgecolor='None', facecolor=fcolor) ax.add_artist(a) for label in ax.get_xticklabels()[::2]: label.set_visible(False) prettyplot_ll(ax,setregion=region) #plt.draw() #a=ax.get_axes().get_position() #print(a) #print(ax.get_aspect()) #figW, figH = f.get_size_inches() #fa = figH / figW #print(fa) #ax.axis(region['region']) space=.01 plots=3 axsub=np.empty((plots,),dtype=object) height=(.450898-((plots-1)*space))/3 for j in range(plots): #print([.575+space,.1+(height+space)*i,.325,height]) axsub[j]=f.add_axes([.575+space/ax.get_aspect(),.1+(height+space)*j,.315,height]) axsub[2].plot(ctime*24-ctime.min()*24,speeder(csua,csva)*cv,'k') axsub[1].quiver(ctime*24-ctime.min()*24,np.zeros((len(ctime),)),csua,csva,scale=3,width=.0025) axsub[0].quiver(ctime2[:-1]*24-ctime2[:-1].min()*24,np.zeros((len(ctime2[:-1]),)),cdxs,cdys,scale=6,width=.0025) for label in axsub[1].get_xticklabels(): label.set_visible(False) for label in axsub[2].get_xticklabels(): label.set_visible(False) axsub[1].xaxis.set_tick_params(labelbottom='off') axsub[2].xaxis.set_tick_params(labelbottom='off') if useKnots: axsub[2].set_ylabel(r'Speed (knot)',fontsize=8) else: axsub[2].set_ylabel(r'Speed (ms$^{-1}$)',fontsize=8) axsub[1].set_ylabel(r'Cur. Dir.',fontsize=8) axsub[0].set_ylabel(r'Ship Dir.',fontsize=8) axsub[0].set_xlabel('Time (h)') if useKnots: axsub[2].set_ylim([0,2]) else: axsub[2].set_ylim([0,1]) axsub[1].set_ylim([-np.pi,np.pi]) for axin in axsub: axin.yaxis.set_tick_params(labelleft='off') axin.yaxis.set_label_position('right') axsub[2].yaxis.set_tick_params(labelright='on') for axin in axsub: axin.axvline(times[i]*24-times.min()*24,color='r') f.savefig(savepath + grid + '_shippath_'+"{:06.6f}".format(time)+'.png',dpi=300) plt.close(f)
if len(mt_idx) == 0: continue #make sure all times are covered mt_idx = np.arange(mt_idx.min() - 1, mt_idx.max() + 2) cnt = 0 lu = np.empty((mlay, len(locs))) lv = np.empty((mlay, len(locs))) uu = np.empty((mlay, len(locs))) uv = np.empty((mlay, len(locs))) for layer in range(0, mlay): lu[layer, :] = ipt.interpEfield_locs(data, 'u', locs, lidx, ll=True, layer=layer) lv[layer, :] = ipt.interpEfield_locs(data, 'v', locs, lidx, ll=True, layer=layer) for i in range(len(mt_idx) - 1): for layer in range(0, mlay): uu[layer, :] = ipt.interpEfield_locs(data, 'u', locs, mt_idx[i + 1],
#proj=gridproj(grid) _,_,proj = lcc(data['lon'],data['lat']) x,y = proj(LON,LAT) length=len(range(starttime,endtime,stride)) ua=np.empty((length,len(x))) va=np.empty((length,len(x))) for i,timein in enumerate(range(starttime,endtime,stride)): print(i) print(timein) ua[i,:]=ipt.interpEfield_locs(data,'ua',np.array([x, y]).T,timein) va[i,:]=ipt.interpEfield_locs(data,'va',np.array([x, y]).T,timein) hosts=data['trigrid_finder'].__call__(LON,LAT) host=hosts==-1 host=host.astype(bool) i=0 u=np.ma.masked_array(ua[i,:],host) v=np.ma.masked_array(va[i,:],host) f=plt.figure() ax=plt.subplot(111) ax.pcolormesh(lon,lat,speeder(u,v).reshape(lon.shape)) f.show()
mt_idx=np.arange(mt_idx.min()-1,mt_idx.max()+2) cnt=0 #get data for first time lidx=mt_idx[0] lu=np.empty((mlay,len(locs))) lv=np.empty((mlay,len(locs))) lh=np.empty((len(locs),)) lz=np.empty((len(locs),)) uu=np.empty((mlay,len(locs))) uv=np.empty((mlay,len(locs))) uh=np.empty((len(locs),)) uz=np.empty((len(locs),)) for layer in range(0,mlay): lu[layer,:]=ipt.interpEfield_locs(data,'u',locs,lidx,ll=True,layer=layer) lv[layer,:]=ipt.interpEfield_locs(data,'v',locs,lidx,ll=True,layer=layer) lh=ipt.interpNfield_locs(data,'h',locs,lidx,ll=True) lz=ipt.interpNfield_locs(data,'zeta',locs,lidx,ll=True) for i in range(len(mt_idx)-1): for layer in range(0,mlay): uu[layer,:]=ipt.interpEfield_locs(data,'u',locs,mt_idx[i+1],ll=True,layer=layer) uv[layer,:]=ipt.interpEfield_locs(data,'v',locs,mt_idx[i+1],ll=True,layer=layer) uh=ipt.interpNfield_locs(data,'h',locs,mt_idx[i+1],ll=True) uz=ipt.interpNfield_locs(data,'zeta',locs,mt_idx[i+1],ll=True) t_idx=np.argwhere( (time>=mtimes[mt_idx[i]]) & (time<mtimes[mt_idx[i+1]]) ) print(i,len(t_idx)) for idx in t_idx: su = ipt.interp1d(mtimes[[mt_idx[i],mt_idx[i+1]]], np.squeeze(np.array([lu[:,idx],uu[:,idx]])).T, time[idx]) sv = ipt.interp1d(mtimes[[mt_idx[i],mt_idx[i+1]]], np.squeeze(np.array([lv[:,idx],uv[:,idx]])).T, time[idx]) sh = ipt.interp1d(mtimes[[mt_idx[i],mt_idx[i+1]]], np.squeeze(np.array([lh[idx],uh[idx]])), time[idx])