Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

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

 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Community Programs   Unidata is a member of the UCAR Community Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690