Home > atmlab > sensors > atovs > avhrr_gac_read_raw.m

avhrr_gac_read_raw

PURPOSE ^

avhrr_gac_read_raw Read raw (uncalibrated, unprocessed) AVHRR GAC

SYNOPSIS ^

function varargout = avhrr_gac_read_raw(fn, varargin)

DESCRIPTION ^

 avhrr_gac_read_raw Read raw (uncalibrated, unprocessed) AVHRR GAC

 Read uncalibrated, unprocessed AVHRR GAC data.  It verifies that the file
 is actually AVHRR GAC and applies scale factors, but no calibration or
 geolocation is performed.  For this, see avhrr_gac_read.

 For valid field names, call <a href="matlab:help avhrr_define_gac_l1b">avhrr_define_gac_l1b</a>

 FORMAT

   [data_head, data_line] = avhrr_gac_read_raw(filename, fields_head, fields_line, lines)

 IN

   filename    string      Path to file to read data from
   fields_head cell-string Fields from header to return
   fields_line cell-string Fields from each line to return
   lines       array       Lines to return (0=none).  Default all.

 OUT

   data_head   struct      With the required fields
   data_line   struct with arrays containing data.  Scale factors ar

 EXAMPLE

 >> data_head = avhrr_gac_read_raw('/local/gerrit/tmp/NSS.GHRR.NN.D08221.S1247.E1434.B1658586.GC', ...
                   {'avh_h_siteid', 'avh_h_startdatayr', 'avh_h_dataname', 'avh_h_satid'})
 
 data_head = 
 
         avh_h_siteid: [3x1 char]
    avh_h_startdatayr: 2008
       avh_h_dataname: [42x1 char]
          avh_h_satid: 7

 See also: avhrr_define_gac_l1b, avhrr_gac_read, avhrr_gac_read_raw

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

avhrr_gac_read_raw.m

SOURCE CODE ^

