[Dart-dev] [6315] DART/trunk/diagnostics/matlab/read_obs_netcdf.m: Added the ability to return ALL the copy information for an observation,

nancy at ucar.edu nancy at ucar.edu
Thu Jul 18 15:21:27 MDT 2013


Revision: 6315
Author:   thoar
Date:     2013-07-18 15:21:27 -0600 (Thu, 18 Jul 2013)
Log Message:
-----------
Added the ability to return ALL the copy information for an observation,
not just a single copy. The help text has been modified to reflect the 
ability to specify CopyString = 'ALL'.
This was based on code from Rahul.

Modified Paths:
--------------
    DART/trunk/diagnostics/matlab/read_obs_netcdf.m

-------------- next part --------------
Modified: DART/trunk/diagnostics/matlab/read_obs_netcdf.m
===================================================================
--- DART/trunk/diagnostics/matlab/read_obs_netcdf.m	2013-07-18 20:14:54 UTC (rev 6314)
+++ DART/trunk/diagnostics/matlab/read_obs_netcdf.m	2013-07-18 21:21:27 UTC (rev 6315)
@@ -1,12 +1,12 @@
 function obsstruct = read_obs_netcdf(fname, ObsTypeString, region, CopyString, ...
-                                     QCString, verbose)
+   QCString, verbose)
 %% read_obs_netcdf reads in the netcdf flavor observation sequence file
 %                  and returns a subsetted structure.
 %
 % fname         = 'obs_epoch_001.nc';
 % ObsTypeString = 'RADIOSONDE_U_WIND_COMPONENT';   % or 'ALL' ...
 % region        = [0 360 -90 90 -Inf Inf];
-% CopyString    = 'NCEP BUFR observation';
+% CopyString    = 'NCEP BUFR observation';         % or 'ALL' ...
 % QCString      = 'DART quality control';
 % verbose       = 1;   % anything > 0 == 'true'
 %
@@ -40,21 +40,34 @@
    error('%s does not exist.',fname)
 end
 
+%% this block uses the native Matlab netcdf routines
+ncid        = netcdf.open(fname,'NOWRITE');
+dimid       = netcdf.inqDimID(ncid,'copy');
+[~,ncopies] = netcdf.inqDim(ncid,dimid);
+netcdf.close(ncid);
+
 %% record the user input
 
 obsstruct.fname         = fname;
 obsstruct.ObsTypeString = ObsTypeString;
 obsstruct.region        = region;
-obsstruct.CopyString    = CopyString;
+
+%%
+switch lower(CopyString)
+   case 'all'
+      obsstruct.CopyString = cellstr(nc_varget(fname,'CopyMetaData'));
+   otherwise
+      obsstruct.CopyString = CopyString;
+end
 obsstruct.QCString      = QCString;
 obsstruct.verbose       = verbose;
 
 %% get going
 
 ObsTypes       = nc_varget(fname,'ObsTypes');
-ObsTypeStrings = nc_varget(fname,'ObsTypesMetaData');
-CopyStrings    = nc_varget(fname,'CopyMetaData');
-QCStrings      = nc_varget(fname,'QCMetaData');
+ObsTypeStrings = cellstr(nc_varget(fname,'ObsTypesMetaData'));
+CopyStrings    = cellstr(nc_varget(fname,'CopyMetaData'));
+QCStrings      = cellstr(nc_varget(fname,'QCMetaData'));
 
 t              = nc_varget(fname,'time');
 obs_type       = nc_varget(fname,'obs_type');
@@ -65,47 +78,57 @@
 obs            = nc_varget(fname,'observations');
 qc             = nc_varget(fname,'qc');
 
-my_types   = unique(obs_type);  % only ones in the file, actually.
-timeunits  = nc_attget(fname,'time','units');
-timerange  = nc_attget(fname,'time','valid_range');
-calendar   = nc_attget(fname,'time','calendar');
-timebase   = sscanf(timeunits,'%*s%*s%d%*c%d%*c%d'); % YYYY MM DD
-timeorigin = datenum(timebase(1),timebase(2),timebase(3));
-timestring = datestr(timerange + timeorigin);
-t          = t + timeorigin;
+my_types       = unique(obs_type);  % only ones in the file, actually.
+timeunits      = nc_attget(fname,'time','units');
+timerange      = nc_attget(fname,'time','valid_range');
+calendar       = nc_attget(fname,'time','calendar');
+timebase       = sscanf(timeunits,'%*s%*s%d%*c%d%*c%d'); % YYYY MM DD
+timeorigin     = datenum(timebase(1),timebase(2),timebase(3));
+timestring     = datestr(timerange + timeorigin);
+t              = t + timeorigin;
 
 obsstruct.timestring = timestring;
 
 %% Echo summary if requested
 
