#!/usr/bin/env python3 import sys from datetime import datetime from time import time import argparse import numpy as np import numpy.ma as ma import os from netCDF4 import Dataset def get_zcor_interp_coefficient(zcor, zinter, kbp): ''' inputs: -zcor: zcor[np,nvrt] for each time record. -zinter: zinter[np] depth where currents will be interpolated at -kbp outputs: -k1[np]: integer, k-level at each node -coeff[np]: interpolation coefficient ''' #surface idxs=zinter>=zcor[:,-1] k1[idxs]=nvrt-2 coeff[idxs]=1.0 #bottom idxs=zinter=zcor[:,k])*(zinter10000)]=-99999 temp_sur[np.where(temp_sur>10000)]=-99999 salt_sur[np.where(salt_sur>10000)]=-99999 uvel_sur[np.where(uvel_sur>10000)]=-99999 vvel_sur[np.where(vvel_sur>10000)]=-99999 temp_bot[np.where(temp_bot>10000)]=-99999 salt_bot[np.where(salt_bot>10000)]=-99999 uvel_bot[np.where(uvel_bot>10000)]=-99999 vvel_bot[np.where(vvel_bot>10000)]=-99999 uvel_inter[np.where(uvel_inter>10000)]=-99999 vvel_inter[np.where(vvel_inter>10000)]=-99999 with Dataset(f"{results}/schout_2d_{sid}.nc", "w", format="NETCDF4") as fout: #dimensions fout.createDimension('time', None) fout.createDimension('nSCHISM_hgrid_node', NP) fout.createDimension('nSCHISM_hgrid_face', NE) fout.createDimension('nMaxSCHISM_hgrid_face_nodes', NV) #variables fout.createVariable('time', 'f', ('time',)) fout['time'].long_name="Time" fout['time'].units = f'seconds since {startdate.year}-{startdate.month:02d}-{startdate.day:02d} {startdate.hour:02d}:00:00 UTC' fout['time'].base_date=f'{startdate.year}-{startdate.month:02d}-{startdate.day:02d} {startdate.hour:02d}:00:00 UTC' fout['time'].standard_name="time" fout['time'][:] = times fout.createVariable('SCHISM_hgrid_node_x', 'f8', ('nSCHISM_hgrid_node',)) fout['SCHISM_hgrid_node_x'].long_name="node x-coordinate" fout['SCHISM_hgrid_node_x'].standard_name="longitude" fout['SCHISM_hgrid_node_x'].units="degrees_east" fout['SCHISM_hgrid_node_x'].mesh="SCHISM_hgrid" fout['SCHISM_hgrid_node_x'][:]=x fout.createVariable('SCHISM_hgrid_node_y', 'f8', ('nSCHISM_hgrid_node',)) fout['SCHISM_hgrid_node_y'].long_name="node y-coordinate" fout['SCHISM_hgrid_node_y'].standard_name="latitude" fout['SCHISM_hgrid_node_y'].units="degrees_north" fout['SCHISM_hgrid_node_y'].mesh="SCHISM_hgrid" fout['SCHISM_hgrid_node_y'][:]=y fout.createVariable('SCHISM_hgrid_face_nodes', 'i', ('nSCHISM_hgrid_face','nMaxSCHISM_hgrid_face_nodes',)) fout['SCHISM_hgrid_face_nodes'].long_name="element" fout['SCHISM_hgrid_face_nodes'].standard_name="face_node_connectivity" fout['SCHISM_hgrid_face_nodes'].start_index=1 fout['SCHISM_hgrid_face_nodes'].units="nondimensional" fout['SCHISM_hgrid_face_nodes'][:]=np.array(tris) fout.createVariable('depth', 'f', ('nSCHISM_hgrid_node',)) fout['depth'].long_name="bathymetry" fout['depth'].units="m" fout['depth'].mesh="SCHISM_hgrid" fout['depth'][:]=depth fout.createVariable('elev', 'f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['elev'].long_name="water elevation" fout['elev'].units="m" fout['elev'].mesh="SCHISM_hgrid" #fout['elev'].missing_value=np.nan fout['elev'][:,:]=elev2d fout.createVariable('temp_surface','f8', ('time', 'nSCHISM_hgrid_node',),fill_value=-99999) fout['temp_surface'].long_name="sea surface temperature" fout['temp_surface'].units="deg C" #fout['temp'].missing_value=np.nan fout['temp_surface'][:,:]=temp_sur fout.createVariable('temp_bottom','f8', ('time', 'nSCHISM_hgrid_node',),fill_value=-99999) fout['temp_bottom'].long_name="Bottom temperature" fout['temp_bottom'].units="deg C" #fout['temp'].missing_value=np.nan fout['temp_bottom'][:,:]=temp_bot fout.createVariable('salt_surface','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['salt_surface'].long_name="sea surface salinity" fout['salt_surface'].units="psu" #fout['salt'].missing_value=np.nan fout['salt_surface'][:,:]=salt_sur fout.createVariable('salt_bottom','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['salt_bottom'].long_name="Bottom salinity" fout['salt_bottom'].units="psu" #fout['salt'].missing_value=np.nan fout['salt_bottom'][:,:]=salt_bot fout.createVariable('uvel_surface','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['uvel_surface'].long_name="U-component at the surface" fout['uvel_surface'].units="m/s" #fout['uvel'].missing_value=np.nan fout['uvel_surface'][:,:]=uvel_sur fout.createVariable('vvel_surface','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['vvel_surface'].long_name="V-component at the surface" fout['vvel_surface'].units="m/s" #fout['vvel'].missing_value=np.nan fout['vvel_surface'][:,:]=vvel_sur fout.createVariable('uvel_bottom','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['uvel_bottom'].long_name="U-component at the bottom" fout['uvel_bottom'].units="m/s" #fout['uvel'].missing_value=np.nan fout['uvel_bottom'][:,:]=uvel_bot fout.createVariable('vvel_bottom','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['vvel_bottom'].long_name="V-component at the bottom" fout['vvel_bottom'].units="m/s" #fout['vvel'].missing_value=np.nan fout['vvel_bottom'][:,:]=vvel_bot fout.createVariable('uvel4.5','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['uvel4.5'].long_name="U-component at 4.5m below free surface" fout['uvel4.5'].units="m/s" #fout['uvel'].missing_value=np.nan fout['uvel4.5'][:,:]=uvel_inter fout.createVariable('vvel4.5','f8', ('time', 'nSCHISM_hgrid_node',), fill_value=-99999) fout['vvel4.5'].long_name="V-component at 4.5m below free surface" fout['vvel4.5'].units="m/s" #fout['vvel'].missing_value=np.nan fout['vvel4.5'][:,:]=vvel_inter fout.title = 'SCHISM Model output' fout.source = 'SCHISM model output version v10' fout.references = 'http://ccrm.vims.edu/schismweb/' print(f'It took {time()-t0} to interpolate')