コード例 #1
0
ファイル: ConvertNcToDfs2.cs プロジェクト: DHI/netcdf-client
        private void _getMinAndInterval(ucar.nc2.Variable var, string minMaxStr, out double interval, out double minVal, out double count)
        {
            ucar.ma2.Array dataArr    = _util.GetAllVariableData(var);
            bool           stepExists = false;

            interval = 1.0; minVal = double.MaxValue; count = 0;
            java.util.List ncVarAtt = var.getAttributes();
            for (int attCount = 0; attCount < ncVarAtt.size(); attCount++)
            {
                ucar.nc2.Attribute varAtt  = (ucar.nc2.Attribute)ncVarAtt.get(attCount);
                string             attName = varAtt.getName();
                if (attName == "step")
                {
                    java.lang.Number attVal = (java.lang.Number)varAtt.getValue(0);
                    interval   = attVal.doubleValue();
                    stepExists = true;
                }
            }

            double prevVal = 0.0;
            int    minCount = 0, maxCount = (int)dataArr.getSize() - 1; count = 0;

            if (!string.IsNullOrEmpty(minMaxStr))
            {
                minCount = Convert.ToInt32(minMaxStr.Split(':')[0]);
                maxCount = Convert.ToInt32(minMaxStr.Split(':')[1]);
                count    = maxCount - minCount + 1;
            }
            else
            {
                count = maxCount + 1;
            }

            for (int dCount = minCount; dCount <= maxCount; dCount++)
            {
                ucar.ma2.Index dIndex = dataArr.getIndex();
                double         data   = dataArr.getDouble(dIndex.set(dCount));
                if (minVal >= data)
                {
                    minVal = data;
                }
                if (!stepExists)
                {
                    if (dCount > 0)
                    {
                        prevVal  = dataArr.getDouble(dIndex.set(dCount - 1));
                        interval = data - prevVal;
                    }
                }
            }
        }
コード例 #2
0
        public float ReadFloatAttribute(string attributeName)
        {
            float ret = 0.0f;

            ucar.nc2.Attribute attrib = _file.findGlobalAttribute(attributeName);
            if (attrib.getDataType().isIntegral())
            {
                ret = (attrib.getValue(0) as java.lang.Number).intValue();
            }
            else if (attrib.getDataType().isFloatingPoint())
            {
                ret = (attrib.getValue(0) as java.lang.Float).floatValue();
            }
            return(ret);
        }
コード例 #3
0
 public string ReadStringAttribute(string attributeName)
 {
     ucar.nc2.Attribute attribute =
         _file.findGlobalAttribute("SIMULATION_START_DATE");
     return(attribute.getValue(0) as string);
 }
