Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmason committed Jun 7, 2016
1 parent 4e74ed4 commit 91c0db4
Show file tree
Hide file tree
Showing 21 changed files with 13,101 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
README for py-roms2roms

Version 1.0.1

py-roms2roms is a Python version of roms2roms (see Mason etal, 2010 and
www.molemaker.org) that was written in Matlab.

pyroms2roms creates ROMS boundary files for offline nesting, but it has
expanded to make:

(1) boundary files with SODA, Mercator, ECCO2, IBI
(2) forcing files using CFSR and/or CCMP.

Copyright (c) 2014 by Evan Mason, IMEDEA
Email: [email protected]
164 changes: 164 additions & 0 deletions add_bry_flux_correction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# %run add_bry_flux_correction.py

'''
===========================================================================
This file is part of py-roms2roms
py-roms2roms is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
py-roms2roms is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with py-roms2roms. If not, see <http://www.gnu.org/licenses/>.
Version 1.0.1
Copyright (c) 2014 by Evan Mason, IMEDEA
Email: [email protected]
===========================================================================
Create a forcing file based on six hourly CFSR data
===========================================================================
'''


import netCDF4 as netcdf
import matplotlib.pyplot as plt
import numpy as np
import time as time

from py_roms2roms import RomsGrid





if __name__ == '__main__':

# ROMS information
roms_dir = '/marula/emason/runs2014/nwmed5km/'


#bry_file = 'bry_nwmed5km_CORR_0pt2.nc'
#bry_file = 'bry_nwmed5km_VARCORR_0pt2.nc'
bry_file = 'bry_nwmed5km_weekly_clim_VARCORR_0pt2.nc'


#roms_grd = 'grd_nwmed5km.nc'
roms_grd = 'grd_nwmed5km_NARROW_STRAIT.nc'

variable_correction = True

flux_correction = 0.2 # Sv

open_boundary = 'east'


if 'roms_grd_NA2014_7pt5km.nc' in roms_grd:
sigma_params = dict(theta_s=6, theta_b=0, hc=120, N=32)
obc_dict = dict(south=1, east=1, north=1, west=1) # 1=open, 0=closed

elif 'grd_nwmed_2km.nc' in roms_grd:
sigma_params = dict(theta_s=6.5, theta_b=0, hc=110, N=36)
obc_dict = dict(south=1, east=1, north=1, west=1)

elif 'grd_na6km.nc' in roms_grd:
sigma_params = dict(theta_s=6., theta_b=0, hc=120, N=32)
obc_dict = dict(south=1, east=1, north=1, west=1)

elif 'grd_nwmed5km.nc' in roms_grd:
sigma_params = dict(theta_s=6.5, theta_b=0, hc=110, N=36)
obc_dict = dict(south=0, east=1, north=1, west=1)

elif 'grd_nwmed5km_NARROW_STRAIT.nc' in roms_grd:
sigma_params = dict(theta_s=6.5, theta_b=0, hc=110, N=36)
obc_dict = dict(south=0, east=1, north=1, west=1)

else:
print 'No sigma parameters defined for grid: %s' %roms_grd
raise Exception




# Set up a RomsGrid object
romsgrd = RomsGrid(''.join((roms_dir, roms_grd)), sigma_params, 'ROMS')
romsgrd.set_bry_dx()
romsgrd.set_bry_maskr()
romsgrd.set_bry_areas()

if variable_correction:

if 'west' in open_boundary:
bry_surface_area = romsgrd.area_west * romsgrd.maskr_west
elif 'east' in open_boundary:
bry_surface_area = romsgrd.area_east * romsgrd.maskr_east
elif 'south' in open_boundary:
bry_surface_area = romsgrd.area_south * romsgrd.maskr_south
elif 'north' in open_boundary:
bry_surface_area = romsgrd.area_north * romsgrd.maskr_north

else:

if 'west' in open_boundary:
bry_surface_area = romsgrd.area_west.sum(axis=0) * romsgrd.maskr_west
elif 'east' in open_boundary:
bry_surface_area = romsgrd.area_east.sum(axis=0) * romsgrd.maskr_east
elif 'south' in open_boundary:
bry_surface_area = romsgrd.area_south.sum(axis=0) * romsgrd.maskr_south
elif 'north' in open_boundary:
bry_surface_area = romsgrd.area_north.sum(axis=0) * romsgrd.maskr_north

# Velocity correction
correction = flux_correction * 1e6 / np.sum(bry_surface_area)


with netcdf.Dataset(roms_dir + bry_file, 'a') as nc:

bry_time = nc.variables['bry_time'][:]
tsize = bry_time.size
for tind in np.arange(tsize):
print tind, tsize

if variable_correction:

u_mask = nc.variables['u_%s' %open_boundary][tind]
u_mask = np.ma.masked_greater(u_mask, 0).mask

correction = flux_correction * 1e6 / np.sum(bry_surface_area * u_mask)

u = nc.variables['u_%s' %open_boundary][tind]
print correction
u[u_mask] += correction
nc.variables['u_%s' %open_boundary][tind] = u
ubar = np.sum(u * bry_surface_area / romsgrd.dx_east, axis=0)
ubar /= romsgrd.h()[:,-1]
nc.variables['ubar_%s' %open_boundary][tind] = ubar

else:

nc.variables['u_%s' %open_boundary][tind] += correction
nc.variables['ubar_%s' %open_boundary][tind] += correction

