Search in sources :

Example 6 with Array

use of org.openmuc.openiec61850.Array in project vcell by virtualcell.

the class NetCDFEvaluator method getTimeSeriesData.

/**
 * Get time series data based on a specific trial number.
 * The state variable has 3 dimensions. The time series data removes the first dimension
 * containing only time and species in a specific trial.
 */
public Array getTimeSeriesData(int trialNo) throws IOException, InvalidRangeException {
    if ((ncreader != null) && (ncreader.getState() != null) && (trialNo > 0) && (trialNo <= ncreader.getNumTrials())) {
        String readIdx = (trialNo - 1) + ":" + (trialNo - 1) + ":" + "1," + "0:" + (ncreader.getNumTimePoints() - 1) + ":1," + "0:" + (ncreader.getNumSpecies() - 1) + ":1";
        System.out.println(readIdx);
        try {
            Array result = ncreader.getState().read(readIdx).reduce();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.err);
            throw new IOException("Unable to read variable " + ncreader.getState().getName() + "!");
        }
    }
    return null;
}
Also used : Array(ucar.ma2.Array) IOException(java.io.IOException)

Example 7 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class GRIB1ImageReader method read.

/**
 * @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam)
 */
@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    BufferedImage image = null;
    Variable variable = null;
    Range indexRange = null;
    GribVariableWrapper wrapper = null;
    for (Range range : indexMap.keySet()) {
        if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
            wrapper = indexMap.get(range);
            indexRange = range;
            break;
        }
    }
    variable = wrapper.getVariable();
    /*
         * Fetches the parameters that are not already processed by utility
         * methods like 'getDestination' or 'computeRegions' (invoked below).
         */
    final int strideX, strideY;
    final int[] srcBands, dstBands;
    if (param != null) {
        strideX = param.getSourceXSubsampling();
        strideY = param.getSourceYSubsampling();
        srcBands = param.getSourceBands();
        dstBands = param.getDestinationBands();
    } else {
        strideX = 1;
        strideY = 1;
        srcBands = null;
        dstBands = null;
    }
    final int rank = wrapper.getRank();
    final int bandDimension = rank - NetCDFUtilities.Z_DIMENSION;
    /*
         * Gets the destination image of appropriate size. We create it now
         * since it is a convenient way to get the number of destination bands.
         */
    final int width = wrapper.getWidth();
    final int height = wrapper.getHeight();
    /*
         * Computes the source region (in the NetCDF file) and the destination
         * region (in the buffered image). Copies those informations into UCAR
         * Range structure.
         */
    final Rectangle srcRegion = new Rectangle();
    final Rectangle destRegion = new Rectangle();
    computeRegions(param, width, height, null, srcRegion, destRegion);
    // flipVertically(param, height, srcRegion);
    int destWidth = destRegion.x + destRegion.width;
    int destHeight = destRegion.y + destRegion.height;
    final List<Range> ranges = new LinkedList<Range>();
    for (int i = 0; i < rank; i++) {
        final int first, length, stride;
        switch(rank - i) {
            case NetCDFUtilities.X_DIMENSION:
                {
                    first = srcRegion.x;
                    length = srcRegion.width;
                    stride = strideX;
                    break;
                }
            case NetCDFUtilities.Y_DIMENSION:
                {
                    first = srcRegion.y;
                    length = srcRegion.height;
                    stride = strideY;
                    break;
                }
            default:
                {
                    if (i == bandDimension) {
                        first = NetCDFUtilities.getZIndex(variable, indexRange, imageIndex);
                    } else {
                        first = NetCDFUtilities.getTIndex(variable, indexRange, imageIndex);
                    }
                    length = 1;
                    stride = 1;
                    break;
                }
        }
        try {
            ranges.add(new Range(first, first + length - 1, stride));
        } catch (InvalidRangeException e) {
        }
    }
    final Section sections = new Section(ranges);
    /*
         * Setting SampleModel and ColorModel.
         */
    SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
    ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
    /*
         * Reads the requested sub-region only.
         */
    final int numDstBands = 1;
    final int size = destHeight * destWidth * numDstBands;
    for (int zi = 0; zi < numDstBands; zi++) {
        final int dstBand = (dstBands == null) ? zi : dstBands[zi];
        final Array array;
        try {
            array = variable.read(sections);
            DataBuffer dataBuffer = null;
            if (array instanceof ArrayByte) {
                dataBuffer = new DataBufferByte((byte[]) array.get1DJavaArray(byte.class), size);
            } else if (array instanceof ArrayShort) {
                dataBuffer = new DataBufferShort((short[]) array.get1DJavaArray(short.class), size);
            } else if (array instanceof ArrayInt) {
                dataBuffer = new DataBufferInt((int[]) array.get1DJavaArray(int.class), size);
            } else if (array instanceof ArrayFloat) {
                dataBuffer = new DataBufferFloat((float[]) array.get1DJavaArray(float.class), size);
            } else if (array instanceof ArrayDouble) {
                dataBuffer = new DataBufferDouble((double[]) array.get1DJavaArray(double.class), size);
            }
            WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
        } catch (InvalidRangeException e) {
        }
    }
    return image;
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) Variable(ucar.nc2.Variable) Rectangle(java.awt.Rectangle) ArrayFloat(ucar.ma2.ArrayFloat) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBufferShort(java.awt.image.DataBufferShort) ArrayDouble(ucar.ma2.ArrayDouble) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ArrayByte(ucar.ma2.ArrayByte) ArrayShort(ucar.ma2.ArrayShort) DataBuffer(java.awt.image.DataBuffer) InvalidRangeException(ucar.ma2.InvalidRangeException) Point(java.awt.Point) Range(ucar.ma2.Range) Section(ucar.ma2.Section) Point(java.awt.Point) LinkedList(java.util.LinkedList) Array(ucar.ma2.Array) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) ArrayInt(ucar.ma2.ArrayInt) DataBufferFloat(java.awt.image.DataBufferFloat)

