Re: [netcdf-java] Linear algebra on data read from a netCDF file

On 16 Oct 2009, at 14:06, Konrad Hinsen wrote:

Thanks! This should at least be efficient. I quickly checked the Colt implementation, which does have a constructor for its array class that takes the plain Java array plus the shape information, so at least in theory this should be simple. If/when I get it to work, I'll post my solution here.

Here is a working example (Clojure source code) for inverting a matrix read from a netCDF file using Colt:

(ns netcdf-read
  (:import (ucar.nc2 NetcdfFile)
           (cern.colt.matrix.tdouble.impl DenseDoubleMatrix2D)
           (cern.colt.matrix.tdouble.algo DenseDoubleAlgebra)))

(defn unidata-to-colt
  [#^ucar.ma2.ArrayDouble$D2 array]
  (let [java-array-1d (.get1DJavaArray array Double)
        [rows, cols]  (.getShape array)]
(new DenseDoubleMatrix2D rows cols java-array-1d 0 0 cols 1 false)))

(let [matrix (with-open [ncfile (NetcdfFile/open "/Users/hinsen/ Temp/matrix.nc")]
                    (.read (.findVariable ncfile "matrix")))
      colt-matrix (unidata-to-colt matrix)
      la          (new DenseDoubleAlgebra)
      inverse     (.inverse la colt-matrix)]
  (prn inverse))


For my current needs, conversion of 2D double arrays is sufficient, but this could of course be generalized to ranks 1 and 3 (that's all there is in Colt) and other element types.

Konrad.



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