Search in sources :

Example 36 with Variable

use of org.flyte.api.v1.Variable in project imageio-ext by geosolutions-it.

the class NetCDFCFExperiment method run.

private void run(String fileNameIn, String fileNameOut) {
    try {
        NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
        ncFileIn.writeCDL(System.out, true);
        NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(fileNameOut);
        // 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 grid3 = ncFileIn.findVariable("grid3");
        final int nLat = grid3.getDimension(0).getLength();
        final int nLon = grid3.getDimension(1).getLength();
        final String units = ((Attribute) grid3.findAttribute(UNITS)).getStringValue();
        final String[] unit = units.split(",");
        Array lat_0_Data = grid3.read("0:" + (nLat - 1) + ":1, 0:0:1, 1:1:1").reduce();
        Index lat_0_Index = lat_0_Data.getIndex();
        Array lon_0_Data = grid3.read("0:0:1, 0:" + (nLon - 1) + ":1, 0:0:1").reduce();
        Index lon_0_Index = lon_0_Data.getIndex();
        // Depth
        Variable z_0 = ncFileIn.findVariable("zout");
        final int nLevels = z_0.getDimension(0).getLength();
        Array z_0_Data = z_0.read("0:" + (nLevels - 1) + ":1, 2:2:1");
        // 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
        this.computeMatrixExtremes(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), lat_0_Index, lon_0_Index);
        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, unit[1].trim());
        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, unit[0].trim());
        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
        ArrayInt depthlevelDim_1_Data = new ArrayInt(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, unit[2].trim());
        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 = variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
            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 = this.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 = 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 = this.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();
    } 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) 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) ArrayInt(ucar.ma2.ArrayInt)

Example 37 with Variable

use of org.flyte.api.v1.Variable in project imageio-ext by geosolutions-it.

the class HDF4APSImageReader method initializeProfile.

/**
 * Initialize main properties for this <code>HDF4APSImageReader</code>
 */
protected void initializeProfile() throws IOException {
    final NetcdfDataset dataset = reader.getDataset();
    if (dataset == null) {
        throw new IOException("Unable to initialize profile due to a null dataset");
    }
    final List<Variable> variables = dataset.getVariables();
    final List<Attribute> attributes = dataset.getGlobalAttributes();
    final int numVars = variables.size();
    reader.setNumGlobalAttributes(attributes.size());
    // //
    // 
    // Getting projection dataset name
    // 
    // //
    final String navAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PFA_NA_MAPPROJECTION);
    if (navAttrib != null && navAttrib.length() > 0) {
        projectionDatasetName = navAttrib;
    }
    final String prodAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PRODLIST);
    int numImages = 0;
    if (prodAttrib != null && prodAttrib.length() > 0) {
        String[] products = prodAttrib.split(",");
        productList = HDF4APSProperties.refineProductList(products);
        numImages = productList.length;
    } else {
        numImages = numVars;
    }
    setNumImages(numImages);
    reader.setNumImages(numImages);
    final Map<Range, APSDatasetWrapper> indexMap = new HashMap<Range, APSDatasetWrapper>(numImages);
    Variable varProjection;
    // //
    // 
    // Setting spatial domain
    // 
    // //
    // getting map dataset
    varProjection = dataset.findVariable(projectionDatasetName);
    if (varProjection != null && varProjection.getName().equalsIgnoreCase(projectionDatasetName)) {
        // TODO: All projection share the same dataset
        // structure?
        Array data = varProjection.read();
        final int datatype = NetCDFUtilities.getRawDataType(varProjection);
        if (projectionMap == null) {
            projectionMap = buildProjectionAttributesMap(data, datatype);
        // Force UoM of MapBoundary product as the last element in
        // the map
        }
    }
    try {
        // Scanning all the datasets
        for (Variable var : variables) {
            final String name = var.getName();
            for (int j = 0; j < numImages; j++) {
                // Checking if the actual dataset is a product.
                if (name.equals(productList[j])) {
                    // Updating the subDatasetsMap map
                    indexMap.put(new Range(j, j + 1), new APSDatasetWrapper(var));
                    break;
                }
            }
        }
    } catch (InvalidRangeException e) {
        throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
    }
    reader.setIndexMap(indexMap);
}
Also used : Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InvalidRangeException(ucar.ma2.InvalidRangeException) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset) IOException(java.io.IOException) Range(ucar.ma2.Range) Array(ucar.ma2.Array)

Example 38 with Variable

