Home > atmlab > deprecated > read_collocs_data_mean.m

read_collocs_data_mean

PURPOSE ^

read_collocs_data_mean read fields from collocation netcdf

SYNOPSIS ^

function M = read_collocs_data_mean(fn, cols, c)

DESCRIPTION ^

 read_collocs_data_mean read fields from collocation netcdf

 Given the paths to a collocation NetCDF file, reads collocations,
 collocation data and collocation meandata respectively. The 'cols'
 cell describes what fields to return.

 FORMAT

   M = read_collocs_data_mean(fn, cols)

 IN

   fn          string      filename with collocations in NetCDF
   cols        cell array  cells with what fields to read
   c           struct      as colloc_constants('cols_cpr_mhs') or so

 OUT

   M           NxM matrix  Matrix containing the collected info

 $Id: read_collocs_data_mean.m 7553 2012-04-27 19:08:16Z gerrit $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

read_collocs_data_mean.m

SOURCE CODE ^

0001 function M = read_collocs_data_mean(fn, cols, c)
0002 
0003 % read_collocs_data_mean read fields from collocation netcdf
0004 %
0005 % Given the paths to a collocation NetCDF file, reads collocations,
0006 % collocation data and collocation meandata respectively. The 'cols'
0007 % cell describes what fields to return.
0008 %
0009 % FORMAT
0010 %
0011 %   M = read_collocs_data_mean(fn, cols)
0012 %
0013 % IN
0014 %
0015 %   fn          string      filename with collocations in NetCDF
0016 %   cols        cell array  cells with what fields to read
0017 %   c           struct      as colloc_constants('cols_cpr_mhs') or so
0018 %
0019 % OUT
0020 %
0021 %   M           NxM matrix  Matrix containing the collected info
0022 %
0023 % $Id: read_collocs_data_mean.m 7553 2012-04-27 19:08:16Z gerrit $
0024 
0025 %% read data
0026 
0027 logtext(atmlab('OUT'), 'Reading %s\n', fn);
0028 tmp = tempname(atmlab('WORK_AREA'));
0029 exec_system_cmd(['zcat ' fn '>' tmp]); % 3x faster than ML gunzip
0030 %filenames = gunzip(fn, atmlab('WORK_AREA'));
0031 %ncid = netcdf.open(filenames{1}, 'NOWRITE');
0032 ncid = netcdf.open(tmp, 'NOWRITE');
0033 %delete(filenames{1});
0034 delete(tmp);
0035 cleanupObj = onCleanup(@()netcdf.close(ncid));
0036 varids = nan(size(cols));
0037 varwidth = zeros(size(cols));
0038 hasmean = false;
0039 % check dimensions, if any mean will need those first
0040 % also to know pre-alloc size
0041 alloc_length = 0;
0042 i = 1;
0043 while i <= length(cols)
0044     if isnumeric(cols{i}) % refers to no. in previous
0045         varids(i) = -1;
0046         i = i + 1;
0047         continue
0048     end
0049     col = cols{i};
0050     % If col is an alias (e.g. MHS = AMSU(16:20)), replace accordingly
0051     if any(strcmp(col, fieldnames(c.aliases)))
0052         % if columns to read specified, replace those as well
0053         if i~=length(cols) && isnumeric(cols{i+1})
0054             cols{i+1} = c.aliases.(col){2}(cols{i+1});
0055         elseif length(c.aliases.(col)) > 1
0056             % insert channels to read
0057             cols = [cols(1:i) c.aliases.(col){2} cols((i+1):end)];
0058         end
0059         cols{i} = c.aliases.(col){1};
0060         col = cols{i};
0061     end
0062     % proceed with getting information
0063     varid = netcdf.inqVarID(ncid, col);
0064     [~, ~, dimids] = netcdf.inqVar(ncid, varid);
0065     % check that it has <3 dimensions
0066     assert(length(dimids)<3, 'atmlab:read_collocs_data_mean', ...
0067         'Error: variable %s has %d dimensions, but shall have max 2', ...
0068         col, length(dimids));
0069     
0070     if length(dimids)==1
0071         % only Collocations/Averaged_collocations, so no width, so it's
0072         % a vector, width=1
0073         varwidth(i) = 1;
0074     end
0075     for dimid = dimids
0076         [dimname, dimlen] = netcdf.inqDim(ncid, dimid);
0077         switch dimname
0078             case 'Collocations'
0079                 if ~hasmean
0080                     alloc_length = dimlen;
0081                 end
0082             case 'Averaged_collocations'
0083                 hasmean = true;
0084                 alloc_length = dimlen;
0085             otherwise
0086                 % not Collocations/Averaged_collocations, so additional
0087                 % dimension, so width=dimlen, unless user has specified
0088                 % the additional dimension
0089                 % check if additional dimension specified:
0090                 if i~=length(cols) && isnumeric(cols{i+1})
0091                     varwidth(i) = length(cols{i+1});
0092                 else
0093                     varwidth(i) = dimlen;
0094                 end
0095         end
0096     end % for each dim
0097     varids(i) = varid;
0098     i = i + 1;
0099 end % for each col
0100 M = zeros(alloc_length, sum(varwidth));
0101 if alloc_length==0 % nothing
0102     return
0103 end
0104 if hasmean
0105     var_mean = netcdf.inqVarID(ncid, 'FIRST');
0106     rows = netcdf.getVar(ncid, var_mean);
0107 else
0108     rows = true(alloc_length, 1);
0109 end
0110 
0111 n = 1; % keep track of dims
0112 for i = 1:length(cols)
0113     if varids(i)==-1 % not a var, see comment about skipping above
0114         continue;
0115     end
0116     var = netcdf.getVar(ncid, varids(i));
0117     % determine what rows to read
0118     if size(var, 1) > alloc_length
0119         rows_here = rows;
0120     elseif size(var, 1) == alloc_length
0121         rows_here = true(alloc_length, 1);
0122     else
0123         error('atmlab:read_collocs_data_mean', ...
0124             'Incorrect number of rows in data');
0125     end
0126     % determine what columns to read
0127     if i~=length(cols) && varids(i+1)==-1 % next var is column
0128         cols_here = cols{i+1};
0129     else % read all columns
0130         cols_here = 1:varwidth(i);
0131     end
0132     M(:, n:(n+varwidth(i)-1)) = ...
0133         reshape(var(rows_here, cols_here), [alloc_length varwidth(i)]);
0134     n = n + varwidth(i);
0135 end

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