<commit_before>//
// NetcdfTools.java
//

/*
OME Bio-Formats package for reading and converting biological file formats.
Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.formats;

import java.io.*;
import java.util.*;
import loci.common.*;

/**
 * Utility class for working with NetCDF/HDF files.  Uses reflection to
 * call the NetCDF Java library.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/bio-formats/src/loci/formats/NetcdfTools.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/bio-formats/src/loci/formats/NetcdfTools.java">SVN</a></dd></dl>
 */
public class NetcdfTools {

  // -- Constants --

  private static final String NO_NETCDF_MSG =
    "NetCDF is required to read NetCDF/HDF variants.  Please obtain " +
    "the necessary JAR files from http://loci.wisc.edu/ome/formats.html";

  // -- Fields --

  private boolean noNetCDF;
  private ReflectedUniverse r;
  private String currentFile;
  private Vector attributeList;
  private Vector variableList;

  // -- Constructor --

  public NetcdfTools(String file) {
    this.currentFile = file;
    initialize();
  }

  // -- NetcdfTools API methods --

  public Vector getAttributeList() {
    return attributeList;
  }

  public Vector getVariableList() {
    return variableList;
  }

  public String getAttributeValue(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);

      r.setVar("name", attr);
      Object v = r.exec("attribute = g.findAttribute(name)");
      if (v == null) return null;

