Java Code Examples for ucar.nc2.util.CancelTask#isCancel()

The following examples show how to use ucar.nc2.util.CancelTask#isCancel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AggDatasetOuter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
/**
 * Get number of coordinates in this Dataset.
 * If not already set, open the file and get it from the aggregation dimension.
 *
 * @param cancelTask allow cancellation
 * @return number of coordinates in this Dataset.
 * @throws IOException if io error
 */
public int getNcoords(CancelTask cancelTask) throws IOException {
  if (ncoord <= 0) {
    try (NetcdfFile ncd = acquireFile(cancelTask)) {
      if ((cancelTask != null) && cancelTask.isCancel())
        return 0;

      Dimension d = ncd.findDimension(aggregationOuter.dimName); // long name of dimension
      if (d != null)
        ncoord = d.getLength();
      else
        throw new IllegalArgumentException("Dimension not found= " + aggregationOuter.dimName);
    }
  }
  return ncoord;
}
 
Example 2
Source File: CatalogCrawler.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Crawl a catalog thats already been opened.
 * When you get to a dataset containing leaf datasets, do all, only the first, or a randomly chosen one.
 *
 * @param cat the catalog
 * @param task user can cancel the task (may be null)
 * @param out send status messages to here (may be null)
 * @param context caller can pass this object in (used for thread safety)
 * @return number of catalog references opened and crawled
 */
public int crawl(InvCatalogImpl cat, CancelTask task, PrintWriter out, Object context) {

  if (out != null)
    out.println("***CATALOG " + cat.getCreateFrom());
  countCatrefs = 0;

  for (InvDataset ds : cat.getDatasets()) {
    if (type == Type.all)
      crawlDataset(ds, task, out, context, true);
    else
      crawlDirectDatasets(ds, task, out, context, true);
    if ((task != null) && task.isCancel())
      break;
  }

  return 1 + countCatrefs;
}
 
Example 3
Source File: AggDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array read(Variable mainv, CancelTask cancelTask) throws IOException {
  NetcdfFile ncd = null;
  try {
    ncd = acquireFile(cancelTask);
    if ((cancelTask != null) && cancelTask.isCancel())
      return null;

    Variable v = findVariable(ncd, mainv);
    if (debugRead)
      System.out.printf("Agg.read %s from %s in %s%n", mainv.getNameAndDimensions(), v.getNameAndDimensions(),
          getLocation());
    return v.read();

  } finally {
    close(ncd);
  }
}
 
Example 4
Source File: Aggregation.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read a section of the local Variable.
 *
 * @param mainv aggregated Variable
 * @param cancelTask let user cancel
 * @param section reletive to the local Variable
 * @return the complete Array for mainv
 * @throws IOException on I/O error
 * @throws InvalidRangeException on section error
 */
protected Array read(Variable mainv, CancelTask cancelTask, List<Range> section)
    throws IOException, InvalidRangeException {
  NetcdfFile ncd = null;
  try {
    ncd = acquireFile(cancelTask);
    if ((cancelTask != null) && cancelTask.isCancel())
      return null;

    Variable v = findVariable(ncd, mainv);
    if (debugRead) {
      Section want = new Section(section);
      System.out.printf("Agg.read(%s) %s from %s in %s%n", want, mainv.getNameAndDimensions(),
          v.getNameAndDimensions(), getLocation());
    }

    return v.read(section);

  } finally {
    close(ncd);
  }
}
 
Example 5
Source File: RecordDatasetHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public List getData(ArrayList records, LatLonRect boundingBox, double startTime, double endTime, CancelTask cancel)
    throws IOException {
  if (debugBB)
    System.out.println("Want bb= " + boundingBox);
  ArrayList result = new ArrayList();
  for (int i = 0; i < records.size(); i++) {
    RecordDatasetHelper.RecordPointObs r = (RecordDatasetHelper.RecordPointObs) records.get(i);
    if (boundingBox.contains(r.getLatLon())) {
      if (debugBB)
        System.out.println(" ok latlon= " + r.getLatLon());
      double timeValue = r.getObservationTime();
      if ((timeValue >= startTime) && (timeValue <= endTime))
        result.add(r);
    }
    if ((cancel != null) && cancel.isCancel())
      return null;
  }
  return result;
}
 
Example 6
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void printStructureDataArray(Formatter out, ArrayStructure array, Indent indent, CancelTask ct) {
  try (StructureDataIterator sdataIter = array.getStructureDataIterator()) {
    int count = 0;
    while (sdataIter.hasNext()) {
      StructureData sdata = sdataIter.next();
      out.format("%n%s{", indent);
      printStructureData(out, sdata, indent, ct);
      out.format("%s} %s(%d)", indent, sdata.getName(), count);
      if (ct != null && ct.isCancel())
        return;
      count++;
    }
  } catch (IOException ioe) {
    out.format("%n%s%n", ioe.getMessage());
  }
}
 