コード例 #4
0
        //*****************************************************************************************
        // Method: ReadCellFile
        //
        /// <summary>
        ///     Method that reads a cell file that is in-line to be aggregated, verifying
        ///     that the file is valid, and can indeed be aggregated with the other files
        /// </summary>
        /// <param name="asaNC"></param>
        /// <param name="fileToRead"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        //*****************************************************************************************
        public void sendASANetCDF2AGOL(string fileToRead, string libPath)
        {
            ImportJavaLibrary(libPath);

            DataSource source = new DataSource();
            ASANetCdf  asaNC  = new ASANetCdf();

            string varName;
            int    dimSize;
            double timeOffset = 0;

            NetcdfFile currentFile = new NetcdfFile(fileToRead);

            Iterator dimensions = currentFile.getDimensionIterator();
            Iterator variables  = currentFile.getVariableIterator();
            Iterator attributes = currentFile.getGlobalAttributeIterator();

            ucar.nc2.Attribute[] atts = new ucar.nc2.Attribute[1];
            Dimension            dim;

            timeName = source.TimeVariableName;
            lonName  = source.LonVariableName;
            latName  = source.LatVariableName;
            uName    = source.UVariableName;
            vName    = source.VVariableName;

            scalar = source.Scalar;

            scaleFactor = source.ScaleFactor;
            //this.waterOrWind = Convert.ToInt32(source.DataType.ToUpper().StartsWith("W"));

            while (dimensions.hasNext())
            {
                dim = (Dimension)dimensions.next();

                varName = dim.getName();
                dimSize = dim.getLength();

                if (varName.Equals(timeName))
                {
                    timeDimSize = dimSize;
                }
                else if (varName.Equals(lonName))
                {
                    lonDimSize = dimSize;
                }
                else if (varName.Equals(latName))
                {
                    latDimSize = dimSize;
                }
                else if (varName.Equals("ncell") || varName.Equals("ncells"))
                {
                    ncellDimSize = dimSize;
                }
            }

            Variable var;
            int      numVars = 0;

            while (variables.hasNext())
            {
                var     = (Variable)variables.next();
                varName = var.getName();

                if (varName.Equals(timeName))
                {
                    numVars += 1;
                }
                else if (varName.Equals(timeName))
                {
                    numVars += 1;
                }
                else if (varName.Equals(timeName))
                {
                    numVars += 1;
                }
                else if (varName.Equals(timeName))
                {
                    numVars += 1;
                }
                else if (varName.Equals(timeName))
                {
                    numVars += 1;
                }
            }

            Variable varTime = currentFile.findVariable(timeName);

            ucar.ma2.Array timeArray = varTime.read();
            Index          timeIndex = timeArray.getIndex();

            double t1 = (double)timeArray.getDouble(timeIndex.set(0));
            double t2 = (double)timeArray.getDouble(timeIndex.set(timeDimSize - 1));

            ucar.nc2.Attribute tUnits = varTime.findAttribute("units");
            timeUnits = tUnits.getStringValue();

            int      idx        = timeUnits.IndexOf("since");
            string   timeString = timeUnits.Substring(idx + 6);
            DateTime date0      = DateTime.Parse(timeString);

            timeOrigin = date0;
            DateTime oldOrigin = timeOrigin;

            DateTime date1 = date0;
            DateTime date2 = date0;

            TimeSpan tSpan;
            TimeSpan tSpan2;

            if (timeUnits.ToUpper().Contains("DAYS"))
            {
                tSpan  = TimeSpan.FromDays(t1);
                tSpan2 = TimeSpan.FromDays(t2);
            }
            else if (timeUnits.ToUpper().Contains("HOURS"))
            {
                tSpan  = TimeSpan.FromHours(t1);
                tSpan2 = TimeSpan.FromHours(t2);
            }
            else if (timeUnits.ToUpper().Contains("MINUTES"))
            {
                tSpan  = TimeSpan.FromMinutes(t1);
                tSpan2 = TimeSpan.FromMinutes(t2);
            }
            else if (timeUnits.ToUpper().Contains("SECONDS"))
            {
                tSpan  = TimeSpan.FromSeconds(t1);
                tSpan2 = TimeSpan.FromSeconds(t2);
            }
            else
            {
                tSpan  = TimeSpan.FromHours(t1);
                tSpan2 = TimeSpan.FromHours(t2);
            }

            date1 = date1.Add(tSpan);
            date2 = date2.Add(tSpan2);

            if (endDate > startDate)
            {
                // Time window is on
                if (startTimeMin < 0)
                {
                    tSpan        = startDate - date0;
                    startTimeMin = (int)tSpan.TotalHours;
                    tSpan        = endDate - date0;
                    endTimeMin   = (int)tSpan.TotalHours;
                }

                if (date1 > endDate || date2 < startDate)
                {
                    timeOrigin = oldOrigin;
                    error      = "CellFileAggregator.GetDimsAndVars - Invalid dates!";
                    currentFile.close();
                    //return false;
                }
            }

            if (curTimeUnit.Length > 0)
            {
                if (!timeUnits.Equals(curTimeUnit))
                {
                    error = "CellFileAggregator.GetDimsAndVars - Time Units conflict!" +
                            fileToRead;
                    currentFile.close();

                    timeOrigin = oldOrigin;
                    //return false;
                }
            }

            if (timeUnitG.Length > 0)
            {
                if (!timeUnits.Equals(timeUnitG))
                {
                    tSpan = date0 - firstFileDate;

                    if (timeUnits.ToUpper().Contains("DAYS"))
                    {
                        timeOffset = (double)tSpan.TotalDays;
                    }
                    else if (timeUnits.ToUpper().Contains("HOURS"))
                    {
                        timeOffset = (double)tSpan.TotalHours;
                    }
                    else if (timeUnits.ToUpper().Contains("MINUTES"))
                    {
                        timeOffset = (double)tSpan.TotalMinutes;
                    }
                    else if (timeUnits.ToUpper().Contains("SECONDS"))
                    {
                        timeOffset = (double)tSpan.TotalSeconds;
                    }
                    else
                    {
                        timeOffset = (double)tSpan.TotalMinutes;
                    }
                }
            }

            int numFiles = fileList.Count;

            if (numFiles == 0)
            {
                lat = new List <float>();
                lon = new List <float>();

                timeUnitG     = timeUnits;
                firstFileDate = date0;

                tStart  = new double[1];
                tEnd    = new double[1];
                tDurHr  = new double[1];
                nTsteps = new int[1];

                // Read the various variables
                Variable       varLat    = currentFile.findVariable(latName);
                ucar.ma2.Array latVals   = varLat.read();
                Index          latIndex  = latVals.getIndex();
                int[]          shapeLats = latVals.getShape();

                Variable       varLon    = currentFile.findVariable(lonName);
                ucar.ma2.Array lonVals   = varLon.read();
                Index          lonIndex  = lonVals.getIndex();
                int[]          shapeLons = lonVals.getShape();

                nCells       = 0;
                areaWindowOn = false;

                if (aoiEast != aoiWest && aoiNorth != aoiSouth)
                {
                    areaWindowOn = true;
                }

                for (int c = 0; c < ncellDimSize; c++)
                {
                    double xLon = lonVals.getDouble(lonIndex.set(c));
                    double yLat = latVals.getDouble(latIndex.set(c));


                    if (areaWindowOn)
                    {
                        if (xLon < aoiWest || xLon > aoiEast || yLat < aoiSouth ||
                            yLat > aoiNorth || double.IsNaN(xLon) || double.IsNaN(yLat))
                        {
                            continue;
                        }
                        else
                        {
                            nCells += 1;

                            lat.Add((float)yLat);
                            lon.Add((float)xLon);
                        }
                    }
                    else if (!double.IsNaN(xLon) && !double.IsNaN(yLat))
                    {
                        nCells += 1;

                        lat.Add((float)yLat);
                        lon.Add((float)xLon);
                    }
                }

                if (nCells <= 0)
                {
                    error = "CellFileAggregator.GetDimsAndVars: No data in the given AOI!";
                    currentFile.close();

                    timeOrigin = oldOrigin;
                    //return false;
                }
            }
            else
            {
                // This is not the first file, therefore we have must less processing to handle
                //util_.ResizeDouble(ref tStart, numFiles + 1);
                //util_.ResizeDouble(ref tEnd, numFiles + 1);
                //util_.ResizeDouble(ref tDurHr, numFiles + 1);
                //util_.ResizeInt(ref nTsteps, numFiles + 1);
            }

            tStart[numFiles]  = (double)timeArray.getDouble(timeIndex.set(0)) + timeOffset;
            tEnd[numFiles]    = (double)timeArray.getDouble(timeIndex.set(timeDimSize - 1)) + timeOffset;
            tDurHr[numFiles]  = tEnd[numFiles] - tStart[numFiles];
            nTsteps[numFiles] = timeDimSize;

            tStart0 = Math.Min(tStart0, tStart[numFiles]);
            tEnd0   = Math.Max(tEnd0, tEnd[numFiles]);

            currentFile.close();
            //return true;
        }
