Explore Changing Bathymetry Data Format

netCDF4 Instead of netCDF3_CLASSIC

In [3]:
%matplotlib inline
import netCDF4 as nc
import numpy as np
from salishsea_tools import bathy_tools
from salishsea_tools import nc_tools

Algorithmic smoothed version of Salish Sea sub-domain bathymetry based on that from 2-Oct-2013 WCSD_PREP tarball:

In [4]:
bathy = nc.Dataset('../../NEMO-forcing/grid/Subdom_bathy_meter_smooth.nc', 'r')
In [6]:
nc_tools.show_dataset_attrs(bathy)
nc_tools.show_dimensions(bathy)
nc_tools.show_variables(bathy)
nc_tools.show_variable_attrs(bathy)
file format: NETCDF3_CLASSIC
CDI: Climate Data Interface version 1.5.9 (http://code.zmaw.de/projects/cdi)
Conventions: CF-1.4
history: Thu Sep 26 11:44:40 2013: cdo selindexbox,1,398,200,544 /users/staff/jppaquin/scratch/WC3_PREP/2_bathy/bathy_meter_NOBCchancomp.nc /users/staff/jppaquin/scratch/WCSD_PREP/SubDom_bathy_meter_NOBCchancomp.nc
Fri Jul 19 16:03:29 2013: cdo setrtoc,0,4,4 ../1_Seagrid/1_bathymetry_seagrid_WestCoast.nc ./bathy_meter.nc
CDO: Climate Data Operators version 1.5.9rc3 (http://code.zmaw.de/projects/cdo)
<type 'netCDF4.Dimension'>: name = 'x', size = 398

<type 'netCDF4.Dimension'>: name = 'y', size = 345

[u'nav_lon', u'nav_lat', u'Bathymetry']
<type 'netCDF4.Variable'>
float32 nav_lon(y, x)
    units: degrees_east
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float32 nav_lat(y, x)
    units: degrees_north
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float32 Bathymetry(y, x)
    units: metre
    _FillValue: 0.0
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float32 nav_lon(y, x)
    units: degrees_east
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float32 nav_lat(y, x)
    units: degrees_north
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float32 Bathymetry(y, x)
    units: metre
    _FillValue: 0.0
unlimited dimensions: 
current shape = (345, 398)

netCDF4 bathymetry dataset we're creating:

In [3]:
new_bathy = nc.Dataset('subdom_bathy_meter_nc4_expt.nc', 'w')
In [5]:
print new_bathy.file_format
nc_tools.show_dataset_attrs(new_bathy)
NETCDF4

Dimensions:

In [60]:
new_bathy.createDimension('y', 345)
new_bathy.createDimension('x', 398)
nc_tools.show_dimensions(new_bathy)
<type 'netCDF4.Dimension'>: name = 'y', size = 345

<type 'netCDF4.Dimension'>: name = 'x', size = 398

Variables and their attributes.

Note that zlib=True for all variables and least significant_digit and fill_value are set for the depths.

In [ ]:
lons = new_bathy.createVariable('nav_lon', float, ('y', 'x'), zlib=True)
lons.setncattr('units', 'degrees_east')
lats = new_bathy.createVariable('nav_lat', float, ('y', 'x'), zlib=True)
lats.setncattr('units', 'degrees_north')
depths = new_bathy.createVariable(
    'Bathymetry', float, ('y', 'x'), 
    zlib=True, least_significant_digit=1, fill_value=0)
depths.setncattr('units', 'metres')
In [8]:
nc_tools.show_variables(new_bathy)
nc_tools.show_variable_attrs(new_bathy)
[u'nav_lon', u'nav_lat', u'Bathymetry']
<type 'netCDF4.Variable'>
float64 nav_lon(y, x)
    units: degrees_east
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float64 nav_lat(y, x)
    units: degrees_north
unlimited dimensions: 
current shape = (345, 398)

<type 'netCDF4.Variable'>
float64 Bathymetry(y, x)
    _FillValue: 0.0
    least_significant_digit: 1
    units: metres
unlimited dimensions: 
current shape = (345, 398)

Copy data values from netCDF3 dataset to our new one:

In [63]:
lons[:] = bathy.variables['nav_lon']
lats[:] = bathy.variables['nav_lat']
depths[:] = bathy.variables['Bathymetry']

Verify the new dataset:

In [9]:
lat_stats = bathy_tools.min_mid_max(lats)
lon_stats = bathy_tools.min_mid_max(lons)
print lat_stats
print lon_stats
print np.min(depths), np.max(depths)
(47.630783081054688, 48.702592849731445, 49.751277923583984)
(-125.20716857910156, -123.60331344604492, -121.98944091796875)
0.0 428.0
In [10]:
fig = bathy_tools.plot_colourmesh(new_bathy, 'Smoothed Sub-domain netCDF4')
In [11]:
new_bathy.close()

The netCDF4 file is 227kb in constrast to the netCDF3 file at 1.6Mb. NEMO-3.4 reads the netCDF4 file without complaint.

In [ ]: