| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Text; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using System.Xml; |
| | 6 | |
|
| | 7 | | namespace MorphoReader |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Read class. |
| | 11 | | /// </summary> |
| | 12 | | class Read |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Binary information. |
| | 16 | | /// </summary> |
| 2 | 17 | | public Dictionary<string, string> Information { get; private set; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Create a new read object. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="path">Full path of the EDT file.</param> |
| 1 | 23 | | public Read(string path) |
| 1 | 24 | | { |
| 1 | 25 | | Information = GetDictionaryFromXml(path); |
| 1 | 26 | | } |
| | 27 | |
|
| | 28 | | private string ReadEdxFile(string path) |
| 1 | 29 | | { |
| 1 | 30 | | string characters = @"[^\s()_<>/,\.A-Za-z0-9=""\P{IsBasicLatin}\p{IsLatin-1Supplement}]+"; |
| | 31 | |
|
| 1 | 32 | | var isoLatin1 = Encoding.GetEncoding(28591); |
| 1 | 33 | | if (!System.IO.File.Exists(path)) |
| 0 | 34 | | throw new Exception($"{path} not found."); |
| 1 | 35 | | string text = System.IO.File.ReadAllText(path, isoLatin1); |
| 1 | 36 | | string res = Regex.Replace(text, characters, ""); |
| | 37 | |
|
| 1 | 38 | | return res.Replace("<Remark for this Source Type>", ""); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | private static string GetValueFromXml(XmlNodeList nodeList, |
| | 42 | | string keyword) |
| 12 | 43 | | { |
| | 44 | | // It must be just 1 |
| 48 | 45 | | foreach (XmlNode child in nodeList) |
| 12 | 46 | | { |
| 12 | 47 | | return child.SelectSingleNode(keyword).InnerText; |
| | 48 | | } |
| 0 | 49 | | return null; |
| 12 | 50 | | } |
| | 51 | |
|
| | 52 | | private Dictionary<string, string> GetDictionaryFromXml(string path) |
| 1 | 53 | | { |
| 1 | 54 | | Dictionary<string, string> values = new Dictionary<string, string>(); |
| 1 | 55 | | XmlDocument xml = new XmlDocument(); |
| | 56 | |
|
| 1 | 57 | | var text = ReadEdxFile(path); |
| 1 | 58 | | xml.LoadXml(text); |
| | 59 | |
|
| 1 | 60 | | XmlNodeList modeldescription = xml.DocumentElement.SelectNodes("modeldescription"); |
| 1 | 61 | | string projectName = GetValueFromXml(modeldescription, "projectname"); |
| 1 | 62 | | string simulationDate = GetValueFromXml(modeldescription, "simulation_date") |
| 2 | 63 | | .Replace(" ", ""); ; |
| 1 | 64 | | string simulationTime = GetValueFromXml(modeldescription, "simulation_time") |
| 2 | 65 | | .Replace(" ", ""); ; |
| 1 | 66 | | string locationName = GetValueFromXml(modeldescription, "locationname"); |
| | 67 | |
|
| 1 | 68 | | XmlNodeList variables = xml.DocumentElement.SelectNodes("variables"); |
| 1 | 69 | | string nameVariables = GetValueFromXml(variables, "name_variables"); |
| | 70 | |
|
| | 71 | |
|
| 1 | 72 | | XmlNodeList datadescription = xml.DocumentElement.SelectNodes("datadescription"); |
| 1 | 73 | | string dateContent = GetValueFromXml(datadescription, "data_content"); |
| 1 | 74 | | string spacingX = GetValueFromXml(datadescription, "spacing_x") |
| 1 | 75 | | .Replace(" ", ""); |
| 1 | 76 | | string spacingY = GetValueFromXml(datadescription, "spacing_y") |
| 1 | 77 | | .Replace(" ", ""); |
| 1 | 78 | | string spacingZ = GetValueFromXml(datadescription, "spacing_z") |
| 1 | 79 | | .Replace(" ", ""); |
| 1 | 80 | | string nrXdata = GetValueFromXml(datadescription, "nr_xdata") |
| 1 | 81 | | .Replace(" ", ""); |
| 1 | 82 | | string nrYdata = GetValueFromXml(datadescription, "nr_ydata") |
| 1 | 83 | | .Replace(" ", ""); |
| 1 | 84 | | string nrZdata = GetValueFromXml(datadescription, "nr_zdata") |
| 1 | 85 | | .Replace(" ", ""); |
| | 86 | |
|
| 1 | 87 | | values.Add("projectname", projectName); |
| 1 | 88 | | values.Add("locationname", locationName); |
| 1 | 89 | | values.Add("simulation_date", simulationDate); |
| 1 | 90 | | values.Add("simulation_time", simulationTime); |
| | 91 | |
|
| 1 | 92 | | values.Add("data_content", dateContent); |
| | 93 | |
|
| 1 | 94 | | values.Add("spacing_x", spacingX); |
| 1 | 95 | | values.Add("spacing_y", spacingY); |
| 1 | 96 | | values.Add("spacing_z", spacingZ); |
| | 97 | |
|
| 1 | 98 | | values.Add("nr_xdata", nrXdata); |
| 1 | 99 | | values.Add("nr_ydata", nrYdata); |
| 1 | 100 | | values.Add("nr_zdata", nrZdata); |
| | 101 | |
|
| 1 | 102 | | values.Add("name_variables", nameVariables); |
| | 103 | |
|
| 1 | 104 | | return values; |
| 1 | 105 | | } |
| | 106 | | } |
| | 107 | | } |