use of org.flyte.api.v1.Variable in project imageio-ext by geosolutions-it.

the class SWANConverter method run.

private void run(String fileNameIn, String fileNameOut) {
    try {
        final File fileIn = new File(fileNameIn);
        final NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
        final File fileOut = new File(fileNameOut);
        // keep original name
        final File outputFile = File.createTempFile(fileIn.getName(), ".tmp");
        final NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(outputFile.getAbsolutePath());
        boolean hasZeta = false;
        // input dimensions
        final Dimension timeDim0 = ncFileIn.findDimension("time");
        final int nTimes = timeDim0.getLength();
        final Dimension latDim0 = ncFileIn.findDimension(NetCDFUtilities.LATITUDE);
        final int nLat = latDim0.getLength();
        final Dimension lonDim0 = ncFileIn.findDimension(NetCDFUtilities.LONGITUDE);
        final int nLon = lonDim0.getLength();
        // input VARIABLES
        final Variable timeOriginalVar = ncFileIn.findVariable("time");
        final Array timeOriginalData = timeOriginalVar.read();
        final Index timeOriginalIndex = timeOriginalData.getIndex();
        final DataType timeDataType = timeOriginalVar.getDataType();
        final Variable lonOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LONGITUDE);
        final DataType lonDataType = lonOriginalVar.getDataType();
        final Variable latOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LATITUDE);
        final DataType latDataType = latOriginalVar.getDataType();
        final Array latOriginalData = latOriginalVar.read();
        final Array lonOriginalData = lonOriginalVar.read();
        // //
        // 
        // Depth related vars
        // 
        // //
        Array levelOriginalData = null;
        int nZeta = 0;
        Array zeta1Data = null;
        Dimension zDim = null;
        DataType zetaDataType = null;
        // Depth
        final Variable levelOriginalVar = ncFileIn.findVariable("z");
        if (levelOriginalVar != null) {
            nZeta = levelOriginalVar.getDimension(0).getLength();
            levelOriginalData = levelOriginalVar.read();
            zetaDataType = levelOriginalVar.getDataType();
            hasZeta = true;
        }
        Dimension timeDim = ncFileOut.addDimension("time", nTimes);
        Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
        Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
        if (hasZeta)
            zDim = ncFileOut.addDimension(NetCDFUtilities.HEIGHT, nZeta);
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // Dimensions
        Variable timeVar = ncFileOut.addVariable("time", timeDataType, new Dimension[] { timeDim });
        NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, new String[] { "long_name" });
        ncFileOut.addVariableAttribute("time", "long_name", "time");
        ncFileOut.addVariable(NetCDFUtilities.LAT, latDataType, new Dimension[] { latDim });
        NetCDFConverterUtilities.setVariableAttributes(latOriginalVar, ncFileOut, NetCDFUtilities.LAT);
        ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
        NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut, NetCDFUtilities.LON);
        if (hasZeta) {
            ncFileOut.addVariable(NetCDFUtilities.HEIGHT, zetaDataType, new Dimension[] { zDim });
            NetCDFConverterUtilities.setVariableAttributes(levelOriginalVar, ncFileOut, NetCDFUtilities.HEIGHT, new String[] { "long_name" });
            ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "positive", "up");
            ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "long_name", NetCDFUtilities.HEIGHT);
        }
        // lat Variable
        Array lat1Data = NetCDFConverterUtilities.getArray(nLat, latDataType);
        NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, latDataType, nLat, true);
        // lon Variable
        Array lon1Data = NetCDFConverterUtilities.getArray(nLon, lonDataType);
        NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, lonDataType, nLon, false);
        if (hasZeta) {
            // depth level Variable
            zeta1Data = NetCDFConverterUtilities.getArray(nZeta, zetaDataType);
            NetCDFConverterUtilities.setData1D(levelOriginalData, zeta1Data, zetaDataType, nZeta, false);
        }
        // {} Variables
        final ArrayList<String> variables = new ArrayList<String>(5);
        int numVars = 0;
        List<Variable> findVariables = ncFileIn.getVariables();
        for (Variable var : findVariables) {
            if (var != null) {
                String varName = var.getName();
                if (varName.equalsIgnoreCase(NetCDFUtilities.LATITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.LONGITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.TIME) || varName.equalsIgnoreCase(NetCDFUtilities.ZETA))
                    continue;
                variables.add(varName);
                List<Dimension> dims = var.getDimensions();
                boolean hasLocalZeta = false;
                for (Dimension dim : dims) {
                    if (dim.getName().equalsIgnoreCase(NetCDFUtilities.ZETA)) {
                        hasLocalZeta = true;
                        break;
                    }
                }
                if (hasZeta && hasLocalZeta)
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, zDim, latDim, lonDim });
                else
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
                NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "missing_value" });
                numVars++;
            }
        }
        // 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++) {
            timeData.setFloat(timeIndex.set(t), timeOriginalData.getFloat(timeOriginalIndex.set(t)));
        }
        ncFileOut.write("time", timeData);
        timeVar = ncFileOut.findVariable("time");
        timeDim.addCoordinateVariable(timeVar);
        ncFileOut.write(NetCDFUtilities.LAT, lat1Data);
        ncFileOut.write(NetCDFUtilities.LON, lon1Data);
        if (hasZeta) {
            Variable heightVar = ncFileOut.findVariable(NetCDFUtilities.HEIGHT);
            zDim.addCoordinateVariable(heightVar);
            ncFileOut.write(NetCDFUtilities.HEIGHT, zeta1Data);
        }
        for (int i = 0; i < numVars; i++) {
            String varName = (String) variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            boolean hasLocalZeta = NetCDFConverterUtilities.hasThisDimension(var, NetCDFUtilities.ZETA);
            Array originalVarArray = var.read();
            DataType varDataType = var.getDataType();
            Array destArray = null;
            int[] dimensions = null;
            if (hasZeta && hasLocalZeta) {
                dimensions = new int[] { timeDim.getLength(), zDim.getLength(), latDim.getLength(), lonDim.getLength() };
            } else {
                dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
            }
            destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
            final boolean setZeta = hasZeta && hasLocalZeta;
            final int[] loopLengths;
            if (setZeta)
                loopLengths = new int[] { nTimes, nZeta, nLat, nLon };
            else
                loopLengths = new int[] { nTimes, nLat, nLon };
            NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, false, false, loopLengths, true);
        }
        ncFileOut.close();
        outputFile.renameTo(fileOut);
    } catch (Exception e) {
        // something bad happened
        if (NetCDFConverterUtilities.LOGGER.isLoggable(Level.INFO))
            NetCDFConverterUtilities.LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        JAI.getDefaultInstance().getTileCache().flush();
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) ArrayList(java.util.ArrayList) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) DataType(ucar.ma2.DataType) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile)

