Home > atmlab > sensors > read_cloudsat_hdf.m

read_cloudsat_hdf

PURPOSE ^

READ_CLOUDSAT_HDF Generic reader for CloudSat data

SYNOPSIS ^

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

DESCRIPTION ^

 READ_CLOUDSAT_HDF Generic reader for CloudSat data

 IN  
     hdfFile  '%s'                string path and name of the hdf file
     sdsName  '%s' or {'%s',..}   string fieldname or a cellarray of fieldnames

 OUT 
     d        structure           structure containing desired fields from sdsName  
     attr     structure           structure containing the associated
                                  attributes of those fields

 Example:

 >> [d, attr] = read_cloudats_hdf( ...
     '2010069075419_20562_CS_2B-CWC-RO_GRANULE_P_R04_E03.hdf', ...
     {'Latitude', 'Longitude'});

 >> d
 d =

   Latitude: [1x37082 single]
   Longitude: [1x37082 single]

 >> attr
 attr =

   Latitude: [1x5 struct]
   Longitude: [1x5 struct]

 USAGE
      [d, attr] = read_cloudsat_hdf(hdfFile, {'field1','field2','etc'})

 NOTE: 
      This function reads the data as is, i.e. no scaling, converting,
      etc. The 2nd output variable contains the attributes of all the fields 
      in sdsName


 Created by Salomon Eliasson based on read_clavrx_hdf by 
 Aleksandar Jelenak <Aleksandar.Jelenak@noaa.gov>

 LAST MODIFICATION: "$Id: read_cloudsat_hdf.m 8316 2013-03-27 17:06:01Z gerrit $"

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

read_cloudsat_hdf.m

SOURCE CODE ^

