Parser.cs 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.IO;
  6. using WebApplication3.Models;
  7. using System.Globalization;
  8. using Microsoft.VisualBasic.FileIO;
  9. namespace WebApplication3.Clases
  10. {
  11. public class Parser
  12. {
  13. public WellData[] ParseWellData(string textFile)
  14. {
  15. if (File.Exists(textFile))
  16. {
  17. StreamReader file = new StreamReader(textFile);
  18. //WellData[] wellData = null;
  19. List<WellData> wellDataList = null;
  20. WellData wellData = null;
  21. string ln;
  22. int cont = 0;
  23. int index = 0;
  24. bool intervalSkipped = false;
  25. int finalBloque = 0;
  26. string[] values;
  27. while ((ln = file.ReadLine()) != null)
  28. {
  29. if (wellData == null)
  30. {
  31. //wellData = new WellData[int.Parse(ln)];
  32. wellDataList = new List<WellData>(int.Parse(ln));
  33. wellData = new WellData();
  34. cont++;
  35. continue;
  36. }
  37. if (!intervalSkipped)
  38. {
  39. switch (cont)
  40. {
  41. case 1:
  42. //wellData[index].ActiveFlag = (ln == "0") ? false : true;
  43. wellData.ActiveFlag = ln != "0";
  44. break;
  45. case 2:
  46. //wellData[index].WellName = ln;
  47. wellData.WellName = ln;
  48. break;
  49. case 3:
  50. //wellData[index].X = double.Parse(ln);
  51. wellData.X = ln;
  52. break;
  53. case 4:
  54. //wellData[index].Y = double.Parse(ln);
  55. wellData.Y = ln;
  56. break;
  57. case 5:
  58. //wellData[index].ZMin = ln;
  59. values = ln.Split(" ");
  60. wellData.ZMin = values[0].Replace(".", ",");
  61. break;
  62. case 6:
  63. //wellData[index].ZMax = ln;
  64. values = ln.Split(" ");
  65. wellData.ZMax = values[0].Replace(".", ",");
  66. break;
  67. case 7:
  68. //wellData[index].ScreenIntervals = int.Parse(ln);
  69. wellData.ScreenIntervals = int.Parse(ln);
  70. //cont += (wellData[index].ScreenIntervals * 2) - 1;
  71. intervalSkipped = true;
  72. //leemos tantas lineas hay como intervalos de pantalla (al ser 2 parametros por cada intervalo es nIntervalos * 2
  73. List<string> linesAux = new();
  74. for (int i = 0; i < wellData.ScreenIntervals * 2; i++)
  75. {
  76. linesAux.Add(file.ReadLine());
  77. cont++;
  78. }
  79. wellData.Bot = linesAux[0];
  80. wellData.Top = linesAux[1];
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. else
  87. {
  88. //if (wellData[index].PumpPeriods == 0)
  89. if (wellData.PumpPeriods == 0)
  90. {
  91. //wellData[index].PumpPeriods = int.Parse(ln);
  92. wellData.PumpPeriods = int.Parse(ln);
  93. //se calcula en que linea termina el bloque del pozo
  94. //finalBloque = cont + wellData[index].PumpPeriods + 1;
  95. finalBloque = cont + 1 + wellData.PumpPeriods * 3;
  96. }
  97. else
  98. {
  99. TimeSerieInterno ts = new TimeSerieInterno();
  100. //string aux = "";
  101. //Rate rate = new Rate();
  102. ts.Hora = ln;
  103. //rate.StartTime = float.Parse(ln.Replace('.', ','));
  104. ln = file.ReadLine();
  105. ts.HoraEnd = ln;
  106. cont++;
  107. //if(cont == finalBloque - 1)
  108. //{
  109. // aux = ln;
  110. //}
  111. ln = file.ReadLine();
  112. cont++;
  113. //Separamos la cadena por " "
  114. //TODO -> igual es un \t
  115. string[] exploded = ln.Split(" ");
  116. //Solo nos interesa el primer valor
  117. string value = exploded[0].Replace('.', ',');
  118. ts.Valor = value;
  119. ts.Info = ln.Replace(exploded[0] + ' ', "");
  120. //rate.Value = double.Parse(value);
  121. //wellData[index].Rates.Add(rate);
  122. wellData.Rates.Add(ts);
  123. //if(aux != "")
  124. //{
  125. // ts = new TimeSerieInterno();
  126. // ts.Hora = aux;
  127. // ts.Valor = value;
  128. // ts.Info = ln.Replace(exploded[0] + ' ', "");
  129. // wellData.Rates.Add(ts);
  130. //}
  131. }
  132. }
  133. cont++;
  134. //hemos llegado al final del primer bloque de pozo, preparamos para el siguiente
  135. if (finalBloque == cont)
  136. {
  137. intervalSkipped = false;
  138. index++;
  139. wellDataList.Add(wellData);
  140. wellData = new WellData();
  141. cont = 1;
  142. }
  143. }
  144. file.Close();
  145. return wellDataList.ToArray();
  146. }
  147. return null;
  148. }
  149. public RechargeData[] ParseRechargeData(string textFile,out string recharge_geometry)
  150. {
  151. List<RechargeData> recharges = new List<RechargeData>();
  152. RechargeData rechargeData = null;
  153. TimeSerieInterno timeSerie;
  154. string rechargeProperty="";
  155. recharge_geometry = "";
  156. if (File.Exists(textFile))
  157. {
  158. Console.WriteLine("File exist");
  159. string text = File.ReadAllText(textFile);
  160. string[] data = text.Split("\r\n\r\n");
  161. int layers = int.Parse(data[0][0].ToString());
  162. int blankLinePerBlock = layers + 1;
  163. int rechargeBlockInex = blankLinePerBlock * 2;
  164. Console.WriteLine(data[rechargeBlockInex]);
  165. string[] explode = data[rechargeBlockInex].Split("\r\n");
  166. for (int i = 1; i < explode.Length; i++)
  167. {
  168. string[] values = explode[i].Split(" ", StringSplitOptions.RemoveEmptyEntries);
  169. //Si hay cambio de propiedad se crea otro RechargeData
  170. if (rechargeProperty != values[0])
  171. {
  172. if (rechargeProperty != "")
  173. {
  174. recharges.Add(rechargeData);
  175. }
  176. rechargeData = new RechargeData();
  177. rechargeData.Nombre = "Recarga_" + values[0];
  178. rechargeProperty = values[0];
  179. //Al hacer el cambio
  180. i += 2;
  181. values = explode[i].Split(" ", StringSplitOptions.RemoveEmptyEntries);
  182. }
  183. timeSerie = new TimeSerieInterno();
  184. timeSerie.Hora = values[2];
  185. timeSerie.HoraEnd = values[3];
  186. timeSerie.Valor = values[1];
  187. timeSerie.Info = values[4] + " " + values[5];
  188. rechargeData.Rates.Add(timeSerie);
  189. ////se anyade el ultimo registro de tiempo ya que solo nos quedamos con la primera marca de tiempo de cada linea
  190. //if(i == explode.Length - 1)
  191. //{
  192. // timeSerie = new TimeSerieInterno();
  193. // timeSerie.Hora = values[3];
  194. // timeSerie.Valor = values[1];
  195. // timeSerie.Info = values[4] + " " + values[5];
  196. // rechargeData.Rates.Add(timeSerie);
  197. //}
  198. }
  199. recharge_geometry = data[rechargeBlockInex + layers];
  200. //Se anyade la ultima instancia de rechargeData ya que no se produce cambio en la ultima iteracion del bucle
  201. recharges.Add(rechargeData);
  202. return recharges.ToArray();
  203. }
  204. else
  205. {
  206. Console.WriteLine("File does not exist");
  207. }
  208. return null;
  209. }
  210. public List<string> ParseInFile(string textFile)
  211. {
  212. Console.WriteLine("File exist");
  213. string text = File.ReadAllText(textFile);
  214. List<string> lines = new();
  215. List<string> aux = new();
  216. //Introducimos todas las lineas
  217. aux.AddRange(text.Split("\r\n").ToList());
  218. //Se eliminan las referencias a ficheros que no son validos para el modelo
  219. aux.RemoveAll(x => x.Contains("WHS") || x.Contains("NDC") || x.Contains("CLB") || x == "");
  220. List<string> units = new();
  221. foreach (string row in aux)
  222. {
  223. if (!aux.Contains("LIST"))
  224. {
  225. string[] rowSplit = row.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  226. units.Add(rowSplit[1]);
  227. }
  228. }
  229. foreach (string row in aux)
  230. {
  231. string[] rowSplit = row.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  232. Console.WriteLine(rowSplit);
  233. if (rowSplit[0] == "LIST" && int.Parse(rowSplit[1]) < 10)
  234. {
  235. if (!units.Contains("11"))
  236. {
  237. rowSplit[1] = "11";
  238. }
  239. else if (!units.Contains("12"))
  240. {
  241. rowSplit[1] = "12";
  242. }
  243. else if (!units.Contains("13"))
  244. {
  245. rowSplit[1] = "13";
  246. }
  247. else if (!units.Contains("14"))
  248. {
  249. rowSplit[1] = "14";
  250. }
  251. }
  252. //Se tiene que parsear la 3a parte del string porque contiene la ruta absoluta del ordenador donde se han originado los ficheros
  253. List<string> ficheroSplit = rowSplit[2].Split("\\").ToList();
  254. rowSplit[2] = ficheroSplit.Last();
  255. lines.Add(rowSplit[0] + " " + rowSplit[1] + " " + rowSplit[2]);
  256. }
  257. return lines;
  258. }
  259. public ZoneBalance[] ParseZonesBalance(string textFile)
  260. {
  261. List<ZoneBalance> zonesOutput = new List<ZoneBalance>();
  262. string text = File.ReadAllText(textFile);
  263. string[] data = text.Split(" test \r\n \r\n", StringSplitOptions.RemoveEmptyEntries);
  264. List<string> data_aux = data[1].Split("\r\n").ToList();
  265. string zones = data_aux.Find(x => x.Contains("zones"));
  266. int index = data_aux.IndexOf(zones);
  267. string[] zone_names = data_aux[index + 1].Split(" ", StringSplitOptions.RemoveEmptyEntries);
  268. int zonesConectionPerZone = zone_names.Length - 1;
  269. Logger logger = Logger.GetInstance();
  270. for (int z = 2; z < data.Length; z++)
  271. {
  272. List<string> balanceData = data[z].Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();
  273. ZoneBalance zoneBalance = new ZoneBalance();
  274. string storage, constantHead, drains, recharge, total, percentDisparity;
  275. string auxFind, auxSplit, zoneConnectionLine;
  276. string[] split;
  277. split = balanceData[0].Trim().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  278. zoneBalance.Zone = split[4];
  279. zoneBalance.StressPeriod = int.Parse(split[12]);
  280. #region IN
  281. //buscamos el indice en el cual empieza el bloque de los datos de salida
  282. auxFind = balanceData.Find(x => x.Contains("IN:"));
  283. index = balanceData.IndexOf(auxFind);
  284. //el primer dado del bloque se encuentra 2 indices despues del obtenido
  285. index += 2;
  286. auxSplit = balanceData[index].Split("STORAGE =")[1];
  287. storage = auxSplit;
  288. index++;
  289. auxSplit = balanceData[index].Split("CONSTANT HEAD =")[1].Trim();
  290. constantHead = auxSplit;
  291. index++;
  292. auxSplit = balanceData[index].Split("DRAINS =")[1].Trim();
  293. drains = auxSplit;
  294. index++;
  295. auxSplit = balanceData[index].Split("RECHARGE =")[1].Trim();
  296. recharge = auxSplit;
  297. index++;
  298. //Zone connections
  299. for (int i = index; i < index + zonesConectionPerZone; i++)
  300. {
  301. zoneConnectionLine = balanceData[i].Trim();
  302. split = zoneConnectionLine.Split("to");
  303. ZoneConnection zoneConnection = new ZoneConnection();
  304. zoneConnection.From = split[0].Split("Zone")[1].Trim();
  305. zoneConnection.To = split[1].Split("=")[0].Trim();
  306. zoneConnection.Value = split[1].Split("=")[1].Trim();
  307. zoneBalance.In.ZoneConnections.Add(zoneConnection);
  308. }
  309. //Para el total hay que buscar el indice porque al haber fluctuacion entre zonas no podemos saber el indice fijo de la linea donde se encuentra de forma directa
  310. auxFind = balanceData.Find(x => x.Contains("Total IN"));
  311. index = balanceData.IndexOf(auxFind);
  312. auxSplit = balanceData[index].Split("Total IN =")[1].Trim();
  313. total = auxSplit;
  314. zoneBalance.In.Storage = storage;
  315. zoneBalance.In.ConstantHead = constantHead;
  316. zoneBalance.In.Drains = drains;
  317. zoneBalance.In.Recharge = recharge;
  318. zoneBalance.In.Total = total;
  319. #endregion
  320. #region OUT
  321. //buscamos el indice en el cual empieza el bloque de los datos de salida
  322. auxFind = balanceData.Find(x => x.Contains("OUT:"));
  323. index = balanceData.IndexOf(auxFind);
  324. //el primer dado del bloque se encuentra 2 indices despues del obtenido
  325. index += 2;
  326. auxSplit = balanceData[index].Split("STORAGE =")[1];
  327. storage = auxSplit;
  328. index++;
  329. auxSplit = balanceData[index].Split("CONSTANT HEAD =")[1].Trim();
  330. constantHead = auxSplit;
  331. index++;
  332. auxSplit = balanceData[index].Split("DRAINS =")[1].Trim();
  333. drains = auxSplit;
  334. index++;
  335. auxSplit = balanceData[index].Split("RECHARGE =")[1].Trim();
  336. recharge = auxSplit;
  337. index++;
  338. //Zone connections
  339. for (int i = index; i < index + zonesConectionPerZone; i++)
  340. {
  341. zoneConnectionLine = balanceData[i].Trim();
  342. split = zoneConnectionLine.Split("to");
  343. ZoneConnection zoneConnection = new ZoneConnection();
  344. zoneConnection.From = split[0].Split("Zone")[1].Trim();
  345. zoneConnection.To = split[1].Split("=")[0].Trim();
  346. zoneConnection.Value = split[1].Split("=")[1].Trim();
  347. zoneBalance.Out.ZoneConnections.Add(zoneConnection);
  348. }
  349. //Para el total hay que buscar el indice porque al haber fluctuacion entre zonas no podemos saber el indice fijo de la linea donde se encuentra de forma directa
  350. auxFind = balanceData.Find(x => x.Contains("Total OUT"));
  351. index = balanceData.IndexOf(auxFind);
  352. auxSplit = balanceData[index].Split("Total OUT =")[1].Trim();
  353. total = auxSplit;
  354. zoneBalance.Out.Storage = storage;
  355. zoneBalance.Out.ConstantHead = constantHead;
  356. zoneBalance.Out.Drains = drains;
  357. zoneBalance.Out.Recharge = recharge;
  358. zoneBalance.Out.Total = total;
  359. #endregion
  360. auxFind = balanceData.Find(x => x.Contains("IN - OUT"));
  361. index = balanceData.IndexOf(auxFind);
  362. auxSplit = balanceData[index].Split("IN - OUT = ")[1].Trim();
  363. total = auxSplit;
  364. auxFind = balanceData.Find(x => x.Contains("Percent Discrepancy = "));
  365. index = balanceData.IndexOf(auxFind);
  366. auxSplit = balanceData[index].Split("Percent Discrepancy = ")[1].Trim();
  367. percentDisparity = auxSplit;
  368. zoneBalance.TotalInOut = total;
  369. zoneBalance.PercentDiscrepancy = percentDisparity;
  370. zonesOutput.Add(zoneBalance);
  371. }
  372. return zonesOutput.ToArray();
  373. }
  374. public ZoneBalance[] ParseWholeModelBalance(string textfile)
  375. {
  376. string text = File.ReadAllText(textfile);
  377. string[] data = text.Split(" DRAWDOWN WILL BE SAVED ON UNIT 151 AT END OF TIME STEP 10, STRESS PERIOD", StringSplitOptions.RemoveEmptyEntries);
  378. List<ZoneBalance> zoneBalances = new List<ZoneBalance>();
  379. for (int i = 1; i < data.Length; i++)
  380. {
  381. ZoneBalance cummulative = new ZoneBalance();
  382. ZoneBalance current = new ZoneBalance();
  383. cummulative.Zone = "Zone 0";
  384. cummulative.StressPeriod = i;
  385. cummulative.Type = 1;
  386. current.Zone = "Zone 0";
  387. current.StressPeriod = i;
  388. current.Type = 2;
  389. List<string> data_aux = data[i].Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();
  390. string[] split = data_aux[2].Split("STRESS PERIOD", StringSplitOptions.RemoveEmptyEntries);
  391. #region IN
  392. split = data_aux[8].Split("STORAGE =", StringSplitOptions.RemoveEmptyEntries);
  393. cummulative.In.Storage = split[1].Trim();
  394. current.In.Storage = split[2].Trim();
  395. split = data_aux[9].Split("CONSTANT HEAD =", StringSplitOptions.RemoveEmptyEntries);
  396. cummulative.In.ConstantHead = split[1].Trim();
  397. current.In.ConstantHead = split[2].Trim();
  398. if (data_aux[10].Contains("DRAINS"))
  399. {
  400. split = data_aux[10].Split("DRAINS =", StringSplitOptions.RemoveEmptyEntries);
  401. cummulative.In.Drains = split[1].Trim();
  402. current.In.Drains = split[2].Trim();
  403. }
  404. else
  405. {
  406. split = data_aux[10].Split("WELLS =", StringSplitOptions.RemoveEmptyEntries);
  407. cummulative.In.Wells = split[1].Trim();
  408. current.In.Wells = split[2].Trim();
  409. }
  410. split = data_aux[11].Split("RECHARGE =", StringSplitOptions.RemoveEmptyEntries);
  411. cummulative.In.Recharge = split[1].Trim();
  412. current.In.Recharge = split[2].Trim();
  413. split = data_aux[13].Split("TOTAL IN =", StringSplitOptions.RemoveEmptyEntries);
  414. cummulative.In.Total = split[1].Trim();
  415. current.In.Total = split[2].Trim();
  416. #endregion
  417. #region OUT
  418. split = data_aux[16].Split("STORAGE =", StringSplitOptions.RemoveEmptyEntries);
  419. cummulative.Out.Storage = split[1].Trim();
  420. current.Out.Storage = split[2].Trim();
  421. split = data_aux[17].Split("CONSTANT HEAD =", StringSplitOptions.RemoveEmptyEntries);
  422. cummulative.Out.ConstantHead = split[1].Trim();
  423. current.Out.ConstantHead = split[2].Trim();
  424. if (data_aux[18].Contains("DRAINS"))
  425. {
  426. split = data_aux[18].Split("DRAINS =", StringSplitOptions.RemoveEmptyEntries);
  427. cummulative.Out.Drains = split[1].Trim();
  428. current.Out.Drains = split[2].Trim();
  429. }
  430. else
  431. {
  432. split = data_aux[18].Split("WELLS =", StringSplitOptions.RemoveEmptyEntries);
  433. cummulative.Out.Wells = split[1].Trim();
  434. current.Out.Wells = split[2].Trim();
  435. }
  436. split = data_aux[19].Split("RECHARGE =", StringSplitOptions.RemoveEmptyEntries);
  437. cummulative.Out.Recharge = split[1].Trim();
  438. current.Out.Recharge = split[2].Trim();
  439. split = data_aux[21].Split("TOTAL OUT =", StringSplitOptions.RemoveEmptyEntries);
  440. cummulative.Out.Total = split[1].Trim();
  441. current.Out.Total = split[2].Trim();
  442. #endregion
  443. split = data_aux[22].Split("IN - OUT =", StringSplitOptions.RemoveEmptyEntries);
  444. cummulative.TotalInOut = split[1].Trim();
  445. current.TotalInOut = split[2].Trim();
  446. split = data_aux[23].Split("PERCENT DISCREPANCY =", StringSplitOptions.RemoveEmptyEntries);
  447. cummulative.PercentDiscrepancy = split[1].Trim();
  448. current.PercentDiscrepancy = split[2].Trim();
  449. zoneBalances.Add(cummulative);
  450. zoneBalances.Add(current);
  451. }
  452. return zoneBalances.ToArray();
  453. }
  454. public List<Piezometria> ParseBinaryFile(string file, uint? simulacionId)
  455. {
  456. List<Piezometria> piezometria = new List<Piezometria>();
  457. Logger.WriteLog("Parseando fichero binario");
  458. if (File.Exists(file))
  459. {
  460. try
  461. {
  462. using (FileStream fs = new FileStream(file, FileMode.Open))
  463. using (BinaryReader brr = new BinaryReader(fs))
  464. {
  465. Logger.WriteLog("Fichero existente y abierto");
  466. while (fs.Length != fs.Position) // This will throw an exception for non-seekable streams (stream.CanSeek == false), but filestreams are seekable so it's OK here
  467. {
  468. Piezometria p = new();
  469. p.IdSimulacion = simulacionId;
  470. int KSTP = brr.ReadInt32();
  471. int KPER = brr.ReadInt32();
  472. var PERTIM = brr.ReadSingle();
  473. var TOTIM = brr.ReadSingle();
  474. char[] auxDESC = brr.ReadChars(16);
  475. string DESC = new(auxDESC);
  476. int NCOL = brr.ReadInt32();
  477. int NROW = brr.ReadInt32();
  478. int ILAY = brr.ReadInt32();
  479. p.Layer = ILAY;
  480. p.StressPeriod = KPER;
  481. p.Rows = NROW;
  482. p.Columns = NCOL;
  483. List<float> values = new();
  484. for (int i = 0; i < NROW * NCOL; i++)
  485. {
  486. values.Add(brr.ReadSingle());
  487. }
  488. int c = 1;
  489. string original = "";
  490. string convertida = "";
  491. foreach (float v in values)
  492. {
  493. if (c < NCOL)
  494. {
  495. original += v + " ";
  496. //Check for range + color conversion
  497. //convertida += Constants.piezometry_conversion[v.ToString()] + ",";
  498. }
  499. else if (c == NCOL)
  500. {
  501. //Add \n to the string
  502. original += "\n";
  503. //convertida += "\n";
  504. c = 0;
  505. }
  506. c++;
  507. }
  508. p.Original = original;
  509. p.Convertida = convertida;
  510. piezometria.Add(p);
  511. }
  512. Logger.WriteLog("Ya se ha leido el fichero");
  513. }
  514. }catch(Exception ex)
  515. {
  516. Logger.WriteLog(ex.Message);
  517. Logger.WriteLog(ex.InnerException.Message);
  518. }
  519. }
  520. return piezometria;
  521. }
  522. public void GetGeolocalizationFromFile(string file,out string worldX,out string worldY, out string angle, out string length, out string height)
  523. {
  524. string[] world_origin_x, world_origin_y, modelAngle, modelLength, modelHeight;
  525. worldX = worldY = angle = length = height = "";
  526. string text = File.ReadAllText(file);
  527. List<string> data = text.Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();
  528. //string geolocationData = data[2];
  529. string geolocation_line = data.Find(x => x.Contains("<Georeference"));
  530. List<string> geolocation_data = geolocation_line.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList();
  531. string wox = geolocation_data.Find(x => x.Contains("World_Origin_X="));
  532. if(wox != null)
  533. {
  534. world_origin_x = wox.Split("World_Origin_X=", StringSplitOptions.RemoveEmptyEntries);
  535. worldX = world_origin_x[0].Replace("\"", string.Empty);
  536. }
  537. string woy = geolocation_data.Find(x => x.Contains("World_Origin_Y="));
  538. if (wox != null)
  539. {
  540. world_origin_y = woy.Split("World_Origin_Y=", StringSplitOptions.RemoveEmptyEntries);
  541. worldY = world_origin_y[0].Replace("\"", string.Empty);
  542. }
  543. string magl = geolocation_data.Find(x => x.Contains("Angle="));
  544. if (magl != null)
  545. {
  546. modelAngle = magl.Split("Angle=", StringSplitOptions.RemoveEmptyEntries);
  547. angle = modelAngle[0].Replace("\"", string.Empty);
  548. }
  549. string mlgth = geolocation_data.Find(x => x.Contains("Length="));
  550. if (mlgth != null)
  551. {
  552. modelLength = mlgth.Split("Length=", StringSplitOptions.RemoveEmptyEntries);
  553. length = modelLength[0].Replace("\"", string.Empty);
  554. }
  555. string mhei = geolocation_data.Find(x => x.Contains("Height="));
  556. if (mhei != null)
  557. {
  558. modelHeight = mhei.Split("Height=", StringSplitOptions.RemoveEmptyEntries);
  559. height = modelHeight[0].Replace("\"", string.Empty);
  560. height = height.Replace("/>", string.Empty);
  561. }
  562. }
  563. public WellData[] parseWellCSV(string file)
  564. {
  565. List<WellData> wellsData = new();
  566. WellData wellData = null;
  567. using (TextFieldParser csvParser = new TextFieldParser(file))
  568. {
  569. csvParser.CommentTokens = new string[] { "#" };
  570. csvParser.SetDelimiters(new string[] { "," });
  571. csvParser.HasFieldsEnclosedInQuotes = true;
  572. // Skip the row with the column names
  573. csvParser.ReadLine();
  574. string wellName = "";
  575. string[] aux = Array.Empty<string>();
  576. TimeSerieInterno tsInterno;
  577. int pumpPeriods = 0;
  578. while (!csvParser.EndOfData)
  579. {
  580. // Read current line fields, pointer moves to the next line.
  581. string[] fields = csvParser.ReadFields();
  582. if (wellName != fields[0])
  583. {
  584. if (wellName == "")
  585. {
  586. aux = new string[fields.Length];
  587. }
  588. else
  589. {
  590. wellData.PumpPeriods = pumpPeriods;
  591. wellsData.Add(wellData);
  592. tsInterno = new();
  593. tsInterno.Hora = aux[11];
  594. tsInterno.Valor = aux[12];
  595. wellData.Rates.Add(tsInterno);
  596. pumpPeriods = 0;
  597. }
  598. wellData = new();
  599. wellData.WellName = fields[0];
  600. wellData.X = fields[1];
  601. wellData.Y = fields[2];
  602. wellData.Top = fields[5];
  603. wellData.Bot = fields[6];
  604. wellData.ZMin = fields[4];
  605. wellData.ZMax = fields[3];
  606. wellData.ScreenIntervals = 1;
  607. //wellData.ZMin = "";
  608. //wellData.ZMax = "";
  609. wellData.ActiveFlag = false;
  610. wellName = fields[0];
  611. }
  612. tsInterno = new ();
  613. tsInterno.Hora = fields[10];
  614. tsInterno.Valor = fields[12];
  615. wellData.Rates.Add(tsInterno);
  616. fields.CopyTo(aux, 0);
  617. pumpPeriods++;
  618. }
  619. wellData.PumpPeriods = pumpPeriods;
  620. tsInterno = new();
  621. tsInterno.Hora = aux[11];
  622. tsInterno.Valor = aux[12];
  623. wellData.Rates.Add(tsInterno);
  624. wellsData.Add(wellData);
  625. }
  626. return wellsData.ToArray();
  627. }
  628. public RechargeData[] parseRechargeCSV(string file)
  629. {
  630. List<RechargeData> rechargeDatas = new();
  631. RechargeData rechargeData = new();
  632. TimeSerieInterno ts;
  633. string[] aux = Array.Empty<string>();
  634. using (TextFieldParser csvParser = new TextFieldParser(file))
  635. {
  636. csvParser.CommentTokens = new string[] { "#" };
  637. csvParser.SetDelimiters(new string[] { "," });
  638. csvParser.HasFieldsEnclosedInQuotes = true;
  639. // Skip the row with the column names
  640. csvParser.ReadLine();
  641. while (!csvParser.EndOfData)
  642. {
  643. // Read current line fields, pointer moves to the next line.
  644. string[] fields = csvParser.ReadFields();
  645. if(rechargeData.Nombre == null)
  646. {
  647. rechargeData.Nombre = fields[0];
  648. aux = new string[fields.Length];
  649. }
  650. ts = new();
  651. ts.Valor = fields[3];
  652. ts.Hora = fields[1];
  653. //ts.HoraEnd = fields[2];
  654. rechargeData.Rates.Add(ts);
  655. fields.CopyTo(aux, 0);
  656. }
  657. ts = new();
  658. ts.Valor = aux[3];
  659. ts.Hora = aux[2];
  660. //ts.HoraEnd = fields[2];
  661. rechargeData.Rates.Add(ts);
  662. rechargeDatas.Add(rechargeData);
  663. }
  664. return rechargeDatas.ToArray();
  665. }
  666. public PiezometerData[] parsePiezometerCSV(string file)
  667. {
  668. List<PiezometerData> piezometerDatas = new();
  669. PiezometerData piezometerData = null;
  670. TimeSerieInterno ts;
  671. string[] aux = Array.Empty<string>();
  672. string piezometerName = "";
  673. using (TextFieldParser csvParser = new TextFieldParser(file))
  674. {
  675. csvParser.CommentTokens = new string[] { "#" };
  676. csvParser.SetDelimiters(new string[] { "," });
  677. csvParser.HasFieldsEnclosedInQuotes = true;
  678. // Skip the row with the column names
  679. csvParser.ReadLine();
  680. while (!csvParser.EndOfData)
  681. {
  682. // Read current line fields, pointer moves to the next line.
  683. string[] fields = csvParser.ReadFields();
  684. if (piezometerName != fields[0])
  685. {
  686. if(piezometerName == "")
  687. {
  688. aux = new string[fields.Length];
  689. }else
  690. {
  691. }
  692. piezometerData = new();
  693. piezometerData.Name = fields[0];
  694. piezometerData.X = fields[1];
  695. piezometerData.Y = fields[2];
  696. piezometerData.Z = fields[3];
  697. piezometerData.Prof = fields[4];
  698. piezometerData.Screen_top = fields[5];
  699. piezometerData.Screen_bot = fields[6];
  700. }
  701. ts = new();
  702. ts.DateIn = fields[8];
  703. ts.Hora = fields[9];
  704. ts.Valor = fields[10];
  705. piezometerData.Rates.Add(ts);
  706. //fields.CopyTo(aux, 0);
  707. }
  708. }
  709. return piezometerDatas.ToArray();
  710. }
  711. public bool ModflowError(string file)
  712. {
  713. string text = File.ReadAllText(file).ToLower();
  714. if (!text.Contains("error"))
  715. {
  716. return false;
  717. }
  718. return true;
  719. }
  720. }
  721. }