def sunhis2initial(hisfile, icfile): """ Main function """ print '#####\nCreating initial condition file (%s) from history file (%s)...' % ( icfile, hisfile) # Load the history file sunhis = Spatial(hisfile, tstep=-1, klayer=[-99]) # Load the initial condition object timeic = datetime.strftime(sunhis.time[-1], '%Y%m%d.%H%M%S') sunic = InitialCond(hisfile, timeic) # Load the last time step into each variable sunic.h = sunhis.loadData(variable='eta').reshape((1, sunic.h.shape)) sunic.T = sunhis.loadData(variable='temp').reshape((1, ) + sunic.T.shape) sunic.S = sunhis.loadData(variable='salt').reshape((1, ) + sunic.S.shape) sunic.uc = sunhis.loadData(variable='uc').reshape((1, ) + sunic.uc.shape) sunic.vc = sunhis.loadData(variable='vc').reshape((1, ) + sunic.vc.shape) print sunic.h.shape print sunic.T.shape # Load the age if sunhis.hasVar('agec'): sunic.agec = sunhis.loadData(variable='agec').reshape((1, ) + sunic.agec.shape) sunic.agealpha = sunhis.loadData( variable='agealpha').reshape((1, ) + sunic.agealpha.shape) # Write to the output file sunic.writeNC(icfile)
def _dumpic(self): """ Dump initial condition plots For each variable (uc,vc,T,S,eta): - surface plot - seabed plot """ varnames = ['uc', 'vc', 'temp', 'salt', 'eta'] sun = Spatial(self.suntanspath + '/' + self.icfile, klayer=[0]) # Plot the surface variables for vv in varnames: h = plt.figure() sun.variable = vv sun.loadData() sun.clim = [sun.data.min(), sun.data.max()] sun.plot() outfile = '%s/IC_%s_surface.png' % (self.plotdir, vv) sun.savefig(outfile) del h # Plot the seabed variables sun.klayer = [-1] for vv in varnames: h = plt.figure() sun.variable = vv sun.loadData() sun.clim = [sun.data.min(), sun.data.max()] sun.plot() outfile = '%s/IC_%s_seabed.png' % (self.plotdir, vv) sun.savefig(outfile) del h
def suntans2untrim(ncfile, outfile, tstart, tend, grdfile=None): """ Converts a suntans averages netcdf file into untrim format for use in particle tracking """ #### # Step 1: Load the suntans data object #### sun = Spatial(ncfile, klayer=[-99]) # Calculate some other variables sun.de = sun.get_edgevar(sun.dv, method='min') sun.mark[sun.mark == 5] = 0 sun.mark[sun.mark == 3] = 2 sun.facemark = np.zeros((sun.Nc, ), dtype=np.int) # Update the grad variable from the ascii grid file if supplied if not grdfile == None: print('Updating grid with ascii values...') grd = Grid(grdfile) sun.grad = grd.grad[:, ::-1] ### # Step 2: Write the grid variables to a netcdf file ### nc = Dataset(outfile, 'w', format='NETCDF4_CLASSIC') # Global variable nc.Description = 'UnTRIM history file converted from SUNTANS output' # Write the dimensions for dd in list(untrim_griddims.keys()): if dd == 'time': nc.createDimension(untrim_griddims[dd], 0) elif dd == 'numsides': nc.createDimension(untrim_griddims[dd], sun.maxfaces) else: nc.createDimension(untrim_griddims[dd], sun[dd]) for dd in other_dims: nc.createDimension(dd, other_dims[dd]) ### # Step 3: Initialize all of the grid variables ### def create_nc_var(name, dimensions, attdict,data=None, \ dtype='f8',zlib=False,complevel=0,fill_value=999999.0): tmp=nc.createVariable(name, dtype, dimensions,\ zlib=zlib,complevel=complevel,fill_value=fill_value) for aa in list(attdict.keys()): tmp.setncattr(aa, attdict[aa]) if not data == None: nc.variables[name][:] = data # Make sure the masked cells have a value of -1 mask = sun['cells'].mask.copy() sun['cells'][mask] = FILLVALUE sun['face'][mask] = FILLVALUE for vv in list(untrim_gridvars.keys()): vname = untrim_gridvars[vv] print('Writing grid variable %s (%s)...' % (vname, vv)) if vv == 'time': continue # add dz_min attribute to z_r variable if vv == 'z_r': ugrid[vname]['attributes'].update({'dz_min': 1e-5}) #sun[vv][:]=sun[vv][::-1] sun[vv][:] = sun['z_w'][0:-1][::-1] # Reverse the order of grad(???) if vv == 'grad': sun[vv][:] = sun[vv][:, ::-1] ## Fix one-based indexing #if vv in ['cells','edges','grad']: # mask = sun[vv][:]==-1 # tmp = sun[vv][:]+1 # tmp[mask]=-1 # #sun[vv][:]=sun[vv][:]+1 # create_nc_var(vname,ugrid[vname]['dimensions'],ugrid[vname]['attributes'],\ # data=tmp,dtype=ugrid[vname]['dtype']) create_nc_var(vname,ugrid[vname]['dimensions'],ugrid[vname]['attributes'],\ data=sun[vv],dtype=ugrid[vname]['dtype']) # Initialize the two time variables vname = untrim_gridvars['time'] create_nc_var(vname,ugrid[vname]['dimensions'],ugrid[vname]['attributes'],\ dtype=ugrid[vname]['dtype']) vname = 'Mesh2_data_time_string' create_nc_var(vname,ugrid[vname]['dimensions'],ugrid[vname]['attributes'],\ dtype=ugrid[vname]['dtype']) ### # Step 4: Initialize all of the time-varying variables (but don't write) ### for vname in varnames: print('Creating variable %s...' % (vname)) create_nc_var(vname,ugrid[vname]['dimensions'],ugrid[vname]['attributes'],\ dtype=ugrid[vname]['dtype'],zlib=True,complevel=1,fill_value=999999.) ### # Step 5: Loop through all of the time steps and write the variables ### tsteps = sun.getTstep(tstart, tend) tdays = othertime.DaysSince(sun.time, basetime=datetime(1899, 12, 31)) for ii, tt in enumerate(tsteps): # Convert the time to the untrim formats timestr = datetime.strftime(sun.time[tt], '%Y-%m-%d %H:%M:%S') print('Writing data at time %s (%d of %d)...' % (timestr, tt, tsteps[-1])) #Write the time variables nc.variables['Mesh2_data_time'][ii] = tdays[ii] nc.variables['Mesh2_data_time_string'][:, ii] = timestr # Load each variable or calculate it and convert it to the untrim format sun.tstep = [tt] ### # Compute a few terms first eta = sun.loadData(variable='eta') U = sun.loadData(variable='U_F') dzz = sun.getdzz(eta) dzf = sun.getdzf(eta) vname = 'Mesh2_sea_surface_elevation' #print '\tVariable: %s...'%vname nc.variables[vname][:, ii] = eta vname = 'Mesh2_salinity_3d' #print '\tVariable: %s...'%vname tmp3d = sun.loadData(variable='salt') nc.variables[vname][:, :, ii] = tmp3d.swapaxes(0, 1)[:, ::-1] vname = 'Mesh2_vertical_diffusivity_3d' #print '\tVariable: %s...'%vname tmp3d = sun.loadData(variable='nu_v') nc.variables[vname][:, :, ii] = tmp3d.swapaxes(0, 1)[:, ::-1] vname = 'h_flow_avg' #print '\tVariable: %s...'%vname nc.variables[vname][:, :, ii] = U.swapaxes(0, 1)[:, ::-1] vname = 'v_flow_avg' #print '\tVariable: %s...'%vname tmp3d = sun.loadData(variable='w') * sun.Ac # m^3/s nc.variables[vname][:, :, ii] = tmp3d.swapaxes(0, 1)[:, ::-1] # Need to calculate a few terms for the other variables vname = 'Mesh2_edge_wet_area' #print '\tVariable: %s...'%vname #dzf = sun.loadData(variable='dzf') tmp3d = dzf * sun.df nc.variables[vname][:, :, ii] = tmp3d.swapaxes(0, 1)[:, ::-1] vname = 'Mesh2_face_water_volume' #print '\tVariable: %s...'%vname #dzz = sun.loadData(variable='dzz') tmp3d = dzz * sun.Ac nc.variables[vname][:, :, ii] = tmp3d.swapaxes(0, 1)[:, ::-1] vname = 'Mesh2_face_wet_area' #print '\tVariable: %s...'%vname tmp3d = np.repeat(sun.Ac[np.newaxis, ...], sun.Nkmax, axis=0) nc.variables[vname][:, :, ii] = tmp3d.swapaxes(0, 1)[:, ::-1] # UnTRIM references from bottom to top i.e. # k = 0 @ bed ; k = Nkmax-1 @ top vname = 'Mesh2_edge_bottom_layer' #print '\tVariable: %s...'%vname #tmp2d = sun.Nkmax-sun.Nke # zero based tmp2d = sun.Nkmax - sun.Nke + 1 # one based nc.variables[vname][:, ii] = tmp2d vname = 'Mesh2_edge_top_layer' #print '\tVariable: %s...'%vname etop = sun.loadData(variable='etop') #tmp2d = sun.Nkmax-etop-1 # zero based tmp2d = sun.Nkmax - etop # one based nc.variables[vname][:, ii] = tmp2d vname = 'Mesh2_face_bottom_layer' #print '\tVariable: %s...'%vname #tmp2d = sun.Nkmax-sun.Nk + 1 # zero based tmp2d = sun.Nkmax - sun.Nk # one based nc.variables[vname][:, ii] = tmp2d vname = 'Mesh2_face_top_layer' #print '\tVariable: %s...'%vname ctop = sun.loadData(variable='ctop') #tmp2d = sun.Nkmax-ctop-1 # zero based tmp2d = sun.Nkmax - ctop # one based nc.variables[vname][:, ii] = tmp2d print(72 * '#') print('\t Finished SUNTANS->UnTRIM conversion') print(72 * '#') # close the file nc.close()
""" Generates a series of animations """ from sunpy import Spatial import numpy as np import matplotlib.pyplot as plt runname='SFBay3D' ncfile = '%s/%s_0*.nc'%('data',runname) k = 1 # depth layer plt.figure() sun = Spatial(ncfile,klayer=[k],variable='salt') sun.tstep=np.arange(0,len(sun.time)) sun.loadData() sun.clim = [28.0,32.0] sun.animate(vector_overlay=False) sun.saveanim('plots/%s_salt.mov'%runname) sun.clim=None plt.figure() sun = Spatial(ncfile,klayer=[k],variable='uc') sun.tstep=np.arange(0,len(sun.time)) sun.loadData() sun.animate(vector_overlay=False) sun.saveanim('plots/%s_uc.mov'%runname) sun.clim=None plt.figure() sun = Spatial(ncfile,klayer=[k],variable='eta')
""" Generates a series of animations from the suntans output """ from sunpy import Spatial import numpy as np import matplotlib.pyplot as plt ncfile = 'rundata/Estuary_SUNTANS_00*nc' plt.figure() sun = Spatial(ncfile,klayer=[1],variable='salt') sun.tstep=np.arange(0,len(sun.time)) sun.loadData() sun.animate(vector_overlay=False) sun.saveanim('plots/salt.mov') plt.figure() sun = Spatial(ncfile,klayer=[1],variable='temp') sun.tstep=np.arange(0,len(sun.time)) sun.loadData() sun.animate(vector_overlay=False) sun.saveanim('plots/temp.mov') plt.figure() sun = Spatial(ncfile,variable='tau_y') sun.tstep=np.arange(0,len(sun.time)) sun.loadData() sun.animate(vector_overlay=False) sun.saveanim('plots/tau_y.mov')
import numpy as np import matplotlib.pyplot as plt from datetime import timedelta from scipy.integrate import cumtrapz import pdb ### # Inputs ncfile = 'data/Heatflux_AVG.0' #cellindex=range(0,9) cellindex=[4] t0 = 0 ### sun = Spatial(ncfile,klayer=[-99]) sun.tstep = range(t0,sun.Nt) time = sun.time[sun.tstep] # Constants dt = sun.globalatts['dt']*sun.globalatts['ntaverage'] RHO0 = 1000.0 Cp = 4186.0 fac = (RHO0*Cp) area = sun.Ac[cellindex] sumarea = np.sum(area) depth = sun.dv[cellindex] volume = area*depth # Cell volume