0001 function [d, attr] = read_cloudsat_hdf(hdfFile, sdsName)
0002 % READ_CLOUDSAT_HDF Generic reader for CloudSat data
0003 %
0004 % IN
0005 %     hdfFile  '%s'                string path and name of the hdf file
0006 %     sdsName  '%s' or {'%s',..}   string fieldname or a cellarray of fieldnames
0007 %
0008 % OUT
0009 %     d        structure           structure containing desired fields from sdsName
0010 %     attr     structure           structure containing the associated
0011 %                                  attributes of those fields
0012 %
0013 % Example:
0014 %
0015 % >> [d, attr] = read_cloudats_hdf( ...
0016 %     '2010069075419_20562_CS_2B-CWC-RO_GRANULE_P_R04_E03.hdf', ...
0017 %     {'Latitude', 'Longitude'});
0018 %
0019 % >> d
0020 % d =
0021 %
0022 %   Latitude: [1x37082 single]
0023 %   Longitude: [1x37082 single]
0024 %
0025 % >> attr
0026 % attr =
0027 %
0028 %   Latitude: [1x5 struct]
0029 %   Longitude: [1x5 struct]
0030 %
0031 % USAGE
0032 %      [d, attr] = read_cloudsat_hdf(hdfFile, {'field1','field2','etc'})
0033 %
0034 % NOTE:
0035 %      This function reads the data as is, i.e. no scaling, converting,
0036 %      etc. The 2nd output variable contains the attributes of all the fields
0037 %      in sdsName
0038 %
0039 %
0040 % Created by Salomon Eliasson based on read_clavrx_hdf by
0041 % Aleksandar Jelenak <Aleksandar.Jelenak@noaa.gov>
0042 %
0043 % LAST MODIFICATION: "$Id: read_cloudsat_hdf.m 8316 2013-03-27 17:06:01Z gerrit $"
0044 
0045 % Error checking.
0046 narginchk(2, 2);
0047 nargoutchk(1, 2);
0048 
0049 errId = ['atmlab:' mfilename];
0050 assert(iscellstr(sdsName) || (ischar(sdsName) && size(sdsName, 1) == 1),...
0051     [errId ':badInput'],'Input argument SDSNAME must be a cell array of strings or a string!');
0052 
0053 % UNCOMPRESS if needed
0054 if any([strcmp(hdfFile(end-2:end),'.gz'),strcmp(hdfFile(end-3:end),{'.zip','.bz2'})])
0055     tmpdir = create_tmpfolder;
0056     c = onCleanup(@() rmdir(tmpdir,'s'));
0057     % use a try-catch clause; in some rare cases, the shell unzip cannot
0058     % unzip but the Matlab built-in unzip can.
0059     try 
0060         hdfFile = uncompress(hdfFile, tmpdir);
0061     catch ME
0062         switch ME.identifier
0063             case 'atmlab:exec_system_cmd:shell'
0064                 hdfFile = uncompress(hdfFile, tmpdir, struct('tool', 'builtin'));
0065             otherwise
0066                 ME.rethrow();
0067         end
0068     end
0069     if isempty(hdfFile)
0070         error(errId,'Uncompressing failed');
0071     end
0072 end
0073 
0074 % Query the contents of the HDF file.
0075 s = hdfinfo(hdfFile);
0076 
0077 % Turn sdsName into a column vector of cell strings.
0078 if iscellstr(sdsName)
0079     sdsName = sdsName(:);
0080 else
0081     sdsName = cellstr(sdsName);
0082 end
0083 
0084 Vgroup = s.Vgroup.Vgroup;
0085 
0086 a = isstruct(s.Attributes);
0087 vd1  = isstruct(Vgroup(1).Vdata);
0088 vd2  = isstruct(Vgroup(1).Vdata);
0089 sds1 = isstruct(Vgroup(1).SDS);
0090 sds2 = isstruct(Vgroup(2).SDS);
0091 
0092 % Unfortunately the following fields are not always filled in
0093 allfields = {};
0094 if a, allfields{end+1} = {s.Attributes.Name}; end % Attributes
0095 if vd1, allfields{end+1} = {Vgroup(1).Vdata.Name}; end % Geolocation Fields
0096 if vd2, allfields{end+1} = {Vgroup(2).Vdata.Name};end % Data Fields
0097 if sds1, allfields{end+1} = {Vgroup(1).SDS.Name}; end % Height 2D
0098 if sds2, allfields{end+1} = {Vgroup(2).SDS.Name}; end % Data 2D
0099 allfields = [allfields{:}];
0100 
0101 if nargout == 2
0102     Attributes = {};
0103     if a, Attributes{end+1} = {s.Attributes.Value}; end
0104     if vd1, Attributes{end+1} = {Vgroup(1).Vdata.DataAttributes}; end
0105     if vd2, Attributes{end+1} = {Vgroup(2).Vdata.DataAttributes}; end
0106     if sds1, Attributes{end+1} = {Vgroup(1).SDS.Attributes}; end
0107     if sds2, Attributes{end+1} = {Vgroup(2).SDS.Attributes}; end
0108     Attributes = [Attributes{:}];
0109 end
0110 
0111 % Start looping over elements of sdsName.
0112 for i = 1:length(sdsName)
0113     
0114     % Look for the SDS var using user-supplied name.
0115     assert(ismember(sdsName{i}, allfields),...
0116         [errId ':MissingFromFile'],'variable %s is missing in %s',sdsName{i}, hdfFile)
0117     
0118     % READ the data.
0119     d.(sdsName{i}) = hdfread(hdfFile,sdsName{i});
0120     
0121     % hdfread outputs the data in a 1x1 cell. We don't need that
0122     if iscell(d.(sdsName{i})) && length(iscell(d.(sdsName{i})))==1
0123         d.(sdsName{i}) = d.(sdsName{i}){1};
0124     end
0125     
0126     % make sure strings are always rowvectors
0127     if ischar(d.(sdsName{i}))
0128         d.(sdsName{i}) = d.(sdsName{i})(:)';
0129     end
0130         
0131     % Return SDS attributes if the user has supplied a second output variable.
0132     if nargout == 2
0133         attr.(sdsName{i}) = Attributes{ismember(allfields,sdsName{i})};
0134     end
0135 end

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