Java Code Examples for java.io.RandomAccessFile#readFloat()

The following examples show how to use java.io.RandomAccessFile#readFloat() . 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: MM5DataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Read big header
 *
 * @param br The randomAccessFile
 * @param isSequential If is sequential
 * @return The big header
 * @throws IOException
 */
public BigHeader readBigHeader(RandomAccessFile br, boolean isSequential) throws IOException {
    BigHeader bh = new BigHeader();
    if (isSequential) {
        br.skipBytes(4);
    }
    byte[] bytes = new byte[80];
    int i, j;
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 50; j++) {
            bh.bhi[j][i] = br.readInt();
        }
    }
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 20; j++) {
            bh.bhr[j][i] = br.readFloat();
        }
    }
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 50; j++) {
            br.read(bytes);
            bh.bhic[j][i] = new String(bytes).trim();
        }
    }
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 20; j++) {
            br.read(bytes);
            bh.bhrc[j][i] = new String(bytes).trim();
        }
    }

    if (isSequential) {
        br.skipBytes(4);
    }

    return bh;
}
 
Example 2
Source File: MM5DataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Read sub header
 *
 * @param br The randomAccessFile
 * @param isSequential If if sequential
 * @return The sub header
 * @throws IOException
 */
public SubHeader readSubHeader(RandomAccessFile br, boolean isSequential) throws IOException {
    SubHeader sh = new SubHeader();
    byte[] bytes = new byte[4];
    int i;
    if (isSequential) {
        br.skipBytes(4);
    }

    sh.ndim = br.readInt();
    for (i = 0; i < 4; i++) {
        sh.start_index[i] = br.readInt();
    }
    for (i = 0; i < 4; i++) {
        sh.end_index[i] = br.readInt();
    }
    sh.xtime = br.readFloat();
    br.read(bytes);
    sh.staggering = new String(bytes).trim();
    br.read(bytes);
    sh.ordering = new String(bytes).trim();
    bytes = new byte[24];
    br.read(bytes);
    sh.current_date = new String(bytes).trim();
    bytes = new byte[9];
    br.read(bytes);
    sh.name = new String(bytes).trim();
    bytes = new byte[25];
    br.read(bytes);
    sh.unit = new String(bytes).trim();
    bytes = new byte[46];
    br.read(bytes);
    sh.description = new String(bytes).trim();

    if (isSequential) {
        br.skipBytes(4);
    }

    return sh;
}
 
Example 3
Source File: HYSPLITPartDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Read array data of the variable
 *
 * @param varName Variable name
 * @param origin The origin array
 * @param size The size array
 * @param stride The stride array
 * @return Array data
 */
@Override
public Array read(String varName, int[] origin, int[] size, int[] stride) {
    try {
        Variable var = this.getVariable(varName);
        int timeIdx = (int)var.findAttribute("time_index").getNumericValue();
        int particleNum = _parameters.get(timeIdx).get(0);
        int pollutantNum = _parameters.get(timeIdx).get(1);
        int pos = _parameters.get(timeIdx).get(2);
        Array r = Array.factory(var.getDataType(), new int[]{particleNum});

        RandomAccessFile br = new RandomAccessFile(this.getFileName(), "r");
        int i, j;
        float lon, lat, alt;

        br.seek(pos);
        br.skipBytes(28);
        for (i = 0; i < particleNum; i++) {
            br.skipBytes(8);
            for (j = 0; j < pollutantNum; j++) {
                br.skipBytes(4);
            }
            br.skipBytes(8);
            lat = br.readFloat();
            lon = br.readFloat();
            alt = br.readFloat();

            if (varName.startsWith("lon"))
                r.setFloat(i, lon);
            else if (varName.startsWith("lat"))
                r.setFloat(i, lat);
            else
                r.setFloat(i, alt);

            br.skipBytes(40);
        }

        return r;
    } catch (IOException e) {
        return null;
    }
}
 
Example 4
Source File: HYSPLITPartDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Read data frame
 *
 * @param timeIdx Time index
 * @return Data frame
 */
public DataFrame readDataFrame(int timeIdx) {
    try {
        int particleNum = _parameters.get(timeIdx).get(0);
        int pollutantNum = _parameters.get(timeIdx).get(1);
        int pos = _parameters.get(timeIdx).get(2);
        List<Array> data = new ArrayList<>();
        ColumnIndex columns = new ColumnIndex();
        for (Variable var : this.variables) {
            if ((int)var.findAttribute("time_index").getNumericValue() == timeIdx) {
                columns.add(new Column(var.getName(), var.getDataType()));
                data.add(Array.factory(var.getDataType(), new int[]{particleNum}));
            }
        }

        RandomAccessFile br = new RandomAccessFile(this.getFileName(), "r");
        int i, j;
        float lon, lat, alt;

        br.seek(pos);
        br.skipBytes(28);
        for (i = 0; i < particleNum; i++) {
            br.skipBytes(8);
            for (j = 0; j < pollutantNum; j++) {
                br.skipBytes(4);
            }
            br.skipBytes(8);
            lat = br.readFloat();
            lon = br.readFloat();
            alt = br.readFloat();

            data.get(0).setFloat(i, lat);
            data.get(1).setFloat(i, lon);
            data.get(2).setFloat(i, alt);

            br.skipBytes(40);
        }

        Index index = Index.factory(particleNum);
        DataFrame df = new DataFrame(data, index, columns);
        return df;
    } catch (IOException e) {
        return null;
    }
}
 
Example 5
Source File: HYSPLITPartDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public StationData getStationData(int timeIdx, String varName, int levelIdx) {
    try {
        StationData stationData = new StationData();
        List<String> stations = new ArrayList<String>();
        int particleNum = _parameters.get(timeIdx).get(0);
        int pollutantNum = _parameters.get(timeIdx).get(1);
        int pos = _parameters.get(timeIdx).get(2);
        double[][] discreteData = new double[particleNum][3];

        RandomAccessFile br = new RandomAccessFile(this.getFileName(), "r");
        byte[] aBytes;
        int i, j;
        float lon, lat, alt;
        float minX, maxX, minY, maxY;
        minX = 0;
        maxX = 0;
        minY = 0;
        maxY = 0;

        br.seek(pos);
        br.skipBytes(28);
        for (i = 0; i < particleNum; i++) {
            br.skipBytes(8);
            for (j = 0; j < pollutantNum; j++) {
                br.skipBytes(4);
            }
            br.skipBytes(8);
            lat = br.readFloat();
            lon = br.readFloat();
            alt = br.readFloat();

            discreteData[i][0] = lon;
            discreteData[i][1] = lat;
            discreteData[i][2] = alt;
            stations.add("P" + String.valueOf(i + 1));

            br.skipBytes(40);

            if (i == 0) {
                minX = lon;
                maxX = minX;
                minY = lat;
                maxY = minY;
            } else {
                if (minX > lon) {
                    minX = lon;
                } else if (maxX < lon) {
                    maxX = lon;
                }
                if (minY > lat) {
                    minY = lat;
                } else if (maxY < lat) {
                    maxY = lat;
                }
            }
        }
        Extent dataExtent = new Extent();
        dataExtent.minX = minX;
        dataExtent.maxX = maxX;
        dataExtent.minY = minY;
        dataExtent.maxY = maxY;

        br.close();

        stationData.data = discreteData;
        stationData.dataExtent = dataExtent;
        stationData.stations = stations;

        return stationData;
    } catch (IOException ex) {
        Logger.getLogger(HYSPLITPartDataInfo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}