Example 7
Source File: StationDatasetHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List getStationObs(List<ucar.unidata.geoloc.Station> stations, CancelTask cancel) throws IOException {
  ArrayList result = new ArrayList();
  for (int i = 0; i < stations.size(); i++) {
    ucar.unidata.geoloc.Station s = stations.get(i);
    result.addAll(obsDataset.getData(s, cancel));
    if ((cancel != null) && cancel.isCancel())
      return null;
  }
  return result;
}
 
Example 8
Source File: UnidataStationObsMultidimDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List getData(CancelTask cancel) throws IOException {
  List<StationObsDatatype> allData = new ArrayList<StationObsDatatype>();
  for (ucar.unidata.geoloc.Station s : getStations()) {
    MStationImpl ms = (MStationImpl) s;
    allData.addAll(ms.readObservations());
    if ((cancel != null) && cancel.isCancel())
      return null;
  }
  return allData;
}
 
Example 9
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Array reallyRead(Variable client, CancelTask cancelTask) throws IOException {
  try (NetcdfFile ncfile = open(location, null)) {
    if ((cancelTask != null) && cancelTask.isCancel())
      return null;
    Variable proxyV = findVariable(ncfile, client);
    return proxyV.read();
  }
}
 
Example 10
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void copyVariableData(NetcdfFormatWriter ncwriter, Group groupIn, Group groupOut, Count counter,
    CancelTask cancel) throws IOException {
  for (Variable oldVar : groupIn.getVariables()) {
    if (cancel.isCancel()) {
      break;
    }
    Variable newVar = groupOut.findVariableLocal(oldVar.getShortName());
    if (debug) {
      System.out.format("write var= %s size = %d type = %s%n", oldVar.getFullName(), oldVar.getSize(),
          oldVar.getDataType());
    }
    cancel.setProgress("writing " + oldVar.getFullName(), counter.countVars++);

    long size = oldVar.getSize() * oldVar.getElementSize();
    counter.bytes += size;

    if (size <= maxSize) {
      copyAll(ncwriter, oldVar, newVar);
    } else {
      copySome(ncwriter, oldVar, newVar, maxSize, cancel);
    }
  }

  for (Group nestedIn : groupIn.getGroups()) {
    if (cancel.isCancel()) {
      break;
    }
    Group nestedOut = groupOut.findGroupLocal(nestedIn.getShortName());
    copyVariableData(ncwriter, nestedIn, nestedOut, counter, cancel);
  }
}
 
Example 11
Source File: Aggregation.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Array reallyRead(Variable mainV, Section section, CancelTask cancelTask)
    throws IOException, InvalidRangeException {
  NetcdfFile ncfile = null;
  try {
    ncfile = dataset.acquireFile(cancelTask);
    Variable proxyV = findVariable(ncfile, mainV);
    if ((cancelTask != null) && cancelTask.isCancel())
      return null;
    return proxyV.read(section);

  } finally {
    dataset.close(ncfile);
  }
}
 
Example 12
Source File: AggProxyReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Array reallyRead(Variable mainV, CancelTask cancelTask) throws IOException {
  NetcdfFile ncfile = null;
  try {
    ncfile = dataset.acquireFile(cancelTask);
    if ((cancelTask != null) && cancelTask.isCancel())
      return null;
    Variable proxyV = findVariable(ncfile, mainV);
    return proxyV.read();
  } finally {
    dataset.close(ncfile);
  }
}
 
Example 13
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void printStringArray(Formatter out, Array ma, Indent indent, CancelTask ct) {
  if (ct != null && ct.isCancel())
    return;

  int rank = ma.getRank();
  Index ima = ma.getIndex();

  if (rank == 0) {
    out.format("  \"%s\"", ma.getObject(ima));
    return;
  }

  if (rank == 1) {
    boolean first = true;
    for (int i = 0; i < ma.getSize(); i++) {
      if (!first)
        out.format(", ");
      out.format("  \"%s\"", ma.getObject(ima.set(i)));
      first = false;
    }
    return;
  }

  int[] dims = ma.getShape();
  int last = dims[0];

  out.format("%n%s{", indent);
  indent.incr();
  for (int ii = 0; ii < last; ii++) {
    ArrayObject slice = (ArrayObject) ma.slice(0, ii);
    if (ii > 0)
      out.format(",");
    printStringArray(out, slice, indent, ct);
  }
  indent.decr();
  out.format("%n%s}", indent);
}
 
