001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
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 https://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   private static final Logger logger = LoggerFactory.getLogger(NetCDFFile.class);
054   private final Map<String, Variable> varMap = new HashMap<>();
055   private final Map<String, String[]> varDimNames = new HashMap<>();
056   private final Map<String, int[]> varDimLengths = new HashMap<>();
057//   private final Map<String, Class> varDataType = new HashMap<>();
058   private final Map<String, DataType> 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       logger.error("Problem creating SAXBuilder", e);
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       varDataType.put(varName, var.getDataType());
143       
144       Attribute attr = var.findAttribute("units");
145       if (attr != null) {
146         String unitStr = attr.getStringValue();
147         varUnits.put(varName, unitStr);
148       }
149     }
150   }
151
152   void analyzeStructure(Structure var) throws Exception {
153           if ((var.getShape()).length == 0) {
154                   return;
155           }
156           String varName = var.getFullName();
157           String[] dimNames = new String[2];
158           int[] dimLengths = new int[2];
159           int cnt = 0;
160           dimLengths[0] = (var.getShape())[0];
161           dimNames[0] = "dim" + cnt;
162
163           cnt++;
164           StructureData sData = var.readStructure(0);
165           List memList = sData.getMembers();
166           dimLengths[1] = memList.size();
167           dimNames[1] = "dim" + cnt;
168
169           varDimNames.put(varName, dimNames);
170           varDimLengths.put(varName, dimLengths);
171           varMap.put(varName, var);
172
173           StructureMembers sMembers = sData.getStructureMembers();
174           Object obj = sData.getScalarObject(sMembers.getMember(0));
175
176//         varDataType.put(varName, obj.getClass());
177       varDataType.put(varName, var.getDataType());
178   }
179
180//   public Class getArrayType(String array_name) {
181//         return varDataType.get(array_name);
182//   }
183    public DataType getArrayType(String array_name) {
184       return varDataType.get(array_name);
185    }
186
187   public String[] getDimensionNames(String array_name) {
188     return varDimNames.get(array_name);
189   }
190
191   public int[] getDimensionLengths(String array_name) {
192     return varDimLengths.get(array_name);
193   }
194
195   public String getArrayUnitString(String array_name) {
196     return varUnits.get(array_name);
197   }
198
199   public int getDimensionLength(String dimName) {
200     Dimension dim = ncfile.findDimension(dimName);
201     return (dim != null) ? dim.getLength() : -1;
202   }
203
204   public float[] getFloatArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
205     return (float[]) readArray(array_name, start, count, stride);
206   }
207
208   public int[] getIntArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
209     return (int[]) readArray(array_name, start, count, stride);
210   }
211
212   public double[] getDoubleArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
213     return (double[]) readArray(array_name, start, count, stride);
214   }
215
216   public short[] getShortArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
217     return (short[]) readArray(array_name, start, count, stride);
218   }
219
220   public byte[] getByteArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
221     return (byte[]) readArray(array_name, start, count, stride);
222   }
223
224   public Object getArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
225     return readArray(array_name, start, count, stride);
226   }
227
228   protected synchronized Object readArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
229     Variable var = varMap.get(array_name);
230     if (var instanceof Structure) {
231       Array array = Array.factory(getArrayType(array_name), count);
232       Index2D idx = new Index2D(count);
233       for (int i=0; i<count[0]; i++) {
234         StructureData sData = ((Structure)var).readStructure(start[0]+i);
235         StructureMembers sMembers = sData.getStructureMembers();
236         for (int j=0; j<count[1]; j++) {
237           Object obj = sData.getScalarObject(sMembers.getMember(start[1]+j));
238           idx.set(i,j);
239           array.setObject(idx, obj);
240         }
241       }
242       return array.copyTo1DJavaArray();
243     }
244     else {
245       List<Range> rangeList = new ArrayList<>(start.length);
246       for (int i=0;i<start.length;i++) {
247         Range rng = new Range(start[i], start[i]+(count[i]-1)*stride[i], stride[i]);
248         rangeList.add(i, rng);
249       }
250       Array array = var.read(rangeList);
251       return array.copyTo1DJavaArray();
252     }
253   }
254
255   public HDFArray getGlobalAttribute(String attr_name) throws Exception {
256     throw new Exception("NetCDFFile.getGlobalAttributes: Unimplemented");
257   }
258
259   public HDFArray getArrayAttribute(String array_name, String attr_name) throws Exception {
260     Object array = null;
261     DataType dataType = null;
262
263     Variable var = varMap.get(array_name);
264     if (var != null) {
265        Attribute attr = var.findAttribute(attr_name);
266        if (attr != null) {
267           Array attrVals = attr.getValues();
268           dataType = attr.getDataType();
269           array = attrVals.copyTo1DJavaArray();
270        }
271     }
272
273     if (array == null) {
274        return null;
275     }
276     
277     HDFArray harray = null;
278
279     if (dataType.getPrimitiveClassType() == Float.TYPE) {
280       harray = HDFArray.make((float[])array);
281     }
282     else if (dataType.getPrimitiveClassType() == Double.TYPE) {
283       harray = HDFArray.make((double[])array);
284     }
285     else if (dataType == DataType.STRING) {
286       harray = HDFArray.make((String[])array);
287     }
288     else if (dataType.getPrimitiveClassType() == Short.TYPE) {
289       harray = HDFArray.make((short[])array);
290     }
291     else if (dataType.getPrimitiveClassType() == Integer.TYPE) {
292       harray = HDFArray.make((int[])array);
293     }
294     return harray;
295   }
296
297   public void close() throws Exception {
298     ncfile.close();
299   }
300
301   public Map<String, Variable> getVarMap() {
302     return varMap;
303   }
304
305   public boolean hasArray(String name) {
306       return varMap.get(name) != null;
307   }
308
309   public boolean hasDimension(String name) {
310       return ncfile.findDimension(name) != null;
311   }
312
313   public NetcdfFile getNetCDFFile() {
314           return ncfile;
315   }
316   
317   public static void main(String[] args) throws Exception {
318     NetCDFFile ncfile = new NetCDFFile(args[0]);
319     ncfile.close();
320   }
321}