Home > atmlab > deprecated > write_collocations_netcdf.m

write_collocations_netcdf

PURPOSE ^

write_collocations Write collocation data to netcdf file

SYNOPSIS ^

function write_collocations_netcdf(year, month, day, S, D, M,sat1, sensor1, ~, sensor2, info)

DESCRIPTION ^

 write_collocations Write collocation data to netcdf file

 (Note: name write_collocations_netcdf is TEMPORARY)

 Write collocation data for year/month/day in structure S to a netcdf file.
 The filename is determined from the satellite and sensors used.

 FORMAT
   
   write_collocations(year, month, day, S, D, M, sat1, sensor1, sat2, sensor2)

 IN

   year    (numeric)   Year for which S contains collocations
   month   (numeric)   Month for which S contains collocations
   day     (numeric)   Day for which S contains collocations
   S       structure   As returned by collocate_date, structure whose
                       field(s) describe the collocations between the sensors
                       in question. (-1 means none)
   D       structure   As returned by collocate_date, for the data (-1=none)
   M       structure   As returned by collocate_date, for the mean data
                       (-1 means none)
   sat1    string      Primary satellite under consideration
   sensor1 string      Sensor at primary satellite
   sat2    string      Secondary satellite under consideration; not used,
                       because those should be the fieldnames in S/D/M
   sensor2 string      Sensor at secondary satellite.
   info    structure   (optional) Additional global attributes,
                       may overwrite default ones

 OUT

   none (but writes a file)

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

write_collocations_netcdf.m

SOURCE CODE ^