Example 14
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void printStructureData(Formatter out, StructureData sdata, Indent indent, CancelTask ct) {
  indent.incr();
  for (StructureMembers.Member m : sdata.getMembers()) {
    Array sdataArray = sdata.getArray(m);
    printArray(out, sdataArray, m.getName(), m.getUnitsString(), indent, ct, true);
    if (ct != null && ct.isCancel())
      return;
  }
  indent.decr();
}
 
Example 15
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void printSequence(Formatter out, ArraySequence seq, Indent indent, CancelTask ct) {
  try (StructureDataIterator iter = seq.getStructureDataIterator()) {
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      out.format("%n%s{", indent);
      printStructureData(out, sdata, indent, ct);
      out.format("%s} %s", indent, sdata.getName());
      if (ct != null && ct.isCancel())
        return;
    }
  } catch (IOException ioe) {
    out.format("%n%s%n", ioe.getMessage());
  }
}
 
Example 16
Source File: FileWriter2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * <strong>ucar.nc2.FileWriter -in fileIn -out fileOut</strong>.
 * <p>
 * where:
 * <ul>
 * <li>fileIn : path of any CDM readable file
 * <li>fileOut: local pathname where netdf-3 file will be written
 * </ol>
 *
 * @param arg -in fileIn -out fileOut [-netcdf4]
 * @throws IOException on read or write error
 * @deprecated use ucar.nc2.write.Nccopy
 */
@Deprecated
public static void main(String[] arg) throws IOException {
  if (arg.length < 4) {
    usage();
    System.exit(0);
  }

  String datasetIn = null, datasetOut = null;
  NetcdfFileWriter.Version version = NetcdfFileWriter.Version.netcdf3;
  for (int i = 0; i < arg.length; i++) {
    String s = arg[i];
    if (s.equalsIgnoreCase("-in"))
      datasetIn = arg[i + 1];
    if (s.equalsIgnoreCase("-out"))
      datasetOut = arg[i + 1];
    if (s.equalsIgnoreCase("-netcdf4"))
      version = NetcdfFileWriter.Version.netcdf4;
  }
  if ((datasetIn == null) || (datasetOut == null)) {
    usage();
    System.exit(0);
  }

  System.out.printf("FileWriter2 copy %s to %s ", datasetIn, datasetOut);
  CancelTask cancel = CancelTask.create();
  NetcdfFile ncfileIn = ucar.nc2.NetcdfFile.open(datasetIn, cancel);
  if (cancel.isCancel())
    return;

  FileWriter2 writer2 = new FileWriter2(ncfileIn, datasetOut, version, null); // currently only the default chunker
  NetcdfFile ncfileOut = writer2.write(cancel);
  if (ncfileOut != null)
    ncfileOut.close();
  ncfileIn.close();
  System.out.printf("%s%n", cancel);
}
 
Example 17
Source File: DODSNetcdfFile.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void constructTopVariables(DodsV rootDodsV, CancelTask cancelTask) throws IOException {
  List<DodsV> topVariables = rootDodsV.children;
  for (DodsV dodsV : topVariables) {
    if (dodsV.bt instanceof DConstructor)
      continue;
    addVariable(rootGroup, null, dodsV);
    if (cancelTask != null && cancelTask.isCancel())
      return;
  }
}
 
Example 18
Source File: FileCacheARC.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Acquire a FileCacheable from the cache, and lock it so no one else can use it.
 * If not already in cache, open it with FileFactory, and put in cache.
 * <p/>
 * Call FileCacheable.close() when done, (rather than FileCacheIF.release() directly) and the file is then released
 * instead of closed.
 * <p/>
 * If cache size goes over maxElement, then immediately (actually in 100 msec) schedule a cleanup in a background
 * thread.
 * This means that the cache should never get much larger than maxElement, unless you have them all locked.
 *
 * @param factory use this factory to open the file if not in the cache; may not be null
 * @param hashKey unique key for this file. If null, the location will be used
 * @param location file location, may also used as the cache name, will be passed to the NetcdfFileFactory
 * @param buffer_size RandomAccessFile buffer size, if <= 0, use default size
 * @param cancelTask user can cancel, ok to be null.
 * @param spiObject sent to iosp.setSpecial() if not null
 * @return FileCacheable corresponding to location.
 * @throws IOException on error
 */
