Search in sources :

Example 11 with Index

use of com.google.firestore.admin.v1beta1.Index in project s1tbx by senbox-org.

the class NetCDFWriter method writeBandRasterData.

/**
 * {@inheritDoc}
 */
public void writeBandRasterData(final Band sourceBand, final int regionX, final int regionY, final int regionWidth, final int regionHeight, final ProductData regionData, ProgressMonitor pm) throws IOException {
    final int[] origin = new int[2];
    origin[1] = regionX;
    origin[0] = regionY;
    try {
        final ArrayDouble dataTemp = new ArrayDouble.D2(regionHeight, regionWidth);
        final Index index = dataTemp.getIndex();
        int i = 0;
        for (int y = 0; y < regionHeight; ++y) {
            for (int x = 0; x < regionWidth; ++x) {
                index.set(y, x);
                dataTemp.set(index, regionData.getElemDoubleAt(i));
                ++i;
            }
        }
        netCDFWriteable.write(sourceBand.getName(), origin, dataTemp);
        pm.worked(1);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
Also used : ArrayDouble(ucar.ma2.ArrayDouble) Index(ucar.ma2.Index) IOException(java.io.IOException) InvalidRangeException(ucar.ma2.InvalidRangeException)

Example 12 with Index

use of com.google.firestore.admin.v1beta1.Index in project ncWMS by Unidata.

the class CdmUtils method readTimeseries.

public static List<Float> readTimeseries(NetcdfDataset nc, GridDatatype grid, HorizontalGrid horizGrid, List<Integer> tIndices, int zIndex, HorizontalPosition xy) throws IOException {
    GridCoordinates gridCoords = horizGrid.findNearestGridPoint(xy);
    if (gridCoords == null) {
        // a list of nulls
        return nullList(tIndices.size());
    }
    int i = gridCoords.getCoordinateValue(0);
    int j = gridCoords.getCoordinateValue(1);
    int firstTIndex = tIndices.get(0);
    int lastTIndex = tIndices.get(tIndices.size() - 1);
    RangesList rangesList = new RangesList(grid);
    rangesList.setTRange(firstTIndex, lastTIndex);
    rangesList.setZRange(zIndex, zIndex);
    rangesList.setYRange(j, j);
    rangesList.setXRange(i, i);
    // We read data for the whole time range.  This may mean grabbing
    // data we don't need.
    // TODO: use a datareadingstrategy here to read point-by-point for
    // local files?
    DataChunk dataChunk = DataChunk.readDataChunk(grid.getVariable(), rangesList);
    // Copy the data to the required array, discarding the points we
    // don't need
    List<Float> tsData = new ArrayList<Float>(tIndices.size());
    Index index = dataChunk.getIndex();
    index.set(new int[index.getRank()]);
    for (int tIndex : tIndices) {
        int tIndexOffset = tIndex - firstTIndex;
        // This will happen if the layer has no t axis
        if (tIndexOffset < 0)
            tIndexOffset = 0;
        index.setDim(rangesList.getTAxisIndex(), tIndexOffset);
        // Read the data from the chunk, applying enhancement if necessary
        float val = dataChunk.readFloatValue(index);
        // Replace missing values with nulls
        tsData.add(Float.isNaN(val) ? null : val);
    }
    return tsData;
}
Also used : GridCoordinates(org.opengis.coverage.grid.GridCoordinates) ArrayList(java.util.ArrayList) Index(ucar.ma2.Index) LatLonPoint(ucar.unidata.geoloc.LatLonPoint)

Example 13 with Index

use of com.google.firestore.admin.v1beta1.Index in project mzmine2 by mzmine.

the class NetCDFReadTask method readNextScan.

/**
 * Reads one scan from the file. Requires that general information has already been read.
 */
private Scan readNextScan() throws IOException {
    // Get scan starting position and length
    int[] scanStartPosition = new int[1];
    int[] scanLength = new int[1];
    Integer[] startAndLength = scansIndex.get(scanNum);
    // End of file
    if (startAndLength == null) {
        return null;
    }
    scanStartPosition[0] = startAndLength[0];
    scanLength[0] = startAndLength[1];
    // Get retention time of the scan
    Double retentionTime = scansRetentionTimes.get(scanNum);
    if (retentionTime == null) {
        logger.severe("Could not find retention time for scan " + scanNum);
        throw (new IOException("Could not find retention time for scan " + scanNum));
    }
    // An empty scan needs special attention..
    if (scanLength[0] == 0) {
        scanNum++;
        return new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0, 0, null, new DataPoint[0], MassSpectrumType.CENTROIDED, PolarityType.UNKNOWN, "", null);
    }
    // Is there any way how to extract polarity from netcdf?
    PolarityType polarity = PolarityType.UNKNOWN;
    // Is there any way how to extract scan definition from netcdf?
    String scanDefinition = "";
    // Read mass and intensity values
    Array massValueArray;
    Array intensityValueArray;
    try {
        massValueArray = massValueVariable.read(scanStartPosition, scanLength);
        intensityValueArray = intensityValueVariable.read(scanStartPosition, scanLength);
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Could not read from variables mass_values and/or intensity_values.", e);
        throw (new IOException("Could not read from variables mass_values and/or intensity_values."));
    }
    Index massValuesIndex = massValueArray.getIndex();
    Index intensityValuesIndex = intensityValueArray.getIndex();
    int arrayLength = massValueArray.getShape()[0];
    DataPoint[] dataPoints = new DataPoint[arrayLength];
    for (int j = 0; j < arrayLength; j++) {
        Index massIndex0 = massValuesIndex.set0(j);
        Index intensityIndex0 = intensityValuesIndex.set0(j);
        double mz = massValueArray.getDouble(massIndex0) * massValueScaleFactor;
        double intensity = intensityValueArray.getDouble(intensityIndex0) * intensityValueScaleFactor;
        dataPoints[j] = new SimpleDataPoint(mz, intensity);
    }
    scanNum++;
    // Auto-detect whether this scan is centroided
    MassSpectrumType spectrumType = ScanUtils.detectSpectrumType(dataPoints);
    SimpleScan buildingScan = new SimpleScan(null, scanNum, 1, retentionTime.doubleValue(), 0, 0, null, dataPoints, spectrumType, polarity, scanDefinition, null);
    return buildingScan;
}
Also used : PolarityType(net.sf.mzmine.datamodel.PolarityType) Index(ucar.ma2.Index) MassSpectrumType(net.sf.mzmine.datamodel.MassSpectrumType) IOException(java.io.IOException) IOException(java.io.IOException) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) Array(ucar.ma2.Array) SimpleScan(net.sf.mzmine.datamodel.impl.SimpleScan) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Example 14 with Index

