Java Code Examples for ucar.unidata.io.RandomAccessFile#getLocation()

The following examples show how to use ucar.unidata.io.RandomAccessFile#getLocation() . 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: Hdf5NewObjectTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setHdf5File(RandomAccessFile raf) throws IOException {
  closeOpenFiles();

  this.location = raf.getLocation();
  List<ObjectBean> beanList = new ArrayList<>();

  iosp = new H5iospNew();
  NetcdfFile ncfile = new NetcdfFileSubclass(iosp, location);
  ncfile.sendIospMessage(H5iospNew.IOSP_MESSAGE_INCLUDE_ORIGINAL_ATTRIBUTES);

  try {
    iosp.open(raf, ncfile, null);
  } catch (Throwable t) {
    StringWriter sw = new StringWriter(20000);
    PrintWriter s = new PrintWriter(sw);
    t.printStackTrace(s);
    dumpTA.setText(sw.toString());
  }

  H5headerNew header = iosp.getHeader();
  for (H5objects.DataObject dataObj : header.getDataObjects()) {
    beanList.add(new ObjectBean(dataObj));
  }

  objectTable.setBeans(beanList);
}
 
Example 2
Source File: GempakGridServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Open the service provider for reading.
 *
 * @param raf file to read from
 * @param ncfile netCDF file we are writing to (memory)
 * @param cancelTask task for cancelling
 * @throws IOException problem reading file
 */
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
  super.open(raf, ncfile, cancelTask);
  // debugProj = true;
  long start = System.currentTimeMillis();
  if (gemreader == null) {
    gemreader = new GempakGridReader(raf.getLocation());
  }
  initTables();
  gemreader.init(raf, true);
  GridIndex index = gemreader.getGridIndex();
  open(index, cancelTask);
  if (debugOpen) {
    System.out.println(
        " GridServiceProvider.open " + ncfile.getLocation() + " took " + (System.currentTimeMillis() - start));
  }
}
 
Example 3
Source File: Hdf4Table.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void setHdf4File(RandomAccessFile raf) throws IOException {
  closeOpenFiles();

  this.location = raf.getLocation();
  List<TagBean> beanList = new ArrayList<>();

  iosp = new H4iosp();
  NetcdfFile ncfile = new NetcdfFileSubclass(iosp, location);

  try {
    iosp.open(raf, ncfile, null);
  } catch (Throwable t) {
    StringWriter sw = new StringWriter(20000);
    t.printStackTrace(new PrintWriter(sw));
    dumpTA.setText(sw.toString());
  }

  header = (H4header) iosp.sendIospMessage("header");
  for (H4header.Tag tag : header.getTags()) {
    beanList.add(new TagBean(tag));
  }

  tagTable.setBeans(beanList);
}
 
Example 4
Source File: Hdf4NewTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void setHdf4File(RandomAccessFile raf) throws IOException {
  closeOpenFiles();

  this.location = raf.getLocation();
  List<TagBean> beanList = new ArrayList<>();

  iosp = new H4iosp();
  NetcdfFile ncfile = new NetcdfFileSubclass(iosp, location);

  try {
    iosp.open(raf, ncfile, null);
  } catch (Throwable t) {
    StringWriter sw = new StringWriter(20000);
    t.printStackTrace(new PrintWriter(sw));
    dumpTA.setText(sw.toString());
  }

  header = (H4header) iosp.sendIospMessage("header");
  for (H4header.Tag tag : header.getTags()) {
    beanList.add(new TagBean(tag));
  }

  tagTable.setBeans(beanList);
}
 
Example 5
Source File: GradsBinaryGridServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Is this a valid file? For this GrADS IOSP, the valid file must be:
 * <ul>
 * <li>raw binary grid (not GRIB, netCDF, HDF, etc)
 * <li>not a cross section (x and y > 1)
 * <li>not an ensemble definded by EDEF/ENDEDEF (need examples)
 * </ul>
 *
 * @param raf RandomAccessFile to check
 * @return true if a valid GrADS grid file of the type listed above
 * @throws IOException problem reading file
 */
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  // need a fast failure for non-GRADS files
  if (GradsDataDescriptorFile.failFast(raf))
    return false;
  raf.seek(0);

  // we think its a GRADS file, but we have lots of restrictions on what we can handle
  try {
    gradsDDF = new GradsDataDescriptorFile(raf.getLocation(), 5000);
    if (gradsDDF.error)
      return false;

    GradsDimension x = gradsDDF.getXDimension();
    GradsDimension y = gradsDDF.getYDimension();
    // J-

    return gradsDDF.getDataType() == null && // only handle raw binary
        gradsDDF.getDataFile() != null && !gradsDDF.hasProjection() && // can't handle projections
        !gradsDDF.getVariables().isEmpty() && // must have valid entries
        !gradsDDF.getDimensions().isEmpty() && (x.getSize() > 1) && (y.getSize() > 1); // can't handle cross sections
    // J+
  } catch (Exception ioe) {
    return false;
  }
}
 