-if ( verbose > 0 ) 
+if ( verbose > 0 )
    for i = 1:length(my_types)
       obtype = my_types(i);
       inds   = find(obs_type == obtype);
       myz    = loc(inds,3);
-     
-      fprintf('N = %6d %s (type %3d) tween levels %.2f and %.2f\n', ...
-               length(inds), ObsTypeStrings(obtype,:), obtype, ...
-               unique(min(myz)), unique(max(myz)))
+
+      fprintf('N = %6d %32s (type %3d) tween levels %.2f and %.2f\n', ...
+         length(inds), ObsTypeStrings{obtype}, obtype, ...
+         unique(min(myz)), unique(max(myz)))
    end
 end
 
+%% Find copies of the correct type.
+%  If 'ALL' is requested ... do not subset.
+
+switch lower(CopyString)
+   case 'all'
+      mytypeind = 1:ncopies;
+   otherwise
+      mytypeind = get_copy_index(fname, CopyString);
+end
+
 %% Find observations of the correct type.
 %  If 'ALL' is requested ... do not subset.
 
-mytypeind = get_copy_index(fname, CopyString);
-
 switch lower(ObsTypeString)
    case 'all'
       inds      = 1:size(obs,1);
 
-   otherwise % subset the desired observation type
-      myind     = strmatch(ObsTypeString, ObsTypeStrings);
-      if ( isempty(myind) ) 
-         fprintf('FYI - no %s observations ...\n',obsstruct.ObsTypeString) 
+   otherwise % subset the ONE desired observation type
+      myind = strcmp(ObsTypeString, ObsTypeStrings);
+
+      if ( any(myind) )
+         myind = find(myind > 0,1); % find first instance of ...
+         inds  = find(obs_type == myind);
+      else
+         fprintf('FYI - no %s observations ...\n',obsstruct.ObsTypeString)
          inds = [];
-      else
-         inds      = find(obs_type == myind);
       end
 end
 
@@ -131,7 +154,7 @@
 obsstruct.lons = mylocs(inds,1);
 obsstruct.lats = mylocs(inds,2);
 obsstruct.z    = mylocs(inds,3);
-obsstruct.obs  =  myobs(inds);
+obsstruct.obs  =  myobs(inds,:);
 obsstruct.Ztyp = myztyp(inds);
 obsstruct.keys = mykeys(inds);
 obsstruct.time = mytime(inds);
@@ -141,7 +164,7 @@
    obsstruct.qc = myqc(inds);
 end
 
-%% Try to determine if large numbers of the Z coordinate are up or down ...  
+%% Try to determine if large numbers of the Z coordinate are up or down ...
 %  The observation sequences don't actually have the units as part of the
 %  metadata. That would be 'nice'.
 
@@ -169,16 +192,17 @@
       obsstruct.Zunits       = 'pressure';
 
    elseif ( ztypes(itype) ==  3 )   % VERTISHEIGHT
-      %% here is the troublemaker. 
+      %% here is the troublemaker.
       obsstruct.Zpositivedir = 'up';
       obsstruct.Zunits       = 'height';
 
       % If the observations are from the World Ocean Database, they
       % are probably depths. large positive numbers are near the
-      % center of the earth.
+      % center of the earth. The thing is, the only reliable way to know
+      % is to check the FIRST copy string.
 
-      inds = strmatch('WOD', obsstruct.CopyString); 
-      if ( ~ isempty(inds) )
+      inds = strncmp(CopyStrings{1},'WOD obs',7);
+      if ( inds > 0 )
          obsstruct.Zpositivedir = 'down';
          obsstruct.Zunits       = 'depth';
       end
@@ -189,7 +213,6 @@
 
 end
 
-
 % <next few lines under version control, do not edit>
 % $URL$
 % $Revision$


More information about the Dart-dev mailing list