private void _writeItemData(DfsUtilities df_in, ucar.nc2.NetcdfFileWriteable newNetcdfFile) { for (int itemCount = 0; itemCount < _settings.Variables.Count; itemCount++) { if (_settings.IsVariablesSelected[itemCount]) { ucar.ma2.ArrayDouble dataArr = new ucar.ma2.ArrayDouble.D4(df_in.tAxis_nTSteps, df_in.Items[0].nPointsX, df_in.Items[0].nPointsY, df_in.Items[0].nPointsZ); ucar.ma2.Index dataIndex = dataArr.getIndex(); for (int i = 0; i < df_in.tAxis_nTSteps; i++) { float[] dfsData = null; df_in.ReadDynData(i, itemCount + 1, out dfsData); int dataCount = 0; for (int l = 0; l < df_in.Items[0].nPointsZ; l++) { for (int j = 0; j < df_in.Items[0].nPointsY; j++) { for (int k = 0; k < df_in.Items[0].nPointsX; k++) { dataArr.setDouble(dataIndex.set(i, k, j, l), dfsData[dataCount]); dataCount++; } } } } newNetcdfFile.write(df_in.Items[itemCount].Name.Replace(' ', '_'), dataArr); } } }
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; } } } }
private void _writeItemData(DfsUtilities df_in, ucar.nc2.NetcdfFileWriteable newNetcdfFile) { for (int itemCount = 0; itemCount < _settings.Variables.Count; itemCount++) { if (_settings.IsVariablesSelected[itemCount]) { ucar.ma2.ArrayDouble dataArr = new ucar.ma2.ArrayDouble.D1(df_in.tAxis_nTSteps); ucar.ma2.Index dataIndex = dataArr.getIndex(); for (int i = 0; i < df_in.tAxis_nTSteps; i++) { float[] dfsData = null; df_in.ReadDynData(i, itemCount + 1, out dfsData); dataArr.setDouble(dataIndex.set(i), dfsData[0]); } newNetcdfFile.write(df_in.Items[itemCount].Name.Replace(' ', '_'), dataArr); } } }
private List <DataIndex> _generateNCIndexes1D(ucar.ma2.Array sourceData, ucar.ma2.Array xData, ucar.ma2.Array yData, ucar.ma2.Index xIndex, ucar.ma2.Index yIndex, int[] resShape, int xPosition, int yPosition, java.util.List attList, List <double> xList, List <double> yList) { //assuming that the file is 1D with the same lat. or same lon. List <DataIndex> ncIndexes = new List <DataIndex>(); ucar.ma2.Index resIndex = sourceData.getIndex(); //find closest x and y points int[] ydataShape = yData.getShape(); int[] xdataShape = xData.getShape(); int yCount = 0; //static for (int xCount = 1; xCount < (int)resShape[xPosition]; xCount++) { double latFromNC = 0; double prevLatFromNC = 0; double prevPrevLatFromNC = 0; if (ydataShape.Length == 2) { latFromNC = yData.getDouble(yIndex.set(yCount, xCount)); prevLatFromNC = yData.getDouble(yIndex.set(yCount - 1, xCount)); if (yCount >= 2) { prevPrevLatFromNC = yData.getDouble(yIndex.set(yCount - 2, xCount)); } } else if (ydataShape.Length == 1) { latFromNC = yData.getDouble(yCount); } double lonFromNC = 0; double prevLonFromNC = 0; double prevPrevLonFromNC = 0; if (xdataShape.Length == 2) { lonFromNC = xData.getDouble(xIndex.set(yCount, xCount)); prevLonFromNC = xData.getDouble(xIndex.set(yCount, xCount - 1)); if (xCount >= 2) { prevPrevLonFromNC = xData.getDouble(xIndex.set(yCount, xCount - 2)); } } else if (xdataShape.Length == 1) { lonFromNC = xData.getDouble(xCount); } int rangeCount = 0; if (latFromNC >= yList.Min()) { rangeCount++; } if (latFromNC <= yList.Max()) { rangeCount++; } if (lonFromNC >= xList.Min()) { rangeCount++; } if (lonFromNC <= xList.Max()) { rangeCount++; } DataIndex newIndex = new DataIndex(); newIndex.xIndex = xCount; newIndex.prevXIndex = xCount - 1; newIndex.yIndex = yCount; newIndex.prevYIndex = yCount - 1; newIndex.nc_X = lonFromNC; newIndex.nc_Y = latFromNC; newIndex.pnc_X = prevLonFromNC; newIndex.pnc_Y = prevLatFromNC; newIndex.data = sourceData.getDouble(resIndex.set(xCount)); newIndex.prevData = sourceData.getDouble(resIndex.set(xCount - 1)); newIndex.lonFromDfs = new List <double>(); foreach (double xPoint in xList) { if (xPoint >= prevLonFromNC && xPoint <= lonFromNC) { newIndex.lonFromDfs.Add(xPoint); } else if (xPoint >= prevPrevLonFromNC && xPoint <= lonFromNC) { newIndex.lonFromDfs.Add(xPoint); } else if (xPoint == lonFromNC) { newIndex.lonFromDfs.Add(xPoint); } } newIndex.latFromDfs = new List <double>(); foreach (double yPoint in yList) { if (yPoint >= prevLatFromNC && yPoint <= latFromNC) { newIndex.latFromDfs.Add(yPoint); } else if (yPoint >= prevPrevLatFromNC && yPoint <= latFromNC) { newIndex.latFromDfs.Add(yPoint); } else if (yPoint == latFromNC) { newIndex.latFromDfs.Add(yPoint); } } if (_util.IsValueValid(attList, newIndex.data) && rangeCount == 4) { ncIndexes.Add(newIndex); } } return(ncIndexes); }
public float[] _reassignData(ucar.ma2.Array sourceData, ucar.ma2.Array xData, ucar.ma2.Array yData, java.util.List attList, float delVal, double j, double k, double lat0, double lon0, double dx, double dy, int xPosition, int yPosition) { try { int resCount = 0; ucar.ma2.Index xIndex = xData.getIndex(); ucar.ma2.Index yIndex = yData.getIndex(); ucar.ma2.Index resIndex = sourceData.getIndex(); int[] resShape = resIndex.getShape(); List <double> xCoorList = new List <double>(); for (int xLayerCount = 0; xLayerCount < j; xLayerCount++) { double lon = lon0 + xLayerCount * dx; xCoorList.Add(lon); } List <double> yCoorList = new List <double>(); for (int yLayerCount = 0; yLayerCount < k; yLayerCount++) { double lat = lat0 + yLayerCount * dy; yCoorList.Add(lat); } //get indexes and values from nc file if (resShape.Length > 1) { _ncIndexes = _generateNCIndexes(sourceData, xData, yData, xIndex, yIndex, resShape, xPosition, yPosition, attList, xCoorList, yCoorList); } else { _ncIndexes = _generateNCIndexes1D(sourceData, xData, yData, xIndex, yIndex, resShape, xPosition, yPosition, attList, xCoorList, yCoorList); } //assign values to dfs2 grid List <DataIndex> matchedIndex = new List <DataIndex>(); float[] resfloat = new float[(int)j * (int)k]; for (int i = 0; i < resfloat.Length; i++) { resfloat[i] = delVal; } int resCountNC = 0; for (int i = 0; i < _ncIndexes.Count; i++) { List <int> xCounts = new List <int>(); for (int a = 0; a < _ncIndexes[i].lonFromDfs.Count; a++) { int xCount = xCoorList.FindIndex(x => x == _ncIndexes[i].lonFromDfs[a]); xCounts.Add(xCount); } List <int> yCounts = new List <int>(); for (int a = 0; a < _ncIndexes[i].latFromDfs.Count; a++) { int yCount = yCoorList.FindIndex(x => x == _ncIndexes[i].latFromDfs[a]); yCounts.Add(yCount); } for (int y = 0; y < yCounts.Count; y++) { for (int x = 0; x < xCounts.Count; x++) { resCountNC = (xCounts[x]) + (yCounts[y]) * (xCoorList.Count); if (resCountNC >= resfloat.Length || resCountNC < 0) { throw new Exception("out of bounds - " + resCountNC.ToString() + " + >= array size of " + resfloat.Length.ToString()); } resfloat[resCountNC] = (float)_ncIndexes[i].data; } } } return(resfloat); } catch (Exception ex) { throw new Exception("_reassignData Error: " + ex.Message); } }
private List <DataIndex> _generateNCIndexes(ucar.ma2.Array sourceData, ucar.ma2.Array xData, ucar.ma2.Array yData, ucar.ma2.Array zData, ucar.ma2.Index xIndex, ucar.ma2.Index yIndex, int[] resShape, int xPosition, int yPosition, int zPosition, java.util.List attList, List <double> xList, List <double> yList, List <double> zList) { List <DataIndex> ncIndexes = new List <DataIndex>(); ucar.ma2.Index resIndex = sourceData.getIndex(); for (int zCount = (int)resShape[zPosition - 1] - 1; zCount > 0; zCount--) { //find closest zValues double zValueFromNC = zData.getDouble(zCount); double prevZValueFromNC = 0; if (zCount > 1) { prevZValueFromNC = zData.getDouble(zCount - 1); } if (zValueFromNC <= zList.Max()) { foreach (double xPoint in xList) { foreach (double yPoint in yList) { DataIndex newIndex = new DataIndex(); newIndex.xPoint = xPoint; newIndex.yPoint = yPoint; newIndex.zFromDfs = new List <double>(); foreach (double zPoint in zList) { if (zPoint >= prevZValueFromNC && zPoint <= zValueFromNC) { newIndex.zFromDfs.Add(zPoint); } } if (zCount == 1) { newIndex.zFromDfs.Add(zList.Min()); } newIndex.zIndex = zCount; newIndex.prevZIndex = zCount - 1; newIndex.nc_Z = zValueFromNC; newIndex.pnc_Z = prevZValueFromNC; int dataCount = 0;//(int)resShape[xPos]; List <double> distances = new List <double>(); List <double> distancesData = new List <double>(); for (int yCount = 0; yCount < (int)resShape[yPosition - 1] - 1; yCount++) { for (int xCount = 0; xCount < (int)resShape[xPosition - 1] - 1; xCount++) { double latFromNC = yData.getDouble(yIndex.set(yCount, xCount)); double lonFromNC = xData.getDouble(xIndex.set(yCount, xCount)); int rangeCount = 0; if (latFromNC >= yList.Min() - 1) { rangeCount++; } if (latFromNC <= yList.Max() + 1) { rangeCount++; } if (lonFromNC >= xList.Min() - 1) { rangeCount++; } if (lonFromNC <= xList.Max() + 1) { rangeCount++; } if (rangeCount == 4) { //find closest x and y points try { double distancex1 = Math.Sqrt((lonFromNC - xPoint) * (lonFromNC - xPoint) + (latFromNC - yPoint) * (latFromNC - yPoint)); double data = sourceData.getDouble(resIndex.set(zCount, yCount, xCount)); distances.Add(distancex1); distancesData.Add(data); dataCount++; } catch (Exception ex) { } } } } int smallestIndex = IndexOfMin(distances); double smallestDistance = distances[smallestIndex]; newIndex.data = distancesData[smallestIndex]; if (_util.IsValueValid(attList, newIndex.data) && smallestDistance <= 3 * Convert.ToDouble(_settings.DX) && newIndex.zFromDfs.Count > 0) { ncIndexes.Add(newIndex); } } } /*List<DataIndex> ncIndexes = new List<DataIndex>(); * * ucar.ma2.Index resIndex = sourceData.getIndex(); * for (int zCount = (int)resShape[zPosition - 1] - 1; zCount > 0; zCount--) * { * for (int yCount = 1; yCount < (int)resShape[yPosition - 1]; yCount++) * { * for (int xCount = 1; xCount < (int)resShape[xPosition - 1]; xCount++) * { * //find closest zValues * double zValueFromNC = zData.getDouble(zCount); * double prevZValueFromNC = zData.getDouble(zCount - 1); * * //find closest x and y points * int[] ydataShape = yData.getShape(); * int[] xdataShape = xData.getShape(); * * double latFromNC = 0; * double prevLatFromNC = 0; * double prevPrevLatFromNC = 0; * if (ydataShape.Length == 2) * { * latFromNC = yData.getDouble(yIndex.set(yCount, xCount)); * prevLatFromNC = yData.getDouble(yIndex.set(yCount - 1, xCount)); * if (yCount >= 2) * prevPrevLatFromNC = yData.getDouble(yIndex.set(yCount - 2, xCount)); * } * else if (ydataShape.Length == 1) * { * latFromNC = yData.getDouble(yCount); * prevLatFromNC = yData.getDouble(yCount - 1); * if (yCount >= 2) * prevPrevLatFromNC = yData.getDouble(yCount - 2); * } * * double lonFromNC = 0; * double prevLonFromNC = 0; * double prevPrevLonFromNC = 0; * if (xdataShape.Length == 2) * { * lonFromNC = xData.getDouble(xIndex.set(yCount, xCount)); * prevLonFromNC = xData.getDouble(xIndex.set(yCount, xCount - 1)); * if (xCount >= 2) * prevPrevLonFromNC = xData.getDouble(xIndex.set(yCount, xCount - 2)); * } * else if (xdataShape.Length == 1) * { * lonFromNC = xData.getDouble(xCount); * prevLonFromNC = xData.getDouble(xCount - 1); * if (xCount >= 2) * prevPrevLonFromNC = xData.getDouble(xCount - 2); * } * * int rangeCount = 0; * * if (latFromNC >= yList.Min()) rangeCount++; * if (latFromNC <= yList.Max()) rangeCount++; * if (lonFromNC >= xList.Min()) rangeCount++; * if (lonFromNC <= xList.Max()) rangeCount++; * if (zValueFromNC >= zList.Min()) rangeCount++; * if (zValueFromNC <= zList.Max()) rangeCount++; * * DataIndex newIndex = new DataIndex(); * newIndex.xIndex = xCount; * newIndex.prevXIndex = xCount - 1; * newIndex.yIndex = yCount; * newIndex.prevYIndex = yCount - 1; * newIndex.zIndex = zCount; * newIndex.prevZIndex = zCount - 1; * newIndex.nc_X = lonFromNC; * newIndex.nc_Y = latFromNC; * newIndex.nc_Z = zValueFromNC; * newIndex.pnc_X = prevLonFromNC; * newIndex.pnc_Y = prevLatFromNC; * newIndex.pnc_Z = prevZValueFromNC; * * newIndex.data = sourceData.getDouble(resIndex.set(zCount, yCount, xCount)); * newIndex.prevData = sourceData.getDouble(resIndex.set(zCount - 1, yCount - 1, xCount - 1)); * * //double closestX = xList.Aggregate((x, y) => Math.Abs(x - lonFromNC) < Math.Abs(y - lonFromNC) ? x : y); * //int closestXinNC = xList.IndexOf(closestX); * newIndex.lonFromDfs = new List<double>(); * foreach (double xPoint in xList) * { * if (xPoint >= prevLonFromNC && xPoint <= lonFromNC) * { * newIndex.lonFromDfs.Add(xPoint); * } * else if (xPoint >= prevPrevLonFromNC && xPoint <= lonFromNC) * { * newIndex.lonFromDfs.Add(xPoint); * } * } * * newIndex.zFromDfs = new List<double>(); * * double closest = zList.Aggregate((x, y) => Math.Abs(x - zValueFromNC) < Math.Abs(y - zValueFromNC) ? x : y); * newIndex.zFromDfs.Add(closest); * * if (newIndex.zFromDfs.Count == 0) * throw new Exception("No z point?"); * * newIndex.latFromDfs = new List<double>(); * foreach (double yPoint in yList) * { * if (yPoint >= prevLatFromNC && yPoint <= latFromNC) * { * newIndex.latFromDfs.Add(yPoint); * } * else if (yPoint >= prevPrevLatFromNC && yPoint <= latFromNC) * { * newIndex.latFromDfs.Add(yPoint); * } * } * * if (_util.IsValueValid(attList, newIndex.data) && rangeCount == 6) * ncIndexes.Add(newIndex); * * }*/ } } return(ncIndexes); }
private void _getMinAndInterval(ucar.nc2.Variable ncDim, string minMaxStr, out double interval, out double minVal, out List <double> interpolatedVal, out List <double> originalVal, bool useDefinedDZ) { ucar.ma2.Array dataArr = _util.GetAllVariableData(ncDim); int minCount = 0, maxCount = (int)dataArr.getSize() - 1; minVal = double.MaxValue; double maxVal = double.MinValue; double prevVal = 0.0; List <double> intervals = new List <double>(); interpolatedVal = new List <double>(); originalVal = new List <double>(); interval = 0.0; if (!string.IsNullOrEmpty(minMaxStr)) { minCount = Convert.ToInt32(minMaxStr.Split(':')[0]); maxCount = Convert.ToInt32(minMaxStr.Split(':')[1]); } java.util.List ncVarAtt = ncDim.getAttributes(); bool stepExists = false; 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; } } for (int dCount = minCount; dCount <= maxCount; dCount++) { ucar.ma2.Index dIndex = dataArr.getIndex(); double data = dataArr.getDouble(dIndex.set(dCount)); originalVal.Add(data); if (minVal >= data) { minVal = data; } if (maxVal <= data) { maxVal = data; } if (!stepExists) { if (dCount > 0) { prevVal = dataArr.getDouble(dIndex.set(dCount - 1)); interval = data - prevVal; intervals.Add(interval); } } } if (!stepExists) { if (intervals.Average() != interval) { if (useDefinedDZ) { for (double min = minVal; min <= maxVal; min += Convert.ToDouble(_settings.DZ)) { interpolatedVal.Add(min); } interval = Convert.ToDouble(_settings.DZ); } else { //generate a list of interpolatedVal for (double min = minVal; min <= maxVal; min += intervals.Min()) { interpolatedVal.Add(min); } interval = intervals.Min(); } } } }
private void _getMinAndInterval(ucar.nc2.Variable ncDim, out double interval, out double minVal, out List <double> interpolatedVal, out List <double> originalVal) { ucar.ma2.Array dataArr = _util.GetAllVariableData(ncDim); minVal = double.MaxValue; double maxVal = double.MinValue; double prevVal = 0.0; List <double> intervals = new List <double>(); interpolatedVal = new List <double>(); originalVal = new List <double>(); interval = 0.0; for (int dCount = 0; dCount < dataArr.getSize(); dCount++) { ucar.ma2.Index dIndex = dataArr.getIndex(); double data = dataArr.getDouble(dIndex.set(dCount)); originalVal.Add(data); if (minVal >= data) { minVal = data; } if (maxVal <= data) { maxVal = data; } if (dCount > 0) { prevVal = dataArr.getDouble(dIndex.set(dCount - 1)); interval = data - prevVal; intervals.Add(interval); } } double dz; if (_settings.DZ > 0) { dz = _settings.DZ; if (intervals.Average() != interval) { //generate a list of interpolatedVal for (double min = minVal; min <= maxVal; min += dz) { interpolatedVal.Add(min); } interval = dz; } } else { if (intervals.Average() != interval) { //generate a list of interpolatedVal for (double min = minVal; min <= maxVal; min += intervals.Min()) { interpolatedVal.Add(min); } interval = intervals.Min(); } } }