[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NetCDF Java questions



Hi James, nice to hear from you again. I remember now you sent me an email from India that I spaced out. I'd like to hear about your travels sometimes....

So where are you working?

James Adams wrote:

Hi John,

I'm working on a servlet to create a NetCDF using Java.  I'm having a
little trouble with a few things which you might be able to help me
with, I certainly hope so.

I want to create a file and I keep getting FileNotFoundExceptions.  Even
if I try to write a file into a directory which has 777 mode (rwx for
everybody) I still get a FileNotFoundException (permission denied). What is the base directory used for the file being created ? Also I'm
running the program as a servlet from Tomcat, so I assume that I will be
creating the file under my web application's base directory,
/var/tomcat/webapps/james1.  I guess that there may be some permissions
issues to resolve as to whether or not the servlet can write files in
the directory etc.  However even when I use an absolute path to a
directory which has all permissions granted such as /home/jadams/netcdf
I still get the  FileNotFoundException (permission denied).  I even had
this working for an hour or so, writing to
/home/jadams/netcdf/mydata.nc, but after a recompile of my servlet and
restart of my browser I started getting the FileNotFoundExceptionms
again.  By this point I'm a bit exasperated, I can't seem to get it to
work.  Can you advise ?


The problem (as far as I can tell) is that tomcat always does you the favor of making all file accesses reletive to some root directory. So you cannot use absolute file paths. I _think_ the default root is the <server>/bin directory.

Try using

  FileInputStream("../webapps/jamesl/<filename>");

or

  URL url = new URL("file:///../webapps/jamesl/<filename>");
  InputStream is = url.openStream();

I need to investigate this more, let me know what you find and I'll do the same.



I'm also having to convert data from float arrays and Vectors of Floats
into ArrayFloat.D1's in order to add them to the NetCDF.  Is it possible
to do a batch copy into these arrays instead of doing the copying one
element at a time with the set() method ?  It would be much more
efficient to do one copy of an entire array or Vector, however it
appears that I can only do a set() for an individual element.  For
example below is a portion of my code which is doing a copy from a
Vector of Floats into a ArrayFloat.D1:



    // convert the Vectors of Floats to NetCDF arrays of floats
    int count = 0;
    Float floatObj;
    ArrayFloat.D1 latArray = new ArrayFloat.D1(latValues.size());
    Enumeration latsEnum = latValues.elements();
    while (latsEnum.hasMoreElements())
    {
        try
        {
            floatObj = (Float)latsEnum.nextElement();
            latArray.set(count, floatObj.floatValue());
        }
        catch (NoSuchElementException ex)
        {
            errorWriter.println("Error copying lat data into NetCDF
array: " + ex);
            return;
        }
        count++;
    }



If you can think of a better more efficient way of doing the above then
please let me know.  Perhaps I've overlooked an obvious method for the
job, maybe in an inherited class of ArrayFloat ?




If you're stuck with Vectors of Floats, I think you can't do any better than an element-by-element copy, because there no access to the underlying data without an element copy. If you can change, at least use ArrayList instead. You still have to do element by element copy but ArrayList is much faster than Vector.

If you have a float[] array, you can do

 int[] shape = new int[1];
 shape[0] = size;
ArrayFloat latArray = (ArrayFloat) ArrayAbstract.factory( float.class, shape, myArray);

which is very efficient (no data copying) and very dangerous!!!! For small arrays its not worth the danger. You might want to do some timing tests to make sure there is really a problem. If not, just do the element-by-element copy. The dynmamic compilers are very good at optimizing.


One other thing I wanted to ask you about - J-F Lamarque has told me
that you may have some software to display irregularly gridded NetCDF
data.  The datasets that I'll need to visualize for the next part of
this application are irregularly gridded, so I'm looking for just such a
thing.  I'd be very interested in hearing about what you've built and
whether or not I might be able to incorporate some of it into my web
application.


I'm not yet doing irregular grids, but I might be able to extend. WHy dont you put a datafile on ftp so i can look at it?

Meanwhile, you can play with GDV at

 http://www.unidata.ucar.edu/projects/metapps/webstart/GDV/