Skip to content

Instantly share code, notes, and snippets.

View adcroft's full-sized avatar

Alistair Adcroft adcroft

View GitHub Profile
@adcroft
adcroft / calc_total_TKE.py
Last active August 29, 2015 13:57
Diagnose total TKE input to internal tide mixing parameterization in MOM6
import netCDF4
import numpy
tkeFile = netCDF4.Dataset('/archive/bls/tikal_mom6_2014.01.17/MOM6z_SIS_025_enstro_newTides/gfdl.ncrc2-intel-prod/history/unpack/19000101.ocean_annual.nc')
TKEi = numpy.ma.array( tkeFile.variables['TKE_itidal'][0], dtype='float64')
TKEf = numpy.ma.array( tkeFile.variables['TKE_tidal'][0], dtype='float64')
staticFile = netCDF4.Dataset('/net2/aja/OWG/ocean_static.nc')
area = numpy.ma.array( staticFile.variables['area_t'][:], dtype='float64')
depth = numpy.ma.array( staticFile.variables['depth_ocean'][:], dtype='float64')
@adcroft
adcroft / demo_plot_MOM6_SST.py
Last active August 29, 2015 13:57
An example interactive python session to plot SST from MOM6 post-processed files, using just matplotlib and netCDF4
# Import necessary modules
import netCDF4
import matplotlib.pyplot as plt
tFiles = netCDF4.MFDataset('/archive/bls/tikal_201406_mom6_2014.07.01/OM4_SIS_baseline/gfdl.ncrc2-intel-prod/pp/ocean_monthly/ts/monthly/10yr/ocean_monthly.*.temp.nc')
print tFiles
tType = tFiles.variables['temp']
print tType
print tType.shape
@adcroft
adcroft / demo_zonal_average_T_difference.py
Created March 31, 2014 16:00
An example of a zonal average plot of potential temperature difference between two runs.
import netCDF4
import numpy
import matplotlib
import matplotlib.pyplot as plt
area = netCDF4.Dataset('/net2/aja/OWG/ocean_static.nc').variables['area_t'][:]
lat = netCDF4.Dataset('/net2/aja/OWG/ocean_static.nc').variables['geolat_c'][:]
T1 = netCDF4.Dataset('/archive/bls/tikal_mom6_2014.01.17/MOM6z_SIS_025_enstro_iTides_1TW/gfdl.ncrc2-intel-prod/pp/ocean_annual/av/annual_20yr/ocean_annual.1900-1919.ann.nc').variables['temp'][0,:]
T2 = netCDF4.Dataset('/archive/bls/tikal_mom6_2014.01.17/MOM6z_SIS_025_enstro_rev5channels/gfdl.ncrc2-intel-prod/pp/ocean_annual/av/annual_20yr/ocean_annual.1900-1919.ann.nc').variables['temp'][0,:]
zInterface = netCDF4.Dataset('/archive/bls/tikal_mom6_2014.01.17/MOM6z_SIS_025_enstro_iTides_1TW/gfdl.ncrc2-intel-prod/pp/ocean_annual/av/annual_20yr/ocean_annual.1900-1919.ann.nc').variables['e'][0,:]
@adcroft
adcroft / plot_zonal_average_T_anomaly.py
Last active August 29, 2015 13:57
An example of plotting the zonal average T anomaly w.r.t. WOA2005 climatology
import netCDF4
import numpy
import matplotlib
import matplotlib.pyplot as plt
[ny,nx] = netCDF4.Dataset('/archive/gold/datasets/OM4_025/mosaic.v20140610.unpacked/ocean_topog.nc').variables['depth'].shape
area = netCDF4.Dataset('/archive/gold/datasets/OM4_025/mosaic.v20140610.unpacked/ocean_hgrid.nc').variables['area'][:].reshape(ny,2,nx,2).sum(axis=-1).sum(axis=1)
lat = netCDF4.Dataset('/archive/gold/datasets/OM4_025/mosaic.v20140610.unpacked/ocean_hgrid.nc').variables['y'][1::2,1::2]
annualFile = '/archive/bls/tikal_201406_mom6_2014.07.01/OM4_SIS_baseline/gfdl.ncrc2-intel-prod/pp/ocean_annual/av/annual_5yr/ocean_annual.1900-1904.ann.nc'
@adcroft
adcroft / plot_tidal_amplitudes.py
Created April 1, 2014 12:30
Plots versions of tidal amplitude file
import netCDF4
import numpy
import m6plot
lon = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['x'][::2,::2]
lat = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['y'][::2,::2]
area = netCDF4.Dataset('/net2/aja/OWG/ocean_static.nc').variables['area_t'][:]
U26 = netCDF4.Dataset('/net2/aja/MOM6.workdir/MOM6/tools/python/tideamp/tidal_amplitude.v20140326.nc').variables['tideamp'][:]
U28 = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/INPUT/tidal_amplitude.v20140328.nc').variables['tideamp'][:]
@adcroft
adcroft / demo_yz_section.py
Created April 15, 2014 20:55
An example of minimal y-z section of temperature at a given longitude.
import netCDF4
import numpy
import matplotlib.pyplot as plt
rootDir = '/archive/bls/tikal_201403_mom6_2014.04.03/MOM6z_SIS_025_PPMh4/gfdl.ncrc2-intel-prod/'
# Read nominal longitudes (to find the section)
xh = netCDF4.Dataset(rootDir+'history/unpack/19300101.ocean_annual.nc').variables['xh'][:]
# Find the index of longitude=-30.0
iSect = numpy.abs(xh + 30.0).argmin()
@adcroft
adcroft / demo_ssh_blue_marble.py
Last active August 29, 2015 14:00
Demo of SSH using a basemap projection and "blue marble" image for land
import netCDF4
import matplotlib.pyplot as plt
import numpy
from mpl_toolkits.basemap import Basemap
lon = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['x'][::2,::2]
lat = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['y'][::2,::2]
ssh = netCDF4.Dataset('/archive/aja/awg/tikal_201403/MOM6z_SIS_025_c192L48_am4a1_2000climo_bergs/gfdl.ncrc2-intel-prod-openmp/history/unpack/00040301.ocean_daily.nc').variables['ssh'][1]
@adcroft
adcroft / demo_animate_ssh_blue_marble.py
Last active August 29, 2015 14:00
Animated SSH on blue marble
import netCDF4
import matplotlib.pyplot as plt
import numpy
from mpl_toolkits.basemap import Basemap
lon = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['x'][::2,::2]
lat = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['y'][::2,::2]
dailyFiles = '/archive/aja/awg/tikal_201403/CM4_c192L48_am4a1_2000/gfdl.ncrc2-intel-prod-openmp/history/unpack/000[3456]0101.ocean_daily.nc'
ssh = netCDF4.MFDataset(dailyFiles).variables['ssh']
@adcroft
adcroft / demo_animate_relative_vortcitiy.py
Last active August 29, 2015 14:00
Animate surface relative vorticity on blue marble earth
import netCDF4
import matplotlib.pyplot as plt
import numpy
from mpl_toolkits.basemap import Basemap
import math
lon = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['x'][1::2,1::2]
lat = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['y'][1::2,1::2]
dx = netCDF4.Dataset('/archive/gold/datasets/MOM6z_SIS_025/siena/mosaic.unpacked/ocean_hgrid.nc').variables['dx'][:]
dx = dx[1::2,1::2]+numpy.roll(dx,-1,axis=-1)[1::2,1::2]
@adcroft
adcroft / dens_write_eos.py
Created May 12, 2014 15:07
Density of sea-water using Wright et al. 1997, equation of state
def dens_wright_eos(T, S, p):
"""
Equation of state for sea water given by Wright, 1997, J. Atmos. Ocean. Tech., 14, 735-740.
Units: T[degC],S[PSU],p[Pa]
Returns density [kg m-3]
"""
a0 = 7.057924e-4; a1 = 3.480336e-7; a2 = -1.112733e-7
b0 = 5.790749e8; b1 = 3.516535e6; b2 = -4.002714e4; b3 = 2.084372e2; b4 = 5.944068e5; b5 = -9.643486e3
c0 = 1.704853e5; c1 = 7.904722e2; c2 = -7.984422; c3 = 5.140652e-2; c4 = -2.302158e2; c5 = -3.079464