@Override
public FileCacheable acquire(FileFactory factory, Object hashKey, DatasetUrl location, int buffer_size,
    CancelTask cancelTask, Object spiObject) throws IOException {

  if (null == hashKey)
    hashKey = location.trueurl;
  if (null == hashKey)
    throw new IllegalArgumentException();

  Tracker t = null;
  if (trackAll) {
    t = new Tracker(hashKey);
    Tracker prev = track.putIfAbsent(hashKey, t);
    if (prev != null)
      t = prev;
  }

  FileCacheable ncfile = acquireCacheOnly(hashKey);
  if (ncfile != null) {
    hits.incrementAndGet();
    if (t != null)
      t.hit++;
    return ncfile;
  }
  miss.incrementAndGet();
  if (t != null)
    t.miss++;

  // open the file
  ncfile = factory.open(location, buffer_size, cancelTask, spiObject);
  if (cacheLog.isDebugEnabled())
    cacheLog.debug("FileCacheARC " + name + " acquire " + hashKey + " " + ncfile.getLocation());
  if (debugPrint)
    System.out.println("  FileCacheARC " + name + " acquire " + hashKey + " " + ncfile.getLocation());

  // user may have canceled
  if ((cancelTask != null) && (cancelTask.isCancel())) {
    if (ncfile != null)
      ncfile.close();
    return null;
  }

  if (disabled.get())
    return ncfile;

  addToCache(hashKey, ncfile);

  return ncfile;
}
 
Example 19
Source File: Ncdump.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void printStringArray(Formatter out, ArrayChar ma, Indent indent, CancelTask ct) {
  if (ct != null && ct.isCancel())
    return;

  int rank = ma.getRank();

  if (rank == 1) {
    out.format("  \"%s\"", ma.getString());
    return;
  }

  if (rank == 2) {
    boolean first = true;
    ArrayChar.StringIterator iter = ma.getStringIterator();
    while (iter.hasNext()) {
      if (!first)
        out.format(", ");
      out.format("  \"%s\"", iter.next());
      first = false;
      if (ct != null && ct.isCancel())
        return;
    }
    return;
  }

  int[] dims = ma.getShape();
  int last = dims[0];

  out.format("%n%s{", indent);
  indent.incr();
  for (int ii = 0; ii < last; ii++) {
    ArrayChar slice = (ArrayChar) ma.slice(0, ii);
    if (ii > 0)
      out.format(",");
    printStringArray(out, slice, indent, ct);
    if (ct != null && ct.isCancel())
      return;
  }
  indent.decr();

  out.format("%n%s}", indent);
}
 
Example 20
Source File: FileWriter2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies data from {@code oldVar} to {@code newVar}. The writes are done in a series of chunks no larger than
 * {@code maxChunkSize} bytes.
 *
 * @param oldVar a variable from the original file to copy data from.
 * @param newVar a variable from the original file to copy data from.
 * @param maxChunkSize the size, <b>in bytes</b>, of the largest chunk to write.
 * @param cancel allow user to cancel, may be null.
 * @throws IOException if an I/O error occurs.
 */
private void copySome(Variable oldVar, Variable newVar, long maxChunkSize, CancelTask cancel) throws IOException {
  long maxChunkElems = maxChunkSize / oldVar.getElementSize();
  long byteWriteTotal = 0;

  ChunkingIndex index = new ChunkingIndex(oldVar.getShape());
  while (index.currentElement() < index.getSize()) {
    try {
      int[] chunkOrigin = index.getCurrentCounter();
      int[] chunkShape = index.computeChunkShape(maxChunkElems);

      if (cancel != null)
        cancel.setProgress(
            "Reading chunk " + new Section(chunkOrigin, chunkShape) + " from variable: " + oldVar.getShortName(), -1);
      /*
       * writeProgressEvent.setWriteStatus("Reading chunk from variable: " + oldVar.getShortName());
       * if (progressListeners != null) {
       * for (FileWriterProgressListener listener : progressListeners) {
       * listener.writeProgress(writeProgressEvent);
       * }
       * }
       */

      Array data = oldVar.read(chunkOrigin, chunkShape);
      if (!version.isNetdf4format() && oldVar.getDataType() == DataType.STRING) {
        data = convertToChar(newVar, data);
      }

      if (data.getSize() > 0) {// zero when record dimension = 0
        if (cancel != null)
          cancel.setProgress(
              "Writing chunk " + new Section(chunkOrigin, chunkShape) + " from variable: " + oldVar.getShortName(),
              -1);

        writer.write(newVar, chunkOrigin, data);
        if (debugWrite)
          System.out.println(" write " + data.getSize() + " bytes at " + new Section(chunkOrigin, chunkShape));

        byteWriteTotal += data.getSize();
      }

      index.setCurrentCounter(index.currentElement() + (int) Index.computeSize(chunkShape));
      if (cancel != null && cancel.isCancel())
        return;

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IOException(e.getMessage());
    }
  }
}