Package ucar.unidata.io

Examples of ucar.unidata.io.RandomAccessFile


     *
     * @throws IOException  problem reading file
     */
    public static void main(String[] args) throws IOException {
        IOServiceProvider mciosp = new McIDASGridServiceProvider();
        RandomAccessFile  rf     = new RandomAccessFile(args[0], "r", 2048);
        NetcdfFile ncfile = new MakeNetcdfFile(mciosp, rf, args[0], null);
    }
View Full Code Here


     if (dataFormat.equals(AR2V0001) || dataFormat.equals(AR2V0003)
            || dataFormat.equals(AR2V0004) || dataFormat.equals(AR2V0006) ) {
      raf.skipBytes(4);
      String BZ = raf.readString(2);
      if (BZ.equals("BZ")) {
        RandomAccessFile uraf;
        File uncompressedFile = DiskCache.getFileStandardPolicy(raf.getLocation() + ".uncompress");

        if (uncompressedFile.exists() && uncompressedFile.length() > 0) {
          // see if its locked - another thread is writing it
          FileInputStream fstream = null;
View Full Code Here

   * @param ufilename write to this file
   * @return raf of uncompressed file
   * @throws IOException on read error
   */
  private RandomAccessFile uncompress(RandomAccessFile inputRaf, String ufilename) throws IOException {
    RandomAccessFile outputRaf = new RandomAccessFile(ufilename, "rw");
    FileLock lock = null;

    while (true) { // loop waiting for the lock
      try {
        lock = outputRaf.getRandomAccessFile().getChannel().lock(0, 1, false);
        break;

      } catch (OverlappingFileLockException oe) { // not sure why lock() doesnt block
        try {
          Thread.sleep(100); // msecs
        } catch (InterruptedException e1) {
        }
      }
    }

    try {
      inputRaf.seek(0);
      byte[] header = new byte[Level2Record.FILE_HEADER_SIZE];
      inputRaf.read(header);
      outputRaf.write(header);

      boolean eof = false;
      int numCompBytes;
      byte[] ubuff = new byte[40000];
      byte[] obuff = new byte[40000];
      try {
        CBZip2InputStream cbzip2 = new CBZip2InputStream();
        while (!eof) {
          try {
            numCompBytes = inputRaf.readInt();
            if (numCompBytes == -1) {
              if (log.isDebugEnabled()) log.debug("  done: numCompBytes=-1 ");
              break;
            }
          } catch (EOFException ee) {
            log.warn("  got EOFException ");
            break; // assume this is ok
          }

          if (log.isDebugEnabled()) {
            log.debug("reading compressed bytes " + numCompBytes + " input starts at " + inputRaf.getFilePointer() + "; output starts at " + outputRaf.getFilePointer());
          }
          /*
          * For some stupid reason, the last block seems to
          * have the number of bytes negated.  So, we just
          * assume that any negative number (other than -1)
          * is the last block and go on our merry little way.
          */
          if (numCompBytes < 0) {
            if (log.isDebugEnabled()) log.debug("last block?" + numCompBytes);
            numCompBytes = -numCompBytes;
            eof = true;
          }
          byte[] buf = new byte[numCompBytes];
          inputRaf.readFully(buf);
          ByteArrayInputStream bis = new ByteArrayInputStream(buf, 2, numCompBytes - 2);

          //CBZip2InputStream cbzip2 = new CBZip2InputStream(bis);
          cbzip2.setStream(bis);
          int total = 0;
          int nread;
          /*
          while ((nread = cbzip2.read(ubuff)) != -1) {
            dout2.write(ubuff, 0, nread);
            total += nread;
          }
          */
          try {
            while ((nread = cbzip2.read(ubuff)) != -1) {
              if (total + nread > obuff.length) {
                byte[] temp = obuff;
                obuff = new byte[temp.length * 2];
                System.arraycopy(temp, 0, obuff, 0, temp.length);
              }
              System.arraycopy(ubuff, 0, obuff, total, nread);
              total += nread;
            }
            if (obuff.length >= 0) outputRaf.write(obuff, 0, total);
          } catch (BZip2ReadException ioe) {
            log.warn("Nexrad2IOSP.uncompress ", ioe);
          }
          float nrecords = (float) (total / 2432.0);
          if (log.isDebugEnabled())
            log.debug("  unpacked " + total + " num bytes " + nrecords + " records; ouput ends at " + outputRaf.getFilePointer());
        }

      } catch (Exception e) {
        if (outputRaf != null) outputRaf.close();
        outputRaf = null;

        // dont leave bad files around
        File ufile = new File(ufilename);
        if (ufile.exists()) {
          if (!ufile.delete())
            log.warn("failed to delete uncompressed file (IOException)" + ufilename);
        }

        if (e instanceof IOException)
          throw (IOException) e;
        else
          throw new RuntimeException(e);
      }

    } finally {
      if (null != outputRaf) outputRaf.flush();
      if (lock != null) lock.release();
    }

    return outputRaf;
  }
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.BIG_ENDIAN);
    raf.seek(0);
    byte[] b = new byte[8];
    raf.read(b);
    String test = new String(b);
    if (test.equals(Level2VolumeScan.ARCHIVE2) || test.equals(Level2VolumeScan.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(Level2VolumeScan.ARCHIVE2) || test.equals(Level2VolumeScan.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("/upc/share/testdata/radar/nexrad/level2/Level2_KFTG_20060818_1814.ar2v.uncompress.missingradials", "r");
    // RandomAccessFile raf = new RandomAccessFile("R:/testdata2/radar/nexrad/level2/problem/KCCX_20060627_1701", "r");
    new Level2VolumeScan(raf, null);
  }
View Full Code Here

  /** Read the header from a COMP* file and return results in the V5DStruct.
      @return true = ok, false = error.
  */
  boolean read_comp_header() throws IOException {
    int id;
    RandomAccessFile f = FileDesc;

    // reset file position to start of file
    f.seek(0);

    // read file ID
    id = f.readInt();

    if (id == 0x80808080 || id == 0x80808081) {
      // Older COMP5D format
      int gridtimes, gridparms;
      int i, j, it, iv, nl;
      int gridsize;
      float hgttop, hgtinc;

      if (id == 0x80808080) {
        // 20 vars, 300 times
        gridtimes = 300;
        gridparms = 20;
      }
      else {
        // 30 vars, 400 times
        gridtimes = 400;
        gridparms = 30;
      }

      FirstGridPos = 12 * 4 + 8 * gridtimes + 4 * gridparms;

      NumTimes = f.readInt();
      NumVars = f.readInt();
      Nr = f.readInt();
      Nc = f.readInt();
      nl = f.readInt();
      for (i=0; i<NumVars; i++) {
        Nl[i] = nl;
        LowLev[i] = 0;
      }
      ProjArgs[0] = f.readFloat();
      ProjArgs[1] = f.readFloat();
      hgttop = f.readFloat();
      ProjArgs[2] = f.readFloat();
      ProjArgs[3] = f.readFloat();
      hgtinc = f.readFloat();
      VerticalSystem = 1;
      VertArgs[0] = hgttop - hgtinc * (nl - 1);
      VertArgs[1] = hgtinc;

      // read dates and times
      for (i=0; i<gridtimes; i++) {
        j = f.readInt();
        DateStamp[i] = v5dDaysToYYDDD(j);
      }
      for (i=0; i<gridtimes; i++) {
        j = f.readInt();
        TimeStamp[i] = v5dSecondsToHHMMSS(j);
      }

      // read variable names
      for (i=0; i<gridparms; i++) {
        char[] name = new char[4];
        for (int q=0; q<4; q++) name[q] = (char) f.readByte();

        // remove trailing spaces, if any
        for (j=3; j>0; j--) {
          if (name[j] == ' ' || name[j] == 0) name[j] = 0;
          else break;
        }
        System.arraycopy(name, 0, VarName[i], 0, 4);
        VarName[i][4] = 0;
      }

      gridsize = ((Nr * Nc * nl + 3) / 4) * 4;
      for (i=0; i<NumVars; i++) {
        GridSize[i] = 8 + gridsize;
      }
      SumGridSizes = (8 + gridsize) * NumVars;

      // read the grids and their ga,gb values to find min and max values

      for (i=0; i<NumVars; i++) {
        MinVal[i] = 999999.9f;
        MaxVal[i] = -999999.9f;
      }

      for (it=0; it<NumTimes; it++) {
        for (iv=0; iv<NumVars; iv++) {
          float ga, gb;
          float min, max;

          ga = f.readFloat();
          gb = f.readFloat();

          // skip ahead by gridsize bytes
          f.skipBytes(gridsize);
          min = -(125.0f + gb) / ga;
          max = (125.0f-gb) / ga;
          if (min < MinVal[iv])  MinVal[iv] = min;
          if (max > MaxVal[iv])  MaxVal[iv] = max;
        }
      }

      // done
    }
    else if (id == 0x80808082 || id == 0x80808083) {
      // Newer COMP5D format
      int gridtimes, gridsize;
      int it, iv, nl, i, j;
      float delta = 0f;

      gridtimes = f.readInt();
      NumVars = f.readInt();
      NumTimes = f.readInt();
      Nr = f.readInt();
      Nc = f.readInt();
      nl = f.readInt();
      for (i=0; i<NumVars; i++) {
        Nl[i] = nl;
      }

      ProjArgs[2] = f.readFloat();
      ProjArgs[3] = f.readFloat();

      // Read height and determine if equal spacing
      VerticalSystem = 1;
      for (i=0; i<nl; i++) {
        VertArgs[i] = f.readFloat();
        if (i == 1) {
          delta = VertArgs[1] - VertArgs[0];
        }
        else if (i > 1) {
          if (delta != (VertArgs[i] - VertArgs[i - 1])) {
            VerticalSystem = 2;
          }
        }
      }
      if (VerticalSystem == 1) VertArgs[1] = delta;

      // read variable names
      for (iv=0; iv<NumVars; iv++) {
        char[] name = new char[8];

        for (int q=0; q<8; q++) name[q] = (char) f.readByte();

        // remove trailing spaces, if any
        for (j=7; j>0; j--) {
          if (name[j] == ' ' || name[j] == 0) name[j] = 0;
          else break;
        }
        System.arraycopy(name, 0, VarName[iv], 0, 8);
        VarName[iv][8] = 0;
      }

      for (iv=0; iv<NumVars; iv++) {
        MinVal[iv] = f.readFloat();
      }
      for (iv=0; iv<NumVars; iv++) {
        MaxVal[iv] = f.readFloat();
      }
      for (it=0; it<gridtimes; it++) {
        j = f.readInt();
        TimeStamp[it] = v5dSecondsToHHMMSS(j);
      }
      for (it=0; it<gridtimes; it++) {
        j = f.readInt();
        DateStamp[it] = v5dDaysToYYDDD(j);
      }
      for (it=0; it<gridtimes; it++) {
        float nlat;
        nlat = f.readFloat();
        if (it == 0) ProjArgs[0] = nlat;
      }
      for (it=0; it<gridtimes; it++) {
        float wlon;
        wlon = f.readFloat();
        if (it == 0) ProjArgs[1] = wlon;
      }

      // calculate grid storage sizes
      if (id == 0x80808082) {
View Full Code Here

    byte[] compdata1) throws IOException, BadFormException
  {
    long pos;
    short bias;
    int i, n, nl;
    RandomAccessFile f = FileDesc;

    // move to position in file
    pos = grid_position(time, vr);
    f.seek(pos);

    if (FileFormat == 0x80808083) {
      // read McIDAS grid and file numbers
      int mcfile, mcgrid;
      mcfile = f.readInt();
      mcgrid = f.readInt();
      McFile[time][vr] = (short) mcfile;
      McGrid[time][vr] = (short) mcgrid;
    }

    nl = Nl[vr];

    if (FileFormat == 0x80808080 || FileFormat == 0x80808081) {
      // single ga, gb pair for whole grid
      float a, b;
      a = f.readFloat();
      b = f.readFloat();
      // convert a, b to new v5d ga, gb values
      for (i=0; i<nl; i++) {
        if (a == 0.0) {
          ga[i] = gb[i] = 0.0f;
        }
        else {
          gb[i] = (b + 128.0f) / -a;
          ga[i] = 1.0f / a;
        }
      }
      bias = 128;
    }
    else {
      // read ga, gb arrays
      read_float4_array(f, ga, Nl[vr]);
      read_float4_array(f, gb, Nl[vr]);

      // convert ga, gb values to v5d system
      for (i=0; i<nl; i++) {
        if (ga[i] == 0.0) {
          ga[i] = gb[i] = 0.0f;
        }
        else {
          // gb[i] = (gb[i]+125.0) / -ga[i];
          gb[i] = (gb[i] + 128.0f) / -ga[i];
          ga[i] = 1.0f / ga[i];
        }
      }
      bias = 128// 125 ???
    }

    // read compressed grid data
    n = Nr * Nc * Nl[vr];
    if (f.read(compdata1, 0, n) != n) return false;

    // convert data values to v5d system
    n = Nr * Nc * Nl[vr];
    for (i=0; i<n; i++) compdata1[i] += bias;

View Full Code Here

    System.out.println(now.toString() + " ... Start of Grib2Netcdf");
    System.out.println("read grib file=" + args[0] + " write to netCDF file=" + args[1]);

    // Reading of Grib files must be inside a try-catch block
    try {
      RandomAccessFile raf;
      raf = new RandomAccessFile(args[0], "r");
      raf.order(RandomAccessFile.BIG_ENDIAN);
     
      Class c = ucar.nc2.iosp.grib.GribGridServiceProvider.class;
      IOServiceProvider iosp = null;
      try {
        iosp = (IOServiceProvider) c.newInstance();
      } catch (InstantiationException e) {
        throw new IOException("IOServiceProvider " + c.getName() + "must have no-arg constructor.");
      } catch (IllegalAccessException e) {
        throw new IOException("IOServiceProvider " + c.getName() + " IllegalAccessException: " + e.getMessage());
      }

      NetcdfFile ncfile = new MakeNetcdfFile(iosp, raf, args[0], null);
      NetcdfFile nc = FileWriter.writeToFile(ncfile, args[1]);
      nc.close();
      raf.close()// done reading

      // Catch thrown errors from GribFile
    } catch (FileNotFoundException noFileError) {
      System.err.println("FileNotFoundException : " + noFileError);
    } catch (IOException ioError) {
View Full Code Here

    //see if we have to uncompress
    if (dataFormat.equals(AR2V0001)) {
      raf.skipBytes(4);
      String BZ = raf.readString(2);
      if (BZ.equals("BZ")) {
        RandomAccessFile uraf = null;
        File uncompressedFile = DiskCache.getFileStandardPolicy(raf.getLocation() + ".uncompress");
        if (uncompressedFile.exists()) {
          uraf = new ucar.unidata.io.RandomAccessFile(uncompressedFile.getPath(), "r");
        } else {
          // nope, gotta uncompress it
          uraf = uncompress(raf, uncompressedFile.getPath(), debug);
          uraf.flush();
          if (debug) log.debug("flushed uncompressed file= " + uncompressedFile.getPath());
        }
        // switch to uncompressed file
        raf.close();
        raf = uraf;
View Full Code Here

   */
  private RandomAccessFile uncompress(RandomAccessFile raf2, String ufilename, boolean debug) throws IOException {
    raf2.seek(0);
    byte[] header = new byte[Cinrad2Record.FILE_HEADER_SIZE];
    raf2.read(header);
    RandomAccessFile dout2 = new RandomAccessFile(ufilename, "rw");
    dout2.write(header);

    boolean eof = false;
    int numCompBytes;
    byte[] ubuff = new byte[40000];
    byte[] obuff = new byte[40000];
    try {
      CBZip2InputStream cbzip2 = new CBZip2InputStream();
      while (!eof) {

        try {
          numCompBytes = raf2.readInt();
          if (numCompBytes == -1) {
            if (debug) log.debug("  done: numCompBytes=-1 ");
            break;
          }
        } catch (EOFException ee) {
          if (debug) log.debug("  got EOFException ");
          break; // assume this is ok
        }

        if (debug) {
          log.debug("reading compressed bytes " + numCompBytes + " input starts at " + raf2.getFilePointer() + "; output starts at " + dout2.getFilePointer());
        }
        /*
        * For some stupid reason, the last block seems to
        * have the number of bytes negated.  So, we just
        * assume that any negative number (other than -1)
        * is the last block and go on our merry little way.
        */
        if (numCompBytes < 0) {
          if (debug) log.debug("last block?" + numCompBytes);
          numCompBytes = -numCompBytes;
          eof = true;
        }
        byte[] buf = new byte[numCompBytes];
        raf2.readFully(buf);
        ByteArrayInputStream bis = new ByteArrayInputStream(buf, 2, numCompBytes - 2);

        //CBZip2InputStream cbzip2 = new CBZip2InputStream(bis);
        cbzip2.setStream(bis);
        int total = 0;
        int nread;
        /*
        while ((nread = cbzip2.read(ubuff)) != -1) {
          dout2.write(ubuff, 0, nread);
          total += nread;
        }
        */
        try {
          while ((nread = cbzip2.read(ubuff)) != -1) {
            if (total + nread > obuff.length) {
              byte[] temp = obuff;
              obuff = new byte[temp.length * 2];
              System.arraycopy(temp, 0, obuff, 0, temp.length);
            }
            System.arraycopy(ubuff, 0, obuff, total, nread);
            total += nread;
          }
          if (obuff.length >= 0) dout2.write(obuff, 0, total);
        } catch (BZip2ReadException ioe) {
          log.debug("Cinrad2IOSP.uncompress ", ioe);
        }
        float nrecords = (float) (total / 2432.0);
        if (debug)
          log.debug("  unpacked " + total + " num bytes " + nrecords + " records; ouput ends at " + dout2.getFilePointer());
      }
    } catch (EOFException e) {
      e.printStackTrace();
    }

    dout2.flush();
    return dout2;
  }
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.