Re: [netcdf-java] Aggregation without NcML

HI Christian,

 

Very interesting... it would be great to see if this mechanism could be
used to generate an FMRC aggregation (i.e. aggregating along a time
dimension) simply by giving it a directory of files, with no need to
(manually) create NcML.

 

Cheers, Jon

 

From: netcdf-java-bounces@xxxxxxxxxxxxxxxx
[mailto:netcdf-java-bounces@xxxxxxxxxxxxxxxx] On Behalf Of Christian D
Ward-Garrison
Sent: 22 March 2010 14:47
To: Christian D Ward-Garrison
Cc: netcdf-java@xxxxxxxxxxxxxxxx
Subject: Re: [netcdf-java] Aggregation without NcML

 

Well, it's pretty clear that John didn't intend for the aggregation
machinery to be used directly (it's not in the public Javadoc), but
after a couple hours with the debugger, I think I've hacked together a
fairly clean solution: 


package gov.usgs.shodan;

import java.io.File;
import java.io.IOException;
import ucar.nc2.NetcdfFile;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.ncml.Aggregation;
import ucar.nc2.ncml.AggregationUnion;
import ucar.nc2.util.CancelTask;

/**
 *
 * @author cwardgar
 */
public class Mlar {
    public static void main(String[] args) throws IOException {
        File cldcFile = new File("C:/Documents and
Settings/cwardgar/Desktop/cldc.mean.nc");
        File lflxFile = new File("C:/Documents and
Settings/cwardgar/Desktop/lflx.mean.nc");
        NetcdfFile cldcNcFile =
NetcdfFile.open(cldcFile.getAbsolutePath());
        NetcdfFile lflxNcFile =
NetcdfFile.open(lflxFile.getAbsolutePath());

        NetcdfDataset unionDataset = unionNetcdf(cldcNcFile,
lflxNcFile);
        System.out.println(unionDataset);
        unionDataset.close();   // Closes cldcNcFile and lflxNcFile.
    }

    public static NetcdfDataset unionNetcdf(NetcdfFile ncFile1,
NetcdfFile ncFile2) throws IOException {
        NetcdfDataset aggDataset = new NetcdfDataset();

        AggregationUnionOpenDataset union = new
AggregationUnionOpenDataset(aggDataset);
        union.addOpenFile(ncFile1);
        union.addOpenFile(ncFile2);

        union.finish(null);
        aggDataset.finish();
        return aggDataset;
    }

    // Aggregation.Dataset is a non-static nested class. So, to subclass
it, I must create an enclosing
    // Aggregation subclass.
    public static class AggregationUnionOpenDataset extends
AggregationUnion {
        public AggregationUnionOpenDataset(NetcdfDataset unionDataset) {
            super(unionDataset, null, null);
        }

        public void addOpenFile(NetcdfFile openFile) {
            addDataset(new OpenUnionDataset(openFile));
        }

        public class OpenUnionDataset extends Aggregation.Dataset {
            private NetcdfFile openFile;

            public OpenUnionDataset(NetcdfFile openFile) {
                super(openFile.getLocation());
                this.openFile = openFile;
            }

            @Override
            public NetcdfFile acquireFile(CancelTask cancelTask) throws
IOException {
                return openFile;
            }
        }
    }
}


Note that this example uses the same datasets that the NcML tutorial
(http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Aggregation.html)
does and will print a similar CDL. 

Now I've just got to figure out how to aggregate on an existing
dimension and scan a directory. 



-----netcdf-java-bounces@xxxxxxxxxxxxxxxx wrote: -----

To: "netcdf-java@xxxxxxxxxxxxxxxx" <netcdf-java@xxxxxxxxxxxxxxxx>
From: Christian D Ward-Garrison <cwardgar@xxxxxxxx>
Sent by: netcdf-java-bounces@xxxxxxxxxxxxxxxx
Date: 03/19/2010 07:30AM
Subject: [netcdf-java] Aggregation without NcML

Hello,

Is it possible to aggregate NetCDF datasets from within my
application--that is, without creating an NcML document for them
beforehand?

Suppose I want to write a function that takes 2 arbitrary NetcdfFile
objects and returns another NetcdfFile object that is the union of them.
What's the best way to do this?

Thanks,
Christian Ward-Garrison
_______________________________________________
netcdf-java mailing list
netcdf-java@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit:
http://www.unidata.ucar.edu/mailing_lists/  

 

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