Home > atmlab > datasets > +satreaders > grasdir.m

grasdir

PURPOSE ^

GRASDIR read directory of gras data

SYNOPSIS ^

function S_res = grasdir(file, varargin)

DESCRIPTION ^

 GRASDIR read directory of gras data

 This is a reading routine for the Global Navigation Satellite System
 (GNSS) Receiver for Atmospheric Sounding (GRAS) data directories.
 In GRAS measurements, a single measurement is stored in a single
 NetCDF-file.  In the framework of the collocation toolkit, this is
 impractical and inefficient.  Therefore, the reading is done for a
 whole directory, which still contains only ~20 MiB of data.

 For calling format, see Contents.m.  Note that you should pass a
 directory, not a file!

 For info on the common format, see <a href="matlab:help SatDataset/reader">SatDataset/reader</a>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

grasdir.m

SOURCE CODE ^

0001 function S_res = grasdir(file, varargin)
0002 % GRASDIR read directory of gras data
0003 %
0004 % This is a reading routine for the Global Navigation Satellite System
0005 % (GNSS) Receiver for Atmospheric Sounding (GRAS) data directories.
0006 % In GRAS measurements, a single measurement is stored in a single
0007 % NetCDF-file.  In the framework of the collocation toolkit, this is
0008 % impractical and inefficient.  Therefore, the reading is done for a
0009 % whole directory, which still contains only ~20 MiB of data.
0010 %
0011 % For calling format, see Contents.m.  Note that you should pass a
0012 % directory, not a file!
0013 %
0014 % For info on the common format, see <a href="matlab:help SatDataset/reader">SatDataset/reader</a>.
0015 
0016 % $Id: grasdir.m 8720 2013-10-21 20:41:39Z gerrit $
0017 
0018 % WARNING: functionality relies on consistent no. and order of data
0019 % files within directory!
0020 
0021 assert(isdir(file), ['atmlab:' mfilename ':notadirectory'], ...
0022     'function ''grasdir'' wants a directory, which %s is not', file);
0023 
0024 core_fields   = {'lat','lon'};
0025 extra_fields  = optargs(varargin, {{}});
0026 all_fields    = [core_fields(:); extra_fields(:)];
0027 
0028 cont = dir(file);
0029 fullpaths = cellfun(@(x) fullfile(file, x), {cont.name}, 'UniformOutput', false);
0030 
0031 % read data into a cell array of structures
0032 path_is_gran = cellfun(@any, strfind(fullpaths, 'nc'));
0033 alldata = cellfun(@(f) loadncvar(f, all_fields), ...
0034         fullpaths(path_is_gran), ...
0035         'UniformOutput', false);
0036 
0037 % convert cell array of structures into structure with matrices
0038 % NB: this would be the direct output from cellfun with 'UniformOutput'
0039 % set to true, but I cannot assume this if some fields have non-constant
0040 % data sizes
0041 S = horzcat(alldata{:});
0042 if isempty(alldata)
0043     S_res = cell2struct(repmat({[]}, size(all_fields)), all_fields);
0044     S_res.epoch = 0;
0045     S_res.time = [];
0046     S_res.version = '?';
0047     return;
0048 end
0049 flds = fieldnames(S(1));
0050 varying_size = cellfun(@(fn) length(unique(cellfun(@length, {S.(fn)}))), flds)>1;
0051 vals = cellfun(@(f) [S.(f)].', flds(~varying_size), 'UniformOutput', false);
0052 S_res = cell2struct(vals.', flds(~varying_size).', 2);
0053 
0054 if length(find(varying_size)) > 0
0055     logtext(atmlab('ERR'), 'Cannot concatenate fields: %s\n', ...
0056         strjoin(flds(varying_size)));
0057 end
0058 
0059 % add time info from filenames
0060 % hardcode regular expression here; this is about files inside the
0061 % 'granule' directory, not the 'granule' directory itself
0062 f_re = 'wet(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})_(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2})_M.._\d*_N\d*_X*\.nc';
0063 info = regexp({cont(path_is_gran).name}, f_re, 'names');
0064 % str to double and cell array to struct array
0065 info = cellfun(@(S) structfun(@str2double, S, 'UniformOutput', false), info, 'UniformOutput', false);
0066 info = horzcat(info{:});
0067 S_res.time = vec2col([info.hour] * 3600 + [info.minute] * 60 + [info.second]);
0068 S_res.epoch = date2unixsecs(info(1).year, info(1).month, info(1).day);
0069 assert(length(unique([info.day]))==1, ['atmlab:' mfilename ':multi-day'], ...
0070     'Data appear to span multiple days, I did not expect that');
0071 
0072 S_res.version = '?';
0073 end

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