How to save a ZIP file within a NetCDF file

Folks,

Here's a problem we encounter all the time in the
modeling business:  we have a model run (netcdf output,
of course), and we can't quite figure out what version of
the code actually was used to produce it.  Oh sure,
it says "version 2.1" but that was before we hacked
the code to fix X, or put in that special little patch
just for this run (or was it taken out)?  Etc.

One solution would be to simply zip the
source code and stuff it *right into the netcdf*
output files, storing it as a byte variable.
I tried it, and it works.  Cool!

This is very simple in Matlab using the
Matlab/NetCDF toolkit <http://mexcdf.sourceforge.net/>
and the codes to put the ZIP file in and get it back out
again look like this:

function nc_put_zip(ncfile,zipfile,zipnam);
% NC_PUT_ZIP puts an existing zip file into an existing NetCDF file,
%    storing it as a byte variable
% Usage: nc_put_zip(ncfile,zipfile,zipvar);
%    ncfile = netcdf file (existing)
%    zipfile = zip file (existing)
%    zipnam = name for netcdf variable to hold zip file data

% 1. Open the zip file and read all the bytes as big-endian
fid=fopen(zipfile,'r','ieee-be');
b=fread(fid,inf,'char');
nbytes=length(b);
fclose(fid);

% 2. Open netcdf file for writing, declare byte variable
% and stuff in the data
nc=netcdf(ncfile,'w');
nc(zipnam)=nbytes;
nc{zipnam}=ncbyte(zipnam);
nc{zipnam}(:)=b;
close(nc);
%%%%%%%%%%%%%%%

function nc_get_zip(ncfile,zipfile,zipnam);
% NC_GET_ZIP gets a zip file byte variable from
%    an existing netCDF file and writes the zip file.
% Usage: nc_get_zip(ncfile,zipfile,zipnam);
%    ncfile = netcdf file (existing)
%    zipfile = zip file to be created (new)
%    zipnam = name for netcdf variable that holds the zip file data

% Open the netcdf file, extract the byte data
nc2=netcdf(ncfile,'r');
c=nc2{zipnam}(:);
close(nc2);
% Write the byte data as big-endian to a new zip file
fid=fopen(zipfile,'w','ieee-be');
fwrite(fid,c,'char');
fclose(fid);

If you want to see an example, look at:
http://cove.whoi.edu/~rsignell/cf

So here's the the question.  After spending 15 minutes
doing this in Matlab, I spent 1.5 hours trying to figure
out how to write subroutines that would do this in FORTRAN,
and then gave up.   Would anyone know how to do this that
could help me (and the community, assuming this is of general
use) out?

Thanks,
Rich

--
Richard P. Signell           rsignell@xxxxxxxx
U.S. Geological Survey       Phone: (508) 457-2229
384 Woods Hole Road          Fax:   (508) 457-2310
Woods Hole, MA 02543-1598

  • 2004 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: