Ruby Interface

Dear the NetCDF users,

My colleagues and I have been developing an interface of the
NetCDF library for the object-oriented script language Ruby
(www.ruby-lang.org). Ruby is a wonderful language that makes
programming easier, faster, and more enjoyable (or less
stressful :-p). The Ruby interface is available from

   http://ruby.gfd-dennou.org/

 or more specifically,

   http://ruby.gfd-dennou.org/products/ruby-netcdf/

The Ruby interface, RubyNetCDF, uses NArray, an efficient
multi-dimensional numeric array library for Ruby, whose
functionality is like arrays in Fortran90.

The latest version of RubyNetCDF is 0.5.0. It covers all the
functionality of the NetCDF library. In addition, there are
combination methods (functions are called "methods" in Ruby)
that facilitate NetCDF data handling. For instance, "iterators"
are useful to scan files and variables. Also, you do not have
to change functions depending on the numeric types to read/write
data. The program at the end of this email makes an exact copy
of any NetCDF file. How many lines would be needed if it is
written in C?  Furthermore, there are methods that automatically
interprets add_offset&scale_factor and data missing, if you like.

In addition to the NetCDF interface, graphic and scientific
math&statistics libraries are available. See the web site above.

Here is the program that copies a file (texts following # are
comments):

--------- copy.rb (from here) ------------------
# 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 our Numerical Ruby suite
include NumRu
raise usage if ARGV.length != 2     # error if # of command args are not two
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 )}
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
--------- copy.rb (to here) ------------------

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.

Hope you enjoy. 

# Any contribution from you is very welcome.

--
Takeshi Horinouchi
Radio Science Center for Space and Atmosphere (RASC)
Kyoto Univ., Uji, 611-0011, Japan



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