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; }