use of com.google.firestore.admin.v1beta1.Index in project imageio-ext by geosolutions-it.

the class HOPSConverter method setTimeVariableAttributes.

// ////////////////////////////////////////////////////////////////////////
// 
// HELPERS !!!
// 
// ////////////////////////////////////////////////////////////////////////
private float setTimeVariableAttributes(Variable time_0, NetcdfFileWriteable ncFileOut) throws IOException {
    Array time_0_Data = time_0.read();
    Index time_0_Index = time_0_Data.getIndex();
    final String name = time_0.getName();
    final float referenceTime = time_0_Data.getFloat(time_0_Index.set(0));
    float fTime = referenceTime;
    Attribute offset = time_0.findAttribute("add_offset");
    if (offset != null)
        fTime += offset.getNumericValue().floatValue();
    GregorianCalendar calendar = null;
    if (time_0.getDescription().toLowerCase().contains("modified julian")) {
        calendar = NetCDFConverterUtilities.fromModifiedJulian(fTime, time_0.getDescription(), time_0.getUnitsString());
    } else {
        calendar = NetCDFConverterUtilities.fromJulian(fTime);
    }
    final String year = Integer.toString(calendar.get(Calendar.YEAR));
    final String month = Integer.toString((calendar.get(Calendar.MONTH) + 1));
    final String day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
    String hour = Integer.toString(calendar.get(Calendar.HOUR));
    if (hour.equalsIgnoreCase("0"))
        hour += "0";
    String minute = Integer.toString(calendar.get(Calendar.MINUTE));
    if (minute.equalsIgnoreCase("0"))
        minute += "0";
    String second = Integer.toString(calendar.get(Calendar.SECOND));
    if (second.equalsIgnoreCase("0"))
        second += "0";
    final String millisecond = Integer.toString(calendar.get(Calendar.MILLISECOND));
    final StringBuffer sbTime = new StringBuffer(year).append("-").append(month).append("-").append(day).append(" ").append(hour).append(":").append(minute).append(":").append(second).append(".").append(millisecond);
    ncFileOut.addVariableAttribute(name, "units", "days since " + sbTime.toString());
    ncFileOut.addVariableAttribute(name, "long_name", "time");
    return referenceTime;
}
Also used : Array(ucar.ma2.Array) Attribute(ucar.nc2.Attribute) GregorianCalendar(java.util.GregorianCalendar) Index(ucar.ma2.Index)

Example 15 with Index

use of com.google.firestore.admin.v1beta1.Index in project imageio-ext by geosolutions-it.

