regriding tideamp.nc MOM4p1

using TPX7.2 OSU Tidal Data Inversion form Oregon as a example

If you have problems to regrid the tideamp.nc file, here I present a solution to this problem.
More info please contact me: Mabel Calim Costa - mabelcalim@gmail.com

The solution is simple. I create a new netcdf file where I allocate the tide amplitude information with new coordinates. So let's get started!


In [24]:
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
import os
import sys
import netCDF4

Create a new netcdf file - tideamp_core1.nc

  • first - regrid the original tideamp.nc from om3_core1 for you own grid_spec.nc
  • then - create a new netcdf change the name of the variable amp to tideamp!

Note that the key variable is 'tideamp', shown below.


In [25]:
# find the path where is your old tideamp.nc from OM3_CORE1 and regrid it!. 
data = netCDF4.Dataset('/path/tideamp.nc','r+') # regrided om3_core1 tideamp.nc for your own grid
#data.variables
data_amp = netCDF4.Dataset('/path/tideamp_m2.nc','r+') # M2 amplitude from tidal model with 1/4 degree of resolution

tideamp have to be the name of the variable! Otherwise it will not be read by MOM4p1.


In [29]:
# create fine netcdf
ncfile = netCDF4.Dataset('tideamp_m2_new.nc','w')
print "-- Open file"

lat_dim = ncfile.createDimension('grid_y_T',len(data.variables['grid_y_T'][:]))   # latitude axis
lon_dim = ncfile.createDimension('grid_x_T',len(data.variables['grid_x_T'][:]))     # longitude axis
x_T_dim = ncfile.createDimension('x_T',len(data.variables['x_T'][:]))
y_T_dim = ncfile.createDimension('y_T',len(data.variables['y_T'][:]))
#wet = ncfile.createDimension('wet',len(data.variables['wet'][:]))

# Define two variables with the same names as dimensions,
# a conventional way to define "coordinate variables".
grid_x_T = ncfile.createVariable('grid_x_T', 'f4', ('grid_x_T',))
grid_x_T.units = 'degrees_east'
grid_x_T.long_name = 'Nominal Longitude of T-cell center'
grid_x_T.cartesian_axis = "X"
grid_x_T[:] = data.variables['grid_x_T'][:]

grid_y_T = ncfile.createVariable('grid_y_T', 'f4', ('grid_y_T',))
grid_y_T.units = 'degrees_north'
grid_y_T.long_name = 'Nominal Latitude of T-cell center'
grid_y_T.cartesian_axis = "Y"
grid_y_T[:] = data.variables['grid_y_T'][:]

x_T = ncfile.createVariable('x_T', 'f4', ('grid_y_T', 'grid_x_T'))
x_T.units = 'degrees_east'
x_T.long_name = 'Geographic longitude of T_cell centers'
x_T[:] = data.variables['x_T'][:]

y_T = ncfile.createVariable('y_T', 'f4', ('grid_y_T', 'grid_x_T'))
y_T.units = 'degree_north'
y_T.long_name = 'Geographic latitude of T_cell centers'
y_T[:] = data.variables['y_T'][:]


#

print "-- Create coordinates"
 
# Define a 3D variable to hold the data
amp = ncfile.createVariable('tideamp','f4',('grid_y_T', 'grid_x_T'))
amp.units = 'meters'
amp.long_name = 'tidal M2 component amplitude'
amp.missing_value = -1.e+36
# writing data

amp[:,:] = data_amp.variables['amp'][:-1,:]     #adjusting the TPXO7.2 OSU Tidal Data to the new grid
print "-- Create tideamp variable"

ncfile.close()
print "-- Created file"


-- Open file
-- Create coordinates
-- Create tideamp variable
-- Created file