コード例 #5
0
        public void ProcessEDS2AGOL(string ncFileName_, string dllPath, string typeFile)
        {
            ImportJavaLibrary(dllPath);

            try
            {
                // Read the data from the NetCDF file
                NetcdfFile currentFile = new NetcdfFile(ncFileName_);

                Iterator dimensions = currentFile.getDimensionIterator();

                Variable       varTime   = currentFile.findVariable("time");
                ucar.ma2.Array timeVals  = varTime.read();
                Index          timeIndex = timeVals.getIndex();

                Variable       varLat   = currentFile.findVariable("lat");
                ucar.ma2.Array latVals  = varLat.read();
                Index          latIndex = latVals.getIndex();
                //int[] shapeLats = latVals.getShape();

                Variable       varLon   = currentFile.findVariable("lon");
                ucar.ma2.Array lonVals  = varLon.read();
                Index          lonIndex = lonVals.getIndex();

                int timeDimSize  = -1;
                int ncellDimSize = -1;
                int serviceIDType;

                Dimension dim;
                string    varName;
                int       dimSize;

                while (dimensions.hasNext())
                {
                    dim = (Dimension)dimensions.next();

                    varName = dim.getName();
                    dimSize = dim.getLength();

                    if (varName.Equals("time"))
                    {
                        timeDimSize = dimSize;
                    }
                    else if (varName.Equals("ncell") || varName.Equals("ncells"))
                    {
                        ncellDimSize = dimSize;
                    }
                }

                ucar.nc2.Attribute tUnits = varTime.findAttribute("units");

                //Winds or Currents file U + V
                Variable varV;
                Variable varU;
                if (typeFile == "current")
                {
                    varU          = currentFile.findVariable("U");
                    varV          = currentFile.findVariable("V");
                    serviceIDType = 3;
                }
                else
                {
                    varU          = currentFile.findVariable("wind_u");
                    varV          = currentFile.findVariable("wind_v");
                    serviceIDType = 4;
                }

                System.Collections.ArrayList postNetCDFGroup = new System.Collections.ArrayList();

                // Loop through time steps and create time
                for (int t = 0; t < timeDimSize; t++)
                {
                    //Get time
                    double timeVal = timeVals.getDouble(timeIndex.set(t));

                    //Add minutes to time
                    DateTime ncEpoch  = new DateTime(2000, 01, 01, 00, 00, 00);
                    DateTime dataTime = ncEpoch.AddMinutes(timeVal);
                    //if (tUnits.name.Contains("minutes")){}

                    ucar.ma2.Array aU;
                    ucar.ma2.Array aV;

                    int[] shapeU  = varU.getShape();
                    int[] shapeV  = varV.getShape();
                    int[] originU = new int[varU.getRank()];
                    int[] originV = new int[varV.getRank()];

                    originU[0] = t;
                    shapeU[0]  = 1;
                    originV[0] = t;
                    shapeV[0]  = 1;
                    if (t == 0)
                    {
                        int[] priorOriginU;
                        priorOriginU    = new int[varU.getRank()];
                        priorOriginU[0] = timeDimSize - 1;
                        aU = varU.read(priorOriginU, shapeU);

                        int[] priorOriginV;
                        priorOriginV    = new int[varV.getRank()];
                        priorOriginV[0] = timeDimSize - 1;
                        aV = varV.read(priorOriginV, shapeV);
                    }
                    else
                    {
                        aU = varU.read(originU, shapeU);
                        aV = varV.read(originV, shapeV);
                    }
                    Index uIndex = aU.getIndex();
                    Index vIndex = aV.getIndex();
                    for (int i = 0; i < ncellDimSize; i++)
                    {
                        double xLon  = lonVals.getDouble(lonIndex.set(i));
                        double yLat  = latVals.getDouble(latIndex.set(i));
                        double uValu = aU.getDouble(uIndex.set(0, i));
                        double vValu = aV.getDouble(vIndex.set(0, i));

                        while (xLon > 360f)
                        {
                            xLon -= 360f;
                        }
                        if (uValu != 0 && uValu != -9999)
                        {
                            NetCDFJSON netcdfObj = new NetCDFJSON {
                            };
                            NetCDFObjAGOL.Geometry   netcdfGeom = new NetCDFObjAGOL.Geometry();
                            NetCDFObjAGOL.Attributes netcdfAttr = new NetCDFObjAGOL.Attributes();
                            netcdfGeom.spatialReference = new NetCDFObjAGOL.Spatialref();

                            netcdfGeom.x           = Math.Round(xLon, 4);
                            netcdfGeom.y           = Math.Round(yLat, 4);
                            netcdfAttr.DateTime    = dataTime.Year.ToString() + "-" + paddingDate(dataTime.Month) + "-" + paddingDate(dataTime.Day) + " " + paddingDate(dataTime.Hour) + ":" + paddingDate(dataTime.Minute) + ":00";
                            netcdfAttr.SCENARIO_ID = Path.GetFileNameWithoutExtension(ncFileName_);
                            netcdfAttr.ncells      = i.ToString();
                            //netcdfAttr.U = Math.Round(uValu, 4).ToString();
                            //netcdfAttr.V = Math.Round(vValu, 4).ToString();
                            //calculate Speed
                            netcdfAttr.Speed = Math.Round(Math.Sqrt(uValu * uValu + vValu * vValu), 3).ToString();
                            //calculate direction and convert from radians
                            //Double dir = vValu / uValu;
                            //Double dir2 = (Math.Atan(dir) * 180 / Math.PI) + 360;
                            //netcdfAttr.Direction = Math.Round(dir2, 2).ToString();

                            //(y,x)
                            double dir = Math.Atan2(vValu, uValu) * (180.0 / Math.PI);

                            // Covernt to compass coords
                            dir = 90.0 - dir;

                            if (dir < 0)
                            {
                                dir = 360.0 + dir;
                            }
                            netcdfAttr.Direction = Math.Round(dir, 2).ToString();

                            netcdfObj.geometry   = netcdfGeom;
                            netcdfObj.attributes = netcdfAttr;
                            postNetCDFGroup.Add(netcdfObj);

                            if (postNetCDFGroup.Count > 4500)
                            {
                                //post2AGOL(postNetCDFGroup, serviceIDType, ncFileName_);
                                InsertFeatures(serviceIDType.ToString(), new JavaScriptSerializer().Serialize(postNetCDFGroup).ToString(), GetAGOToken());
                                postNetCDFGroup.Clear();
                            }
                        }
                    }
                }

                //post2AGOL(postNetCDFGroup, serviceIDType, ncFileName_);
                InsertFeatures(serviceIDType.ToString(), new JavaScriptSerializer().Serialize(postNetCDFGroup).ToString(), GetAGOToken());
            }
            catch (Exception ex)
            {
                string t = ex.ToString();
            }
        }