nc.variables['u_%s' %open_boundary].variable_flux_correction = correction
nc.variables['ubar_%s' %open_boundary].variable_flux_correction = correction

print 'done'










179 changes: 179 additions & 0 deletions diag_frc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# -*- coding: utf-8 -*-
# %run diag_frc.py

'''
Compute annual means of two forcing files for comparison
'''

import netCDF4 as netcdf
import pylab as plt
import numpy as np

def u2rho_2d(uu_in):
'''
Convert a 2D field at u points to a field at rho points
Checked against Jeroen's u2rho.m
'''
def uu2ur(uu_in, Mp, Lp):
L = Lp - 1
Lm = L - 1
u_out = np.zeros((Mp, Lp))
u_out[:, 1:L] = 0.5 * (uu_in[:, 0:Lm] + \
uu_in[:, 1:L])
u_out[:, 0] = u_out[:, 1]
u_out[:, L] = u_out[:, Lm]
return (np.squeeze(u_out))
# First check to see if has time dimension
if uu_in.ndim < 3:
# No time dimension
Mshp, Lshp = uu_in.shape
u_out = uu2ur(uu_in, Mshp, Lshp+1)
else:
# Has time dimension
time, Mshp, Lshp = uu_in.shape
u_out = np.zeros((time, Mshp, Lshp+1))
for t in np.arange(time):
u_out[t] = uu2ur(uu_in[t], Mshp, Lshp+1)
return u_out


def v2rho_2d(vv_in):
# Convert a 2D field at v points to a field at rho points
def vv2vr(vv_in, Mp, Lp):
M = Mp - 1
Mm = M - 1
v_out = np.zeros((Mp, Lp))
v_out[1:M, :] = 0.5 * (vv_in[0:Mm, :] + \
vv_in[1:M, :])
v_out[0, :] = v_out[1, :]
v_out[M, :] = v_out[Mm, :]
return (np.squeeze(v_out))
# First check to see if has time dimension
if vv_in.ndim < 3:
# No time dimension
Mshp, Lshp = vv_in.shape
v_out = vv2vr(vv_in, Mshp+1, Lshp)
else:
# Has time dimension
time, Mshp, Lshp = vv_in.shape
v_out = np.zeros((time, Mshp+1, Lshp))
for t in np.arange(time):
v_out[t] = vv2vr(vv_in[t], Mshp+1, Lshp)
return v_out






def get_frc(directory, frcname, varname):
'''
'''
nc = netcdf.Dataset(directory + frcname)
#lon = nc.variables['lon_rho'][:]
#lat = nc.variables['lat_rho'][:]
if 'sustr' in varname or 'svstr' in varname:
time = nc.variables['sms_time'][:]
elif 'shflux' in varname:
time = nc.variables['shf_time'][:]
elif 'swflux' in varname:
time = nc.variables['swf_time'][:]
elif 'SST' in varname or 'dQdSST' in varname:
time = nc.variables['sst_time'][:]
elif 'SSS' in varname:
time = nc.variables['sss_time'][:]
elif 'srflux' in varname or 'swrad' in varname:
time = nc.variables['srf_time'][:]

#print time
if 'sustr' in varname:
var = 0. * u2rho_2d(nc.variables[varname][0])
elif 'svstr' in varname:
var = 0. * v2rho_2d(nc.variables[varname][0])
else:
var = 0. * nc.variables[varname][0]
ind = 0

for tout in time:
#print ind
if 'sustr' in varname:
var += u2rho_2d(nc.variables[varname][ind])
elif'svstr' in varname:
var += v2rho_2d(nc.variables[varname][ind])
else:
var += nc.variables[varname][ind]
ind += 1

nc.close()
var /= ind
print 'Averaged over', ind, 'records'

return var




plt.close('all')

directory1 = '/home/emason/runs2012_tmp/MedSea5_R2.5/'
directory2 = '/shared/emason/marula/emason/runs2012/MedSea5/'

file1 = 'frc_intann_MedSea5.nc.TEST'
file2 = 'frc_MedSea5.nc'


grd = 'grd_MedSea5_R2.5.nc'

#var = 'sustr'
#var = 'svstr'
#var = 'shflux'
#var = 'swflux'
var = 'SST'
#var = 'SSS'
#var = 'dQdSST'
#var = 'swrad'



#----------------------------------------------------------------

nc = netcdf.Dataset(directory1 + grd)
lon = nc.variables['lon_rho'][:]
lat = nc.variables['lat_rho'][:]
mask = nc.variables['mask_rho'][:]
nc.close()

var1 = get_frc(directory1, file1, var)
var2 = get_frc(directory2, file2, var)

var1 = np.ma.masked_where(mask == 0, var1)
var2 = np.ma.masked_where(mask == 0, var2)

vmin = np.ma.minimum(var1.min(), var2.min())
vmax = np.ma.maximum(var1.max(), var2.max())


plt.figure()
plt.title(file1)
plt.pcolormesh(lon, lat, var1)
plt.axis('image')
plt.clim(vmin, vmax)
plt.colorbar()


plt.figure()
plt.title(file2)
plt.pcolormesh(lon, lat, var2)
plt.axis('image')
plt.clim(vmin, vmax)
plt.colorbar()


plt.show()







Loading

0 comments on commit 91c0db4

Please sign in to comment.