def create_spng(spngname, grdname, str): dimy, dimx, units = setstr(str) grid = NF(grdname, 'r') lon = grid.variables[dimx][:] lat = grid.variables[dimy][:] grid.close() ff = NF(spngname, 'w', format='NETCDF3_CLASSIC') ff.createDimension(dimy, np.size(lat)) ff.createDimension(dimx, np.size(lon)) ff.createVariable(dimy, 'd', (dimy, )) ff.createVariable(dimx, 'd', (dimx, )) ff.createVariable('Idamp', 'd', ( dimy, dimx, )) ff.close() ff = NF(spngname, 'a') ff.variables[dimx][:] = lon ff.variables[dimy][:] = lat ff.close()
def volume_flux(sname,gname,tind,lonind): print lonind ff=NF(sname,'r') u=ff.variables['u'][-tind:,:,:,lonind:lonind+2] thick=ff.variables['h'][-tind:,:,:,lonind] time=ff.variables['Time'][-tind:] ff.close() ff=NF(gname,'r') dyh=ff.variables['dyh'][:,lonind].flatten() ff.close() u=u[:,:,:,:-1]+0.5*np.diff(u,axis=3) #regrid u=u[...,0] thick=thick[...,0] dyh=np.tile(dyh,(u.shape[0],u.shape[1],1)) U=thick*u*dyh U=np.sum(U,axis=2) U=U[:,:]/1e6 U1m=np.mean(U[:,0]) U2m=np.mean(U[:,1]) U1std=np.std(U[:,0]) U2std=np.std(U[:,1]) return U1m, U2m, U1std, U2std
def create_grd(grdname, lat, lon, str): dimy, dimx, units = setstr(str) ff = NF(grdname, 'w', format='NETCDF3_CLASSIC') ff.createDimension(dimy, np.size(lat)) ff.createDimension(dimx, np.size(lon)) ff.createVariable(dimy, 'd', (dimy, )) tmp = ff.variables[dimy] if str == 'l': setattr(tmp, "long_name", "Latitude") elif str == 'x': setattr(tmp, "long_name", "y dist") setattr(tmp, "units", units) ff.createVariable(dimx, 'd', (dimx, )) tmp = ff.variables[dimx] if str == 'l': setattr(tmp, "long_name", "Longitude") elif str == 'x': setattr(tmp, "long_name", "x dist") setattr(tmp, "units", units) ff.createVariable('D', 'd', ( dimy, dimx, )) tmp = ff.variables['D'] setattr(tmp, "long_name", "Depth") setattr(tmp, "units", "meter") ff.close()
def create_bdy(bdyname, grdname, str): dimy, dimx, units = setstr(str) grid = NF(grdname, 'r') lon = grid.variables[dimx][:] lat = grid.variables[dimy][:] grid.close() ff = NF(bdyname, 'w', format='NETCDF3_CLASSIC') ff.createDimension(dimy, np.size(lat)) ff.createDimension(dimx, np.size(lon)) ff.createDimension('interface', 3) ff.createDimension('LAYER', 2) ff.createVariable(dimy, 'd', (dimy, )) ff.createVariable(dimx, 'd', (dimx, )) ff.createVariable('interface', 'd', ('interface', )) ff.createVariable('LAYER', 'd', ('LAYER', )) ff.createVariable('ETA', 'd', ( 'interface', dimy, dimx, )) ff.createVariable('u', 'd', ( 'LAYER', dimy, dimx, )) ff.createVariable('v', 'd', ( 'LAYER', dimy, dimx, )) ff.close() # copy lon/lat from grid ff = NF(bdyname, 'a') ff.variables[dimx][:] = lon ff.variables[dimy][:] = lat ff.close()
xs = 160. xh = xh[xs:] bot = bot[:, xs:] if 1: #Alboran run90+ xs = 140. xh = xh[xs:] bot = bot[:, xs:] bot[bot == 0.] = 0.1 bot[0, :] = 0.1 bot[-1, :] = 0.1 # dirty md.create_grd(grdname, yh, xh, 'x') grid = NF(grdname, 'a') grid.variables['xh'][:] = xh grid.variables['yh'][:] = yh grid.variables['D'][:] = bot grid.close() # print "" print "lenlat: " + str(np.diff(yh)[0] * yh.shape[0]) print "lenlon: " + str(np.diff(xh)[0] * xh.shape[0]) print "" print "lowlat: " + str(min(yh)) print "westlon: " + str(min(xh)) # print "size: " + str(bot.shape) plt.figure()
# -*- coding: utf-8 -*- #from Scientific.IO.NetCDF import NetCDFFile as NF from netCDF4 import Dataset as NF import matplotlib.pyplot as plt import numpy as np import mod_data as md import os as os import inspect as inspect rundir = os.path.dirname(inspect.getfile(inspect.currentframe())) grdname = rundir + '/../data/grd.nc' initname = rundir + '/../data/init.nc' bdyname = rundir + '/../data/bdy.nc' grid = NF(grdname, 'r') h = grid.variables['D'][:] lon = grid.variables['xh'][:] lat = grid.variables['yh'][:] grid.close() #fac=np.linspace(0.,-0.5,h.shape[1]) #eta1=-np.ones(h.shape)*np.tile(fac,(h.shape[0],1)) eta1 = np.zeros(h.shape) e2east = 80. eta2a = eta1 + e2east eta2b = h - 1e-9 #eta2b[eta2b<eta2a]=eta2a[eta2b<eta2a]+1e-9 fac = np.linspace(1, 0, 10) mat = np.ones(h.shape) [x1, x2] = np.meshgrid(fac, np.arange(mat.shape[0])) #mat[:,22:34]= x1;
line = file.readline() ll = np.fromstring(line.rstrip(), dtype=float, count=-1, sep=' ') lon[:, jj, offs + ii] = lo lat[:, jj, offs + ii] = la file.close() offs = offs + 120 oo = np.ones((1, 341, 720)) # make pressure (depth) p = p_[:, None, None] * oo ds = NF('wghc.nc', 'w', format='NETCDF4') ds.createDimension('z', 45) ds.createDimension('y', 341) ds.createDimension('x', 720) ds.createVariable('lat', 'd', ('z', 'y', 'x')) ds.createVariable('lon', 'd', ('z', 'y', 'x')) ds.createVariable('p', 'd', ('z', 'y', 'x')) ds.createVariable('s', 'd', ('z', 'y', 'x')) # single precision! ds.createVariable('tis', 'd', ('z', 'y', 'x')) ds.variables['lat'][:] = lat ds.variables['lon'][:] = lon ds.variables['p'][:] = p ds.variables['s'][:] = s
#vec=range(215,219) #vec=range(219,223) stats=[[] for i in range(len(vec))] for jj in range(len(vec)): ddir='../run'+str(vec[jj])+'/'; sname=ddir+'saves/save0.00e00.517.085.nc'; statsname=ddir+'saves/timestats.nc'; aname=ddir+'saves/avfld0.00e00.517.085.nc'; dname=ddir+'saves/D.517.85.2.nc'; gname=ddir+'saves/grid.517.85.nc'; grdname=ddir+'data/grd.nc'; spngname=ddir+'data/sponge.nc'; ininame=ddir+'data/init.nc'; ff=NF(dname,'r') D=ff.variables['D'][:] ff.close() jmid=np.argmax(D[:,0]) isill=np.argwhere(D[jmid,:]==Ds)[0] inarrow=np.argwhere(D[jmid,:]==Dn)[0] iexit=np.argwhere(D[10,inarrow:]>1)[0]+inarrow-1 #index of strait exit ff=NF(ininame,'r') e_ini=ff.variables['ETA'][1,10,-1] yh=ff.variables['yh'][:]*1e-3 ff.close() ff=NF(sname,'r') e=ff.variables['e'][-tind:,1,jmid,:]
fig = plt.figure(figsize=(fac * 9.2, fac * 3.2)) g = [] cblevs = [] for jj in range(len(vec)): print 'processing ' + str(jj) + ' of ' + str(len(vec)) ddir = '../run' + str(vec[jj]) + '/' sname = ddir + 'saves/save0.00e00.517.085.nc' aname = ddir + 'saves/avfld0.00e00.517.085.nc' gname = ddir + 'saves/grid.517.85.nc' gname1 = ddir + 'data/grd.nc' ininame = ddir + 'data/init.nc' dname = ddir + 'saves/D.517.85.2.nc' ff = NF(ininame, 'r') xh = ff.variables['xh'][:] * 1e-3 yh = ff.variables['yh'][:] * 1e-3 ff.close() ff = NF(aname, 'r') e = ff.variables['etm'][-tind:, ifc, :, :] ff.close() ff = NF(gname1, 'r') D = ff.variables['D'][:] ff.close() ff = NF(dname, 'r') r2 = ff.variables['R'][1] ff.close()
handles = [] col = ['b', 'g', 'r'] for dr, icol in zip([light, basic, dense], range(3)): print dr q = [] qstd = [] eta_exit = [] for ii in range(4): ddir = '../run' + str(dr[ii]) + '/' sname = ddir + 'saves/save0.00e00.517.085.nc' aname = ddir + 'saves/avfld0.00e00.517.085.nc' dname = ddir + 'saves/D.517.85.2.nc' gname = ddir + 'saves/grid.517.85.nc' ff = NF(dname, 'r') D = ff.variables['D'][:] ff.close() jmid = np.argmax(D[:, 0]) inarrow = np.argwhere(D[jmid, :] == Dn)[0] iexit = np.argwhere( D[10, inarrow:] > 1)[0] + inarrow - 1 #index of strait exit ff = NF(aname, 'r') etm = ff.variables['etm'][-tind:, 1, jmid, :] ff.close() em_exit = np.mean(etm[:, iexit], axis=0) U1m, U2m, U1std, U2std = volume_flux(sname, gname, tind, inarrow) q.append(U1m)
#from Scientific.IO.NetCDF import NetCDFFile as NF from netCDF4 import Dataset as NF import matplotlib.pyplot as plt import numpy as np import mod_data as md import os as os import inspect as inspect rundir = os.path.dirname(inspect.getfile(inspect.currentframe())) grdname = rundir + '/../data/grd.nc' spngname = rundir + '/../data/sponge.nc' grid = NF(grdname, 'r') h = grid.variables['D'][:] grid.close() idamp = np.zeros(h.shape) #dmp=1./77. dmp = 1e-4 #dmp=0.; #dmp=1./20; n = 15 nv = dmp * np.linspace(1, 0, n) [x1, x2] = np.meshgrid(nv, np.arange(h.shape[0])) idamp[:, :n] = x1 dmp = 2.5e-5 n = 150 nv = dmp * np.linspace(1, 0, n) [x1, x2] = np.meshgrid(nv, np.arange(h.shape[0]))
# Isotropic grid gibRadEarth=6371e3; #gibDx=4e3; gibDx=2e3; dl = ((gibDx/gibRadEarth)/np.cos(36*np.pi/180))*180/np.pi; lon=np.arange(lonmin,lonmax,dl); i=0; lat=[latmin]; while lat[-1]<=latmax: i=i+1; b=[lat[i-1]+dl*np.cos(lat[i-1]*np.pi/180)] lat=np.r_[lat,b]; ff = NF('./ETOPO1_Ice_g_gdal.grd', 'r') x=np.linspace(-180.,180.,360*60+1); y=np.linspace(-90.,90.,180*60+1); xi=np.where((x>lonmin) & (x < lonmax))[0]; xi=np.arange(xi[0]-4,xi[-1]+4) yi=np.where((y>latmin) & (y < latmax))[0]; yi=np.arange(yi[0]-4,yi[-1]+4) x1=np.arange(np.size(yi)) x2=np.arange(np.size(yi)) for j in np.arange(0,np.size(yi)): x1[j]=np.size(x)*(np.size(y)-yi[j])+xi[0]; x2[j]=np.size(x)*(np.size(y)-yi[j])+xi[-1];