Home > atmlab > files > quickly_read_gzipped_netcdf_header.m

quickly_read_gzipped_netcdf_header

PURPOSE ^

Quickly read a gzipped netcdf header

SYNOPSIS ^

function [varnames, globattr] = quickly_read_gzipped_netcdf_header(file)

DESCRIPTION ^

 Quickly read a gzipped netcdf header

 If you have a large compressed netcdf file and all you need to know is
 what variables are in there, this function may or may not work.  It
 compresses only the beginning of the file, hopefully enough to read out
 header information.  So far it returns only the cell array of strings
 with the variable names.

 WARNING: this function can cause Matlab (2013a) to crash if the header
 gets truncated prematurely.

 FORMAT

   varnames = quickly_read_gzipped_netcdf_header(file)

 IN

   file        string, path to file

 OUT

   varnames    cell array of strings, names of variables

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

quickly_read_gzipped_netcdf_header.m

SOURCE CODE ^

0001 function [varnames, globattr] = quickly_read_gzipped_netcdf_header(file)
0002 
0003 % Quickly read a gzipped netcdf header
0004 %
0005 % If you have a large compressed netcdf file and all you need to know is
0006 % what variables are in there, this function may or may not work.  It
0007 % compresses only the beginning of the file, hopefully enough to read out
0008 % header information.  So far it returns only the cell array of strings
0009 % with the variable names.
0010 %
0011 % WARNING: this function can cause Matlab (2013a) to crash if the header
0012 % gets truncated prematurely.
0013 %
0014 % FORMAT
0015 %
0016 %   varnames = quickly_read_gzipped_netcdf_header(file)
0017 %
0018 % IN
0019 %
0020 %   file        string, path to file
0021 %
0022 % OUT
0023 %
0024 %   varnames    cell array of strings, names of variables
0025 
0026 % $Id: quickly_read_gzipped_netcdf_header.m 8516 2013-06-26 21:33:48Z gerrit $
0027 
0028 % hopefully enough bytes to read the header, otherwise Matlab crashes
0029 % badly.  Therefore, rather on the safe side!  25000 bytes takes <002
0030 header_estimate = 25000;
0031 
0032 d = create_tmpfolder();
0033 c1 = onCleanup(@()rmdir(d,'s'));
0034 outfile = fullfile(d, 'temp.nc');
0035 
0036 % don't use uncompress, I only want the first part
0037 exec_system_cmd(sprintf('gunzip -c %s | head -c %d > %s', ...
0038                         file, header_estimate, outfile));
0039 
0040 globattr = loadncglobalattr(outfile);
0041                     
0042 ncid = netcdf.open(outfile, 'NOWRITE');
0043 c2 = onCleanup(@() netcdf.close (ncid));
0044 
0045 [~,nvars] = netcdf.inq(ncid);
0046 varnames = cell(1, nvars);
0047 for i = 0:nvars-1
0048     varnames{i+1} = netcdf.inqVar(ncid, i);
0049 end
0050 
0051 
0052 end

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