0001 function write_collocations_netcdf(year, month, day, S, D, M, ...
0002     sat1, sensor1, ~, sensor2, info)
0003 
0004 % write_collocations Write collocation data to netcdf file
0005 %
0006 % (Note: name write_collocations_netcdf is TEMPORARY)
0007 %
0008 % Write collocation data for year/month/day in structure S to a netcdf file.
0009 % The filename is determined from the satellite and sensors used.
0010 %
0011 % FORMAT
0012 %
0013 %   write_collocations(year, month, day, S, D, M, sat1, sensor1, sat2, sensor2)
0014 %
0015 % IN
0016 %
0017 %   year    (numeric)   Year for which S contains collocations
0018 %   month   (numeric)   Month for which S contains collocations
0019 %   day     (numeric)   Day for which S contains collocations
0020 %   S       structure   As returned by collocate_date, structure whose
0021 %                       field(s) describe the collocations between the sensors
0022 %                       in question. (-1 means none)
0023 %   D       structure   As returned by collocate_date, for the data (-1=none)
0024 %   M       structure   As returned by collocate_date, for the mean data
0025 %                       (-1 means none)
0026 %   sat1    string      Primary satellite under consideration
0027 %   sensor1 string      Sensor at primary satellite
0028 %   sat2    string      Secondary satellite under consideration; not used,
0029 %                       because those should be the fieldnames in S/D/M
0030 %   sensor2 string      Sensor at secondary satellite.
0031 %   info    structure   (optional) Additional global attributes,
0032 %                       may overwrite default ones
0033 %
0034 % OUT
0035 %
0036 %   none (but writes a file)
0037 %
0038 % $Id: write_collocations_netcdf.m 7553 2012-04-27 19:08:16Z gerrit $
0039 
0040 % FIXME: update to new-style
0041 warning(['atmlab:' mfilename], 'old style function, being phased out, use OO way');
0042 
0043 if ~exist('info', 'var')
0044     info = struct;
0045 end
0046 cols = colloc_constants(['cols_' sensor1 '_' sensor2]);
0047 
0048 fields = fieldnames(S);
0049 for i = 1:length(fields) % for each satellite
0050     satname = fields{i};
0051     if strcmp(satname, 'version') % but not for version
0052         continue
0053     end
0054     
0055     switch number_sats_in_dataset(['collocation_' sensor1 '_' sensor2])
0056         case 1
0057             s = satname;
0058         case 2
0059             s = {sat1, satname};
0060     end
0061     fn = find_datafile_by_date([year month day], s, ...
0062         ['collocation_' sensor1 '_' sensor2]);
0063     
0064     if strcmp(fn(end-1:end), 'gz') % take off this part
0065         fn(end-2:end) = '';
0066     end
0067     
0068     outdir = fileparts(fn);
0069     temp_out = tempname(atmlab('WORK_AREA'));
0070     if ~exist(outdir, 'dir')
0071         logtext(atmlab('OUT'), 'Creating %s\n', outdir);
0072         mkdir(outdir);
0073     end
0074     
0075     %% create the file
0076     
0077     logtext(atmlab('OUT'), 'Writing %s\n', temp_out);
0078     ncid = netcdf.create(temp_out, 'NC_CLOBBER'); % overwrite existing
0079     %cleanupObj = onCleanup(@() cleanup(temp_out, ncid)); % FIXME...
0080     
0081     %% define the dimensions
0082     
0083     ncollocs = size(S.(satname), 1);
0084     dim_collocs = netcdf.defDim(ncid, 'Collocations', ncollocs);
0085     if isstruct(M) % also mean-data
0086         try
0087             dim_meancollocs = netcdf.defDim(ncid, 'Averaged_collocations', size(M.(satname), 1));
0088         catch ME
0089             switch ME.identifier
0090                 case {'MATLAB:netcdf:defDim:onlyOneUnlimitedDimensionAllowed', ...
0091                         'MATLAB:netcdf:defDim:eunlimit:onlyOneUnlimitedDimensionAllowed', ... 
0092                         'MATLAB:imagesci:netcdf:libraryFailure'}
0093                     dim_meancollocs = dim_collocs;
0094                 otherwise
0095                     ME.rethrow();
0096             end
0097         end
0098     end
0099     
0100     %% put global attributes
0101     
0102     utc_time = java.lang.System.currentTimeMillis;
0103     [utc_date{1:6}] = unixsecs2date(utc_time/1000);
0104     utc_datestr = sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ', ...
0105         [utc_date{1:5} round(utc_date{6})]);
0106     global_atts = struct(...
0107         'Conventions', 'CF-1.4', ...
0108         'title', 'Collocations', ...
0109         'history', [datestr(now, 'YYYY-mm-dd') ' Collocations generated from scratch'], ...
0110         'date', utc_datestr, ...
0111         'institution', ['Department of Space Science, Lule' char(unicode2native('å')) ' University of Technology, Kiruna, Sweden'], ...
0112         'source', 'Collocation codes, part of atmlab', ...
0113         'references', 'Holl et al. (2010)', ...
0114         'software_version', atmlab_version, ...
0115         'maxdist_km', colloc_config('distance'), ...
0116         'maxtime_s', colloc_config('interval'), ...
0117         'primary_satellite', sat1, ...
0118         'primary_sensor', sensor1, ...
0119         'primary_version', S.version{1}, ...
0120         'secondary_satellite', satname, ...
0121         'secondary_sensor', sensor2, ...
0122         'secondary_version', S.version{2}, ...
0123         'start_time', double(date2unixsecs(year, month, day)));
0124     % add caller-contributed ones
0125     warning('off', 'catstruct:DuplicatesFound')
0126     global_atts = catstruct(global_atts, info);
0127     % convert to cell-array
0128     global_atts = mat2cell([fieldnames(global_atts) struct2cell(global_atts)], ...
0129         ones(1, length(fieldnames(global_atts))), 2).';
0130     addncattributes(ncid, global_atts);
0131     
0132     %% define variables, variable attributes, additional dimensions
0133     
0134     colloc_vars = intersect(fieldnames(cols.overlap), fieldnames(cols.stored));
0135     data_vars = intersect(fieldnames(cols.data), fieldnames(cols.stored));
0136     if isfield(cols, 'meandata')
0137         mean_vars = intersect(fieldnames(cols.meandata), fieldnames(cols.stored));
0138     else
0139         mean_vars = {};
0140     end
0141     vars = [colloc_vars; data_vars; mean_vars];
0142     vartypes = [repmat({'overlap'}, size(colloc_vars)); ...
0143         repmat({'data'}, size(data_vars)); ...
0144         repmat({'meandata'}, size(mean_vars))];
0145     varids = zeros(size(vars));
0146     dims = struct();
0147     for j = 1:length(vars)
0148         varname = vars{j};
0149         type = cols.stored.(varname).type;
0150         atts = cols.stored.(varname).atts;
0151         % determine length dimension: all collocs or mean collocs?
0152         switch vartypes{j}
0153             case {'overlap', 'data'}
0154                 dim_n = dim_collocs;
0155             case 'meandata'
0156                 dim_n = dim_meancollocs;
0157             otherwise
0158                 error('atmlab:write_collocations_netcdf', 'Unknown vartype: %s', vartypes{j});
0159         end
0160         % check if we have other dimensions besides the length
0161         if isfield(cols.stored.(varname), 'dims') && ~isempty(S.(satname))
0162             dimname = cols.stored.(varname).dims{1};
0163             dimsize = cols.stored.(varname).dims{2};
0164             try
0165                 if ~isfield(dims, dimname)
0166                     dims.(dimname) = netcdf.defDim(ncid, dimname, dimsize);
0167                 end
0168             catch ME
0169                 switch ME.identifier
0170                     case 'MATLAB:netcdf:defDim:nameIsAlreadyInUse'
0171                         % no problem
0172                     otherwise
0173                         ME.rethrow();
0174                 end
0175             end
0176             thisdim = [dim_n dims.(dimname)];
0177         else
0178             thisdim = dim_n;
0179         end
0180         % define variable and put attributes
0181         varid = netcdf.defVar(ncid, varname, type, thisdim);
0182         varids(j) = varid;
0183         for k = fieldnames(atts)'
0184             netcdf.putAtt(ncid, varid, k{1}, atts.(k{1}));
0185         end
0186     end
0187     
0188     %% write data
0189     
0190     % end define mode
0191     
0192     netcdf.endDef(ncid);
0193     
0194     if isempty(S.(satname))
0195         logtext(atmlab('OUT'), 'Nothing to write\n');
0196     else
0197         % put vars
0198         logtext(atmlab('OUT'), 'Writing: ');
0199         
0200         for j = 1:length(vars)
0201             varname = vars{j};
0202             fprintf(atmlab('OUT'), '%s ', varname);
0203             
0204             vartype = vartypes{j};
0205             varid = varids(j);
0206             switch vartype
0207                 case 'overlap'
0208                     netcdf.putVar(ncid, varid, S.(satname)(:, cols.(vartype).(varname)));
0209                 case 'data'
0210                     netcdf.putVar(ncid, varid, D.(satname)(:, cols.(vartype).(varname)));
0211                 case 'meandata'
0212                     if isempty(M.(satname))
0213                         logtext(atmlab('ERR'), 'Warning: No meandata\n');
0214                     else
0215                         netcdf.putVar(ncid, varid, M.(satname)(:, cols.(vartype).(varname)));
0216                     end
0217             end
0218         end
0219         fprintf(atmlab('OUT'), '\n');
0220     end
0221     logtext(atmlab('OUT'), 'Finalising\n');
0222     logtext(atmlab('OUT'), 'Gzipping to %s and removing uncompressed\n', outdir);
0223     netcdf.close(ncid);
0224     gzipped_filename = gzip(temp_out, outdir);
0225     movefile(gzipped_filename{1}, [fn '.gz']);
0226     logtext(atmlab('OUT'), 'Done\n');
0227 
0228 end
0229 end
0230 
0231 function cleanup(temp_out, ncid)
0232 logtext(atmlab('OUT'), 'Cleaning up\n');
0233 try
0234     netcdf.close(ncid);
0235 catch ME
0236     switch ME.identifier
0237         case {'MATLAB:netcdf:inq:notNetcdfID', 'MATLAB:netcdf:close:notNetcdfID', ...
0238                 'MATLAB:netcdf:close:ebadid:notNetcdfID'} % already closed
0239         otherwise
0240             ME.rethrow();
0241     end
0242 end
0243 delete(temp_out);
0244 end

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