Home > atmlab > sensors > ssmt2_read.m

ssmt2_read

PURPOSE ^

SSMT2_READ Read SSM/T-2 data file - old style.

SYNOPSIS ^

function S = ssmt2_read (filename, apply_calibration)

DESCRIPTION ^

 SSMT2_READ   Read SSM/T-2 data file - old style.

 ************************************************
 *                DEPRECATED                    *
 *       Use ssmt2_read_ngdc instead!           *
 *                DEPRECATED                    *
 ************************************************

    Reads an SSM/T-2 file into a structure and applies calibration.

    This function is based on the ssmt2list C program written by Don Moss,
    University of Alabama in Huntsville.

    To get the exact timestamp for each scan of one scanline use:
    datestr(S.recs(1).timestamp)

 FORMAT   S = ssmt2_read (filename, apply_calibration)
        
 OUT   S   SSM/T-2 structure
             Header information:
               dsname   Dataset name
               nscan    Number of scans
               ngap     Data gaps
               precal   Preflight calibration (not used)
               apc      Antenna pattern correction coefficients
               qc_earth QC of earth locations
               qc_scene QC of scene data
               qc_cal   Calibration QC flags
             Data records S.recs():
               orbit    Orbit number
               scan     Scan number
               t1x      ?
               year     Year of first scan in file
               doy      Day of year of first scan in file
               ols      seconds since start of day
               ts       milliseconds since ols
               lat      Latitude earth location vector
               lon      Longitude earth location vector
               time     Scene time data
               timestamp Scene time data as matlab datenum value
               count    Scene raw count data (not returned if
                        apply_calibration is set to true or not given)
               temp     Scene calibrated antenna temperatures (not returned if
                        apply_calibration is set to false)
               rawcal   Raw calibration data (not used)
               slopes   Calibration slopes
               offset   Calibration intercepts
               qc_earth QC flags of earth locations
               qc_scene QC flags of scene data
               qc_cal   Calibration QC flags
               
               
 IN    filename            Name of SSM/T-2 file
 OPT   apply_calibration   Pass false to get the raw count values,
                           false: don't apply calibration
                           true: apply calibration to retrieve BTs

 ************************************************
 *                DEPRECATED                    *
 *       Use ssmt2_read_ngdc instead!           *
 *                DEPRECATED                    *
 ************************************************

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

ssmt2_read.m

SOURCE CODE ^

