edu.wisc.ssec.adapter.NetCDFFile.java Source code

Java tutorial

Introduction

Here is the source code for edu.wisc.ssec.adapter.NetCDFFile.java

Source

/*
 * This file is part of McIDAS-V
 *
 * Copyright 2007-2013
 * Space Science and Engineering Center (SSEC)
 * University of Wisconsin - Madison
 * 1225 W. Dayton Street, Madison, WI 53706, USA
 * http://www.ssec.wisc.edu/mcidas
 * 
 * All Rights Reserved
 * 
 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
 * some McIDAS-V source code is based on IDV and VisAD source code.  
 * 
 * McIDAS-V is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * McIDAS-V is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

package edu.wisc.ssec.adapter;

import ucar.nc2.*;
import ucar.nc2.ncml.NcMLReader;
import ucar.ma2.*;

import java.lang.Number;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.net.URL;
import java.io.InputStream;
import java.io.ByteArrayInputStream;

import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.jdom2.Document;
import org.jdom2.Element;

public class NetCDFFile implements MultiDimensionReader {

    HashMap<String, Variable> varMap = new HashMap<String, Variable>();
    HashMap<String, String[]> varDimNames = new HashMap<String, String[]>();
    HashMap<String, int[]> varDimLengths = new HashMap<String, int[]>();
    HashMap<String, Class> varDataType = new HashMap<String, Class>();
    HashMap<String, String> varUnits = new HashMap<String, String>();

    NetcdfFile ncfile = null;

    public static NetCDFFile makeUnion(String filename, String other) throws Exception {
        Object obj = new Object();
        URL url = obj.getClass().getResource("/edu/wisc/ssec/mcidasv/data/hydra/resources/union.ncml");
        SAXBuilder builder = new SAXBuilder(false);
        Document doc = null;

        try {
            doc = builder.build(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();

        List list = root.getChildren();

        list = ((Element) list.get(1)).getChildren();

        org.jdom2.Attribute attr1 = (org.jdom2.Attribute) (((Element) list.get(0)).getAttributes()).get(0);
        attr1.setValue(filename);

        org.jdom2.Attribute attr2 = (org.jdom2.Attribute) (((Element) list.get(1)).getAttributes()).get(0);
        attr2.setValue(other);

        XMLOutputter xmlOut = new XMLOutputter();
        String newStr = xmlOut.outputString(doc);
        ByteArrayInputStream is = new ByteArrayInputStream(newStr.getBytes());
        return new NetCDFFile(is);
    }

    public NetCDFFile(InputStream is) throws Exception {
        ncfile = NcMLReader.readNcML(is, null);
        init();
    }

    public NetCDFFile(String filename) throws Exception {
        if (filename.endsWith(".ncml")) {
            java.io.FileReader rdr = new java.io.FileReader(filename);
            ncfile = NcMLReader.readNcML(rdr, null);
        } else {
            ncfile = NetcdfFile.open(filename);
        }
        init();
    }

    public NetCDFFile(String filename, org.jdom2.Element root) throws Exception {
        ncfile = NcMLReader.readNcML(filename, root, null);
        init();
    }

    private void init() throws Exception {
        Iterator varIter = ncfile.getVariables().iterator();
        while (varIter.hasNext()) {
            Variable var = (Variable) varIter.next();

            if (var instanceof Structure) {
                analyzeStructure((Structure) var);
                continue;
            }

            int rank = var.getRank();
            String varName = var.getFullName();
            varMap.put(varName, var);
            Iterator dimIter = var.getDimensions().iterator();
            String[] dimNames = new String[rank];
            int[] dimLengths = new int[rank];
            int cnt = 0;
            while (dimIter.hasNext()) {
                Dimension dim = (Dimension) dimIter.next();
                String dim_name = dim.getShortName();
                if (dim_name == null)
                    dim_name = "dim" + cnt;
                dimNames[cnt] = dim_name;
                dimLengths[cnt] = dim.getLength();
                cnt++;
            }
            varDimNames.put(varName, dimNames);
            varDimLengths.put(varName, dimLengths);
            varDataType.put(varName, var.getDataType().getPrimitiveClassType());

            Attribute attr = var.findAttribute("units");
            if (attr != null) {
                String unitStr = attr.getStringValue();
                varUnits.put(varName, unitStr);
            }
        }
    }

    void analyzeStructure(Structure var) throws Exception {
        if ((var.getShape()).length == 0) {
            return;
        }
        String varName = var.getFullName();
        String[] dimNames = new String[2];
        int[] dimLengths = new int[2];
        int cnt = 0;
        dimLengths[0] = (var.getShape())[0];
        dimNames[0] = "dim" + cnt;

        cnt++;
        StructureData sData = var.readStructure(0);
        List memList = sData.getMembers();
        dimLengths[1] = memList.size();
        dimNames[1] = "dim" + cnt;

        varDimNames.put(varName, dimNames);
        varDimLengths.put(varName, dimLengths);
        varMap.put(varName, var);

        StructureMembers sMembers = sData.getStructureMembers();
        Object obj = sData.getScalarObject(sMembers.getMember(0));
        varDataType.put(varName, obj.getClass());
    }

    public Class getArrayType(String array_name, int idx) throws Exception {
        Structure var = (Structure) varMap.get(array_name);
        StructureData sData = var.readStructure(0);
        StructureMembers sMembers = sData.getStructureMembers();
        Object obj = sData.getScalarObject(sMembers.getMember(idx));
        return obj.getClass();
    }

    public Class getArrayType(String array_name) {
        return varDataType.get(array_name);
    }

    public String[] getDimensionNames(String array_name) {
        return varDimNames.get(array_name);
    }

    public int[] getDimensionLengths(String array_name) {
        return varDimLengths.get(array_name);
    }

    public String getArrayUnitString(String array_name) {
        return varUnits.get(array_name);
    }

    public int getDimensionLength(String dimName) {
        Dimension dim = ncfile.findDimension(dimName);
        if (dim != null) {
            return dim.getLength();
        } else {
            return -1;
        }
    }

    public float[] getFloatArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
        return (float[]) readArray(array_name, start, count, stride);
    }

    public int[] getIntArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
        return (int[]) readArray(array_name, start, count, stride);
    }

    public double[] getDoubleArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
        return (double[]) readArray(array_name, start, count, stride);
    }

    public short[] getShortArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
        return (short[]) readArray(array_name, start, count, stride);
    }

    public byte[] getByteArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
        return (byte[]) readArray(array_name, start, count, stride);
    }

    public Object getArray(String array_name, int[] start, int[] count, int[] stride, Object obj) throws Exception {
        return readArray(array_name, start, count, stride);
    }

    protected synchronized Object readArray(String array_name, int[] start, int[] count, int[] stride)
            throws Exception {
        Variable var = varMap.get(array_name);
        if (var instanceof Structure) {
            Array array = Array.factory(getArrayType(array_name, start[1]), count);
            Index2D idx = new Index2D(count);
            for (int i = 0; i < count[0]; i++) {
                StructureData sData = ((Structure) var).readStructure(start[0] + i * stride[0]);
                StructureMembers sMembers = sData.getStructureMembers();
                for (int j = 0; j < count[1]; j++) {
                    StructureMembers.Member mem = sMembers.getMember(start[1] + j * stride[1]);
                    int size = mem.getSize();
                    if (size == 1) {
                        Object obj = sData.getScalarObject(mem);
                        idx.set(i, j);
                        array.setObject(idx, obj);
                    } else { // if size > 1 assume array and just return  
                        array = sData.getArray(mem);
                    }
                }
            }
            return array.copyTo1DJavaArray();
        } else {
            ArrayList rangeList = new ArrayList();
            for (int i = 0; i < start.length; i++) {
                Range rng = new Range(start[i], start[i] + (count[i] - 1) * stride[i], stride[i]);
                rangeList.add(i, rng);
            }
            Array array = var.read(rangeList);
            return array.copyTo1DJavaArray();
        }
    }

    public Number getAttributeValue(String path, String attr_name) {
        Group grp = ncfile.findGroup(path);
        Attribute attr = grp.findAttribute(attr_name);
        Number num = attr.getNumericValue();
        return num;
    }

    public HDFArray getGlobalAttribute(String attr_name) throws Exception {
        Object array = null;
        DataType dataType = null;
        Array attrVals = null;

        Attribute attr = ncfile.findGlobalAttribute(attr_name);
        if (attr != null) {
            attrVals = attr.getValues();
            dataType = attr.getDataType();
            if (dataType.isNumeric()) {
                array = attrVals.copyTo1DJavaArray();
            }
        } else {
            return null;
        }

        HDFArray harray = null;

        if (dataType.getPrimitiveClassType() == Float.TYPE) {
            harray = HDFArray.make((float[]) array);
        } else if (dataType.getPrimitiveClassType() == Double.TYPE) {
            harray = HDFArray.make((double[]) array);
        } else if (dataType.getPrimitiveClassType() == Short.TYPE) {
            harray = HDFArray.make((short[]) array);
        } else if (dataType.getPrimitiveClassType() == Integer.TYPE) {
            harray = HDFArray.make((int[]) array);
        } else if (dataType.getPrimitiveClassType() == Byte.TYPE) {
            harray = HDFArray.make((byte[]) array);
        } else if (dataType == DataType.STRING) {
            int len = (int) attrVals.getSize();
            String[] sa = new String[len];
            for (int k = 0; k < sa.length; k++) {
                sa[k] = (String) attrVals.getObject(k);
            }
            harray = HDFArray.make(sa);
        }

        return harray;
    }

    public HDFArray getArrayAttribute(String array_name, String attr_name) throws Exception {
        Object array = null;
        DataType dataType = null;
        Array attrVals = null;

        Variable var = varMap.get(array_name);
        if (var != null) {
            Attribute attr = var.findAttribute(attr_name);
            if (attr != null) {
                attrVals = attr.getValues();
                dataType = attr.getDataType();
                if (dataType.isNumeric()) {
                    array = attrVals.copyTo1DJavaArray();
                }
            } else {
                return null;
            }
        }

        HDFArray harray = null;

        if (dataType.getPrimitiveClassType() == Float.TYPE) {
            harray = HDFArray.make((float[]) array);
        } else if (dataType.getPrimitiveClassType() == Double.TYPE) {
            harray = HDFArray.make((double[]) array);
        } else if (dataType.getPrimitiveClassType() == Short.TYPE) {
            harray = HDFArray.make((short[]) array);
        } else if (dataType.getPrimitiveClassType() == Integer.TYPE) {
            harray = HDFArray.make((int[]) array);
        } else if (dataType.getPrimitiveClassType() == Byte.TYPE) {
            harray = HDFArray.make((byte[]) array);
        } else if (dataType == DataType.STRING) {
            int len = (int) attrVals.getSize();
            String[] sa = new String[len];
            for (int k = 0; k < sa.length; k++) {
                sa[k] = (String) attrVals.getObject(k);
            }
            harray = HDFArray.make(sa);
        }

        return harray;
    }

    public void close() throws Exception {
        ncfile.close();
    }

    public HashMap getVarMap() {
        return varMap;
    }

    public boolean hasArray(String name) {
        if (varMap.get(name) == null) {
            return false;
        } else {
            return true;
        }
    }

    public boolean hasDimension(String name) {
        if (ncfile.findDimension(name) != null) {
            return true;
        } else {
            return false;
        }
    }

    public NetcdfFile getNetCDFFile() {
        return ncfile;
    }

    public static void main(String[] args) throws Exception {
        NetCDFFile ncfile = new NetCDFFile(args[0]);
        ncfile.close();
    }
}