protected void InternalAddTimeSeries(string identifier, double[] timeSeries, string variableName)
        {
            if (timeSeries.Length != timeLength)
            {
                throw new ArgumentException("The length of the time series data provided must match the length of the time dimension");
            }

            if (!identifierIndices.ContainsKey(identifier))
            {
                // This is a new identifier; we need to expand the corresponding unlimited dimension
                identifiers.Add(identifier);
                identifierIndices.Add(identifier, identifiers.Count - 1);
                writeableFile.findDimension(itemIndexDimname).setLength(identifiers.Count);
                var cellIdentifiers = identifier.ToCharArray();
                // The origin to use to write the identifier for each record
                int[]          id_origin      = new int[] { identifiers.Count - 1, 0 };
                ucar.ma2.Array identifierData = ucar.ma2.Array.factory(DataType.CHAR, new int[] { 1, cellIdentifiers.Length }, cellIdentifiers);
                writeableFile.write(itemIndexVarname, id_origin, identifierData);
                writeableFile.write(itemIndexDimname, ucar.ma2.Array.makeArray(DataType.INT, identifiers.Count, 1, 1));
            }

            var index = identifierIndices[identifier];

            // The origin to use to write the variable record
            int[]  origin = new int[] { index, 0 }; // tsIdentifier, time
            object values = null;

            if (dataType == DataType.FLOAT)
            {
                values = System.Array.ConvertAll(timeSeries, (x => (float)x));
            }
            else if (dataType == DataType.DOUBLE)
            {
                values = timeSeries;
            }

            ucar.ma2.Array variableData = ucar.ma2.Array.factory(dataType, new int[] { 1, timeLength }, values);

            writeableFile.write(variableName, origin, variableData);
        }
Esempio n. 2
0
        public static T[] GetOneDimArray <T>(ucar.ma2.Array theArray)
        {
            if (typeof(string) != typeof(T))
            {
                return((T[])theArray.get1DJavaArray(typeof(T)));
            }
            else
            {
                var shape = theArray.getShape();
                var size  = shape[1];


                char[]        temp     = theArray.get1DJavaArray(typeof(char)) as char[];
                List <char>   charList = new List <char>();
                List <string> strList  = new List <string>();
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i] == '\0')
                    {
                        if (charList.Count > 0)
                        {
                            strList.Add(new string(charList.ToArray()));
                            charList.Clear();
                        }
                    }
                    else
                    {
                        charList.Add(temp[i]);
                        if (charList.Count == size)
                        {
                            strList.Add(new string(charList.ToArray()));
                            charList.Clear();
                        }
                    }
                }
                return(strList.ToArray() as T[]);
            }
        }
        //*****************************************************************************************
        // 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;
        }
        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();
            }
        }