Example 6
Source File: GribCdmIndex.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a grib collection / partition collection from an existing ncx2 file.
 * PartionCollection.partition.getGribCollection().
 *
 * @param indexRaf the ncx2 file already open
 * @param config special configuration
 * @return the resulting GribCollection, or null on failure
 * @throws IOException on io error
 */
@Nullable
public static GribCollectionImmutable openGribCollectionFromIndexFile(RandomAccessFile indexRaf,
    FeatureCollectionConfig config, org.slf4j.Logger logger) throws IOException {

  GribCollectionType type = getType(indexRaf);

  String location = indexRaf.getLocation();
  File f = new File(location);
  int pos = f.getName().lastIndexOf(".");
  String name = (pos > 0) ? f.getName().substring(0, pos) : f.getName(); // remove ".ncx2"

  switch (type) {
    case Partition1:
      return Grib1PartitionBuilderFromIndex.createTimePartitionFromIndex(name, indexRaf, config, logger);
    case GRIB1:
      return Grib1CollectionBuilderFromIndex.readFromIndex(name, indexRaf, config, logger);
    case Partition2:
      return Grib2PartitionBuilderFromIndex.createTimePartitionFromIndex(name, indexRaf, config, logger);
    case GRIB2:
      return Grib2CollectionBuilderFromIndex.readFromIndex(name, indexRaf, config, logger);
  }

  return null;
}
 
Example 7
Source File: N3iospNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Deprecated
@Override
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
  super.open(raf, ncfile, cancelTask);

  String location = raf.getLocation();
  if (!location.startsWith("http:")) {
    File file = new File(location);
    if (file.exists())
      lastModified = file.lastModified();
  }

  raf.order(RandomAccessFile.BIG_ENDIAN);
  header = createHeader();

  Group.Builder rootGroup = Group.builder().setName("").setNcfile(ncfile);
  header.read(raf, rootGroup, null);
  ncfile.setRootGroup(rootGroup.build());
  ncfile.finish();
}
 
Example 8
Source File: N3iospNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void build(RandomAccessFile raf, Group.Builder rootGroup, CancelTask cancelTask) throws IOException {
  super.open(raf, rootGroup.getNcfile(), cancelTask);

  String location = raf.getLocation();
  if (!location.startsWith("http:")) {
    File file = new File(location);
    if (file.exists())
      lastModified = file.lastModified();
  }

  raf.order(RandomAccessFile.BIG_ENDIAN);
  header = createHeader();
  header.read(raf, rootGroup, null);
}
 
Example 9
Source File: BufrConfig.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private BufrConfig(RandomAccessFile raf) {
  this.filename = raf.getLocation();
  try {
    scanBufrFile(raf);
  } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e.getMessage());
  }
}
 
Example 10
Source File: GribCollectionMutable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * public by accident, do not use
 *
 * @param indexRaf the open raf of the index file
 */
void setIndexRaf(RandomAccessFile indexRaf) {
  this.indexRaf = indexRaf;
  if (indexRaf != null) {
    this.indexFilename = indexRaf.getLocation();
  }
}
 
Example 11
Source File: Hdf5ObjectTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *
 */
public void setHdf5File(RandomAccessFile raf) throws IOException {
  closeOpenFiles();

  this.location = raf.getLocation();
  List<ObjectBean> beanList = new ArrayList<>();

  iosp = new H5iosp();
  NetcdfFile ncfile = new NetcdfFileSubclass(iosp, location);
  ncfile.sendIospMessage(H5iosp.IOSP_MESSAGE_INCLUDE_ORIGINAL_ATTRIBUTES);

  try {
    iosp.open(raf, ncfile, null);
  } catch (Throwable t) {
    StringWriter sw = new StringWriter(20000);
    PrintWriter s = new PrintWriter(sw);
    t.printStackTrace(s);
    dumpTA.setText(sw.toString());
  }

  H5header header = (H5header) iosp.sendIospMessage("header");
  for (H5header.DataObject dataObj : header.getDataObjects()) {
    beanList.add(new ObjectBean(dataObj));
  }

  objectTable.setBeans(beanList);
}
 
Example 12
Source File: Ghcnm2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void open(RandomAccessFile raff, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
  String dataFile = raff.getLocation();
  int pos = dataFile.lastIndexOf(".");
  String base = dataFile.substring(0, pos);
  String ext = dataFile.substring(pos);

  if (ext.equals(IDX_EXT)) {
    dataRaf = RandomAccessFile.acquire(base + DAT_EXT);
    stnRaf = RandomAccessFile.acquire(base + STN_EXT);

  } else if (ext.equals(DAT_EXT)) {
    dataRaf = raff;
    stnRaf = RandomAccessFile.acquire(base + STN_EXT);

  } else {
    stnRaf = raff;
    dataRaf = RandomAccessFile.acquire(base + DAT_EXT);
  }

  NcmlConstructor ncmlc = new NcmlConstructor();
  if (!ncmlc.populateFromResource("resources/nj22/iosp/ghcnm.ncml", ncfile)) {
    throw new IllegalStateException(ncmlc.getErrlog().toString());
  }
  ncfile.finish();

  dataVinfo = setVinfo(dataRaf, ncfile, dataPattern, "all_data");
  stnVinfo = setVinfo(stnRaf, ncfile, stnPattern, "station");

  StructureMembers.Member m = stnVinfo.sm.findMember(STNID);
  StructureDataRegexp.VinfoField f = (StructureDataRegexp.VinfoField) m.getDataObject();
  stn_fldno = f.fldno;

  // make index file if needed
  File idxFile = new File(base + IDX_EXT);
  if (!idxFile.exists())
    makeIndex(stnVinfo, dataVinfo, idxFile);
  else
    readIndex(idxFile.getPath());
}
 
