001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2017
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see http://www.gnu.org/licenses.
027 */
028
029package edu.wisc.ssec.mcidasv.data.hydra;
030
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033import ucar.nc2.*;
034import ucar.nc2.ncml.NcMLReader;
035import ucar.ma2.*;
036
037import java.util.HashMap;
038import java.util.List;
039import java.util.ArrayList;
040import java.util.Iterator;
041import java.net.URL;
042import java.io.InputStream;
043import java.io.ByteArrayInputStream;
044import java.util.Map;
045
046import org.jdom2.input.SAXBuilder;
047import org.jdom2.output.XMLOutputter;
048import org.jdom2.Document;
049import org.jdom2.Element;
050
051
052public class NetCDFFile implements MultiDimensionReader {
053    
054    private static final Logger logger = LoggerFactory.getLogger(NetCDFFile.class);
055   private final Map<String, Variable> varMap = new HashMap<>();
056   private final Map<String, String[]> varDimNames = new HashMap<>();
057   private final Map<String, int[]> varDimLengths = new HashMap<>();
058   private final Map<String, Class> varDataType = new HashMap<>();
059   private final Map<String, String> varUnits = new HashMap<>();
060
061   NetcdfFile ncfile = null;
062
063   public static NetCDFFile makeUnion(String filename, String other) throws Exception {
064     Object obj = new Object();
065     URL url = obj.getClass().getResource("/edu/wisc/ssec/mcidasv/data/hydra/resources/union.ncml");
066     SAXBuilder builder = new SAXBuilder(false);
067     Document doc = null;
068
069     try {
070       doc = builder.build(url);
071     } catch (Exception e) {
072       e.printStackTrace();
073     }
074     Element root = doc.getRootElement();
075
076     List list = root.getChildren();
077
078     list = ((Element)list.get(1)).getChildren();
079
080     org.jdom2.Attribute attr1 = (((Element)list.get(0)).getAttributes()).get(0);
081     attr1.setValue(filename);
082
083     org.jdom2.Attribute attr2 = (((Element)list.get(1)).getAttributes()).get(0);
084     attr2.setValue(other);
085
086     XMLOutputter xmlOut = new XMLOutputter();
087     String newStr = xmlOut.outputString(doc);
088       logger.trace("union string:\n{}", newStr);
089     ByteArrayInputStream is = new ByteArrayInputStream(newStr.getBytes());
090     return new NetCDFFile(is);
091   }
092
093   public NetCDFFile(InputStream is) throws Exception {
094     ncfile = NcMLReader.readNcML(is, null);
095     init();
096   }
097
098   public NetCDFFile(String filename) throws Exception {
099     if (filename.endsWith(".ncml")) {
100       java.io.FileReader rdr = new java.io.FileReader(filename);
101       ncfile = NcMLReader.readNcML(rdr, null);
102     }
103     else {
104       ncfile = NetcdfFile.open(filename);
105     }
106     init();
107   }
108     
109   public NetCDFFile(String filename, org.jdom2.Element root) throws Exception {
110          ncfile = NcMLReader.readNcML(filename, root, null);
111          init();
112   }
113   
114   private void init() throws Exception {
115     Iterator varIter = ncfile.getVariables().iterator();
116     while(varIter.hasNext()) {
117       Variable var = (Variable) varIter.next();
118
119       if (var instanceof Structure) {
120         analyzeStructure((Structure) var);
121         continue;
122       }
123
124       int rank = var.getRank();
125       String varName = var.getFullName();
126       varMap.put(varName, var);
127       Iterator dimIter = var.getDimensions().iterator();
128       String[] dimNames = new String[rank];
129       int[] dimLengths = new int[rank];
130       int cnt = 0;
131       while(dimIter.hasNext()) {
132         Dimension dim = (Dimension) dimIter.next();
133         String dim_name = dim.getShortName();
134         if (dim_name == null) dim_name = "dim"+cnt;
135         dimNames[cnt] = dim_name;
136         dimLengths[cnt] = dim.getLength();
137         cnt++;
138       }
139       varDimNames.put(varName, dimNames);
140       varDimLengths.put(varName, dimLengths);
141       varDataType.put(varName, var.getDataType().getPrimitiveClassType());
142       
143       Attribute attr = var.findAttribute("units");
144       if (attr != null) {
145         String unitStr = attr.getStringValue();
146         varUnits.put(varName, unitStr);
147       }
148     }
149   }
150
151   void analyzeStructure(Structure var) throws Exception {
152           if ((var.getShape()).length == 0) {
153                   return;
154           }
155           String varName = var.getFullName();
156           String[] dimNames = new String[2];
157           int[] dimLengths = new int[2];
158           int cnt = 0;
159           dimLengths[0] = (var.getShape())[0];
160           dimNames[0] = "dim" + cnt;
161
162           cnt++;
163           StructureData sData = var.readStructure(0);
164           List memList = sData.getMembers();
165           dimLengths[1] = memList.size();
166           dimNames[1] = "dim" + cnt;
167
168           varDimNames.put(varName, dimNames);
169           varDimLengths.put(varName, dimLengths);
170           varMap.put(varName, var);
171
172           StructureMembers sMembers = sData.getStructureMembers();
173           Object obj = sData.getScalarObject(sMembers.getMember(0));
174           varDataType.put(varName, obj.getClass());
175   }
176
177   public Class getArrayType(String array_name) {
178           return varDataType.get(array_name);
179   }
180
181   public String[] getDimensionNames(String array_name) {
182     return varDimNames.get(array_name);
183   }
184
185   public int[] getDimensionLengths(String array_name) {
186     return varDimLengths.get(array_name);
187   }
188
189   public String getArrayUnitString(String array_name) {
190     return varUnits.get(array_name);
191   }
192
193   public int getDimensionLength(String dimName) {
194     Dimension dim = ncfile.findDimension(dimName);
195     return (dim != null) ? dim.getLength() : -1;
196   }
197
198   public float[] getFloatArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
199     return (float[]) readArray(array_name, start, count, stride);
200   }
201
202   public int[] getIntArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
203     return (int[]) readArray(array_name, start, count, stride);
204   }
205
206   public double[] getDoubleArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
207     return (double[]) readArray(array_name, start, count, stride);
208   }
209
210   public short[] getShortArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
211     return (short[]) readArray(array_name, start, count, stride);
212   }
213
214   public byte[] getByteArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
215     return (byte[]) readArray(array_name, start, count, stride);
216   }
217
218   public Object getArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
219     return readArray(array_name, start, count, stride);
220   }
221
222   protected synchronized Object readArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
223     Variable var = varMap.get(array_name);
224     if (var instanceof Structure) {
225       Array array = Array.factory(getArrayType(array_name), count);
226       Index2D idx = new Index2D(count);
227       for (int i=0; i<count[0]; i++) {
228         StructureData sData = ((Structure)var).readStructure(start[0]+i);
229         StructureMembers sMembers = sData.getStructureMembers();
230         for (int j=0; j<count[1]; j++) {
231           Object obj = sData.getScalarObject(sMembers.getMember(start[1]+j));
232           idx.set(i,j);
233           array.setObject(idx, obj);
234         }
235       }
236       return array.copyTo1DJavaArray();
237     }
238     else {
239       List<Range> rangeList = new ArrayList<>(start.length);
240       for (int i=0;i<start.length;i++) {
241         Range rng = new Range(start[i], start[i]+(count[i]-1)*stride[i], stride[i]);
242         rangeList.add(i, rng);
243       }
244       Array array = var.read(rangeList);
245       return array.copyTo1DJavaArray();
246     }
247   }
248
249   public HDFArray getGlobalAttribute(String attr_name) throws Exception {
250     throw new Exception("NetCDFFile.getGlobalAttributes: Unimplemented");
251   }
252
253   public HDFArray getArrayAttribute(String array_name, String attr_name) throws Exception {
254     Object array = null;
255     DataType dataType = null;
256
257     Variable var = varMap.get(array_name);
258     if (var != null) {
259        Attribute attr = var.findAttribute(attr_name);
260        if (attr != null) {
261           Array attrVals = attr.getValues();
262           dataType = attr.getDataType();
263           array = attrVals.copyTo1DJavaArray();
264        }
265     }
266
267     if (array == null) {
268        return null;
269     }
270     
271     HDFArray harray = null;
272
273     if (dataType.getPrimitiveClassType() == Float.TYPE) {
274       harray = HDFArray.make((float[])array);
275     }
276     else if (dataType.getPrimitiveClassType() == Double.TYPE) {
277       harray = HDFArray.make((double[])array);
278     }
279     else if (dataType == DataType.STRING) {
280       harray = HDFArray.make((String[])array);
281     }
282     else if (dataType.getPrimitiveClassType() == Short.TYPE) {
283       harray = HDFArray.make((short[])array);
284     }
285     else if (dataType.getPrimitiveClassType() == Integer.TYPE) {
286       harray = HDFArray.make((int[])array);
287     }
288     return harray;
289   }
290
291   public void close() throws Exception {
292     ncfile.close();
293   }
294
295   public Map<String, Variable> getVarMap() {
296     return varMap;
297   }
298
299   public boolean hasArray(String name) {
300       return varMap.get(name) != null;
301   }
302
303   public boolean hasDimension(String name) {
304       return ncfile.findDimension(name) != null;
305   }
306
307   public NetcdfFile getNetCDFFile() {
308           return ncfile;
309   }
310   
311   public static void main(String[] args) throws Exception {
312     NetCDFFile ncfile = new NetCDFFile(args[0]);
313     ncfile.close();
314   }
315}