package gov.usgs.trinli;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.NetcdfFile;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.ncml.AggregationUtil;

/**
 *
 * @author cwardgar
 */
public class Mlar {
    public static void main(String[] args) throws IOException, InvalidRangeException {
        union();
        joinExisting();
    }
    
    public static void union() throws IOException {
        File cldcFile = new File("/z/foldem/home/cwardgar/Desktop/cldc.mean.nc");
        File lflxFile = new File("/z/foldem/home/cwardgar/Desktop/lflx.mean.nc");
        NetcdfFile cldcNcFile = NetcdfFile.open(cldcFile.getAbsolutePath());
        NetcdfFile lflxNcFile = NetcdfFile.open(lflxFile.getAbsolutePath());

        NetcdfDataset unionDataset = AggregationUtil.union(Arrays.asList(cldcNcFile, lflxNcFile));
        try {
            System.out.println(unionDataset);
        } finally {
            unionDataset.close();   // Closes cldcNcFile and lflxNcFile.
        }
    }
    
    public static void joinExisting() throws IOException, InvalidRangeException {
        File janFile = new File("/z/foldem/home/cwardgar/Desktop/jan.nc");
        File febFile = new File("/z/foldem/home/cwardgar/Desktop/feb.nc");
        NetcdfFile janNcFile = NetcdfFile.open(janFile.getAbsolutePath());
        NetcdfFile febNcFile = NetcdfFile.open(febFile.getAbsolutePath());
        
        NetcdfDataset joinExistingDataset = AggregationUtil.joinExisting("time", Arrays.asList(janNcFile, febNcFile));
        try {
            System.out.println(joinExistingDataset);
            System.out.println(joinExistingDataset.readSection("time"));
        } finally {
            joinExistingDataset.close();    // Closes janFile and febFile.
        }
    }
}