      Boolean isString = (Boolean) r.exec("isString = attribute.isString()");
      if (isString.booleanValue()) {
        r.exec("array = attribute.getValues()");
        Object[] s = (Object[]) r.exec("s = array.copyTo1DJavaArray()");
        StringBuffer sb = new StringBuffer();
        for (int i=0; i<s.length; i++) {
          sb.append((String) s[i]);
        }
        return sb.toString();
      }
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return null;
  }

  public Object getVariableValue(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);

      r.setVar("name", attr);
      r.exec("var = g.findVariable(name)");
      r.exec("data = var.read()");
      return r.exec("data = data.copyToNDJavaArray()");
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return null;
  }

  public Hashtable getVariableAttributes(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);

      r.setVar("name", attr);
      r.exec("var = g.findVariable(name)");
      List l = (List) r.exec("attributes = var.getAttributes()");

      Hashtable h = new Hashtable();
      for (int i=0; i<l.size(); i++) {
        r.setVar("attr", l.get(i));

        r.exec("array = attr.getValues()");
        h.put(r.exec("attr.getName()"),
        r.exec("s = array.copyTo1DJavaArray()"));
      }

      return h;
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return null;
  }

  public int getDimension(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);
      r.setVar("name", attr);
      r.exec("dim = g.findDimension(name)");
      return ((Integer) r.exec("dim.getLength()")).intValue();
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return -1;
  }

  public void close() {
    try {
      r.exec("ncfile.close()");
    }
    catch (ReflectException e) { }
  }

  // -- Helper methods --

  private void initialize() {
    r = null;
    try {
      r = new ReflectedUniverse();
      r.exec("import ucar.ma2.Array");
      r.exec("import ucar.ma2.ArrayByte");
      r.exec("import ucar.nc2.Attribute");
      r.exec("import ucar.nc2.Group");
      r.exec("import ucar.nc2.NetcdfFile");
    }
    catch (ReflectException exc) {
      noNetCDF = true;
      LogTools.trace(exc);
    }
    catch (UnsupportedClassVersionError exc) {
      noNetCDF = true;
      LogTools.trace(exc);
    }

    // HACK - NetCDF prints a fair number of warning messages to stdout
    // we need to filter these out so that they don't interfere with omebf
    PrintStream out = new PrintStream(System.out) {
      public void print(String s) {
        if (s == null || !s.trim().startsWith("WARN:")) super.print(s);
      }
      public void println(String s) {
        if (s == null || !s.trim().startsWith("WARN:")) super.println(s);
      }
    };
    System.setOut(out);

    try {
      r.setVar("currentId", Location.getMappedId(currentFile));
      r.exec("ncfile = NetcdfFile.open(currentId)");
      r.exec("root = ncfile.getRootGroup()");

      attributeList = new Vector();
      variableList = new Vector();
      Vector groups = new Vector();
      groups.add(r.getVar("root"));
      parseAttributesAndVariables(groups);
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
  }<commit_msg>Display a more informative message if NetCDF library is not found.<commit_after>//
// NetcdfTools.java
//

/*
OME Bio-Formats package for reading and converting biological file formats.
Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.formats;

import java.io.*;
import java.util.*;
import loci.common.*;

/**
 * Utility class for working with NetCDF/HDF files.  Uses reflection to
 * call the NetCDF Java library.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/bio-formats/src/loci/formats/NetcdfTools.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/bio-formats/src/loci/formats/NetcdfTools.java">SVN</a></dd></dl>
 */
public class NetcdfTools {

  // -- Constants --

  private static final String NO_NETCDF_MSG =
    "NetCDF is required to read NetCDF/HDF variants.  Please obtain " +
    "the necessary JAR files from http://loci.wisc.edu/ome/formats.html.\n" +
    "Required JAR files are netcdf-4.0.jar, bufr-1.1.00.jar, grib-5.1.03.jar " +
    ", and slf4j-jdk14.jar.";

  // -- Fields --

  private boolean noNetCDF;
  private ReflectedUniverse r;
  private String currentFile;
  private Vector attributeList;
  private Vector variableList;

  // -- Constructor --

  public NetcdfTools(String file) throws FormatException {
    this.currentFile = file;
    initialize();
  }

  // -- NetcdfTools API methods --

  public Vector getAttributeList() {
    return attributeList;
  }

  public Vector getVariableList() {
    return variableList;
  }

  public String getAttributeValue(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);

      r.setVar("name", attr);
      Object v = r.exec("attribute = g.findAttribute(name)");
      if (v == null) return null;

      Boolean isString = (Boolean) r.exec("isString = attribute.isString()");
      if (isString.booleanValue()) {
        r.exec("array = attribute.getValues()");
        Object[] s = (Object[]) r.exec("s = array.copyTo1DJavaArray()");
        StringBuffer sb = new StringBuffer();
        for (int i=0; i<s.length; i++) {
          sb.append((String) s[i]);
        }
        return sb.toString();
      }
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return null;
  }

  public Object getVariableValue(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);

      r.setVar("name", attr);
      r.exec("var = g.findVariable(name)");
      r.exec("data = var.read()");
      return r.exec("data = data.copyToNDJavaArray()");
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return null;
  }

  public Hashtable getVariableAttributes(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);

      r.setVar("name", attr);
      r.exec("var = g.findVariable(name)");
      List l = (List) r.exec("attributes = var.getAttributes()");

      Hashtable h = new Hashtable();
      for (int i=0; i<l.size(); i++) {
        r.setVar("attr", l.get(i));

        r.exec("array = attr.getValues()");
        h.put(r.exec("attr.getName()"),
        r.exec("s = array.copyTo1DJavaArray()"));
      }

      return h;
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return null;
  }

  public int getDimension(String name) {
    String dir = name.substring(0, name.lastIndexOf("/"));
    String attr = name.substring(name.lastIndexOf("/") + 1);

    try {
      setupGroup(dir);
      r.setVar("name", attr);
      r.exec("dim = g.findDimension(name)");
      return ((Integer) r.exec("dim.getLength()")).intValue();
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
    return -1;
  }

  public void close() {
    try {
      r.exec("ncfile.close()");
    }
    catch (ReflectException e) { }
  }

  // -- Helper methods --

  private void initialize() throws FormatException {
    r = null;
    try {
      r = new ReflectedUniverse();
      r.exec("import ucar.ma2.Array");
      r.exec("import ucar.ma2.ArrayByte");
      r.exec("import ucar.nc2.Attribute");
      r.exec("import ucar.nc2.Group");
      r.exec("import ucar.nc2.NetcdfFile");
    }
    catch (ReflectException exc) {
      noNetCDF = true;
      throw new FormatException(NO_NETCDF_MSG, exc);
    }
    catch (UnsupportedClassVersionError exc) {
      noNetCDF = true;
      throw new FormatException(NO_NETCDF_MSG, exc);
    }

    // HACK - NetCDF prints a fair number of warning messages to stdout
    // we need to filter these out so that they don't interfere with omebf
    PrintStream out = new PrintStream(System.out) {
      public void print(String s) {
        if (s == null || !s.trim().startsWith("WARN:")) super.print(s);
      }
      public void println(String s) {
        if (s == null || !s.trim().startsWith("WARN:")) super.println(s);
      }
    };
    System.setOut(out);

    try {
      r.setVar("currentId", Location.getMappedId(currentFile));
      r.exec("ncfile = NetcdfFile.open(currentId)");
      r.exec("root = ncfile.getRootGroup()");

      attributeList = new Vector();
      variableList = new Vector();
      Vector groups = new Vector();
      groups.add(r.getVar("root"));
      parseAttributesAndVariables(groups);
    }
    catch (ReflectException e) {
      LogTools.trace(e);
    }
  }
{'fraction_non_alphanumeric': 0.14147443135989676, 'fraction_numerical': 0.007017260848523955, 'mean_word_length': 2.8233117483811285, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885637', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3922, 'n_words': 1019}