PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Tools/HydroForecaster/LagTool.cs

#
C# | 368 lines | 233 code | 92 blank | 43 comment | 18 complexity | 90d20ed6e77c49fd678e6d5c2841722e MD5 | raw file
Possible License(s): MIT
  1. //********************************************************************************************************
  2. // Product Name: MapWindowTools.LagTool
  3. // Description: This tool is used to get the data from SQLITE database.
  4. //
  5. //********************************************************************************************************
  6. // The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
  7. // you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8. // http://www.mozilla.org/MPL/
  9. //
  10. // Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
  11. // ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and
  12. // limitations under the License.
  13. //
  14. // The Original Code is Toolbox.dll for the MapWindow 4.6/6 ToolManager project
  15. //
  16. // The Initializeializeial Developer of this Original Code is Teva Veluppillai. Created in 2010 March.
  17. //
  18. // Contributor(s): (Open source contributors should list themselves and their modifications here).
  19. //
  20. //********************************************************************************************************
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Spatial.Tools;
  26. using System.Spatial.Tools.Param;
  27. using System.Data.OleDb;
  28. using System.IO;
  29. using System.Data;
  30. using System.Text.RegularExpressions;
  31. using System.Collections;
  32. namespace MapWindowTools.Analysis
  33. {
  34. class LagTool : ITool
  35. {
  36. private Parameter[] _inputParameters;
  37. private Parameter[] _outputParameters;
  38. private string _workingPath;
  39. private DataTable tblDataTable;
  40. #region Private Methods
  41. private DataTable ParseCSV(string inputString)
  42. {
  43. DataTable dt = new DataTable();
  44. // declare the Regular Expression that will match versus the input string
  45. Regex re = new Regex("((?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")+)\")(,|(?<rowbreak>\\r\\n|\\n|$))");
  46. ArrayList colArray = new ArrayList();
  47. ArrayList rowArray = new ArrayList();
  48. int colCount = 0;
  49. int maxColCount = 0;
  50. string rowbreak = "";
  51. string field = "";
  52. MatchCollection mc = re.Matches(inputString);
  53. foreach (Match m in mc)
  54. {
  55. // retrieve the field and replace two double-quotes with a single double-quote
  56. field = m.Result("${field}").Replace("\"\"", "\"");
  57. rowbreak = m.Result("${rowbreak}");
  58. if (field.Length > 0)
  59. {
  60. colArray.Add(field);
  61. colCount++;
  62. }
  63. if (rowbreak.Length > 0)
  64. {
  65. // add the column array to the row Array List
  66. rowArray.Add(colArray.ToArray());
  67. // create a new Array List to hold the field values
  68. colArray = new ArrayList();
  69. if (colCount > maxColCount)
  70. maxColCount = colCount;
  71. colCount = 0;
  72. }
  73. }
  74. if (rowbreak.Length == 0)
  75. {
  76. // this is executed when the last line doesn't
  77. // end with a line break
  78. rowArray.Add(colArray.ToArray());
  79. if (colCount > maxColCount)
  80. maxColCount = colCount;
  81. }
  82. // convert the row Array List into an Array object for easier access
  83. Array ra = rowArray.ToArray();
  84. Array ss;
  85. for (int t = 0; t < 1; t++)
  86. {
  87. // convert the column Array List into an Array object for easier access
  88. ss = (Array)(ra.GetValue(t));
  89. // create the columns for the table
  90. for (int i = 0; i < ss.Length; i++)
  91. dt.Columns.Add(ss.GetValue(i).ToString());
  92. }
  93. for (int i = 1; i < ra.Length; i++)
  94. {
  95. // create a new DataRow
  96. DataRow dr = dt.NewRow();
  97. // convert the column Array List into an Array object for easier access
  98. Array ca = (Array)(ra.GetValue(i));
  99. // add each field into the new DataRow
  100. for (int j = 0; j < ca.Length; j++)
  101. dr[j] = ca.GetValue(j);
  102. // add the new DataRow to the DataTable
  103. dt.Rows.Add(dr);
  104. }
  105. // in case no data was parsed, create a single column
  106. if (dt.Columns.Count == 0)
  107. dt.Columns.Add("NoData");
  108. return dt;
  109. }
  110. private DataTable ParseCSVFile(string path)
  111. {
  112. string inputString = "";
  113. // check that the file exists before opening it
  114. if (File.Exists(path))
  115. {
  116. StreamReader sr = new StreamReader(path);
  117. inputString = sr.ReadToEnd();
  118. sr.Close();
  119. }
  120. return ParseCSV(inputString);
  121. }
  122. private void DataTable2CSV(DataTable table, string filename, string seperateChar)
  123. {
  124. StreamWriter sr = null;
  125. try
  126. {
  127. sr = new StreamWriter(filename);
  128. string seperator = "";
  129. StringBuilder builder = new StringBuilder();
  130. foreach (DataColumn col in table.Columns)
  131. {
  132. builder.Append(seperator).Append(col.ColumnName);
  133. seperator = seperateChar;
  134. }
  135. sr.WriteLine(builder.ToString());
  136. foreach (DataRow row in table.Rows)
  137. {
  138. seperator = "";
  139. builder = new StringBuilder();
  140. foreach (DataColumn col in table.Columns)
  141. {
  142. builder.Append(seperator).Append(row[col.ColumnName]);
  143. seperator = seperateChar;
  144. }
  145. sr.WriteLine(builder.ToString());
  146. }
  147. }
  148. finally
  149. {
  150. if (sr != null)
  151. {
  152. sr.Close();
  153. }
  154. }
  155. }
  156. #endregion
  157. #region ITool Members
  158. public string Author
  159. {
  160. get { return "Lag Tool"; }
  161. }
  162. public string Category
  163. {
  164. get { return "Forecasting"; }
  165. }
  166. public string Description
  167. {
  168. get { return "Forecasting Lag Tool"; }
  169. }
  170. public bool Execute(ICancelProgressHandler cancelProgressHandler)
  171. {
  172. string fileName = _inputParameters[0].Value.ToString();
  173. string destinationfileName = _outputParameters[0].Value.ToString();
  174. Execute(fileName, destinationfileName, cancelProgressHandler);
  175. return true;
  176. }
  177. public bool Execute(string CSVFilePath, string CSVDestination, ICancelProgressHandler cancelProgressHandler)
  178. {
  179. tblDataTable = ParseCSVFile(CSVFilePath);
  180. DataTable LagValueTable = new DataTable();
  181. LagValueTable = LagTable(tblDataTable);
  182. DataTable2CSV(LagValueTable, CSVDestination, ",");
  183. for (int j = 0; j < LagValueTable.Rows.Count; j++)
  184. {
  185. cancelProgressHandler.Progress("", Convert.ToInt32((Convert.ToDouble(j) / Convert.ToDouble(LagValueTable.Rows.Count)) * 100), LagValueTable.Rows[j][0].ToString() + ":" + LagValueTable.Rows[j][1].ToString());
  186. if (cancelProgressHandler.Cancel)
  187. return false;
  188. }
  189. return true;
  190. }
  191. public DataTable LagTable(DataTable dt)
  192. {
  193. DataTable dtLagTable = new DataTable();
  194. dtLagTable.Columns.Add("Date");
  195. dtLagTable.Columns.Add("t");
  196. dtLagTable.Columns.Add("t-1");
  197. //dtLagTable.Columns.Add("t-2");
  198. //dtLagTable.Columns.Add("t-3");
  199. //dtLagTable.Columns.Add("t-4");
  200. //dtLagTable.Columns.Add("t-5");
  201. //dtLagTable.Columns.Add("t-6");
  202. DataRow SiteId = dtLagTable.NewRow();
  203. SiteId[0] = (Convert.ToDouble(dt.Rows[0][0])); //site Id
  204. SiteId[1] = (Convert.ToDouble(dt.Rows[0][1])); //Variable Id
  205. SiteId[2] = 0; //this is dummy value
  206. dtLagTable.Rows.Add(SiteId);
  207. //DataRow VariableId = dtLagTable.NewRow();
  208. //VariableId[0] = (Convert.ToDouble(dt.Rows[2][1]));
  209. //dtLagTable.Rows.Add(SiteId);
  210. for (int l = 1; l < dt.Rows.Count; l++)
  211. {
  212. DataRow dr = dtLagTable.NewRow();
  213. dr[0] = dt.Rows[l][0];
  214. dr[1] = (Convert.ToDouble(dt.Rows[l][1]));
  215. dr[2] = (Convert.ToDouble(dt.Rows[l - 1][1]));
  216. dtLagTable.Rows.Add(dr);
  217. }
  218. return dtLagTable;
  219. }
  220. public System.Drawing.Bitmap HelpImage
  221. {
  222. get { return null; }
  223. }
  224. public string HelpText
  225. {
  226. get { return "This tool helps to get the lag value based on the input data source."; }
  227. }
  228. public string HelpUrl
  229. {
  230. get { return "LagTool"; }
  231. }
  232. public System.Drawing.Bitmap Icon
  233. {
  234. get { return null; }
  235. }
  236. public void Initialize()
  237. {
  238. _inputParameters = new Parameter[1];
  239. _inputParameters[0] = new FileParam("Select the Data table");
  240. _outputParameters = new Parameter[1];
  241. _outputParameters[0] = new FileParam("Save Lag Values");
  242. }
  243. public Parameter[] InputParameters
  244. {
  245. get { return _inputParameters; }
  246. }
  247. public string Name
  248. {
  249. get { return "Lag Tool"; }
  250. }
  251. public Parameter[] OutputParameters
  252. {
  253. get { return _outputParameters; }
  254. }
  255. public void ParameterChanged(Parameter sender)
  256. {
  257. }
  258. public string ToolTip
  259. {
  260. get { return "Lag Tool"; }
  261. }
  262. public string UniqueName
  263. {
  264. get { return "Lag Tool"; }
  265. }
  266. public Version Version
  267. {
  268. get { return (new Version(1, 0, 0, 0)); }
  269. }
  270. public string WorkingPath
  271. {
  272. set { _workingPath = value; }
  273. }
  274. #endregion
  275. }
  276. }