The following is all the Ruby code needed to copy a netCDF file using only the Ruby interface:
# Usage # ruby copy.rb filename_from filename_to def usage "\n\nUSAGE:\n% ruby #{$0} filename_from filename_to\n" end require "numru/netcdf" # "numru" is Numerical Ruby suite include NumRu raise usage if ARGV.length != 2 # error if wrong number of args filename_from, filename_to = ARGV from = NetCDF.open(filename_from) # open a NetCDF dataset(default:readonly) to = NetCDF.create(filename_to) from.each_dim{|dim| to.def_dim( dim.name, dim.length_ul0 )} # preserve unlimited from.each_att{|att| to.put_att( att.name, att.get )} # copy global atts from.each_var{|var| newvar = to.def_var( var.name, var.ntype, var.dim_names ) var.each_att{|att| newvar.put_att( att.name, att.get )} } to.enddef from.each_var{|var| to.var(var.name).put(var.get)} to.close
Here, each_dim, each_att, and each_var are iterators. The each_att method handles global attributes if applied to a NetCDF dataset; if it is applied to a variable, it handles the variable's attributes.