Home > atmlab > datasets > read_clavrx_hdf.m

read_clavrx_hdf

PURPOSE ^

READ_CLAVRX_HDF Generic reader for CLAVR-x HDF data.

SYNOPSIS ^

function [d, attr] = read_clavrx_hdf(hdfFile, sdsName)

DESCRIPTION ^

 READ_CLAVRX_HDF Generic reader for CLAVR-x HDF data.

 D = read_clavrx_hdf(HDFFILE, SDSNAME) reads in data from an SDS array
 named SDSNAME in an HDF file HDFFILE.

 [D, ATTR] = read_clavrx_hdf(HDFFILE, SDSNAME) stores SDSNAME's attributes
 into ATTR as a struct array.

 When SDSNAME is a cell array of strings, D and ATTR become struct
 variables, whose field names are elements of the SDSNAME cell array.

 Missing elements in the HDF data are replaced with NaNs. Replacement of
 NaNs with original values for missing data can be achieved by using
 attributes stored in ATTR for that SDS variable.

 Example:

 >> [d, attr] = read_clavrx_hdf( ...
     'NSS.GHRR.NJ.D98195.S1931.E2125.B1823335.GC.obs.hdf', ...
     {'ch1_reflectance', 'ch2_reflectance'});

 >> d
 d = 

   ch1_reflectance: [13724x409 double]
   ch2_reflectance: [13724x409 double]

 >> attr
 attr = 

   ch1_reflectance: [1x12 struct]
   ch2_reflectance: [1x12 struct]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

read_clavrx_hdf.m

SOURCE CODE ^

0001 function [d, attr] = read_clavrx_hdf(hdfFile, sdsName)
0002 % READ_CLAVRX_HDF Generic reader for CLAVR-x HDF data.
0003 %
0004 % D = read_clavrx_hdf(HDFFILE, SDSNAME) reads in data from an SDS array
0005 % named SDSNAME in an HDF file HDFFILE.
0006 %
0007 % [D, ATTR] = read_clavrx_hdf(HDFFILE, SDSNAME) stores SDSNAME's attributes
0008 % into ATTR as a struct array.
0009 %
0010 % When SDSNAME is a cell array of strings, D and ATTR become struct
0011 % variables, whose field names are elements of the SDSNAME cell array.
0012 %
0013 % Missing elements in the HDF data are replaced with NaNs. Replacement of
0014 % NaNs with original values for missing data can be achieved by using
0015 % attributes stored in ATTR for that SDS variable.
0016 %
0017 % Example:
0018 %
0019 % >> [d, attr] = read_clavrx_hdf( ...
0020 %     'NSS.GHRR.NJ.D98195.S1931.E2125.B1823335.GC.obs.hdf', ...
0021 %     {'ch1_reflectance', 'ch2_reflectance'});
0022 %
0023 % >> d
0024 % d =
0025 %
0026 %   ch1_reflectance: [13724x409 double]
0027 %   ch2_reflectance: [13724x409 double]
0028 %
0029 % >> attr
0030 % attr =
0031 %
0032 %   ch1_reflectance: [1x12 struct]
0033 %   ch2_reflectance: [1x12 struct]
0034 
0035 % LAST MODIFICATION: "$Id: read_clavrx_hdf.m 6482 2011-05-06 05:27:46Z seliasson $"
0036 % Author: Aleksandar Jelenak <Aleksandar.Jelenak@noaa.gov>
0037 
0038 % Error checking.
0039 error(nargchk(2, 2, nargin));
0040 error(nargoutchk(1, 2, nargout));
0041 
0042 if ~(iscellstr(sdsName) || (ischar(sdsName) && (size(sdsName, 1) == 1)))
0043    error('Input argument SDSNAME must be a cell array of strings or a string!');
0044 end
0045 
0046 % Query the contents of the HDF file.
0047 s = hdfinfo(hdfFile);
0048 
0049 % Turn sdsName into a column vector of cell strings.
0050 if iscellstr(sdsName)
0051    sdsName = sdsName(:);
0052 else
0053    sdsName = cellstr(sdsName);
0054 end
0055 
0056 oneSds = length(sdsName) == 1;
0057 
0058 % Start looping over elements of sdsName.
0059 for i=1:length(sdsName)
0060 
0061    % Look for the SDS var using user-supplied name.
0062    sdsIdx = strmatch(sdsName{i}, {s.SDS(:).Name}, 'exact');
0063    if isempty(sdsIdx)
0064       error('SDS variable "%s" not found in "%s"!', sdsName{i}, hdfFile);
0065    end
0066 
0067    % Read in the data.
0068    x = double(hdfread(s.SDS(sdsIdx)));
0069 
0070    % Unscale it, if needed.
0071    scaled = read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'SCALED');
0072    if scaled
0073       sclMiss = double(read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'SCALED_MISSING'));
0074       % miss = double(read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'MISSING'));
0075       sclMin = double(read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'SCALED_MIN'));
0076       sclMax = double(read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'SCALED_MAX'));
0077       rngMin = double(read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'RANGE_MIN'));
0078       rngMax = double(read_clavrx_hdf_read_attr(s.SDS(sdsIdx), 'RANGE_MAX'));
0079 
0080       % Find all SCALED_MISSING elements.
0081       %sclMissIdx = find(x == sclMiss);
0082       sclMissIdx = x == sclMiss; % salomon
0083       
0084       % Unscale the data.
0085       if scaled == 1
0086          x = (rngMax-rngMin)/(sclMax-sclMin)*(x-sclMin)+rngMin;
0087       elseif scaled == 2
0088          x = 10.^x;
0089       else
0090          error('SDS "%s" has unsupported scaling type: %d', sdsName{i}, scaled);
0091       end
0092 
0093       % Set the missing elements.
0094       x(sclMissIdx) = NaN;
0095    end
0096 
0097    if oneSds
0098       d = x;
0099    else
0100       d.(sdsName{i}) = x;
0101    end
0102    clear x;
0103 
0104    % Return SDS attributes if the user has supplied a second output variable.
0105    if nargout == 2
0106       if oneSds
0107          attr = s.SDS(sdsIdx).Attributes;
0108       else
0109          attr.(sdsName{i}) = s.SDS(sdsIdx).Attributes;
0110       end
0111    end
0112 
0113 end
0114 
0115 return
0116 
0117 %
0118 % Internal function
0119 %
0120 function val = read_clavrx_hdf_read_attr(s, attrName)
0121 
0122 attrIdx = strmatch(attrName, {s.Attributes(:).Name}, 'exact');
0123 if isempty(attrIdx)
0124    warning('clavrx:badinput','read''Attribute "%s" for SDS variable "%s" not found!', attrName, s.Name);
0125    val = NaN;
0126 else
0127    val = s.Attributes(attrIdx).Value;
0128 end
0129 
0130 return

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005