the class HOPSConverter method runTest.

public void runTest() throws IOException {
    try {
        final NetcdfFile ncFileIn = NetcdfFile.open(inputFile.getAbsolutePath());
        final File outDir = /*createTodayDirectory(this.outDir)*/
        new File("C:/work/data/testWritten/");
        // keep original name
        final File outputFile = File.createTempFile(inputFile.getName(), ".tmp");
        final NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(outputFile.getAbsolutePath());
        // input dimensions
        Dimension timeDim0 = ncFileIn.findDimension("time");
        Dimension latDim0 = ncFileIn.findDimension("lat");
        Dimension lonDim0 = ncFileIn.findDimension("lon");
        // input variables
        Variable time_0 = ncFileIn.findVariable("time");
        Array time_0_Data = time_0.read();
        Index time_0_Index = time_0_Data.getIndex();
        Variable lon_0 = ncFileIn.findVariable("lon");
        final int nLon = lon_0.getDimension(0).getLength();
        Variable lat_0 = ncFileIn.findVariable("lat");
        final int nLat = lat_0.getDimension(0).getLength();
        Array lat_0_Data = lat_0.read();
        Index lat_0_Index = lat_0_Data.getIndex();
        Array lon_0_Data = lon_0.read();
        Index lon_0_Index = lon_0_Data.getIndex();
        // Depth
        Variable z_0 = ncFileIn.findVariable("z");
        final int nLevels = z_0.getDimension(0).getLength();
        Array z_0_Data = z_0.read();
        Index z_0_Index = z_0_Data.getIndex();
        Dimension timeDim = ncFileOut.addDimension("time", timeDim0.getLength());
        Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, latDim0.getLength());
        Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, lonDim0.getLength());
        Dimension depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nLevels);
        // writing file
        computeMatrixExtremes(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), lat_0_Index, lon_0_Index);
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // //
        // 
        // Time requires a special Management
        // 
        // //
        // time Variable
        ncFileOut.addVariable("time", DataType.FLOAT, new Dimension[] { timeDim });
        // ncFileOut.addVariableAttribute("Time", "long_name", "Time");
        final float referenceTime = setTimeVariableAttributes(time_0, ncFileOut);
        // lat Variable
        ArrayFloat lat_1_Data = new ArrayFloat(new int[] { latDim0.getLength() });
        Index lat_1_Index = lat_1_Data.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, "long_name", NetCDFUtilities.LATITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, UNITS, lat_0.getUnitsString());
        for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
            lat_1_Data.setFloat(lat_1_Index.set(yPos), new Float(this.ymax - (new Float(yPos).floatValue() * this.periodY)).floatValue());
        // new Float(
        // this.ymin
        // + (new Float(yPos)
        // .floatValue() * this.periodY))
        // .floatValue());
        }
        // lon Variable
        ArrayFloat lon_1_Data = new ArrayFloat(new int[] { lonDim0.getLength() });
        Index lon_1_Index = lon_1_Data.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, "long_name", NetCDFUtilities.LONGITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, UNITS, lon_0.getUnitsString());
        for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
            lon_1_Data.setFloat(lon_1_Index.set(xPos), new Float(this.xmin + (new Float(xPos).floatValue() * this.periodX)).floatValue());
        }
        // depth level Variable
        ArrayFloat depthlevelDim_1_Data = new ArrayFloat(new int[] { depthDim.getLength() });
        Index depthlevelDim_1_Index = depthlevelDim_1_Data.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "long_name", NetCDFUtilities.DEPTH);
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, UNITS, z_0.getUnitsString());
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "positive", "up");
        for (int wPos = 0; wPos < depthDim.getLength(); wPos++) {
            depthlevelDim_1_Data.setFloat(depthlevelDim_1_Index.set(wPos), z_0_Data.getFloat(depthlevelDim_1_Index));
        }
        // {} Variables
        for (int i = 0; i < NUMVARS; i++) {
            String varName = (String) variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
            NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "positions" });
        }
        // writing bin data ...
        ncFileOut.create();
        ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
        Index timeIndex = timeData.getIndex();
        for (int t = 0; t < timeDim.getLength(); t++) {
            float julianTime = time_0_Data.getFloat(time_0_Index.set(t)) - referenceTime;
            timeData.setFloat(timeIndex.set(t), julianTime);
        }
        ncFileOut.write("time", timeData);
        Variable timeVar = ncFileOut.findVariable("time");
        timeDim.addCoordinateVariable(timeVar);
        ncFileOut.write(NetCDFUtilities.LAT, lat_1_Data);
        ncFileOut.write(NetCDFUtilities.LON, lon_1_Data);
        Variable depthVar = ncFileOut.findVariable("depth");
        depthDim.addCoordinateVariable(depthVar);
        ncFileOut.write(NetCDFUtilities.DEPTH, depthlevelDim_1_Data);
        // TODO: AutoApply MASK?
        ArrayFloat maskMatrix = new ArrayFloat.D2(latDim.getLength(), lonDim.getLength());
        Index maskIma = maskMatrix.getIndex();
        if (APPLY_MASK) {
            Variable mask = ncFileIn.findVariable("mask");
            Array maskData = mask.read();
            Index maskIndex = maskData.getIndex();
            ArrayFloat tempData = new ArrayFloat(new int[] { latDim0.getLength(), lonDim0.getLength() });
            Index tempIndex = tempData.getIndex();
            for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
                for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
                    tempData.setFloat(tempIndex.set(yPos, xPos), maskData.getFloat(maskIndex.set(yPos, xPos)));
                }
            }
            WritableRaster outData = Resampler(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), 2, tempData, -1);
            for (int j = 0; j < latDim0.getLength(); j++) {
                for (int k = 0; k < lonDim0.getLength(); k++) {
                    float sample = outData.getSampleFloat(k, j, 0);
                    maskMatrix.setFloat(maskIma.set(j, k), sample);
                }
            }
        }
        for (int i = 0; i < NUMVARS; i++) {
            String varName = (String) variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            Array originalVarData = var.read();
            Index varIndex = originalVarData.getIndex();
            Attribute fv = var.findAttribute("_FillValue");
            float fillValue = Float.NaN;
            if (fv != null) {
                fillValue = (fv.getNumericValue()).floatValue();
            }
            ArrayFloat T_tmp_Data = new ArrayFloat(new int[] { latDim0.getLength(), lonDim0.getLength() });
            Index T_tmp_Index = T_tmp_Data.getIndex();
            ArrayFloat Tmatrix = new ArrayFloat.D4(timeDim.getLength(), depthDim.getLength(), latDim.getLength(), lonDim.getLength());
            Index Tima = Tmatrix.getIndex();
            for (int tPos = 0; tPos < timeDim0.getLength(); tPos++) {
                for (int levelPos = 0; levelPos < depthDim.getLength(); levelPos++) {
                    for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
                        for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
                            T_tmp_Data.setFloat(T_tmp_Index.set(yPos, xPos), originalVarData.getFloat(varIndex.set(tPos, yPos, xPos, levelPos)));
                        }
                    }
                    WritableRaster outData = Resampler(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), 2, T_tmp_Data, fillValue);
                    for (int j = 0; j < latDim0.getLength(); j++) {
                        for (int k = 0; k < lonDim0.getLength(); k++) {
                            float sample = outData.getSampleFloat(k, j, 0);
                            if (APPLY_MASK) {
                                float maskValue = maskMatrix.getFloat(maskIma.set(j, k));
                                if (maskValue == 0)
                                    sample = fillValue;
                            }
                            Tmatrix.setFloat(Tima.set(tPos, levelPos, j, k), sample);
                        }
                    }
                }
            }
            ncFileOut.write(varName, Tmatrix);
        }
        ncFileOut.close();
        outputFile.renameTo(new File(outDir + File.separator + inputFile.getName()));
    } catch (Exception e) {
        // something bad happened
        if (LOGGER.isLoggable(Level.INFO))
            LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        JAI.getDefaultInstance().getTileCache().flush();
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) ArrayFloat(ucar.ma2.ArrayFloat) WritableRaster(java.awt.image.WritableRaster) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile)

Aggregations

Index (ucar.ma2.Index)30 Array (ucar.ma2.Array)20 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)12 Attribute (ucar.nc2.Attribute)11 AbstractMessage (com.google.protobuf.AbstractMessage)10 WritableRaster (java.awt.image.WritableRaster)10 IOException (java.io.IOException)10 ArrayFloat (ucar.ma2.ArrayFloat)10 Index (com.google.firestore.admin.v1.Index)9 Dimension (ucar.nc2.Dimension)9 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)8 Variable (ucar.nc2.Variable)8 ArrayDouble (ucar.ma2.ArrayDouble)7 NetcdfFile (ucar.nc2.NetcdfFile)7 Index (com.google.datastore.admin.v1.Index)4 FirestoreAdminBlockingStub (com.google.firestore.admin.v1beta1.FirestoreAdminGrpc.FirestoreAdminBlockingStub)4 Operation (com.google.longrunning.Operation)4 SampleModel (java.awt.image.SampleModel)4 File (java.io.File)4