0001 function varargout = avhrr_gac_read_raw(fn, varargin)
0002 
0003 % avhrr_gac_read_raw Read raw (uncalibrated, unprocessed) AVHRR GAC
0004 %
0005 % Read uncalibrated, unprocessed AVHRR GAC data.  It verifies that the file
0006 % is actually AVHRR GAC and applies scale factors, but no calibration or
0007 % geolocation is performed.  For this, see avhrr_gac_read.
0008 %
0009 % For valid field names, call <a href="matlab:help avhrr_define_gac_l1b">avhrr_define_gac_l1b</a>
0010 %
0011 % FORMAT
0012 %
0013 %   [data_head, data_line] = avhrr_gac_read_raw(filename, fields_head, fields_line, lines)
0014 %
0015 % IN
0016 %
0017 %   filename    string      Path to file to read data from
0018 %   fields_head cell-string Fields from header to return
0019 %   fields_line cell-string Fields from each line to return
0020 %   lines       array       Lines to return (0=none).  Default all.
0021 %
0022 % OUT
0023 %
0024 %   data_head   struct      With the required fields
0025 %   data_line   struct with arrays containing data.  Scale factors ar
0026 %
0027 % EXAMPLE
0028 %
0029 % >> data_head = avhrr_gac_read_raw('/local/gerrit/tmp/NSS.GHRR.NN.D08221.S1247.E1434.B1658586.GC', ...
0030 %                   {'avh_h_siteid', 'avh_h_startdatayr', 'avh_h_dataname', 'avh_h_satid'})
0031 %
0032 % data_head =
0033 %
0034 %         avh_h_siteid: [3x1 char]
0035 %    avh_h_startdatayr: 2008
0036 %       avh_h_dataname: [42x1 char]
0037 %          avh_h_satid: 7
0038 %
0039 % See also: avhrr_define_gac_l1b, avhrr_gac_read, avhrr_gac_read_raw
0040 
0041 % Format is defined in NOAA KLM User's Guide, section 8.3.1.4.3.2, in
0042 % particular in Table 8.3.1.4.3.2-1.  Page 8-79 or page 363.
0043 % http://www.ncdc.noaa.gov/oa/pod-guide/ncdc/docs/klm/html/c8/sec83143-2.htm
0044 
0045 %
0046 % $Id: avhrr_gac_read_raw.m 8345 2013-04-17 18:16:40Z gerrit $
0047 
0048 record_size = 4608; % KLM Guide, Section 8.3.1.4.3.2
0049 %n_scanpos = 682;
0050 
0051 % get total no. of records
0052 % S = dir(fn);
0053 % nrecords = S.bytes/record_size-1;
0054 
0055 [fields_head, fields_line, lines] = optargs(varargin, {{}, {}, -1});
0056 
0057 [def_head, def_line] = avhrr_define_gac_l1b();
0058 
0059 fp = fopen(fn, 'r', 'b'); % b=big-endian
0060 c = onCleanup(@()fclose(fp));
0061 
0062 %% read header fields
0063 
0064 data_head = cell2struct(cellfun(@(v) readfield(fp, v, def_head, 0), fields_head, 'UniformOutput', false), fields_head, 2);
0065 
0066 %% check data integrity (first 3 characters should do) and verify GAC
0067 
0068 fseek(fp, 0, 'bof');
0069 siteid = fread(fp, 3, 'char=>char').';
0070 if ~any(strcmp(siteid, {'NSS', 'DSS', 'CMS', 'UKM'}))
0071     error(['atmlab:' mfilename ':invalid'], ...
0072         'Unexpected start of file %s.  Expected first 3 bytes to be one of: NSS DSS CMS UKM.  Not a l1a granule?', fn);
0073 end
0074 
0075 fseek(fp, 77, 'bof');
0076 typecode = fread(fp, 1, 'uint16=>uint16');
0077 if typecode ~= 512
0078     error(['atmlab:' mfilename ':notGAC'], ...
0079         'Data does not appear to be GAC (typecode = %d, expected 512)', typecode);
0080 end
0081 
0082 %% read data fields
0083 
0084 nrecords = single(data_head.avh_h_scnlin);
0085 if lines == -1
0086     lines = nrecords;
0087 end
0088 
0089 if isscalar(lines) % interpret as 1,...,lines
0090     lines = 1:lines;
0091 end
0092 
0093 fseek(fp, record_size, 'bof');
0094 data_raw = fread(fp, record_size*nrecords, 'uint8=>uint8', 0);
0095 
0096 for field = fields_line
0097     octets = bsxfun(@plus, ...
0098         uint32((lines-1).*record_size).', ...
0099         uint32(def_line.(field{1}).End_Octet:-1:def_line.(field{1}).Start_Octet)).';
0100     data_casted = typecast(data_raw(octets(:)), def_line.(field{1}).cast_type);
0101     
0102     NoW = def_line.(field{1}).Number_of_Words;
0103     data_reshaped = reshape(data_casted, [NoW, length(data_casted)/NoW]);
0104     % due to the reading in reverse order, scanlines are reversed
0105     data_fixed = data_reshaped(end:-1:1, :);
0106     if def_line.(field{1}).Scale_Factor ~= 0
0107         data_fixed = single(data_fixed) / 10.^single(def_line.(field{1}).Scale_Factor);
0108     end
0109     data_alt.(field{1}) = data_fixed;
0110     typecast(data_raw(octets(:)), def_line.(field{1}).cast_type);
0111     
0112 end
0113 
0114 if exist('data_alt', 'var')
0115 %    varargout = {data_head, data_line};
0116     varargout = {data_head, data_alt};    
0117 else
0118     varargout = {data_head};
0119 end
0120 
0121 end
0122 function val = readfield(fp, f, def, offset)
0123 % readfield Reads field from AVHRR GAC
0124 %
0125 
0126 
0127 code = fseek(fp, offset+uint32(def.(f).Start_Octet)-1, 'bof');
0128 if code ~= 0
0129     error(['atmlab:' mfilename ':IOError'], ferror(fp));
0130 end
0131 %skip = 0;
0132 val = fread(fp, def.(f).read_size, def.(f).read_type, 0);
0133 % Scale factor from 8.3.1.1 Data Set Structure
0134 if def.(f).Scale_Factor ~= 0
0135     val = single(val) / 10.^single(def.(f).Scale_Factor);
0136 end
0137 end

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