Skip to content

Instantly share code, notes, and snippets.

@drf5n
Last active November 26, 2018 18:49
Show Gist options
  • Save drf5n/b62049c358c51d1412850aa21798a295 to your computer and use it in GitHub Desktop.
Save drf5n/b62049c358c51d1412850aa21798a295 to your computer and use it in GitHub Desktop.
Using nctoolbox to access and plot a slice of Hycom Reanalysis
% This uses the Matlab NCToolbox to access HYCOM data and plot a salinity field
%
% NCToolbox is available from https://github.com/nctoolbox/nctoolbox
% HYCOM is available from https://hycom.org/
%
% Dig down from HYCOM to find a DODS Data URL
%
% This is setup to look for the 2012 GOFS 3.0 Reanalysis
%
% Google for Hycom opendap
% https://hycom.org/dataserver/
% Choose GOFS 3.0 Reanalysis from the sidebar:
% https://hycom.org/dataserver/gofs-3pt0/reanalysis
% Choose the 1995-08-01 → 2012-12-31 dataset:
% https://hycom.org/data/glbu0pt08/expt-19pt1
% choose "Access Data Here" button: to get the list of years
% http://tds.hycom.org/thredds/GLBu0.08/expt_19.1.html
% choose the 2012 dataset to get the TDS page showing different format
% http://tds.hycom.org/thredds/GLBu0.08/expt_19.1.html?dataset=GLBu0.08-expt_19.1-2012
% Choose the OpenDAP UI:
% http://tds.hycom.org/thredds/dodsC/GLBu0.08/expt_19.1/2012.html
% Copy the "Data URL"
% http://tds.hycom.org/thredds/dodsC/GLBu0.08/expt_19.1/2012
%
uri = 'http://tds.hycom.org/thredds/dodsC/GLBu0.08/expt_19.1/2012'
nc = ncgeodataset(uri) % get a ncgeodataset handle
nc.variables % list the variables
sv = nc{'salinity'}; % get a ncgeovariable handle
sv.attributes
svg = sv.grid_interop(:,:,:,:) % get the time,z,lon,lat grid info
datestr(sv.timeextent(),29) % find out it ranges from 2012-01-01 to 2012-12-31
% do some plotting like https://github.com/nctoolbox/nctoolbox/blob/master/demos/geodemo_3.m
[toi,junk] =near(svg.time,datenum(2012,06,01)) % find the index nearest a particular date
doi = 1;
pcolorjw(svg.lon,svg.lat,double(sv.data(toi,doi,:,:))); % plot
title({sprintf('Hycom %s @ %.0fm',...
sv.attribute('long_name'),svg.z(doi));...
uri;datestr(svg.time(toi))},'interpreter','none');
% add colorbar with units
hcb = colorbar;
title(hcb,sv.attribute('units'));
print('hycom_salinity_nctoolbox','-dpng')
@hohonuuli
Copy link

Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment