Package ucar.unidata.io

Examples of ucar.unidata.io.RandomAccessFile


  */
  boolean read_v5d_header() throws IOException, BadFormException {
    boolean end_of_header = false;
    int id;
    int idlen, vr, numargs;
    RandomAccessFile f;

    f = FileDesc;
    int order = f.BIG_ENDIAN;

    // first try to read the header id, check the endianness
    while (true) {
      f.seek(0);
      f.order(order);
      id = f.readInt();
      idlen = f.readInt();
      if (id == TAG_ID && idlen == 0) {
        // this is a v5d file
        FileFormat = 0;
        break;
      }
      else if (id >= 0x80808080 && id <= 0x80808083) {
        // this is an old COMP* file
        FileFormat = id;
        return read_comp_header();
      }
      else if (order == f.BIG_ENDIAN) {
        order = f.LITTLE_ENDIAN;
        continue// try again
      } else {
        // unknown file type
        System.out.println("unknown file type");
        return false;
      }
    }

    // default
    CompressMode = 1;

    while (!end_of_header) {
      int tag, length;
      int i, time, nl, lev;

      tag = f.readInt();
      length = f.readInt();

      switch (tag) {
        case TAG_VERSION:
          V5Dassert(length == 10);
          byte[] b = new byte[10];
          f.read(b, 0, 10);
          int index = 10;
          for (int q=0; q<10; q++) {
            if (b[q] == 0) {
              index = q;
              break;
            }
          }
          FileVersion = new String(b, 0, index);
          // Check if reading a file made by a future version of Vis5D
          if (FileVersion.compareTo(FILE_VERSION) > 0) {
            System.err.println("Warning: Trying to read a version " +
              FileVersion + " file, you should upgrade Vis5D.");
          }
          break;
        case TAG_NUMTIMES:
          V5Dassert(length == 4);
          NumTimes = f.readInt();
          break;
        case TAG_NUMVARS:
          V5Dassert(length == 4);
          NumVars = f.readInt();
          break;
        case TAG_VARNAME:
          // 1 int + 10 char
          V5Dassert(length == 14);
          vr = f.readInt();
          for (int q=0; q<10; q++) VarName[vr][q] = (char) f.readByte();
          break;
        case TAG_NR:
          // Number of rows for all variables
          V5Dassert(length == 4);
          Nr = f.readInt();
          break;
        case TAG_NC:
          // Number of columns for all variables
          V5Dassert(length == 4);
          Nc = f.readInt();
          break;
        case TAG_NL:
          // Number of levels for all variables
          V5Dassert(length == 4);
          nl = f.readInt();
          for (i=0; i<NumVars; i++) {
            Nl[i] = nl;
          }
          break;
        case TAG_NL_VAR:
          // Number of levels for one variable
          V5Dassert(length == 8);
          vr = f.readInt();
          Nl[vr] = f.readInt();
          break;
        case TAG_LOWLEV_VAR:
          // Lowest level for one variable
          V5Dassert(length == 8);
          vr = f.readInt();
          LowLev[vr] = f.readInt();
          break;

        case TAG_TIME:
          // Time stamp for 1 timestep
          V5Dassert(length == 8);
          time = f.readInt();
          TimeStamp[time] = f.readInt();
          break;
        case TAG_DATE:
          // Date stamp for 1 timestep
          V5Dassert(length == 8);
          time = f.readInt();
          DateStamp[time] = f.readInt();
          break;

        case TAG_MINVAL:
          // Minimum value for a variable
          V5Dassert(length == 8);
          vr = f.readInt();
          MinVal[vr] = f.readFloat();
          break;
        case TAG_MAXVAL:
          // Maximum value for a variable
          V5Dassert(length == 8);
          vr = f.readInt();
          MaxVal[vr] = f.readFloat();
          break;
        case TAG_COMPRESS:
          // Compress mode
          V5Dassert(length == 4);
          CompressMode = f.readInt();
          break;
        case TAG_UNITS:
          // physical units
          V5Dassert(length == 24);
          vr = f.readInt();
          for (int q=0; q<20; q++) Units[vr][q] = (char) f.readByte();
          break;

        // Vertical coordinate system
        case TAG_VERTICAL_SYSTEM:
          V5Dassert(length == 4);
          VerticalSystem = f.readInt();
          if (VerticalSystem < 0 || VerticalSystem > 3) {
            System.err.println("Error: bad vertical coordinate system: " +
              VerticalSystem);
          }
          break;
        case TAG_VERT_ARGS:
          numargs = f.readInt();
          V5Dassert(numargs <= MAXVERTARGS);
          for (int q=0; q<numargs; q++) VertArgs[q] = f.readFloat();
          V5Dassert(length == numargs * 4 + 4);
          break;
        case TAG_HEIGHT:
          // height of a grid level
          V5Dassert(length == 8);
          lev = f.readInt();
          VertArgs[lev] = f.readFloat();
          break;
        case TAG_BOTTOMBOUND:
          V5Dassert(length == 4);
          VertArgs[0] = f.readFloat();
          break;
        case TAG_LEVINC:
          V5Dassert(length == 4);
          VertArgs[1] = f.readFloat();
          break;

        // Map projection information
        case TAG_PROJECTION:
          V5Dassert(length == 4);
          Projection = f.readInt();
          // WLH 4-21-95
          if (Projection < 0 || Projection > 4) {
            System.err.println("Error while reading header, bad projection (" +
              Projection + ")");
            return false;
          }
          break;
        case TAG_PROJ_ARGS:
          numargs = f.readInt();
          V5Dassert(numargs <= MAXPROJARGS);
          for (int q=0; q<numargs; q++) ProjArgs[q] = f.readFloat();
          V5Dassert(length == 4 * numargs + 4);
          break;
        case TAG_NORTHBOUND:
          V5Dassert(length == 4);
          if (Projection == 0 || Projection == 1 || Projection == 4) {
            ProjArgs[0] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_WESTBOUND:
          V5Dassert(length == 4);
          if (Projection == 0 || Projection == 1 || Projection == 4) {
            ProjArgs[1] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_ROWINC:
          V5Dassert(length == 4);
          if (Projection == 0 || Projection == 1 || Projection == 4) {
            ProjArgs[2] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_COLINC:
          V5Dassert(length == 4);
          if (Projection == 0 || Projection == 1 || Projection == 4) {
            ProjArgs[3] = f.readFloat();
          }
          else if (Projection == 2) {
            ProjArgs[5] = f.readFloat();
          }
          else if (Projection == 3) {
            ProjArgs[4] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_LAT1:
          V5Dassert(length == 4);
          if (Projection == 2) {
            ProjArgs[0] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_LAT2:
          V5Dassert(length == 4);
          if (Projection == 2) {
            ProjArgs[1] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_POLE_ROW:
          V5Dassert(length == 4);
          if (Projection == 2) {
            ProjArgs[2] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_POLE_COL:
          V5Dassert(length == 4);
          if (Projection == 2) {
            ProjArgs[3] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_CENTLON:
          V5Dassert(length == 4);
          if (Projection == 2) {
            ProjArgs[4] = f.readFloat();
          }
          else if (Projection == 3) {
            ProjArgs[1] = f.readFloat();
          }
          // WLH 4-21-95
          else if (Projection == 4) {
            ProjArgs[5] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_CENTLAT:
          V5Dassert(length == 4);
          if (Projection == 3) {
            ProjArgs[0] = f.readFloat();
          }
          // WLH 4-21-95
          else if (Projection == 4) {
            ProjArgs[4] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_CENTROW:
          V5Dassert(length == 4);
          if (Projection == 3) {
            ProjArgs[2] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_CENTCOL:
          V5Dassert(length == 4);
          if (Projection == 3) {
            ProjArgs[3] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;
        case TAG_ROTATION:
          V5Dassert(length == 4);
          // WLH 4-21-95
          if (Projection == 4) {
            ProjArgs[6] = f.readFloat();
          }
          else {
            f.skipBytes(4);
          }
          break;

        case TAG_END:
          // end of header
          end_of_header = true;
          f.skipBytes(length);
          break;

        default:
          // unknown tag, skip to next tag
          System.err.println("Unknown tag: " + tag + "  length=" + length);
          f.skipBytes(length);
          break;
      }

    }

    v5dVerifyStruct();

    // Now we're ready to read the grid data

    // Save current file pointer
    FirstGridPos = (int) f.getFilePointer();

    // compute grid sizes
    SumGridSizes = 0;
    for (vr=0;vr<NumVars;vr++) {
      GridSize[vr] = 8 * Nl[vr] + v5dSizeofGrid(0, vr);
View Full Code Here


    File file = new File(location);
    File stnFile = getStnFile(location);

    if (ext.equals(IDX_EXT)) {
      stnRaf = new RandomAccessFile(stnFile.getPath(), "r");

    } else if (ext.equals(DAT_EXT)) {
      stnRaf = new RandomAccessFile(stnFile.getPath(), "r");
      dataRaf = raff;

      //extract the station id
      String name = file.getName();
      stationId = name.substring(0, name.length() - DAT_EXT.length());
View Full Code Here

  // check if compressed file seems ok
  static public long testValid(String ufilename) throws IOException {
    boolean lookForHeader = false;

    // gotta make it
    RandomAccessFile raf = new RandomAccessFile(ufilename, "r");
    raf.order(RandomAccessFile.LITTLE_ENDIAN); //.BIG_ENDIAN);
    raf.seek(0);
    byte[] b = new byte[8];
    raf.read(b);
    String test = new String(b);
    if (test.equals(Cinrad2VolumeScan.ARCHIVE2) || test.equals(Cinrad2VolumeScan.AR2V0001)) {
      System.out.println("--Good header= " + test);
      raf.seek(24);
    } else {
      System.out.println("--No header ");
      lookForHeader = true;
      raf.seek(0);
    }

    boolean eof = false;
    int numCompBytes;
    try {

      while (!eof) {

        if (lookForHeader) {
          raf.read(b);
          test = new String(b);
          if (test.equals(Cinrad2VolumeScan.ARCHIVE2) || test.equals(Cinrad2VolumeScan.AR2V0001)) {
            System.out.println("  found header= " + test);
            raf.skipBytes(16);
            lookForHeader = false;
          } else {
            raf.skipBytes(-8);
          }
        }

        try {
          numCompBytes = raf.readInt();
          if (numCompBytes == -1) {
            System.out.println("\n--done: numCompBytes=-1 ");
            break;
          }
        } catch (EOFException ee) {
          System.out.println("\n--got EOFException ");
          break; // assume this is ok
        }

        System.out.print(" " + numCompBytes + ",");
        if (numCompBytes < 0) {
          System.out.println("\n--last block " + numCompBytes);
          numCompBytes = -numCompBytes;
          if (!lookForHeader) eof = true;
        }

        raf.skipBytes(numCompBytes);
      }
    } catch (EOFException e) {
      e.printStackTrace();
    }

    return raf.getFilePointer();
  }
View Full Code Here

  }

  public static void main(String args[]) throws IOException {
    NexradStationDB.init();

    RandomAccessFile raf = new RandomAccessFile("R:/testdata/radar/nexrad/Cinrad2/problem/KCCX_20060627_1701", "r");
    new Cinrad2VolumeScan(raf, null);
  }
View Full Code Here

      if (!exists) return;
      try {
        if (dataRaf != null)
          this.timeSeriesRaf = dataRaf; // single station case - data file already open
        else
          this.timeSeriesRaf = new RandomAccessFile( file.getPath(), "r");// LOOK check exists LOOK who closes?

        totalBytes = timeSeriesRaf.length();
        timeSeriesRaf.seek(0);
      } catch (IOException e) {
        throw new RuntimeException(e);
View Full Code Here

* @since May 9, 2008
*/
public class ScannerPqact extends Scanner {

  static void extract(String filename) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(filename, "r");
    out.format("Open %s size = %d Kb \n", raf.getLocation(), raf.length() / 1000);

    MessageScanner scan = new MessageScanner(raf);
    while (scan.hasNext()) {

      Message m = scan.next();
      if (m == null) {
        out.format(" bad message in file %s %n", filename);
        bad_msgs++;
        continue;
      }
      readBytes += m.is.getBufrLength();

      // incomplete tables
      try {
        if (!m.isTablesComplete()) {
          bad_tables++;
          //out.format(" missing table in file %s %n", filename);
          continue;
        }
      } catch (UnsupportedOperationException e) {
        bad_operation++;
        out.format(" missing operation in file %s, %s %n", filename, e.getMessage());
        continue;

      } catch (Exception e) {
        out.format(" Exception in file %s, %n", filename);
        m.dumpHeader(out);
        continue;
      }

      // track desc to headers
      String ttaaii = extractWMO(m.getHeader());
      if (ttaaii == null) {
        bad_wmo++;
        out.format(" bad wmo header in file %s, %s %n", filename, m.getHeader());
        continue;
      }

      //readBytes += m.is.getBufrLength();

      // run through the pattern matching
      boolean hasMatch = false;
      for (Pqact pqact : pqactList) {
        if (pqact.match(ttaaii, m)) {
          hasMatch = true;
          writeBytes+= scan.writeCurrentMessage(pqact.getWBC());
          writemsg++;
          break;
        }
      }

      if (!hasMatch && (wbc != null)) {
        writemsg++;
        writeBytes+= scan.writeCurrentMessage(wbc);
      }
    }
    raf.close();
    total_msgs += scan.getTotalMessages();

  }
View Full Code Here

  static int nomatch, badmatch, writemsg;
  static long writeBytes, readBytes;

  static void scanMessageTypesPqact(String filename) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(filename, "r");
    //out.format("\n-----\nOpen %s size = %d Kb \n", raf.getLocation(), raf.length() / 1000);
    file_size += raf.length();

    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext()) {

      Message m = scan.next();
      if (m == null) {
        out.format(" bad message in file %s %n", filename);
        bad_msgs++;
        continue;
      }

      // incomplete tables
      try {
        if (!m.isTablesComplete()) {
          bad_tables++;
          //out.format(" missing table in file %s %n", filename);
          continue;
        }
      } catch (UnsupportedOperationException e) {
        bad_operation++;
        out.format(" missing operation in file %s %n", filename);
        if (wbc != null)
          scan.writeCurrentMessage(wbc);
        continue;
      }

      // track desc to headers
      String ttaaii = extractWMO(m.getHeader());
      if (ttaaii == null) {
        bad_wmo++;
        continue;
      }

      // run through the pattern matching
      Pqact matched = null;
      for (Pqact pqact : pqactList) {
        boolean match = pqact.match(ttaaii, m);
        if ((matched != null) && match)
          System.out.println("double match <" + ttaaii + "> with " + matched.pats + " and " + pqact.pats);
        if (match)
          matched = pqact;
      }
      if (matched == null) {
        System.out.println("no match <" + ttaaii + ">");
        nomatch++;
      }

      count++;
    }
    raf.close();
    //out.format("total_msgs= %d good=%d total_obs = %d\n", scan.getTotalMessages(), count, scan.getTotalObs());
    total_msgs += scan.getTotalMessages();
    good_msgs += count;
    total_obs += scan.getTotalObs();
  }
View Full Code Here

  //////////////////////////////////////////////////////////////

  static void scanDDS(String filename) throws IOException {
    long start = System.nanoTime();
    RandomAccessFile raf = new RandomAccessFile(filename, "r");
    out.format("\nOpen %s size = %d Kb \n", raf.getLocation(), raf.length() / 1000);

    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext()) {
      Message m = scan.next();
      if (m == null) continue;
      m.dump(out);

      if (!m.isTablesComplete()) {
        out.format("**INCOMPLETE ");
      }

      long startPos = m.is.getStartPos();
      out.format(" msg= %d time=%s starts=%d len=%d end=%d dataEnd=%d\n",
              count, m.getReferenceTime(), startPos, m.is.getBufrLength(), (startPos + m.is.getBufrLength()),
              (m.dataSection.getDataPos() + m.dataSection.getDataLength()));
      out.format("  ndatasets=%d isCompressed=%s datatype=0x%x header=%s\n",
              m.getNumberDatasets(), m.dds.isCompressed(), m.dds.getDataType(), m.getHeader());

      count++;
      break;
    }
    raf.close();
  }
View Full Code Here

  //////////////////////////////////////////////////////////////

  // o = minimal, 1=header, 2=dump dds
  static int dumpMessages(String filename, int mode) throws IOException {
    long start = System.nanoTime();
    RandomAccessFile raf = new RandomAccessFile(filename, "r");
    out.format("\nOpen %s size = %d Kb \n", raf.getLocation(), raf.length() / 1000);

    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext()) {
      Message m = scan.next();
      if (m == null) continue;
      //if (count == 0) new BufrDump2().dump(out, m);

      if (!m.isTablesComplete()) {
        out.format("**INCOMPLETE ");
      }

      if (mode >= 0) {
        long startPos = m.is.getStartPos();
        out.format(" msg= %d time=%s starts=%d len=%d end=%d dataEnd=%d hash=[0x%x]\n",
                count, m.getReferenceTime(), startPos, m.is.getBufrLength(), (startPos + m.is.getBufrLength()),
                (m.dataSection.getDataPos() + m.dataSection.getDataLength()), m.hashCode());
        out.format("  ndatasets=%d isCompressed=%s datatype=0x%x header=%s\n",
                m.getNumberDatasets(), m.dds.isCompressed(), m.dds.getDataType(), m.getHeader());
      }

      if (mode == 2)
        m.dump(out);
      else if (mode == 1)
        m.dumpHeader(out);

      if (mode >= 0) out.format("%n");
      count++;
    }
    raf.close();

    long took = (System.nanoTime() - start);
    double rate = (took > 0) ? ((double) (1000 * 1000) * count / took) : 0.0;
    out.format(" nmsgs= %d nobs = %d took %d msecs rate = %f msgs/msec\n", count, scan.getTotalObs(), took / (1000 * 1000), rate);
    return scan.getTotalObs();
View Full Code Here

  //////////////////////////////////////////////////////////////

  static void scanTimes(String filename) throws IOException {
    long start = System.nanoTime();
    RandomAccessFile raf = new RandomAccessFile(filename, "r");
    out.format("\nOpen %s size = %d Kb \n", raf.getLocation(), raf.length() / 1000);

    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext()) {
      Message m = scan.next();
      if (m == null) continue;
      //if (count == 0) new BufrDump2().dump(out, m);

      out.format(" %s time=%s\n", m.getHeader(), m.getReferenceTime());
      count++;
    }
    raf.close();
  }
View Full Code Here

TOP

Related Classes of ucar.unidata.io.RandomAccessFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.