def __init__(self, imp): self.ROOTDIR = os.path.join(os.environ['HOME'],'Studies/Masters/Myroms/Msc_idealized/') self.roms = RunSetup( os.path.join( self.ROOTDIR, "experiments.yaml"), imp ) grdfile = self.roms.retrive_grid('grd') self.Mr, self.Lr = grdfile['lat_rho'].shape # Number of J/I-direction INTERIOR RHO-points self.Mu, self.Lu = grdfile['lat_u'].shape # Number of J/I-direction U-points self.Mv, self.Lv = grdfile['lat_v'].shape # Number of J/I-direction V-points self.h = grdfile['h'] ################################################################################ # Date/Time variables ################################################################################ self.dstart = 0 tdays = (self.roms.ntimes*self.roms.dt)/(60*60*24) self.smstime = range(0, tdays+2) self.ndays = len(self.smstime) ################################################################################ # Thermohaline field variables - SISPRES TS CLIMATOLOGY FIELDS ################################################################################ lims = [-43.80, -42.00, -24.25, -22.65] DATASETdir = '/Users/Phellipe/Studies/Masters/Datasets/' tsclim_file = '%sClimatology/rio_ts_climatology.nc'%DATASETdir self.tsclim = xray.open_dataset( tsclim_file )\ .sel(y=slice(lims[2],lims[3]), x=slice(lims[0], lims[1])) self.x = self.tsclim.coords['x'].values self.y = self.tsclim.coords['y'].values
#! /usr/bin/python import os import sys import argparse from myromstools import RunSetup ####################################################################################### parser = argparse.ArgumentParser(description="Prepare a ROMS simulation environment.") parser.add_argument("imp", metavar="imp", type=str, help="ROMS implementation name: Ex: [upwelling, msc01 ...]") args = parser.parse_args() ####################################################################################### roms = RunSetup("experiments.yaml", args.imp) #################################################################### ## handling experiments' environments #################################################################### if os.path.isdir(roms.IMPDIR): overwrite = raw_input( "\n\n\t%s environment already exists.\ \n\tAre you sure you want to overwrite? [yes/no]\n" % args.imp.upper() ) if overwrite == "y" or overwrite == "yes": roms.build_env() elif overwrite == "n" or overwrite == "no": sys.exit() else:
################################################################################ parser = argparse.ArgumentParser(description='Makes a ROMS grid netcdf file.') parser.add_argument('imp', metavar='imp', type=str, help='ROMS implementation name: Ex: [nsea, makas]') args = parser.parse_args() ################################################################################ # SCRIPT START ###################################################### # Basic Settings: print ' \n' + '==> ' + ' READING ASCII METADATA FILE ...\n' + ' ' ROOTDIR = os.path.join(os.environ['HOME'],'Studies/Masters/Myroms/Msc_idealized/') roms = RunSetup(os.path.join( ROOTDIR, "experiments.yaml"), args.imp) # defining functions instead of importing roms.core # from roms.core import get_zlev, stretching VTRANSFORM = 2 # ROMS default vertical transformation equation VSTRETCHING = 4 # ROMS default vertical stretching function def stretching(sc, Vstretching, theta_s, theta_b): """ Computes S-coordinates INPUT: sc : normalized levels [ndarray] Vstretching : ROMS stretching algorithm [int] theta_s : [int]
args = parser.parse_args() ################################################################################ ## SCRIPT START ################################################################################ # Basic Settings: ROOTDIR = os.path.join(os.environ["HOME"], "Studies/Masters/Myroms/Msc_idealized/") ################################################################################ ## READING PREVIOUSLY BUILT RELEVANT FILES ################################################################################ print " \n" + "==> " + " READING ASCII METADATA FILE ...\n" + " " roms = RunSetup(os.path.join(ROOTDIR, "experiments.yaml"), args.imp) print " \n" + "==> " + " RUNNING FORCING CODE FROM *> RomsFields class <* ...\n" + " " fields = RomsFields(args.imp) sustr, svstr = eval("fields.%s()" % roms.smflux) sms_time = fields.smstime print len(sms_time) ################################################################################ ## WRITING NETCDF GRID FILE ## Based on "sms_uvstress.cdl" NETCDF sample structure ################################################################################ print " \n" + "==> " + " WRITING NETCDF Frc FILE ...\n" + " " filetypestr = "ROMS Forcing file - %s" % args.imp
args = parser.parse_args() ################################################################################ # SCRIPT START ###################################################### # Basic Settings: # READING PREVIOUSLY BUILT RELEVANT FILES: ########################### # metadata ascii file # grid netcdf file print ' \n' + '==> ' + ' READING ASCII METADATA FILE ...\n' + ' ' ROOTDIR = os.path.join(os.environ['HOME'],'Studies/Masters/Myroms/Msc_idealized') roms = RunSetup(os.path.join( ROOTDIR, "experiments.yaml"), args.imp) print ' \n' + '==> ' + ' READING RomsFields Class TS arrays ...\n' + ' ' fields = RomsFields( args.imp ) # assigning some variables from data file # temp, salt, Zlev, lon, lat = fields.ts_initial_1() # Zlev = np.negative( np.abs(Zlev).ravel() ) # N1 = Zlev.size datafile = sp.loadmat('/Users/Phellipe/Studies/Undergrad/IEAPM/Myroms_ieapm/piloto/init/climatology.mat') temp = datafile['temp'][:15] # January Climatology salt = datafile['salt'][:15] Zlev = datafile['z'][:15].ravel(); Zlev = np.abs(Zlev); Zlev = -Zlev
class RomsFields(object): """ROMS initial fields""" def __init__(self, imp): self.ROOTDIR = os.path.join(os.environ['HOME'],'Studies/Masters/Myroms/Msc_idealized/') self.roms = RunSetup( os.path.join( self.ROOTDIR, "experiments.yaml"), imp ) grdfile = self.roms.retrive_grid('grd') self.Mr, self.Lr = grdfile['lat_rho'].shape # Number of J/I-direction INTERIOR RHO-points self.Mu, self.Lu = grdfile['lat_u'].shape # Number of J/I-direction U-points self.Mv, self.Lv = grdfile['lat_v'].shape # Number of J/I-direction V-points self.h = grdfile['h'] ################################################################################ # Date/Time variables ################################################################################ self.dstart = 0 tdays = (self.roms.ntimes*self.roms.dt)/(60*60*24) self.smstime = range(0, tdays+2) self.ndays = len(self.smstime) ################################################################################ # Thermohaline field variables - SISPRES TS CLIMATOLOGY FIELDS ################################################################################ lims = [-43.80, -42.00, -24.25, -22.65] DATASETdir = '/Users/Phellipe/Studies/Masters/Datasets/' tsclim_file = '%sClimatology/rio_ts_climatology.nc'%DATASETdir self.tsclim = xray.open_dataset( tsclim_file )\ .sel(y=slice(lims[2],lims[3]), x=slice(lims[0], lims[1])) self.x = self.tsclim.coords['x'].values self.y = self.tsclim.coords['y'].values #################################################################################### ## Topo/Bathymetry ## #################################################################################### def topo_1(self): ''' Docstring....... Describe the bathy that's written here ''' if code == 'py': global h, h_min, h_max h_min = 5.0 h_max = 200.0 x_len = M h = np.zeros((L, M)) dx = ( h_min/h_max-1) / (x_len-1) for j in range(M-2): topo = h_max * ( 1 + (j*dx) ) for i in range(L): h [i, j] = topo h = h.transpose() return h # Continent (mask) width (value must be in grid points) # x_len = self.Mr # maskw = 5 # = 5*dl = 5km # h = np.ones((self.Mr, self.Lr))*-1 # dx = (h_min/h_max-1) / (x_len-1) # for j in range(self.Mr-maskw): # topo = h_max * ( 1 + (j*dx) ) # for i in range(self.Lr): # h [j,i] = topo # return h def topo_2(self): ''' Docstring....... Describe the bathy that's written here ''' if code == 'py': depth = 200.0 h = np.zeros((L, M)) for j in range(1, M+1): val1 = Mm+1-j val2 = min(depth, 84.5+70*np.tanh( (val1-15.0)/10.0) ) for i in range(1, L+1): h [i-1, j-1] = val2 h = h.transpose() return h def topo_3(self): ''' Docstring....... Describe the bathy that's written here ''' # for cape-like experiment horizontal resolution must be: dx=dy!!!! if code == 'py': Lm = 300 # ! Number of I-direction INTERIOR RHO-points Mm = 120 # ! Number of J-direction INTERIOR RHO-points N = 20 # ! Number of vertical levels L = Lm + 2 M = Mm + 2 hmin = 5.0 hmax = 200.0 coast = 100 # Coast position - grid point at M (lat) h = np.zeros( (L, coast) ) dx = ( hmin/hmax - 1 ) / (coast-1) for j in range(1, coast+1): topo = hmax * ( 1 + ( (j-1)*dx) ) for i in range(1, L+1): h [i-1, j-1] = topo h = h.transpose() H = np.zeros((M,L)) for m in range(coast): H[m, 0 : L - m] = h[:,0][m] H[m: L, (m*-1)-1] = h[:,0][m] #################################################################################### ## Thermohaline Field ## #################################################################################### def ts_initial_1(self): lonc, latc = np.meshgrid(self.x, self.y) zlev = self.tsclim['zlev'][:] zarr = np.where( zlev <= abs( self.h.values.max() ) ) Zlev = zlev[zarr] temp = self.tsclim['temp'][0,:,:,:][zarr].values salt = self.tsclim['salt'][0,:,:,:][zarr].values # Tclim = np.ones((temp.shape[0], self.Mr, self.Lr)) # Sclim = np.ones((temp.shape[0], self.Mr, self.Lr)) Tclim = np.ones((temp.shape[0], self.y.size, self.x.size)) Sclim = np.ones((temp.shape[0], self.y.size, self.x.size)) for n in range(temp.shape[0]): # TEMP ty = [ temp[n,:,x].mean(axis=0) for x in range(temp.shape[2]) ][0] tx = [ temp[n,y,:].mean(axis=0) for y in range(temp.shape[1]) ][0] tmean = np.mean([tx,ty]) Tclim[n,:,:] = Tclim[n,:,:]*tmean #SALT sy = [ salt[n,:,x].mean(axis=0) for x in range(salt.shape[2]) ][0] sx = [ salt[n,y,:].mean(axis=0) for y in range(salt.shape[1]) ][0] smean = np.mean([sx,sy]) Sclim[n,:,:] = Sclim[n,:,:]*smean return Tclim, Sclim, Zlev.values, lonc, latc #################################################################################### ## Winds ## #################################################################################### def smflux_1(self): ''' Docstring....... Describe the wind field that's is writen here ''' ramp = 2 # ramping days mxst = - 0.1 # max. stress (N/m2) sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): if self.smstime[k]-self.dstart <= ramp: Uwindamp = mxst*np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) else: Uwindamp = mxst for ju in range(self.Mu): for iu in range(self.Lu): sustr[k,ju,iu] = Uwindamp return sustr, svstr def smflux_2(self): ''' Docstring....... Describe the wind field that's is writen here ''' ramp = 5 # ramping days sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): for i in range(self.Lu): if self.smstime[k]-self.dstart <= ramp: if i <= self.Lu/2: windamp = 0.07*np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) else: windamp = -0.1*np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) else: if i <= self.Lu/2: windamp = 0.07 else: windamp = -0.1 for j in range(self.Mu): sustr[k,j,i] = windamp sustr = np.transpose(sustr, (0,2,1)) return sustr, svstr def smflux_3(self): ''' Docstring....... Describe the wind field that's is writen here ''' ramp = 5 sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): for i in range(self.Lu): if self.smstime[k]-self.dstart <= ramp: windamp = -0.1*np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) elif self.smstime[k]-self.dstart > 5 and self.smstime[k]-self.dstart <= 15: if i <= self.Lu/2: windamp = 0.1*(-np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0))) else: windamp = -0.1 else: if i <= self.Lu/2: windamp = 0.1 else: windamp = -0.1 for j in range(self.Mu): sustr[k,j,i]=windamp sustr = np.transpose(sustr,(0,2,1)) return sustr, svstr def smflux_4(self): ''' Docstring....... Describe the wind field that's is writen here ''' ramp = 2 # ramping days #upwelling-favourable wind (E) mxstE = -0.1 # max. stress (N/m2) #downwelling-favourable wind (W) mxstW = 0.07 # max. stress (N/m2) sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): for i in range(self.Lu): # West Domain if i <= self.Lu/2: windamp = mxstE * np.sin( np.pi*(self.smstime[k]-self.dstart)/10.0 ) if (self.smstime[k]-self.dstart) >= ramp and (self.smstime[k]-self.dstart) <= 10.0: windamp = mxstE elif (self.smstime[k]-self.dstart) > 10.0 and (self.smstime[k]-self.dstart) <= 15.0: windamp = np.linspace(mxstE+0.05, mxstW, 5)[k-10] elif (self.smstime[k]-self.dstart) > 15.0: windamp = mxstW * np.cos( np.pi*(self.smstime[k]-self.dstart-0.8)/3.25 ) #East Domain elif i > self.Lu/2: windamp = mxstE * np.sin( np.pi*(self.smstime[k]-self.dstart)/10.0 ) if (self.smstime[k]-self.dstart) >= ramp and (self.smstime[k]-self.dstart) <= 11.0: windamp = mxstE elif (self.smstime[k]-self.dstart) > 11.0: windamp = -np.abs( mxstE * np.cos( np.pi*(self.smstime[k]-self.dstart+0.4)/6.0) ) for j in range(self.Mu): sustr[k,j,i]=windamp return sustr, svstr ############################## # Fields with ZONAL DIVERGENCE ############################## def smflux_5(self): ''' This wind field ramps from 0 to mxstW/mxstE in X days mantaining constant along the domain (ZERO at specified DEAD ZONE) until the end of model's integration. Its magnitude field consists in: Linear zonal wind divergence -- dTAUx/dx < 0 (convergence) ''' ramp = 2 # ramping days # wind stress magnitude (N/m2) Eustr = -0.07 # max. stress (N/m2) Wustr = -0.02 # max. stress (N/m2) sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): if self.smstime[k]-self.dstart <= ramp: windamp = np.linspace(Wustr, Eustr, self.Lu) * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) else: windamp = np.linspace(Wustr, Eustr, self.Lu) for j in range(self.Mu): for i in range(self.Lu): sustr[k,j,:] = windamp return sustr, svstr def smflux_6(self): ''' Periodic Relaxation of Easterly winds at 6 day frequency...''' ramp = 2 # ramping days #upwelling-favourable wind (W/E) Eustr = -0.07 # max. stress (N/m2) Wustr = -0.02 # max. stress (N/m2) freq = 7 # frequency of "frontal system" approach sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): if self.smstime[k]-self.dstart <= ramp: windamp = np.linspace(Wustr, Eustr, self.Lu) * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) # elif self.smstime[k]-self.dstart > ramp and self.smstime[k]-self.dstart <= ramp : # windamp = np.linspace(Wustr, Eustr, self.Lu) elif self.smstime[k]-self.dstart > ramp: sin = max( 0.0, Eustr * np.sin( np.pi*(self.smstime[k]-self.dstart)/(freq)) ) div = np.linspace(Wustr, Eustr, self.Lu) windamp = np.array( [ min(0.0, d+sin) for d in div ] ) for j in range(self.Mu): sustr[k,j,:] = windamp return sustr, svstr def smflux_7(self): ''' Latest code, frontal system arrivals seems more realistic...''' ramp = 2 # ramping days #upwelling-favourable wind (W/E) Eustr = -0.07 # max. stress (N/m2) Wustr = -0.02 # max. stress (N/m2) freq = 7 # frequency of "frontal system" approach sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): if self.smstime[k]-self.dstart <= ramp: windamp = np.linspace(Wustr, Eustr, self.Lu) * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) elif self.smstime[k]-self.dstart > ramp and self.smstime[k]-self.dstart <= ramp+5 : windamp = np.linspace(Wustr, Eustr, self.Lu) elif self.smstime[k]-self.dstart > ramp: sin = max( 0.0, -0.2 * np.sin( np.pi*(self.smstime[k]-self.dstart)/(freq)) ) div = np.linspace(Wustr, Eustr, self.Lu) windamp = np.array( [ min(0.1, d+sin) for d in div ] ) for j in range(self.Mu): sustr[k,j,:] = windamp return sustr, svstr def smflux_8(self): ''' Latest code, frontal system arrivals seems more realistic...''' ramp = 2 # ramping days #upwelling-favourable wind (W/E) Eustr = -0.07 # max. stress (N/m2) Wustr = -0.02 # max. stress (N/m2) freq = 7 # frequency of "frontal system" approach sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) for k in range(self.ndays): if self.smstime[k]-self.dstart <= ramp: windamp = np.linspace(Wustr, Eustr, self.Lu) * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) elif self.smstime[k]-self.dstart > ramp and self.smstime[k]-self.dstart <= ramp+5 : windamp = np.linspace(Wustr, Eustr, self.Lu) elif self.smstime[k]-self.dstart > ramp: sin = max( 0.0, 0.1*np.sin( np.pi*(self.smstime[k]-self.dstart)/(freq)) ) div = np.linspace(Wustr, Eustr, self.Lu) windamp = np.array( [ min(0.1, d+sin) for d in div ] ) for j in range(self.Mu): sustr[k,j,:] = windamp return sustr, svstr # def smflux_7(self): # ''' ... ''' # ramp = 10 # ramping days # #upwelling-favourable wind (E) # mxstE = -0.2 # max. stress (N/m2) # #downwelling-favourable wind (W) # mxstW = 0.07 # max. stress (N/m2) # freq = 6 # frequency of "frontal system" approach # sustr = np.zeros( (self.ndays, self.Mu, self.Lu ) ) # svstr = np.zeros( (self.ndays, self.Mv, self.Lv ) ) # for k in range(self.ndays): # for i in range(self.Lu): # # West Domain # if i <= self.Lu/2: # div = np.linspace(-0.02, -0.02, self.Lu/2) # windamp = div * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) # if (self.smstime[k]-self.dstart) > ramp: # sin = 0.08 * np.abs( np.sin( np.pi*(self.smstime[k]-self.dstart)/(freq)) ) # windamp = div + sin # for j in range(self.Mu): # sustr[k,j,:self.Lu/2] = windamp # #East Domain # elif i > self.Lu/2: # div = np.linspace(-0.02, mxstE, self.Lu/2) # windamp = div * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) # if (self.smstime[k]-self.dstart) > ramp: # windamp = div # for j in range(self.Mu): # sustr[k,j,self.Lu/2:] = windamp # return sustr, svstr ############################## # Realistic ASCAT Fields ############################## def smflux_10(self): ''' wind ramps for 2 days until intial state measured by ascat then keeps 5 days, with the same state, until the series begins to evolve ''' ### Reading Ascat U-wind stress for January 2015 lims = [-44.5, -42, -24, -23] ASCATdir = '/Users/Phellipe/Studies/Masters/Datasets/Winds/Ascat/' ASCATfile = os.path.join( ASCATdir, 'ascat_daily_2008-2015_pcse.nc') ascat = xray.open_dataset(ASCATfile).sel(time=slice('20150101','20150201'), latitude=slice(lims[2],lims[3]), longitude=slice(lims[0], lims[1]) ) lon, lat = ascat.coords['longitude'], ascat.coords['latitude'] ustr = ascat['surface_downward_eastward_stress'][:,0,:,:].values ### Compute and Interpolate wind field to model grid USTR = np.zeros( (ustr.shape[0], self.Mu, self.Lu) ) # Univariate interpolation: setting new shape for ustr array x = np.linspace(0, ustr.shape[2], ustr.shape[2]) xi = np.linspace(0, ustr.shape[2], self.Lu) for k in range(ustr.shape[0]): for m in range(self.Mu): # Meridionally averaged u-stress field over shelf Uymean = ustr[k,:,:].mean(axis=0) Uymeani = InterpolatedUnivariateSpline(x, Uymean) USTR[k,m,:] = Uymeani(xi) ### Ramping from zero to the initial state and kept for 5 days ### to spinup model upwelling ramp, spinup = 2, 7 spinup = spinup-ramp Uascat = USTR[0,:,:] Uramp = np.zeros( (spinup, self.Mu, self.Lu) ) for k in range(ramp): if self.smstime[k]-self.dstart <= ramp: Uramp[k,:,:] = Uascat * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) else: Uramp[k,:,:] = Uascat USTR = np.concatenate( [Uramp,USTR] ) VSTR = np.zeros( USTR.shape ) return USTR, VSTR def smflux_11(self): ''' ... ''' ### Reading Ascat U-wind stress for January-February 2015 lims = [-44.5, -42, -25, -23] ASCATdir = '/Users/Phellipe/Studies/Masters/Datasets/Winds/Ascat/' ASCATfile = os.path.join( ASCATdir, 'ascat_daily_2008-2015_pcse.nc') ascat = xray.open_dataset(ASCATfile).sel(time=slice('20141201','20150305'), latitude=slice(lims[2],lims[3]), longitude=slice(lims[0], lims[1]) ) lons = ascat.coords['longitude'] lats = ascat.coords['latitude'] ustr = ascat['surface_downward_eastward_stress'][:,0,:,:].values ### Compute and Interpolate wind field to model grid USTR = np.zeros( (ustr.shape[0], self.Mu, self.Lu) ) # Univariate interpolation: setting new shape for ustr array x = np.linspace(0, ustr.shape[2], ustr.shape[2]) xi = np.linspace(0, ustr.shape[2], self.Lu) for k in range(ustr.shape[0]): for m in range(self.Mu): # Meridionally averaged u-stress field over shelf Uymean = ustr[k,:,:].mean(axis=0) Uymeani = InterpolatedUnivariateSpline(x, Uymean) USTR[k,m,:] = Uymeani(xi) ### Ramping from zero to the initial state and kept for 5 days ### to spinup model upwelling ramp, spinup = 2, 7 spinup = spinup-ramp Uascat = USTR[0,:,:] Uramp = np.zeros( (spinup, self.Mu, self.Lu) ) for k in range(ramp): if self.smstime[k]-self.dstart <= ramp: Uramp[k,:,:] = Uascat * np.sin( np.pi*(self.smstime[k]-self.dstart)/(ramp*2.0)) else: Uramp[k,:,:] = Uascat USTR = np.concatenate( [Uramp,USTR] ) VSTR = np.zeros( (USTR.shape[0], self.Mv, self.Lv) ) return USTR, VSTR