private void SaveCascade(string filename) { StreamWriter sw = new StreamWriter(filename); int[] item1 = new int[] { Property.HRUFLG, Property.STRMFLG, Property.FLOWFLG, Property.VISFLG, Property.IPRN, Property.IFILL }; string line = string.Format("{0}\t{1}\t{2}\t#HRUFLG STRMFLG FLOWFLG VISFLG IPRN IFILL DPIT OUTITMAX", string.Join("\t", item1), Property.DPIT, Property.OUTITMAX); sw.WriteLine(line); var hru_type = _Prms.MMSPackage.Parameters["hru_type"].ToInt32().ToArray(); int aci = 0; short[] ht = new short[mGrid.ColumnCount]; for (uint r = 0; r < mGrid.RowCount; r++) { line = ""; MatrixExtension <short> .Set(ht, 0); for (uint c = 0; c < mGrid.ColumnCount; c++) { if (mGrid.IBound[0, (int)r, (int)c] > 0) { ht[c] = (short)hru_type[aci]; aci++; } } line = string.Join("\t", ht); sw.WriteLine(line); } sw.Close(); }
public int[] GetIntevalSteps(TimeUnits time, int maxstep) { int count = Timeline.Count; if (maxstep < Timeline.Count) { count = maxstep; } if (time == TimeUnits.Day) { var inteval = new int[count]; MatrixExtension <int> .Set(inteval, 1); return(inteval); } else if (time == TimeUnits.Month) { var list = Timeline; if (maxstep < Timeline.Count) { var newlist = new List <DateTime>(); for (int i = 0; i < maxstep; i++) { newlist.Add(Timeline[i]); } list = newlist; } var derivedValues = list.GroupBy(t => new { t.Year, t.Month }).Select(group => group.Count()); var inteval = derivedValues.ToArray(); return(inteval); } return(null); }
public int[] GetIntevalSteps(TimeUnits time) { if (time == TimeUnits.Day) { var inteval = new int[Timeline.Count]; MatrixExtension <int> .Set(inteval, 1); return(inteval); } else if (time == TimeUnits.Month) { var derivedValues = Timeline.GroupBy(t => new { t.Year, t.Month }).Select(group => group.Count()); var inteval = derivedValues.ToArray(); return(inteval); } else if (time == TimeUnits.CommanYear) { var derivedValues = Timeline.GroupBy(t => new { t.Year }).Select(group => group.Count()); var inteval = derivedValues.ToArray(); return(inteval); } return(null); }
public override void AlterDimLength(int new_length) { if (new_length == Values.Length) { return; } var default_vv = Values[0]; Values = new T[new_length]; MatrixExtension <T> .Set(Values, default_vv); }
/// <summary> /// Provide quick summerization on specified cells.. Add an offeset when reading. Return 3D array: [subset zone][var][step] /// </summary> /// <param name="filename"></param> /// <param name="intevals"></param> /// <param name="step"></param> /// <param name="varNames"></param> /// <param name="subindex"></param> /// <param name="offset"></param> /// <param name="statFlag"></param> /// <returns></returns> public float[][][] Summerize(string filename, int[] intevals, ref int step, ref string[] varNames, int[][] subindex, float [] offset, NumericalDataType statFlag = NumericalDataType.Average) { step = 0; int feaNum = 0; FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs); int varnum = br.ReadInt32(); int nzone = subindex.Length; varNames = new string[varnum]; float[][][] stat = new float[nzone][][]; for (int i = 0; i < nzone; i++) { stat[i] = new float[varnum][]; } for (int i = 0; i < varnum; i++) { int varname_len = br.ReadInt32(); varNames[i] = new string(br.ReadChars(varname_len)).Trim(); feaNum = br.ReadInt32(); for (int z = 0; z < nzone; z++) { stat[z][i] = new float[intevals.Length]; } } float[][] matrix = new float[varnum][]; for (int i = 0; i < varnum; i++) { matrix[i] = new float[feaNum]; } for (int i = 0; i < intevals.Length; i++) { MatrixExtension <float> .Set(matrix, 0); for (int d = 0; d < intevals[i]; d++) { for (int s = 0; s < feaNum; s++) { for (int v = 0; v < varnum; v++) { matrix[v][s] += (br.ReadSingle() + offset[s]); } } step++; } for (int z = 0; z < nzone; z++) { for (int v = 0; v < varnum; v++) { float temp = 0; foreach (var id in subindex[z]) { temp += matrix[v][id]; } if (statFlag == NumericalDataType.Average) { stat[z][v][i] = temp / subindex[z].Length / intevals[i]; } else if (statFlag == NumericalDataType.Cumulative) { stat[z][v][i] = temp / subindex[z].Length; } } } } br.Close(); fs.Close(); return(stat); }
/// <summary> /// read 3D matrix from binary file with the given filename. 3D array: [variable][step][feature] /// </summary> /// <param name="filename"></param> /// <param name="flag">flag: 1 mean; 2 sum; </param> /// <param name="step"></param> /// <param name="varNames"></param> /// <param name="maxsteps"></param> /// <param name="scale"></param> /// <returns>3D array: [variable][step][feature]</returns> public float[][][] Read(string filename, int flag, int[] intevals, ref int step, ref string[] varNames, int maxsteps = -1, float scale = 1) { float[][][] acmatrix = null; step = 0; int feaNum = 0; FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs); int ninteval = intevals.Length; int varnum = br.ReadInt32(); varNames = new string[varnum]; for (int i = 0; i < varnum; i++) { int varname_len = br.ReadInt32(); varNames[i] = new string(br.ReadChars(varname_len)).Trim(); feaNum = br.ReadInt32(); } if (flag > 0) { acmatrix = new float[varnum][][]; float[][] derived = new float[varnum][]; for (int i = 0; i < varnum; i++) { acmatrix[i] = new float[ninteval][]; derived[i] = new float[feaNum]; for (int t = 0; t < ninteval; t++) { acmatrix[i][t] = new float[feaNum]; } } for (int n = 0; n < ninteval; n++) { MatrixExtension <float> .Set(derived, 0); for (int t = 0; t < intevals[n]; t++) { for (int s = 0; s < feaNum; s++) { for (int v = 0; v < varnum; v++) { //if(!(fs.Position == fs.Length)) //{ derived[v][s] += br.ReadSingle() * scale; //} } } } if (flag == 1) { for (int s = 0; s < feaNum; s++) { for (int v = 0; v < varnum; v++) { derived[v][s] = derived[v][s] / intevals[n]; } } } for (int s = 0; s < feaNum; s++) { for (int v = 0; v < varnum; v++) { acmatrix[v][n][s] = derived[v][s]; } } step++; if (maxsteps > 0) { if (step >= maxsteps) { break; } } } } br.Close(); fs.Close(); return(acmatrix); }
public override void Read(string filename = "") { if (filename == "") { filename = GetInputFile(this.Name); } if (File.Exists(filename)) { var grid = mfgrid; int layer = grid.ActualLayerCount; var upw = this; layer = 3; StreamReader sr = new StreamReader(filename); //Data Set 1: # ILPFCB, HDRY, NPLPF string newline = ReadCommet(sr); float[] fv = TypeConverterEx.Split <float>(newline, 4); upw.ILPFCB = (short)fv[0]; upw.HDRY = fv[1]; upw.NPLPF = (short)fv[2]; upw.IPHDRY = (short)fv[3]; //Data Set 2: # newline = sr.ReadLine(); upw.LAYTYP = TypeConverterEx.Split <int>(newline, layer); //Data Set 3: # newline = sr.ReadLine(); upw.LAYAVG = TypeConverterEx.Split <int>(newline, layer); //Data Set 4: # newline = sr.ReadLine(); upw.CHANI = TypeConverterEx.Split <int>(newline, layer); //Data Set 5: # newline = sr.ReadLine(); upw.LAYVKA = TypeConverterEx.Split <int>(newline, layer); //Data Set 6: # newline = sr.ReadLine(); upw.LAYWET = TypeConverterEx.Split <int>(newline, layer); //Data Set 7: # //grid.LPFProperty.HK = new LayeredMatrix<float>(layer); //grid.LPFProperty.HANI = new LayeredMatrix<float>(layer); //grid.LPFProperty.VKA = new LayeredMatrix<float>(layer); //grid.LPFProperty.SS = new LayeredMatrix<float>(layer); //grid.LPFProperty.SY = new LayeredMatrix<float>(layer); var hania = new float[layer]; MatrixExtension <float> .Set(hania, 1); var hk = new MatrixCube <float>(layer); var hani = new MatrixCube <float>(layer); var vka = new MatrixCube <float>(layer); var ss = new MatrixCube <float>(layer); var sy = new MatrixCube <float>(layer); var wetdry = new MatrixCube <float>(layer); Parameters["HK"].Value = hk; Parameters["HANI"].Value = hani; Parameters["VKA"].Value = vka; Parameters["SS"].Value = ss; Parameters["SY"].Value = sy; Parameters["WETDRY"].Value = wetdry; for (int l = 0; l < layer; l++) { ReadInternalMatrix(sr, hk, grid.RowCount, grid.ColumnCount, l); ReadInternalMatrix(sr, hani, grid.RowCount, grid.ColumnCount, l); ReadInternalMatrix(sr, vka, grid.RowCount, grid.ColumnCount, l); ReadInternalMatrix(sr, ss, grid.RowCount, grid.ColumnCount, l); if (upw.LAYTYP[l] != 0) { ReadInternalMatrix(sr, sy, grid.RowCount, grid.ColumnCount, l); } } sr.Close(); } }
public override void Load(object[] para) { if (para == null) { return; } var dt_domain = DotSpatial.Data.FeatureSet.Open(para[0].ToString()).DataTable; var dt_grid = DotSpatial.Data.FeatureSet.Open(para[1].ToString()).DataTable; dt_grid.DefaultView.Sort = para[2].ToString(); string hkField = para[3].ToString(); int[] ty = new int[] { 1, 1, 0, 0, 0 }; DataRow dr = dt_domain.Rows[0]; Parameters["LAYTYP"].Value = TypeConverterEx.Split <int>(dr["LAYTYP"].ToString()); Parameters["LAYTYP"].Value = ty; Parameters["LAYAVG"].Value = TypeConverterEx.Split <int>(dr["LAYAVG"].ToString()); Parameters["CHANI"].Value = TypeConverterEx.Split <int>(dr["CHANI"].ToString()); Parameters["LAYVKA"].Value = TypeConverterEx.Split <int>(dr["LAYVKA"].ToString()); Parameters["LAYWET"].Value = TypeConverterEx.Split <int>(dr["LAYWET"].ToString()); int layer = int.Parse(dr["NLayer"].ToString()); var hania = new float[layer]; MatrixExtension <float> .Set(hania, 1); var hk = new MatrixCube <float>(layer); var hani = new MatrixCube <float>(hania); var vka = new MatrixCube <float>(layer); var ss = new MatrixCube <float>(layer); var sy = new MatrixCube <float>(layer); var wetdry = new MatrixCube <float>(layer); Parameters["HK"].Value = hk; Parameters["HANI"].Value = hani; Parameters["VKA"].Value = vka; Parameters["SS"].Value = ss; Parameters["SY"].Value = sy; Parameters["WETDRY"].Value = wetdry; for (int l = 0; l < mfgrid.ActualLayerCount; l++) { hk.LayeredValues[l] = new float[mfgrid.RowCount, mfgrid.ColumnCount]; vka.LayeredValues[l] = new float[mfgrid.RowCount, mfgrid.ColumnCount]; ss.LayeredValues[l] = new float[mfgrid.RowCount, mfgrid.ColumnCount]; if (Parameters["LAYTYP"].IntValues[l] != 0) { sy.LayeredValues[l] = new float[mfgrid.RowCount, mfgrid.ColumnCount]; } if (Parameters["LAYTYP"].IntValues[l] != 0 && Parameters["LAYWET"].IntValues[l] != 0) { wetdry.LayeredValues[l] = new float[mfgrid.RowCount, mfgrid.ColumnCount]; } } int i = 0; foreach (var ac in mfgrid.Topology.ActiveCellLocation.Values) { hk.LayeredValues[0][ac[0], ac[1]] = (float)dt_grid.Rows[i][hkField]; i++; } CreateUnifiedLPF(); }