Example 39 with Variable

use of org.flyte.api.v1.Variable in project imageio-ext by geosolutions-it.

the class BaseNetCDFImageReader method getAttribute.

public KeyValuePair getAttribute(final int imageIndex, final int attributeIndex) throws IOException {
    KeyValuePair attributePair = null;
    final Variable var = getVariable(imageIndex);
    if (var != null)
        attributePair = NetCDFUtilities.getAttribute(var, attributeIndex);
    return attributePair;
}
Also used : Variable(ucar.nc2.Variable) KeyValuePair(it.geosolutions.imageio.plugins.netcdf.NetCDFUtilities.KeyValuePair)

Example 40 with Variable

use of org.flyte.api.v1.Variable in project orbdetpy by ut-astria.

the class WAM method getDensity.

@Override
public double getDensity(AbsoluteDate date, Vector3D position, Frame frame) {
    double tt = date.durationFrom(AbsoluteDate.J2000_EPOCH);
    Map.Entry<Double, String> finfo = metaData.floorEntry(tt);
    if (finfo == null || (!overrideTime && tt - finfo.getKey() > 86400.0))
        throw (new RuntimeException("WAM data not found for " + date.toString()));
    GeodeticPoint gp = DataManager.earthShape.transform(position, frame, date);
    double lat = FastMath.toDegrees(gp.getLatitude());
    double lon = FastMath.toDegrees(MathUtils.normalizeAngle(gp.getLongitude(), FastMath.PI));
    double alt = gp.getAltitude();
    CacheEntry entry;
    synchronized (dataCache) {
        entry = dataCache.get(finfo.getValue());
        if (entry == null) {
            entry = new CacheEntry();
            dataCache.put(finfo.getValue(), entry);
            try (NetcdfFile data = NetcdfFiles.open(finfo.getValue())) {
                entry.latitude = (float[]) data.findVariable("lat").read("1:90").copyTo1DJavaArray();
                entry.longitude = (float[]) data.findVariable("lon").read().copyTo1DJavaArray();
                entry.altitude = (float[][][]) data.findVariable("height").read(":,1:90,:").copyToNDJavaArray();
                Variable var = data.findVariable("thermosphere_mass_density");
                if (var == null)
                    var = data.findVariable("neutral_density");
                entry.density = (float[][][]) var.read(":,1:90,:").copyToNDJavaArray();
                var = data.findVariable("temp_neutral");
                if (var != null) {
                    entry.temperature = (float[][][]) var.read("149,1:90,:").copyToNDJavaArray();
                    entry.densityN2 = (float[][][]) data.findVariable("N2_Density").read("149,1:90,:").copyToNDJavaArray();
                    entry.densityO = (float[][][]) data.findVariable("O_Density").read("149,1:90,:").copyToNDJavaArray();
                    entry.densityO2 = (float[][][]) data.findVariable("O2_Density").read("149,1:90,:").copyToNDJavaArray();
                }
            } catch (Exception exc) {
                throw (new RuntimeException(exc));
            }
        }
    }
    int[] xb = angleBounds(entry.latitude, (float) lat);
    double[] gridX = { entry.latitude[xb[0]], entry.latitude[xb[1]] };
    int[] yb = angleBounds(entry.longitude, (float) lon);
    double[] gridY = { entry.longitude[yb[0]], entry.longitude[yb[1]] };
    if (gridY[1] < gridY[0])
        gridY[1] += 360.0;
    int[] zb = altitudeBounds(entry.altitude, xb[0], yb[0], (float) alt);
    double[] gridZ = { entry.altitude[zb[0]][xb[0]][yb[0]], entry.altitude[zb[1]][xb[0]][yb[0]] };
    double[][][] gridF = new double[gridX.length][gridY.length][gridZ.length];
    for (int i = 0; i < gridX.length; i++) for (int j = 0; j < gridY.length; j++) for (int k = 0; k < gridZ.length; k++) gridF[i][j][k] = entry.density[zb[k]][xb[i]][yb[j]];
    TricubicInterpolatingFunction function = interpolator.interpolate(gridX, gridY, gridZ, gridF);
    if (alt >= gridZ[0] && alt <= gridZ[gridZ.length - 1])
        return (function.value(lat, lon, alt));
    if (alt < gridZ[0])
        return (function.value(lat, lon, gridZ[0]));
    double[] species = { entry.densityN2[0][xb[0]][yb[0]], entry.densityO[0][xb[0]][yb[0]], entry.densityO2[0][xb[0]][yb[0]] };
    while (gridZ[gridZ.length - 1] < alt) {
        double scale = -FastMath.min(alt - gridZ[gridZ.length - 1], 10E3) * 9.80665 * FastMath.pow(6371008.8 / (gridZ[gridZ.length - 1] + 6371008.8), 2) / (1.380649E-23 * entry.temperature[0][xb[0]][yb[0]]);
        for (int i = 0; i < ATOMIC_MASS.length; i++) species[i] *= FastMath.exp(scale * ATOMIC_MASS[i]);
        gridZ[gridZ.length - 1] += 10E3;
    }
    species[0] *= ATOMIC_MASS[0];
    for (int i = 1; i < ATOMIC_MASS.length; i++) species[0] += species[i] * ATOMIC_MASS[i];
    return (species[0]);
}
Also used : Variable(ucar.nc2.Variable) GeodeticPoint(org.orekit.bodies.GeodeticPoint) TricubicInterpolatingFunction(org.hipparchus.analysis.interpolation.TricubicInterpolatingFunction) NetcdfFile(ucar.nc2.NetcdfFile) GeodeticPoint(org.orekit.bodies.GeodeticPoint) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

Variable (ucar.nc2.Variable)103 IOException (java.io.IOException)51 Attribute (ucar.nc2.Attribute)34 Variable (org.jpl7.Variable)33 Query (org.jpl7.Query)32 Array (ucar.ma2.Array)30 Term (org.jpl7.Term)29 ArrayList (java.util.ArrayList)22 Dimension (ucar.nc2.Dimension)22 NetcdfFile (ucar.nc2.NetcdfFile)20 Compound (org.jpl7.Compound)19 Map (java.util.Map)18 InvalidRangeException (ucar.ma2.InvalidRangeException)16 Atom (org.jpl7.Atom)15 ArrayDouble (ucar.ma2.ArrayDouble)15 File (java.io.File)10 List (java.util.List)10 ArrayFloat (ucar.ma2.ArrayFloat)10 WritableRaster (java.awt.image.WritableRaster)8 HashMap (java.util.HashMap)8