def load_proj(self): # load and parse attrs needed for basemap projection self.proj_info={} for i in ['proj4string','basemap_opts']: if i in netcdf.fatt(self.nc): self.proj_info[i]=netcdf.fatt(self.nc,i) else: self.proj_info[i]='' o={} if self.proj_info['basemap_opts']: # parse it for i in self.proj_info['basemap_opts'].split(): name,val=i[1:].split('=') try: val=float(val) except: pass o[name]=val self.proj_info['basemap_opts0']=o # Now let ys make sure the projection is well centered (may not be # at grid creation): # - build a set of reasonable options; user can always change them # after. Let us consider only the lcc and merc projections, and use # lcc by default if none is provided at grid atts if o: prj=o['projection'] else: prj='lcc' if self.spherical: xlim=self.lon.min(),self.lon.max() ylim=self.lat.min(),self.lat.max() dlon=xlim[1]-xlim[0] dlat=ylim[1]-ylim[0] if prj=='lcc': lon_0=0.5*(xlim[0]+xlim[1]) lat_0=0.5*(ylim[0]+ylim[1]) lat_1=lat_0-dlat/4. lat_2=lat_0+dlat/4. W=110*np.cos(lat_0*np.pi/180)*(xlim[1]-xlim[0])*1e3 H=110*(ylim[1]-ylim[0])*1e3 W*=1.2 H*=1.2 o=dict(resolution='i',projection=prj, lat_1=lat_1,lat_2=lat_2,lat_0=lat_0,lon_0=lon_0, width=W,height=H) elif prj=='merc': dlon_=dlon_/10 dlat_=dlat_/10 o=dict(resolution='c',projection=prj, llcrnrlon=xlim[0]-dlon_, llcrnrlat=ylim[0]-dlat_, urcrnrlon=xlim[1]+dlon_, urcrnrlat=ylim[1]+dlat_, lon_0=0.5*(xlim[0]+xlim[1]), lat_0=0.5*(ylim[0]+ylim[1])) self.proj_info['basemap_opts']=o
def load_grid(self,grd=False): ats=netcdf.fatt(self.nc) if not grd: if 'grd_file' in ats: grd=ats['grd_file'].value if grd and not grd.startswith('http') and not os.path.isfile(grd): grd=False if grd: self.grid=Grid(grd,self.quiet) else: try: self.grid=Grid(self.name) except: self.grid=False
def load_grid(self,grd=False): ats=netcdf.fatt(self.name) if not grd: if 'grd_file' in ats.keys(): grd=ats['grd_file'].value if grd and not os.path.isfile(grd): grd=False if grd: self.grid=Grid(grd,self.quiet) else: try: self.grid=Grid(self.name) except: self.grid=False
def load_atts(self): atts=netcdf.fatt(self.nc) self.atts={} self.atts.update(atts)
def load_atts(self): atts=netcdf.fatt(self.name) self.atts={} for k in atts.keys(): self.atts[k]=atts[k].value
def load_atts(self): atts = netcdf.fatt(self.name) self.atts = {} for k in atts.keys(): self.atts[k] = atts[k].value
def update_wind(fname, data, new_wind_info, **kargs): ''' new_wind_info will be added to fname as global attribute (ex: 'new wind from database xxx') ''' quiet = False Margin = 4 # for griddata and for original data to keep grd = False keepOriginal = 2 for k in kargs.keys(): if k == 'quiet': quiet = kargs[k] elif k == 'margin': Margin = kargs[k] elif k == 'grid': grd = kargs[k] elif k.lower().startswith('keepor'): keepOriginal = kargs[k] if not grd: grd = netcdf.fatt(fname, 'grd_file') g = roms.Grid(grd) x0 = data['x'] y0 = data['y'] if x0.ndim == 1: x0, y0 = np.meshgrid(x0, y0) dates = data.keys()[:] dates.remove('x') dates.remove('y') dates = np.array(dates) # interp in time: time = netcdf.nctime(fname, 'time') cond = False tind = -1 fob = gennc.GenBlk(fname, grd) for t in time: newWind = {} tind += 1 I, = np.where(dates == t) if I.size: I = I[0] uv = data[dates[I]] if not quiet: print(t, dates[I]) else: i1, = np.where(dates > t) i0, = np.where(dates < t) if i0.size and i1.size: i1 = i1[0] i0 = i0[-1] d0 = t - dates[i0] d1 = dates[i1] - t d0 = d0.days + d0.seconds / 86400. d1 = d1.days + d1.seconds / 86400. uv = (data[dates[i0]] * d1 + data[dates[i1]] * d0) / (d0 + d1) if not quiet: print(t, dates[i0], dates[i1], d0, d1) elif not i1.size: uv = data[dates[-1]] if not quiet: print(t, dates[-1]) elif not i0.size: uv = data[dates[0]] if not quiet: print(t, dates[0]) # interp to grid: if cond is False: cond, inds = rt.grid_vicinity(grd, x0, y0, margin=Margin, rect=True, retinds=True) i1, i2, j1, j2 = inds if not quiet: print(' --> inter uv %s' % t.isoformat(' ')) u = calc.griddata(x0[cond], y0[cond], uv.real[cond], g.lon, g.lat, extrap=True) v = calc.griddata(x0[cond], y0[cond], uv.imag[cond], g.lon, g.lat, extrap=True) # rotate wind, calc stress: if not quiet: print(' --> rot U,V wind and U,V wind stress') wspd = np.sqrt(u**2 + v**2) sustr, svstr = air_sea.wind_stress(u, v) angle = g.use('angle') uwnd, vwnd = calc.rot2d(u, v, angle) sustr, svstr = calc.rot2d(sustr, svstr, angle) sustr = rt.rho2uvp(sustr, 'u') svstr = rt.rho2uvp(svstr, 'v') # update wind data: newWind['date'] = t newWind['uwnd'] = uwnd newWind['vwnd'] = vwnd newWind['sustr'] = sustr newWind['svstr'] = svstr newWind['wspd'] = wspd # original xy: if tind == 0: newWind['attr'] = {'new_wind_info': new_wind_info} if not quiet: print(' --> original xy') if keepOriginal == 1: newWind['x_wind'] = x0 newWind['y_wind'] = y0 elif keepOriginal == 2: newWind['x_wind'] = x0[j1:j2, i1:i2] newWind['y_wind'] = y0[j1:j2, i1:i2] # add to file: if not quiet: print(' --> adding to file') fob.update_wind(newWind, quiet=quiet) if not quiet: print('')
def update_wind_blended2(fname, datapaths, **kargs): ''' In days without blended data will try to use quikscat data ''' from okean.datasets import quikscat from okean.datasets import blended_wind a = blended_wind.WINDData(datapaths[0]) b = quikscat.WINDData(datapaths[1]) time = netcdf.nctime(fname, 'time') date0 = dts.next_date(time[0], -1) date1 = dts.next_date(time[-1], +2) data = a.data(date0, date1) # limit are... otherwise, quikscat interp will be very slow! grd = netcdf.fatt(fname, 'grd_file') import os if not os.path.isfile(grd): grd = kargs['grd'] cond, inds = rt.grid_vicinity(grd, data['x'], data['y'], margin=5, rect=True, retinds=True) i1, i2, j1, j2 = inds for d in data.keys(): if d == 'x': data[d] = data[d][i1:i2] elif d == 'y': data[d] = data[d][j1:j2] else: data[d] = data[d][j1:j2, i1:i2] # check for missing days: time0 = data.keys() x0 = data['x'] y0 = data['y'] x0, y0 = np.meshgrid(x0, y0) time0.remove('x') time0.remove('y') out = OrderedDict() out['x'] = x0 out['y'] = y0 info = '' qs_ij_limits_done = False for d in dts.drange(date0, date1): found = 0 for t in time0: if (t.year, t.month, t.day) == (d.year, d.month, d.day): print('==> blended : ', t) out[t] = data[t] found = 1 if not found: # use quikscat: print('==> quikscat : ', d.strftime('%Y-%m-%d')) tmp = b.data(d, dts.next_date(d)) if not tmp.has_key('x'): continue x, y = tmp['x'], tmp['y'] x, y = np.meshgrid(x, y) # reduce qs data: if not qs_ij_limits_done: i1, i2, j1, j2 = calc.ij_limits(x, y, [x0.min(), x0.max()], [y0.min(), y0.max()]) qs_ij_limits_done = True x = x[j1:j2, i1:i2] y = y[j1:j2, i1:i2] tmp[tmp.keys()[0]] = tmp[tmp.keys()[0]][j1:j2, i1:i2] print(' griddata u') u = calc.griddata(x, y, tmp[tmp.keys()[0]].real, x0, y0) print(' griddata v') v = calc.griddata(x, y, tmp[tmp.keys()[0]].imag, x0, y0) out[tmp.keys()[0]] = u + 1.j * v info += '#' + d.strftime('%Y%m%d') new_wind_info = 'blended+quikscat at days: ' + info update_wind(fname, out, new_wind_info, **kargs)
def update_wind(fname, data, new_wind_info, **kargs): """ new_wind_info will be added to fname as global attribute (ex: 'new wind from database xxx') """ quiet = False Margin = 4 # for griddata and for original data to keep grd = False keepOriginal = 2 for k in kargs.keys(): if k == "quiet": quiet = kargs[k] elif k == "margin": Margin = kargs[k] elif k == "grid": grd = kargs[k] elif k.lower().startswith("keepor"): keepOriginal = kargs[k] if not grd: grd = netcdf.fatt(fname, "grd_file") g = roms.Grid(grd) x0 = data["x"] y0 = data["y"] if x0.ndim == 1: x0, y0 = np.meshgrid(x0, y0) dates = data.keys()[:] dates.remove("x") dates.remove("y") dates = np.array(dates) # interp in time: time = netcdf.nctime(fname, "time") cond = False tind = -1 fob = gennc.GenBlk(fname, grd) for t in time: newWind = {} tind += 1 I, = np.where(dates == t) if I.size: I = I[0] uv = data[dates[I]] if not quiet: print t, dates[I] else: i1, = np.where(dates > t) i0, = np.where(dates < t) if i0.size and i1.size: i1 = i1[0] i0 = i0[-1] d0 = t - dates[i0] d1 = dates[i1] - t d0 = d0.days + d0.seconds / 86400.0 d1 = d1.days + d1.seconds / 86400.0 uv = (data[dates[i0]] * d1 + data[dates[i1]] * d0) / (d0 + d1) if not quiet: print t, dates[i0], dates[i1], d0, d1 elif not i1.size: uv = data[dates[-1]] if not quiet: print t, dates[-1] elif not i0.size: uv = data[dates[0]] if not quiet: print t, dates[0] # interp to grid: if cond is False: cond, inds = rt.grid_vicinity(grd, x0, y0, margin=Margin, rect=True, retinds=True) i1, i2, j1, j2 = inds if not quiet: print " --> inter uv %s" % t.isoformat(" ") u = calc.griddata(x0[cond], y0[cond], uv.real[cond], g.lon, g.lat, extrap=True) v = calc.griddata(x0[cond], y0[cond], uv.imag[cond], g.lon, g.lat, extrap=True) # rotate wind, calc stress: if not quiet: print " --> rot U,V wind and U,V wind stress" wspd = np.sqrt(u ** 2 + v ** 2) sustr, svstr = air_sea.wind_stress(u, v) angle = g.use("angle") uwnd, vwnd = calc.rot2d(u, v, angle) sustr, svstr = calc.rot2d(sustr, svstr, angle) sustr = rt.rho2uvp(sustr, "u") svstr = rt.rho2uvp(svstr, "v") # update wind data: newWind["date"] = t newWind["uwnd"] = uwnd newWind["vwnd"] = vwnd newWind["sustr"] = sustr newWind["svstr"] = svstr newWind["wspd"] = wspd # original xy: if tind == 0: newWind["attr"] = {"new_wind_info": new_wind_info} if not quiet: print " --> original xy" if keepOriginal == 1: newWind["x_wind"] = x0 newWind["y_wind"] = y0 elif keepOriginal == 2: newWind["x_wind"] = x0[j1:j2, i1:i2] newWind["y_wind"] = y0[j1:j2, i1:i2] # add to file: if not quiet: print " --> adding to file" fob.update_wind(newWind, quiet=quiet) if not quiet: print ""
def update_wind_blended2(fname, datapaths, **kargs): """ In days without blended data will try to use quikscat data """ from okean.datasets import quikscat from okean.datasets import blended_wind a = blended_wind.WINDData(datapaths[0]) b = quikscat.WINDData(datapaths[1]) time = netcdf.nctime(fname, "time") date0 = dts.next_date(time[0], -1) date1 = dts.next_date(time[-1], +2) data = a.data(date0, date1) # limit are... otherwise, quikscat interp will be very slow! grd = netcdf.fatt(fname, "grd_file") import os if not os.path.isfile(grd): grd = kargs["grd"] cond, inds = rt.grid_vicinity(grd, data["x"], data["y"], margin=5, rect=True, retinds=True) i1, i2, j1, j2 = inds for d in data.keys(): if d == "x": data[d] = data[d][i1:i2] elif d == "y": data[d] = data[d][j1:j2] else: data[d] = data[d][j1:j2, i1:i2] # check for missing days: time0 = data.keys() x0 = data["x"] y0 = data["y"] x0, y0 = np.meshgrid(x0, y0) time0.remove("x") time0.remove("y") out = cb.odict() out["x"] = x0 out["y"] = y0 info = "" qs_ij_limits_done = False for d in dts.drange(date0, date1): found = 0 for t in time0: if (t.year, t.month, t.day) == (d.year, d.month, d.day): print "==> blended : ", t out[t] = data[t] found = 1 if not found: # use quikscat: print "==> quikscat : ", d.strftime("%Y-%m-%d") tmp = b.data(d, dts.next_date(d)) if not tmp.has_key("x"): continue x, y = tmp["x"], tmp["y"] x, y = np.meshgrid(x, y) # reduce qs data: if not qs_ij_limits_done: i1, i2, j1, j2 = calc.ij_limits(x, y, [x0.min(), x0.max()], [y0.min(), y0.max()]) qs_ij_limits_done = True x = x[j1:j2, i1:i2] y = y[j1:j2, i1:i2] tmp[tmp.keys()[0]] = tmp[tmp.keys()[0]][j1:j2, i1:i2] print " griddata u" u = calc.griddata(x, y, tmp[tmp.keys()[0]].real, x0, y0) print " griddata v" v = calc.griddata(x, y, tmp[tmp.keys()[0]].imag, x0, y0) out[tmp.keys()[0]] = u + 1.0j * v info += "#" + d.strftime("%Y%m%d") new_wind_info = "blended+quikscat at days: " + info update_wind(fname, out, new_wind_info, **kargs)
def update_wind(fname,data,new_wind_info,**kargs): ''' new_wind_info will be added to fname as global attribute (ex: 'new wind from database xxx') ''' quiet = False Margin = 4 # for griddata and for original data to keep grd = False keepOriginal = 2 for k in kargs.keys(): if k=='quiet': quiet=kargs[k] elif k=='margin': Margin=kargs[k] elif k=='grid': grd=kargs[k] elif k.lower().startswith('keepor'): keepOriginal=kargs[k] if not grd: grd=netcdf.fatt(fname,'grd_file') g=roms.Grid(grd) x0=data['x'] y0=data['y'] if x0.ndim==1: x0,y0=np.meshgrid(x0,y0) dates=data.keys()[:] dates.remove('x') dates.remove('y') dates=np.array(dates) # interp in time: time=netcdf.nctime(fname,'time') cond=False tind=-1 fob=gennc.GenBlk(fname,grd) for t in time: newWind={} tind+=1 I,=np.where(dates==t) if I.size: I=I[0] uv=data[dates[I]] if not quiet: print(t,dates[I]) else: i1,=np.where(dates>t) i0,=np.where(dates<t) if i0.size and i1.size: i1=i1[0] i0=i0[-1] d0=t-dates[i0] d1=dates[i1]-t d0=d0.days+d0.seconds/86400. d1=d1.days+d1.seconds/86400. uv=(data[dates[i0]]*d1+data[dates[i1]]*d0)/(d0+d1) if not quiet: print(t,dates[i0],dates[i1], d0,d1) elif not i1.size: uv=data[dates[-1]] if not quiet: print(t,dates[-1]) elif not i0.size: uv=data[dates[0]] if not quiet: print(t,dates[0]) # interp to grid: if cond is False: cond,inds=rt.grid_vicinity(grd,x0,y0,margin=Margin,rect=True,retinds=True) i1,i2,j1,j2=inds if not quiet: print(' --> inter uv %s' % t.isoformat(' ')) u=calc.griddata(x0[cond],y0[cond],uv.real[cond],g.lon,g.lat,extrap=True) v=calc.griddata(x0[cond],y0[cond],uv.imag[cond],g.lon,g.lat,extrap=True) # rotate wind, calc stress: if not quiet: print(' --> rot U,V wind and U,V wind stress') wspd=np.sqrt(u**2+v**2) sustr,svstr=air_sea.wind_stress(u,v) angle=g.use('angle') uwnd,vwnd=calc.rot2d(u,v,angle) sustr,svstr=calc.rot2d(sustr,svstr,angle) sustr=rt.rho2uvp(sustr,'u') svstr=rt.rho2uvp(svstr,'v') # update wind data: newWind['date'] = t newWind['uwnd'] = uwnd newWind['vwnd'] = vwnd newWind['sustr'] = sustr newWind['svstr'] = svstr newWind['wspd'] = wspd # original xy: if tind==0: newWind['attr']={'new_wind_info':new_wind_info} if not quiet: print(' --> original xy') if keepOriginal==1: newWind['x_wind']=x0 newWind['y_wind']=y0 elif keepOriginal==2: newWind['x_wind']=x0[j1:j2,i1:i2] newWind['y_wind']=y0[j1:j2,i1:i2] # add to file: if not quiet: print(' --> adding to file') fob.update_wind(newWind,quiet=quiet) if not quiet: print('')
def update_wind_blended2(fname,datapaths,**kargs): ''' In days without blended data will try to use quikscat data ''' from okean.datasets import quikscat from okean.datasets import blended_wind a=blended_wind.WINDData(datapaths[0]) b=quikscat.WINDData(datapaths[1]) time=netcdf.nctime(fname,'time') date0=dts.next_date(time[0],-1) date1=dts.next_date(time[-1],+2) data=a.data(date0,date1) # limit are... otherwise, quikscat interp will be very slow! grd=netcdf.fatt(fname,'grd_file') import os if not os.path.isfile(grd): grd=kargs['grd'] cond,inds=rt.grid_vicinity(grd,data['x'],data['y'],margin=5,rect=True,retinds=True) i1,i2,j1,j2=inds for d in data.keys(): if d == 'x': data[d]=data[d][i1:i2] elif d == 'y': data[d]=data[d][j1:j2] else: data[d]=data[d][j1:j2,i1:i2] # check for missing days: time0=data.keys() x0=data['x'] y0=data['y'] x0,y0=np.meshgrid(x0,y0) time0.remove('x') time0.remove('y') out=OrderedDict() out['x']=x0 out['y']=y0 info='' qs_ij_limits_done=False for d in dts.drange(date0,date1): found=0 for t in time0: if (t.year,t.month,t.day)==(d.year,d.month,d.day): print('==> blended : ',t) out[t]=data[t] found=1 if not found: # use quikscat: print('==> quikscat : ',d.strftime('%Y-%m-%d')) tmp= b.data(d,dts.next_date(d)) if not tmp.has_key('x'): continue x,y=tmp['x'],tmp['y'] x,y=np.meshgrid(x,y) # reduce qs data: if not qs_ij_limits_done: i1,i2,j1,j2=calc.ij_limits(x,y,[x0.min(),x0.max()],[y0.min(),y0.max()]) qs_ij_limits_done=True x=x[j1:j2,i1:i2] y=y[j1:j2,i1:i2] tmp[tmp.keys()[0]]=tmp[tmp.keys()[0]][j1:j2,i1:i2] print(' griddata u') u=calc.griddata(x,y,tmp[tmp.keys()[0]].real,x0,y0) print(' griddata v') v=calc.griddata(x,y,tmp[tmp.keys()[0]].imag,x0,y0) out[tmp.keys()[0]]=u+1.j*v info+='#'+d.strftime('%Y%m%d') new_wind_info='blended+quikscat at days: '+info update_wind(fname,out,new_wind_info,**kargs)