#!/usr/bin/perl -w # # Example program which uses the ncmanipulate library # to produce an revised version of a NetCDF file. # # This example program is quite long because it shows the various # things you can do, but if you delete all the lines between the # "nc_read" line at the top and the "nc_write" line at the bottom # you will have a very short program which gives identical input # and output files. So if all you want to do is one or two changes # then you will still have a very short program. # #--------------------------------- use NetCDF; use ncmanipulate; $infile="foo.nc"; $outfile="bar.nc"; #---------------------------------------- # read in header info from a NetCDF file $nc = nc_read($infile); # show contents (but not data, as "ncdump -h") nc_show($nc,1); print "----------\n"; # rename the dimension called "dim0" to "dim0a" $d=nc_find_dim($nc,'dim0') and $d->{'name'}='dim0a'; # delete the dimension called "strlen" nc_del_dim($nc,'strlen'); # delete the variable called "recvarstr" nc_del_var($nc,'recvarstr'); # delete the attribute called "att_float" from variable called "fixvar" $v=nc_find_var($nc,'fixvar') and nc_del_att($v,'att_float'); # add a couple of attributes to variable "recvar0" $a1 = nc_att_new('units',NetCDF::CHAR,string_to_chars('bananas')); $a2 = nc_att_new('monkeys',NetCDF::SHORT,[10, 5, 2]); $v=nc_find_var($nc,'recvar0') and nc_add_att($v,$a1, $a2); # add a global attribute nc_add_att($nc,$a2); # add a couple of dims $d1 = nc_dim_new('foo',3); $d2 = nc_dim_new('bar',2); nc_add_dim($nc,$d1,$d2); # and a var whose data values are contained in a list $a3 = nc_att_new('missing_data',NetCDF::LONG,[-32768]); $v = nc_var_new('thing',NetCDF::LONG,[$d1,$d2],[$a3]); @values=(5,1,2,5,32,7); nc_var_setsource($v,'list',[@values]); nc_add_var($nc,$v); # add "_big" to name of any dimensions of size > 2 for $d (nc_dims($nc)) { $d->{'name'} .= "_big" if $d->{'size'} > 2; } # and rename all variables to upper case for $v (nc_vars($nc)) { $v->{'name'} = uc $v->{'name'}; } # and rename global and variable attributes to upper case first letter for $v ($nc, nc_vars($nc)) { for $a (nc_atts($v)) { $a->{'name'} = ucfirst $a->{'name'}; } } # change the record dimension to be one of the dimensions we added $nc->{'unlim'} = $d1; #---------------------------------------- # show revised contents (as "ncdump") nc_show($nc); # and write the output file nc_write($outfile,$nc); nc_close($nc);