0001 % SSMT2_READ   Read SSM/T-2 data file - old style.
0002 %
0003 % ************************************************
0004 % *                DEPRECATED                    *
0005 % *       Use ssmt2_read_ngdc instead!           *
0006 % *                DEPRECATED                    *
0007 % ************************************************
0008 %
0009 %    Reads an SSM/T-2 file into a structure and applies calibration.
0010 %
0011 %    This function is based on the ssmt2list C program written by Don Moss,
0012 %    University of Alabama in Huntsville.
0013 %
0014 %    To get the exact timestamp for each scan of one scanline use:
0015 %    datestr(S.recs(1).timestamp)
0016 %
0017 % FORMAT   S = ssmt2_read (filename, apply_calibration)
0018 %
0019 % OUT   S   SSM/T-2 structure
0020 %             Header information:
0021 %               dsname   Dataset name
0022 %               nscan    Number of scans
0023 %               ngap     Data gaps
0024 %               precal   Preflight calibration (not used)
0025 %               apc      Antenna pattern correction coefficients
0026 %               qc_earth QC of earth locations
0027 %               qc_scene QC of scene data
0028 %               qc_cal   Calibration QC flags
0029 %             Data records S.recs():
0030 %               orbit    Orbit number
0031 %               scan     Scan number
0032 %               t1x      ?
0033 %               year     Year of first scan in file
0034 %               doy      Day of year of first scan in file
0035 %               ols      seconds since start of day
0036 %               ts       milliseconds since ols
0037 %               lat      Latitude earth location vector
0038 %               lon      Longitude earth location vector
0039 %               time     Scene time data
0040 %               timestamp Scene time data as matlab datenum value
0041 %               count    Scene raw count data (not returned if
0042 %                        apply_calibration is set to true or not given)
0043 %               temp     Scene calibrated antenna temperatures (not returned if
0044 %                        apply_calibration is set to false)
0045 %               rawcal   Raw calibration data (not used)
0046 %               slopes   Calibration slopes
0047 %               offset   Calibration intercepts
0048 %               qc_earth QC flags of earth locations
0049 %               qc_scene QC flags of scene data
0050 %               qc_cal   Calibration QC flags
0051 %
0052 %
0053 % IN    filename            Name of SSM/T-2 file
0054 % OPT   apply_calibration   Pass false to get the raw count values,
0055 %                           false: don't apply calibration
0056 %                           true: apply calibration to retrieve BTs
0057 %
0058 % ************************************************
0059 % *                DEPRECATED                    *
0060 % *       Use ssmt2_read_ngdc instead!           *
0061 % *                DEPRECATED                    *
0062 % ************************************************
0063 
0064 % 2009-11-20   Created by Oliver Lemke and Mathias Milz.
0065 
0066 function S = ssmt2_read (filename, apply_calibration)
0067 
0068 if nargin == 1
0069   apply_calibration = true;
0070 end
0071 
0072 if strcmp( filename(length(filename)-2:end), '.gz')
0073   uncompressed_filename = tempname(atmlab( 'WORK_AREA' ));
0074   cleanupObj = onCleanup(@() (delete(uncompressed_filename)));
0075   cmd = [ 'gunzip -cd ' filename ' > ' uncompressed_filename ];
0076   st = system (cmd);
0077   if st
0078     error ('atmlab:ssmt2_read', 'Failed to uncompress SSM/T-2 file %s', filename);
0079   end
0080   filename = uncompressed_filename;
0081 end
0082 
0083 % times of known switches in datasets
0084 %dateswitch1 = datenum(1999, 2, 8, 17, 2, 00); %1,59.9808);
0085 %dateswitch2 = datenum(2001, 9, 18, 14, 44, 0);% 44, 0096);
0086 
0087 
0088 experimental_offset_calibration_xx = [0.2815 0.2915 0.2782 0.3762 1.1355];
0089 experimental_offset_calibration_20 = [1 1 1 1 1];
0090 experimental_offset_calibration_100 = [0.2 0.2 0.2 0.2 0.2];
0091 %HDRSIZE = 692;
0092 %DATSIZE = 692;
0093 NPOS    = 28;
0094 NCHAN   = 5;
0095 
0096 ebcdic_ascii = [ ...
0097     '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', ...
0098     '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', ...
0099     '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', ...
0100     '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', ...
0101     ' ','?','?','?','?','?','?','?','?','?','?','.','?','?','+','?', ...
0102     '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', ...
0103     '-','?','?','?','?','?','?','?','?','?','?','?','?','_','?','?', ...
0104     '?','?','?','?','?','?','?','?','?','?',':','#','@','?','=','?', ...
0105     '?','a','b','c','d','e','f','g','h','i','?','?','?','?','?','?', ...
0106     '?','j','k','l','m','n','o','p','q','r','?','?','?','?','?','?', ...
0107     '?','?','s','t','u','v','w','x','y','z','?','?','?','?','?','?', ...
0108     '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', ...
0109     '?','A','B','C','D','E','F','G','H','I','?','?','?','?','?','?', ...
0110     '?','J','K','L','M','N','O','P','Q','R','?','?','?','?','?','?', ...
0111     '?','?','S','T','U','V','W','X','Y','Z','?','?','?','?','?','?', ...
0112     '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?' ...
0113     ];
0114 
0115 fid = fopen (filename, 'rb', 'b');
0116 
0117 if (fid == -1)
0118   error ('atmlab:ssmt2_read', 'Can''t open input file %s', filename);
0119 end
0120 cleanupObj2 = onCleanup(@() (fclose(fid)));
0121 
0122 %%%%%%%%%% Read Header %%%%%%%%%%
0123 %
0124 S.dsname  = ebcdic_ascii(fread (fid, 44, 'uint8=>char'));
0125 
0126 if (~strcmp(S.dsname(1:8), 'NSS.SMT2'))
0127   error ('atmlab:ssmt2_read', 'Not a valid SSM/T-2 file: %s', filename);
0128 end
0129 
0130 S.nscan   = fread (fid, 1, 'uint16');
0131 S.ngap    = fread (fid, 1, 'uint16');
0132 S.precal  = fread (fid, 108, 'uint8');
0133 
0134 S.apc  = reshape (fread (fid, NPOS*NCHAN, 'uint16') / 100, NCHAN, NPOS)';
0135 
0136 S.qc_earth = fread (fid, 1, 'uint16');
0137 S.qc_scene = fread (fid, 1, 'uint16');
0138 S.qc_cal   = fread (fid, NCHAN, 'uint16');
0139 
0140 %%%%%%%%%% Read Data Records %%%%%%%%%%
0141 %
0142 recn = 0;
0143 while (~feof (fid) && recn < S.nscan)
0144     recn = recn + 1;
0145     fseek (fid, recn*692, 'bof');
0146 
0147     rec.orbit = fread (fid, 1, 'uint32');
0148     
0149     if (~rec.orbit), break, end;
0150     
0151     rec.scan  = fread (fid, 1, 'uint16');
0152     rec.t1x   = fread (fid, 1, 'uint16');
0153     yyddd = fread (fid, 1, 'uint32');
0154     
0155     rec.year = floor(yyddd/1000);
0156     rec.doy = yyddd - rec.year*1000;
0157     
0158     if (rec.year < 50)
0159         rec.year = rec.year+2000;
0160     else
0161         rec.year=rec.year+1900;
0162     end;
0163     
0164     rec.ols   = fread (fid, 1, 'uint32');
0165     if (recn == 1)
0166         firstols = rec.ols;
0167     elseif (rec.ols < firstols)
0168         rec.ols = rec.ols + 86400;
0169     end
0170     
0171     rec.ts    = fread (fid, 1, 'uint32');
0172 
0173     latlon = reshape (fread (fid, NPOS*2, 'int16') / 128, 2, NPOS);
0174     rec.lat = latlon (1, :);
0175     rec.lon = latlon (2, :);
0176     
0177     timecount = reshape (fread (fid, (1+NCHAN)*NPOS, 'uint16'), 1+NCHAN, NPOS);
0178     rec.time  = timecount (1, :);
0179     rec.timestamp = datenum(rec.year,1,1) + rec.doy-1 + rec.ols/86400 + rec.time/1000/86400;
0180     if (recn == 1)
0181         firsttimestamp = rec.timestamp(1);
0182     end
0183     rec.count = timecount (2:NCHAN+1, :);
0184     
0185     rec.rawcal = fread (fid, 172, 'uint8');
0186     
0187     rec.slope  = fread (fid, NCHAN, 'int16')' / 10000;
0188     %FIXME OLE: The offset factor was changed at some point
0189     %rec.offset  = fread (fid, NCHAN, 'int16')' / 100;
0190     rec.offset = fread (fid, NCHAN, 'int16')' / 20;
0191     
0192     rec.qc_earth = fread (fid, 1, 'uint16');
0193     rec.qc_scene = fread (fid, 1, 'uint16');
0194     rec.qc_cal   = fread (fid, NCHAN, 'uint16')';
0195     
0196     %%%%%%%%%% Apply calibration %%%%%%%%%%
0197     %
0198     if (apply_calibration)
0199         if  firsttimestamp < 730159.709722
0200       % if  firsttimestamp < dateswitch1
0201             experimental_offset_calibration=experimental_offset_calibration_100;
0202         elseif firsttimestamp >= 730159.709722  && ...
0203                 firsttimestamp < 731112.613889
0204 %        elseif firsttimestamp >= dateswitch1  && ...
0205 %                firsttimestamp < dateswitch2
0206             experimental_offset_calibration=experimental_offset_calibration_20;
0207         else
0208             experimental_offset_calibration=experimental_offset_calibration_xx;
0209         end
0210         for chan = 1:NCHAN
0211             rec.temp(chan,:) = rec.count(chan,:) * rec.slope(chan) ...
0212                 + rec.offset(chan) * experimental_offset_calibration(chan);
0213         end
0214     end
0215     
0216     S.recs(recn) = rec;
0217     clear rec;
0218 end
0219 
0220 if (S.nscan ~= recn)
0221     error ('atmlab:ssmt2_read', ...
0222         'Premature end of file %s, expected %d records, found %d', ...
0223         filename, S.nscan, recn);
0224 end

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