Example 13
Source File: Cinrad2IOServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isValidFileOld(RandomAccessFile raf) {
  try {
    String loc = raf.getLocation();
    int posFirst = loc.lastIndexOf('/') + 1;
    if (posFirst < 0)
      posFirst = 0;
    String stationId = loc.substring(posFirst, posFirst + 4);
    NexradStationDB.init();
    NexradStationDB.Station station = NexradStationDB.get("K" + stationId);
    return station != null;
  } catch (IOException ioe) {
    return false;
  }
}
 
Example 14
Source File: AreaReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Check to see if this is a valid AREA file.
 *
 * @param raf the file in question
 * @return true if it is an AREA file.
 */
public static boolean isValidFile(RandomAccessFile raf) {
  String fileName = raf.getLocation();
  AreaFile af = null;
  try {
    af = new AreaFile(fileName); // LOOK opening again not ok for isValidFile
    return true;
  } catch (AreaFileException e) {
    return false; // barfola
  } finally {
    if (af != null)
      af.close(); // LOOK need to look at this code
  }
}
 
Example 15
Source File: GempakGridServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Is this a valid file?
 *
 * @param raf RandomAccessFile to check
 * @return true if a valid GEMPAK grid file
 * @throws IOException problem reading file
 */
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  try {
    gemreader = new GempakGridReader(raf.getLocation());
    return gemreader.init(raf, false);
  } catch (Exception ioe) {
    return false;
  }
}
 
Example 16
Source File: AbstractIOServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
  this.raf = raf;
  this.location = (raf != null) ? raf.getLocation() : null;
  this.ncfile = ncfile;
}
 
Example 17
Source File: BufrCdmIndex.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected boolean readIndex(RandomAccessFile raf) {
  this.idxFilename = raf.getLocation();

  try {
    raf.order(RandomAccessFile.BIG_ENDIAN);
    raf.seek(0);

    //// header message
    if (!NcStream.readAndTest(raf, MAGIC_START.getBytes(StandardCharsets.UTF_8))) {
      log.error("BufrCdmIndex {}: invalid index", raf.getLocation());
      return false;
    }

    int indexVersion = raf.readInt();
    boolean versionOk = (indexVersion == version);
    if (!versionOk) {
      log.warn("BufrCdmIndex {}: index found version={}, want version= {}", raf.getLocation(), indexVersion, version);
      return false;
    }

    int size = NcStream.readVInt(raf);
    if ((size < 0) || (size > 100 * 1000 * 1000)) {
      log.warn("BufrCdmIndex {}: invalid or empty index ", raf.getLocation());
      return false;
    }

    byte[] m = new byte[size];
    raf.readFully(m);

    BufrCdmIndexProto.BufrIndex proto = BufrCdmIndexProto.BufrIndex.parseFrom(m);
    bufrFilename = proto.getFilename();
    root = proto.getRoot();
    stations = proto.getStationsList();
    start = proto.getStart();
    end = proto.getEnd();
    nobs = proto.getNobs();

    // showProtoRoot(root);

  } catch (Throwable t) {
    log.error("Error reading index " + raf.getLocation(), t);
    return false;
  }

  return true;
}
 
Example 18
Source File: BufrConfig.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private BufrConfig(RandomAccessFile raf, Message m) throws IOException {
  this.filename = raf.getLocation();
  this.messHash = m.hashCode();
  this.rootConverter = new FieldConverter(m.ids.getCenterId(), m.getRootDataDescriptor());
  standardFields = StandardFields.extract(m);
}
 
Example 19
Source File: EmbeddedTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
EmbeddedTable(Message m, RandomAccessFile raf) {
  this.raf = raf;
  this.ids = m.ids;
  b = new TableB("embed", raf.getLocation());
  d = new TableD("embed", raf.getLocation());
}
 
Example 20
Source File: GempakGridReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initialize the file, read in all the metadata (ala DM_OPEN)
 *
 * @param raf RandomAccessFile to read.
 * @param fullCheck if true, check entire structure
 * @return A GempakGridReader
 * @throws IOException problem reading file
 */
public static GempakGridReader getInstance(RandomAccessFile raf, boolean fullCheck) throws IOException {
  GempakGridReader ggr = new GempakGridReader(raf.getLocation());
  ggr.init(raf, fullCheck);
  return ggr;
}