Example 8 with Array

use of org.openmuc.openiec61850.Array 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)

Example 9 with Array

use of org.openmuc.openiec61850.Array 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 10 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class InterpolateVNetCDF method run.

private void run() {
    try {
        NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
        ncFileIn.writeCDL(System.out, true);
        NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(fileNameOut);
        NetcdfFileWriteable ncFileOutV = NetcdfFileWriteable.createNew(fileNameOutV);
        File iniFile = new File("D:/tmp/netcdf/pi_ini.in");
        // //
        // 
        // Determining Initialization Time
        // 
        // //
        float julianDay = Float.NaN;
        if (iniFile.exists()) {
            FileImageInputStream fiis = new FileImageInputStream(iniFile);
            boolean goOn = true;
            while (goOn) {
                try {
                    String line = fiis.readLine();
                    if (line.contains("TSTART")) {
                        final int startIndex = line.indexOf("TSTART");
                        String time = line.substring(0, startIndex).trim();
                        julianDay = Float.parseFloat(time);
                        julianDay += 2440000;
                        break;
                    }
                } catch (IOException ioe) {
                    goOn = false;
                }
            }
        }
        GregorianCalendar calendar;
        if (!Float.isNaN(julianDay)) {
            calendar = NetCDFConverterUtilities.fromJulian(julianDay);
        } else
            calendar = new GregorianCalendar();
        // input dimensions
        Dimension timeDim0 = ncFileIn.findDimension("time");
        Dimension tLatDim0 = ncFileIn.findDimension("tlat");
        Dimension tLonDim0 = ncFileIn.findDimension("tlon");
        Dimension vLatDim0 = ncFileIn.findDimension("vlat");
        Dimension vLonDim0 = ncFileIn.findDimension("vlon");
        // input variablesT
        Variable time_0 = ncFileIn.findVariable("time");
        Array time_0_Data = time_0.read();
        Index time_0_Index = time_0_Data.getIndex();
        Variable tGrid3 = ncFileIn.findVariable("tgrid3");
        Variable vGrid3 = ncFileIn.findVariable("vgrid3");
        // //
        // 
        // Handling tracers-related objects
        // 
        // //
        final int nTLat = tGrid3.getDimension(0).getLength();
        final int nTLon = tGrid3.getDimension(1).getLength();
        final int nTLevels = tGrid3.getDimension(2).getLength();
        final String tUnits = ((Attribute) tGrid3.findAttribute(UNITS)).getStringValue();
        final String[] tUnit = tUnits.split(",");
        // //
        // 
        // Getting tracers grid lat/lon/depth
        // 
        // //
        Array tLatOriginalData = tGrid3.read("0:" + (nTLat - 1) + ":1, 0:0:1, 0:0:1, 1:1:1").reduce();
        Index tLatOriginalIndex = tLatOriginalData.getIndex();
        Array tLonOriginalData = tGrid3.read("0:0:1, 0:" + (nTLon - 1) + ":1, 0:0:1, 0:0:1").reduce();
        Index tLonOriginalIndex = tLonOriginalData.getIndex();
        Array tLevOriginalData = tGrid3.read("0:" + (nTLat - 1) + ":1, 0:" + (nTLon - 1) + ":1, 0:" + (nTLevels - 1) + ":1, 2:2:1").reduce();
        Index tLevOriginalIndex = tLevOriginalData.getIndex();
        // //
        // 
        // Handling velocity-related objects
        // 
        // //
        final int nVLat = vGrid3.getDimension(0).getLength();
        final int nVLon = vGrid3.getDimension(1).getLength();
        final int nVLevels = vGrid3.getDimension(2).getLength();
        final String vUnits = ((Attribute) vGrid3.findAttribute(UNITS)).getStringValue();
        final String[] vUnit = tUnits.split(",");
        // //
        // 
        // Getting velocity grid lat/lon/depth
        // 
        // //
        Array vLatOriginalData = vGrid3.read("0:" + (nVLat - 1) + ":1, 0:0:1, 0:0:1, 1:1:1").reduce();
        Index vLatOriginalIndex = vLatOriginalData.getIndex();
        Array vLonOriginalData = vGrid3.read("0:0:1, 0:" + (nVLon - 1) + ":1, 0:0:1, 0:0:1").reduce();
        Index vLonOriginalIndex = vLonOriginalData.getIndex();
        Array vLevOriginalData = vGrid3.read("0:" + (nVLat - 1) + ":1, 0:" + (nVLon - 1) + ":1, 0:" + (nVLevels - 1) + ":1, 2:2:1").reduce();
        Index vLevOriginalIndex = vLevOriginalData.getIndex();
        // //
        // 
        // Adding dimensions to the first dataset
        // 
        // //
        Dimension timeDimT = ncFileOut.addDimension("time", timeDim0.getLength());
        Dimension latDimT = ncFileOut.addDimension(NetCDFUtilities.LAT, tLatDim0.getLength());
        Dimension lonDimT = ncFileOut.addDimension(NetCDFUtilities.LON, tLonDim0.getLength());
        Dimension depthDimT = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nTLevels);
        // //
        // 
        // Adding dimensions to the second dataset
        // 
        // //
        Dimension timeDimV = ncFileOutV.addDimension("time", timeDim0.getLength());
        Dimension latDimV = ncFileOutV.addDimension(NetCDFUtilities.LAT, vLatDim0.getLength());
        Dimension lonDimV = ncFileOutV.addDimension(NetCDFUtilities.LON, vLonDim0.getLength());
        Dimension depthDimV = ncFileOutV.addDimension(NetCDFUtilities.DEPTH, nVLevels);
        // //
        // 
        // Computing TGrid extremes
        // 
        // //
        computeMatrixExtremes(tLatOriginalData, tLonOriginalData, tLevOriginalData, tLonDim0.getLength(), tLatDim0.getLength(), depthDimT.getLength(), tLatOriginalIndex, tLonOriginalIndex, tLevOriginalIndex, T);
        // //
        // 
        // Computing VGrid extremes
        // 
        // //
        computeMatrixExtremes(vLatOriginalData, vLonOriginalData, vLevOriginalData, vLonDim0.getLength(), vLatDim0.getLength(), depthDimV.getLength(), vLatOriginalIndex, vLonOriginalIndex, vLevOriginalIndex, V);
        // //
        // 
        // Setting global attributes
        // 
        // //
        List globalAttributes = ncFileIn.getGlobalAttributes();
        copyGlobalAttributes(ncFileOut, globalAttributes);
        copyGlobalAttributes(ncFileOutV, globalAttributes);
        // //
        // 
        // Adding coord vars to the output files
        // 
        // //
        // time variable
        ncFileOut.addVariable("time", DataType.FLOAT, new Dimension[] { timeDimT });
        ncFileOutV.addVariable("time", DataType.FLOAT, new Dimension[] { timeDimV });
        setTimeVariableAttributes(time_0, ncFileOut, calendar);
        setTimeVariableAttributes(time_0, ncFileOutV, calendar);
        // ////////////////////////////////////////////////////////////////
        // 
        // Adding coord vars to the first dataset
        // 
        // ////////////////////////////////////////////////////////////////
        // lat Variable
        ArrayFloat tLatData = new ArrayFloat(new int[] { tLatDim0.getLength() });
        Index tLatIndex = tLatData.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDimT });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, "long_name", LATITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, UNITS, tUnit[1].trim());
        for (int yPos = 0; yPos < tLatDim0.getLength(); yPos++) {
            tLatData.setFloat(tLatIndex.set(yPos), new Float(this.ymaxTV[T] - (new Float(yPos).floatValue() * this.yperiodTV[T])).floatValue());
        }
        // lon Variable
        ArrayFloat tLonData = new ArrayFloat(new int[] { tLonDim0.getLength() });
        Index tLonIndex = tLonData.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDimT });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, "long_name", LONGITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, UNITS, tUnit[0].trim());
        for (int xPos = 0; xPos < tLonDim0.getLength(); xPos++) {
            tLonData.setFloat(tLonIndex.set(xPos), new Float(this.xminTV[T] + (new Float(xPos).floatValue() * this.xperiodTV[T])).floatValue());
        }
        // depth level Variable
        ArrayFloat tDepthData = new ArrayFloat(new int[] { depthDimT.getLength() });
        Index tDepthIndex = tDepthData.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDimT });
        ncFileOut.addVariableAttribute(DEPTH, "long_name", DEPTH);
        ncFileOut.addVariableAttribute(DEPTH, UNITS, tUnit[2].trim());
        for (int zPos = 0; zPos < depthDimT.getLength(); zPos++) {
            tDepthData.setFloat(tDepthIndex.set(zPos), new Float(this.zmaxTV[T] - (new Float(zPos).floatValue() * this.zperiodTV[T])).floatValue());
        }
        // ////////////////////////////////////////////////////////////////
        // 
        // Adding coord vars to the second dataset
        // 
        // ////////////////////////////////////////////////////////////////
        // lat Variable
        ArrayFloat vLatData = new ArrayFloat(new int[] { tLatDim0.getLength() });
        Index vLatIndex = vLatData.getIndex();
        ncFileOutV.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDimV });
        ncFileOutV.addVariableAttribute(NetCDFUtilities.LAT, "long_name", LATITUDE);
        ncFileOutV.addVariableAttribute(NetCDFUtilities.LAT, UNITS, vUnit[1].trim());
        for (int yPos = 0; yPos < vLatDim0.getLength(); yPos++) {
            vLatData.setFloat(vLatIndex.set(yPos), new Float(this.ymaxTV[V] - (new Float(yPos).floatValue() * this.yperiodTV[V])).floatValue());
        }
        // lon Variable
        ArrayFloat vLonData = new ArrayFloat(new int[] { tLonDim0.getLength() });
        Index vLonIndex = vLonData.getIndex();
        ncFileOutV.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDimV });
        ncFileOutV.addVariableAttribute(NetCDFUtilities.LON, "long_name", LONGITUDE);
        ncFileOutV.addVariableAttribute(NetCDFUtilities.LON, UNITS, vUnit[0].trim());
        for (int xPos = 0; xPos < vLonDim0.getLength(); xPos++) {
            vLonData.setFloat(vLonIndex.set(xPos), new Float(this.xminTV[V] + (new Float(xPos).floatValue() * this.xperiodTV[V])).floatValue());
        }
        // depth level Variable
        ArrayFloat vDepthData = new ArrayFloat(new int[] { depthDimT.getLength() });
        Index vDepthIndex = vDepthData.getIndex();
        ncFileOutV.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDimV });
        ncFileOutV.addVariableAttribute(DEPTH, "long_name", DEPTH);
        ncFileOutV.addVariableAttribute(DEPTH, UNITS, vUnit[2].trim());
        for (int zPos = 0; zPos < depthDimV.getLength(); zPos++) {
            vDepthData.setFloat(vDepthIndex.set(zPos), new Float(this.zmaxTV[V] - (new Float(zPos).floatValue() * this.zperiodTV[V])).floatValue());
        }
        // Tracers related Variables
        for (int i = 0; i < NUMVARS_T; i++) {
            String varName = variablesT.get(i);
            Variable var = ncFileIn.findVariable(varName);
            ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDimT, depthDimT, latDimT, lonDimT });
            setVariableAttributes(var, ncFileOut, new String[] { "positions" });
        }
        // Velocity related Variables
        for (int i = 0; i < NUMVARS_V; i++) {
            String varName = variablesV.get(i);
            Variable var = ncFileIn.findVariable(varName);
            ncFileOutV.addVariable(varName + "0", var.getDataType(), new Dimension[] { timeDimV, depthDimV, latDimV, lonDimV });
            ncFileOutV.addVariable(varName + "1", var.getDataType(), new Dimension[] { timeDimV, depthDimV, latDimV, lonDimV });
            setVariableAttributes(var, ncFileOutV, new String[] { "positions" }, 0);
            setVariableAttributes(var, ncFileOutV, new String[] { "positions" }, 1);
        }
        // writing bin data ...
        ncFileOut.create();
        ncFileOutV.create();
        ArrayFloat timeData = new ArrayFloat(new int[] { timeDimT.getLength() });
        Index timeIndex = timeData.getIndex();
        for (int t = 0; t < timeDimT.getLength(); t++) {
            float seconds = time_0_Data.getFloat(time_0_Index.set(t));
            timeData.setFloat(timeIndex.set(t), seconds);
        }
        // //
        // 
        // Writing coordVars for the first dataset
        // 
        // //
        ncFileOut.write("time", timeData);
        Variable timeVar = ncFileOut.findVariable("time");
        timeDimT.addCoordinateVariable(timeVar);
        Variable depthVarT = ncFileOut.findVariable("depth");
        depthDimT.addCoordinateVariable(depthVarT);
        ncFileOut.write(NetCDFUtilities.DEPTH, vDepthData);
        ncFileOut.write(NetCDFUtilities.LAT, tLatData);
        ncFileOut.write(NetCDFUtilities.LON, tLonData);
        // //
        // 
        // Writing coordVars for the second dataset
        // 
        // //
        ncFileOutV.write("time", timeData);
        Variable timeVarV = ncFileOutV.findVariable("time");
        timeDimV.addCoordinateVariable(timeVarV);
        Variable depthVarV = ncFileOut.findVariable("depth");
        depthDimV.addCoordinateVariable(depthVarV);
        ncFileOutV.write(NetCDFUtilities.DEPTH, vDepthData);
        ncFileOutV.write(NetCDFUtilities.LAT, vLatData);
        ncFileOutV.write(NetCDFUtilities.LON, vLonData);
        // TODO: AutoApply MASK?
        ArrayFloat maskMatrixT = new ArrayFloat.D2(latDimT.getLength(), lonDimT.getLength());
        Index maskImaT = maskMatrixT.getIndex();
        if (APPLY_MASK) {
            Variable mask = ncFileIn.findVariable("landt");
            Array maskData = mask.read();
            Index maskIndex = maskData.getIndex();
            ArrayFloat tempData = new ArrayFloat(new int[] { tLatDim0.getLength(), tLonDim0.getLength() });
            Index tempIndex = tempData.getIndex();
            for (int yPos = 0; yPos < tLatDim0.getLength(); yPos++) {
                for (int xPos = 0; xPos < tLonDim0.getLength(); xPos++) {
                    tempData.setFloat(tempIndex.set(yPos, xPos), maskData.getFloat(maskIndex.set(yPos, xPos)));
                }
            }
            WritableRaster outData = this.Resampler(tLatOriginalData, tLonOriginalData, tLonDim0.getLength(), tLatDim0.getLength(), 2, tempData, -1, T);
            for (int j = 0; j < tLatDim0.getLength(); j++) {
                for (int k = 0; k < tLonDim0.getLength(); k++) {
                    float sample = outData.getSampleFloat(k, j, 0);
                    maskMatrixT.setFloat(maskImaT.set(j, k), sample);
                }
            }
        }
        ArrayFloat maskMatrixV = new ArrayFloat.D2(latDimT.getLength(), lonDimT.getLength());
        Index maskImaV = maskMatrixV.getIndex();
        if (APPLY_MASK) {
            Variable mask = ncFileIn.findVariable("landv");
            Array maskData = mask.read();
            Index maskIndex = maskData.getIndex();
            ArrayFloat tempData = new ArrayFloat(new int[] { vLatDim0.getLength(), vLonDim0.getLength() });
            Index tempIndex = tempData.getIndex();
            for (int yPos = 0; yPos < vLatDim0.getLength(); yPos++) {
                for (int xPos = 0; xPos < vLonDim0.getLength(); xPos++) {
                    tempData.setFloat(tempIndex.set(yPos, xPos), maskData.getFloat(maskIndex.set(yPos, xPos)));
                }
            }
            WritableRaster outData = this.Resampler(vLatOriginalData, vLonOriginalData, vLonDim0.getLength(), vLatDim0.getLength(), 2, tempData, -1, V);
            for (int j = 0; j < vLatDim0.getLength(); j++) {
                for (int k = 0; k < vLonDim0.getLength(); k++) {
                    float sample = outData.getSampleFloat(k, j, 0);
                    maskMatrixV.setFloat(maskImaV.set(j, k), sample);
                }
            }
        }
        // //
        // 
        // Writing Tracers related Variables
        // 
        // //
        System.out.print("T Process complete...0.0%");
        for (int i = 0; i < NUMVARS_T; i++) {
            String varName = variablesT.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 tempData = new ArrayFloat(new int[] { tLatDim0.getLength(), tLonDim0.getLength() });
            Index tempIndex = tempData.getIndex();
            ArrayFloat tMatrix = new ArrayFloat.D4(timeDimT.getLength(), depthDimT.getLength(), latDimT.getLength(), lonDimT.getLength());
            Index tIma = tMatrix.getIndex();
            for (int tPos = 0; tPos < timeDim0.getLength(); tPos++) {
                for (int levelPos = 0; levelPos < depthDimT.getLength(); levelPos++) {
                    for (int yPos = 0; yPos < tLatDim0.getLength(); yPos++) {
                        for (int xPos = 0; xPos < tLonDim0.getLength(); xPos++) {
                            // Vertical Interpolation (nearest neighbor)
                            final double targetZ = new Double(this.zmaxTV[T] - (new Double(levelPos).doubleValue() * this.zperiodTV[T])).doubleValue();
                            int targetOutlev = 0;
                            double distance = Double.POSITIVE_INFINITY;
                            for (int ol = 0; ol < depthDimT.getLength(); ol++) {
                                final double designatedZ = tLevOriginalData.getDouble(tLevOriginalIndex.set(yPos, xPos, ol));
                                if (Math.abs(designatedZ - targetZ) < distance) {
                                    distance = Math.abs(designatedZ - targetZ);
                                    targetOutlev = ol;
                                }
                            }
                            tempData.setFloat(tempIndex.set(yPos, xPos), originalVarData.getFloat(varIndex.set(tPos, yPos, xPos, targetOutlev)));
                        }
                    }
                    WritableRaster outData = this.Resampler(tLatOriginalData, tLonOriginalData, tLonDim0.getLength(), tLatDim0.getLength(), 2, tempData, fillValue, T);
                    for (int j = 0; j < tLatDim0.getLength(); j++) {
                        for (int k = 0; k < tLonDim0.getLength(); k++) {
                            float sample = outData.getSampleFloat(k, j, 0);
                            if (APPLY_MASK) {
                                float maskValue = maskMatrixV.getFloat(maskImaV.set(j, k));
                                if (maskValue == 0)
                                    sample = fillValue;
                            }
                            tMatrix.setFloat(tIma.set(tPos, levelPos, j, k), sample);
                        }
                    }
                }
            }
            ncFileOut.write(varName, tMatrix);
            System.out.print("..." + new Double((new Double(i + 1).doubleValue() / new Double(NUMVARS_T).doubleValue()) * 100.0).floatValue() + "%");
        }
        // //
        // 
        // Writing Velocity related Variables
        // 
        // //
        System.out.print("V Process complete...0.0%");
        for (int i = 0; i < NUMVARS_V; i++) {
            String varName = variablesV.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 tempData0 = new ArrayFloat(new int[] { vLatDim0.getLength(), vLonDim0.getLength() });
            Index tempIndex0 = tempData0.getIndex();
            ArrayFloat tempData1 = new ArrayFloat(new int[] { vLatDim0.getLength(), vLonDim0.getLength() });
            Index tempIndex1 = tempData1.getIndex();
            ArrayFloat vMatrix0 = new ArrayFloat.D4(timeDimV.getLength(), depthDimV.getLength(), latDimV.getLength(), lonDimV.getLength());
            Index vIma0 = vMatrix0.getIndex();
            ArrayFloat vMatrix1 = new ArrayFloat.D4(timeDimV.getLength(), depthDimV.getLength(), latDimV.getLength(), lonDimV.getLength());
            Index vIma1 = vMatrix1.getIndex();
            for (int tPos = 0; tPos < timeDim0.getLength(); tPos++) {
                for (int levelPos = 0; levelPos < depthDimV.getLength(); levelPos++) {
                    for (int yPos = 0; yPos < vLatDim0.getLength(); yPos++) {
                        for (int xPos = 0; xPos < vLonDim0.getLength(); xPos++) {
                            // Vertical Interpolation (nearest neighbor)
                            final double targetZ = new Double(this.zmaxTV[V] - (new Double(levelPos).doubleValue() * this.zperiodTV[V])).doubleValue();
                            int targetOutlev = 0;
                            double distance = Double.POSITIVE_INFINITY;
                            for (int ol = 0; ol < depthDimV.getLength(); ol++) {
                                final double designatedZ = vLevOriginalData.getDouble(vLevOriginalIndex.set(yPos, xPos, ol));
                                if (Math.abs(designatedZ - targetZ) < distance) {
                                    distance = Math.abs(designatedZ - targetZ);
                                    targetOutlev = ol;
                                }
                            }
                            tempData0.setFloat(tempIndex0.set(yPos, xPos), originalVarData.getFloat(varIndex.set(tPos, yPos, xPos, targetOutlev, 0)));
                            tempData1.setFloat(tempIndex1.set(yPos, xPos), originalVarData.getFloat(varIndex.set(tPos, yPos, xPos, targetOutlev, 1)));
                        }
                    }
                    WritableRaster outData0 = this.Resampler(vLatOriginalData, vLonOriginalData, vLonDim0.getLength(), vLatDim0.getLength(), 2, tempData0, fillValue, V);
                    for (int j = 0; j < vLatDim0.getLength(); j++) {
                        for (int k = 0; k < vLonDim0.getLength(); k++) {
                            float sample = outData0.getSampleFloat(k, j, 0);
                            if (APPLY_MASK) {
                                float maskValue = maskMatrixV.getFloat(maskImaV.set(j, k));
                                if (maskValue == 0)
                                    sample = fillValue;
                            }
                            vMatrix0.setFloat(vIma0.set(tPos, levelPos, j, k), sample);
                        }
                    }
                    WritableRaster outData1 = this.Resampler(vLatOriginalData, vLonOriginalData, vLonDim0.getLength(), vLatDim0.getLength(), 2, tempData1, fillValue, V);
                    for (int j = 0; j < vLatDim0.getLength(); j++) {
                        for (int k = 0; k < vLonDim0.getLength(); k++) {
                            float sample = outData1.getSampleFloat(k, j, 0);
                            if (APPLY_MASK) {
                                float maskValue = maskMatrixV.getFloat(maskImaV.set(j, k));
                                if (maskValue == 0)
                                    sample = fillValue;
                            }
                            vMatrix1.setFloat(vIma1.set(tPos, levelPos, j, k), sample);
                        }
                    }
                }
            }
            ncFileOutV.write(varName + "0", vMatrix0);
            ncFileOutV.write(varName + "1", vMatrix1);
            System.out.print("..." + new Double((new Double(i + 1).doubleValue() / new Double(NUMVARS_V).doubleValue()) * 100.0).floatValue() + "%");
        }
        ncFileOut.close();
        ncFileOutV.close();
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) WritableRaster(java.awt.image.WritableRaster) ArrayList(java.util.ArrayList) List(java.util.List) GregorianCalendar(java.util.GregorianCalendar) IOException(java.io.IOException) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) FileImageInputStream(javax.imageio.stream.FileImageInputStream) Array(ucar.ma2.Array) ArrayFloat(ucar.ma2.ArrayFloat) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile)

Aggregations

Array (ucar.ma2.Array)53 IOException (java.io.IOException)34 Variable (ucar.nc2.Variable)29 InvalidRangeException (ucar.ma2.InvalidRangeException)18 Attribute (ucar.nc2.Attribute)18 Index (ucar.ma2.Index)15 File (java.io.File)12 ArrayList (java.util.ArrayList)12 Dimension (ucar.nc2.Dimension)12 NetcdfFile (ucar.nc2.NetcdfFile)12 ArrayFloat (ucar.ma2.ArrayFloat)11 WritableRaster (java.awt.image.WritableRaster)8 ProcedureTree (org.geotoolkit.observation.model.ExtractionResult.ProcedureTree)8 GeoSpatialBound (org.geotoolkit.observation.model.GeoSpatialBound)8 Process (org.geotoolkit.observation.xml.Process)8 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)8 DataType (ucar.ma2.DataType)7 ArrayDouble (ucar.ma2.ArrayDouble)5 ArrayInt (ucar.ma2.ArrayInt)5 ArrayShort (ucar.ma2.ArrayShort)5