Recipe: Matlab program as OPeNDAP client: Difference between revisions
Created page with "Back to G5NR Data Access Guide. == Problem == By accessing the collection <code>inst01hr_3d_T_Cv</code> via the OPeNDAP server http://opendap.nccs.nasa.gov/dods/OSSE/G5..." |
|||
Line 9: | Line 9: | ||
== Solution == | == Solution == | ||
MATLAB version 2012a and later has native openDAP support. The following code has been tested with MATLAB version 2014a. | |||
The metadata for the collection <code>inst01hr_3d_T_Cv</code> is available at | The metadata for the collection <code>inst01hr_3d_T_Cv</code> is available at | ||
Line 23: | Line 19: | ||
NOTE: | NOTE: | ||
# Instead of reading a downloaded NetCDF-4 file, we read an OPeNDAP URL. | # Instead of reading a downloaded NetCDF-4 file, we read an OPeNDAP URL. | ||
# While in the downloaded file, the temperature variable appears in the uppercase (T), on the OPeNDAP server, this variable is in lowercase | # While in the downloaded file, the temperature variable appears in the uppercase (T), on the OPeNDAP server, this variable is in lowercase. | ||
# Via the OPeNDAP URL, we now have access to all times for which data exists. The hourly <code>inst</code> files are available starting at 2005/15/15, 2200z. Our desired time, 2006/09/18, 0900z is then the 11772<sup>th</sup> step. | # Via the OPeNDAP URL, we now have access to all times for which data exists. The hourly <code>inst</code> files are available starting at 2005/15/15, 2200z. Our desired time, 2006/09/18, 0900z is then the 11772<sup>th</sup> step. | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="matlab" line> | ||
% opendap url | |||
opdurl = 'http://opendap.nccs.nasa.gov:9090/dods/OSSE/G5NR/Ganymed/7km/0.5000_deg/inst/inst01hr_3d_T_Cv' | |||
% bounding box | |||
% lons = -130:0.5:-65 | |||
% lats = 25:0.5:50 | |||
imin = round((-130. + 180.)/0.5); | |||
imax = round(( -65. + 180.)/0.5); | |||
jmin = round(( 25 + 90.)/0.5); | |||
jmax = round(( 50 + 90.)/0.5); | |||
% corresponding array sizes | |||
im = imax-imin+1; | |||
jm = jmax-jmin+1; | |||
lm = 72; % read all 72 levels | |||
% read temperature inside the bounding box | |||
% time 11772 corresponds to 2006/09/18, 9z | |||
start = [imin, jmin, 1, 11772]; | |||
count = [im, jm, lm, 1]; | |||
fprintf('Reading Tsub (subset of T)...'); | |||
Tsub = ncread(opdurl, 't', start, count); | |||
fprintf('done.\n') | |||
% compute max/min of Tsub | |||
fprintf('max(Tsub): %f\n', max(Tsub(:))); | |||
fprintf('min(Tsub): %f\n', min(Tsub(:))); | |||
% compute max/min of Tsub at surface | |||
level = 72; % surface | |||
Tsub_surface = Tsub(:,:,72); | |||
fprintf('max(Tsub at surface): %f\n', max(Tsub_surface(:))); | |||
fprintf('min(Tsub at surface): %f\n', min(Tsub_surface(:))); | |||
</syntaxhighlight> | </syntaxhighlight> | ||