javanetcdf-java

How do I get a NetcdfFile out of a byte array?


I am relatively new to using the netcdf-java library, and I've immediately run into a problem when trying to load a file. The problem is that there doesn't seem to be a way to load a NetcdfFile from a byte array stored in memory, and that is the base form of my data. To elaborate a little, it is actually a .cdf file uploaded through a client, which the client then converts into a byte array for the server code to read. So the server, where my code is running, cannot see the uploaded file at all. I also cannot assume the server itself is writable, so essentially there is no "location" to pass into the typical NetcdfFile loading methods.

The FAQ on ucar.edu does mention the possibility of reading from a non-file source, here. It says I should write my own IOSP, which I am happy to do. However, there is very little guidance on how to do this.

I don't know how to implement isValidFile when the only thing passed into the function is a RandomAccessFile, which the FAQ says can be ignored.

I don't know how my IOSP will obtain the byte array in question for use in readData.

I don't know why the minimal example in the FAQ advises me to make a new NetcdfFile class, when it seems I could just use the default one but pass in my custom IOSP.

This question is a little vague, but I am truly lost without many clues on where to even begin. Any guidance would be appreciated.

EDIT: I'm using 5.4.2 of the netcdf-java library.


Solution

  • I found this answer in the support archives. The solution is to use InMemoryRandomAccessFile. The constructor takes a String location and a byte array containing the file's contents. From my testing, I think the location can be any arbitrary string. Here is the code that worked for me.

    byte[] filebytes = retrieveFileBytes(clientFilepath);
    InMemoryRandomAccessFile raf = new InMemoryRandomAccessFile(clientFilepath, filebytes);
    
    NetcdfFile file = NetcdfFiles.open(raf, clientFilepath, null, null);
    Variable peakRetentionTime = file.findVariable("peak_retention_time");
    if (peakRetentionTime == null) {
        displayWarning("peak_retention_time null!");
    } else {
        Array data = peakRetentionTime.read();
        displayInfo(Ncdump.printArray(data));
    }