SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
JournalDataExtractor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.XPath;
7 using System.Xml.Xsl;
8 using System.IO;
9 using System.Text.RegularExpressions;
10 
11 namespace SeciControls
12 {
13  public class Entry
14  {
15  public string RunNumber { get; private set; }
16  public string Title { get; private set; }
17  public string Start { get; private set; }
18  public string Duration { get; private set; }
19  public string Uamps { get; private set; }
20  public string Mev { get; private set; }
21  public string Users { get; private set; }
22  public string RB { get; private set; }
23 
24  public Entry(String run, String title, String start, String duration, String uamps, String mev, String users, String rb)
25  {
26  RunNumber = run;
27  Title = title;
28  Start = start;
29  Duration = duration;
30  Uamps = uamps;
31  Mev = mev;
32  Users = users;
33  RB = rb;
34  }
35  }
36 
38  {
39  FileInfo _journal;
40  DateTime _lastModified = new DateTime();
41 
42  public Boolean GetCurrentCycleData(String journalDir, String mainFileName, out List<Entry> entries)
43  {
44  entries = new List<Entry>();
45 
46  //Get the most recent cycle from journal_main.xml
47  XmlDocument mainXml = new XmlDocument();
48  mainXml.Load(journalDir + "\\" + mainFileName);
49 
50  XmlNodeList nodelist = mainXml.SelectNodes("/journal/file");
51 
52  if (nodelist == null)
53  {
54  return true;
55  }
56 
57  Double currentCycle = 0;
58  String currentCycleFileName = "";
59 
60  //The last entry is probably the most recent cycle but we should check
61  for (int i = 0; i < nodelist.Count; ++i)
62  {
63  //filename is formatted like "journal_10_2.xml"
64  String filename = nodelist[i].Attributes["name"].Value.ToString();
65 
66  //Extract the numbers
67  String[] vals = Regex.Split(filename, @"[_\.]");
68 
69  if (vals.Length > 3)
70  {
71  Double cycleNumber;
72  if (Double.TryParse(vals[1] + "." + vals[2], out cycleNumber) && cycleNumber > currentCycle)
73  {
74  currentCycleFileName = journalDir + "\\" + filename;
75  }
76  }
77  else
78  {
79  if (nodelist.Count == 1)
80  {
81  //Probably on a test machine, so try this file
82  currentCycleFileName = journalDir + "\\" + filename;
83  }
84  }
85  }
86 
87  if (currentCycleFileName != "")
88  {
89  FileInfo fi = new FileInfo(currentCycleFileName);
90 
91  if (!fi.Exists)
92  {
93  throw new FileNotFoundException("The cycle journal file does not exist");
94  }
95 
96  if (_journal == null || fi.FullName != _journal.FullName)
97  {
98  //It is a new file
99  _journal = fi;
100  _lastModified = new DateTime();
101  }
102 
103  if (fi.LastWriteTime != _lastModified)
104  {
105  //The file has changed so read it
106 
107  //Load the file
108  XmlDocument cycleXml = new XmlDocument();
109 
110  //Use Western European charset even though the XML claims to be UTF8
111  //as the likes of é causes problems.
112  FileStream inStream = new FileStream(currentCycleFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
113  StreamReader sr = new StreamReader(inStream, Encoding.GetEncoding("Windows-1252"));
114  cycleXml.Load(sr);
115 
116  _lastModified = fi.LastWriteTime;
117 
118  XmlNamespaceManager nsMgr = new XmlNamespaceManager(cycleXml.NameTable);
119  nsMgr.AddNamespace("nx", "http://definition.nexusformat.org/schema/3.0");
120 
121  //XmlNodeList newnodelist = cycleXml.SelectNodes("nx:nxroot/nx:nxentry", nsMgr);
122 
123  //most recent entry is last
124  for (int i = cycleXml.ChildNodes[1].ChildNodes.Count - 1; i >= 0; --i)
125  {
126  String title = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:title", nsMgr).InnerText.ToString().Trim();
127  String username = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:user_name", nsMgr).InnerText.ToString().Trim();
128  String runnumber = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:run_number", nsMgr).InnerText.ToString().Trim();
129  String start = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:start_time", nsMgr).InnerText.ToString().Trim().Replace('T', ' ');
130  String duration = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:duration", nsMgr).InnerText.ToString().Trim();
131  String uamps = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:proton_charge", nsMgr).InnerText.ToString().Trim();
132  String mev = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:total_mevents", nsMgr).InnerText.ToString().Trim();
133  String rb = cycleXml.ChildNodes[1].ChildNodes[i].SelectSingleNode("nx:experiment_identifier", nsMgr).InnerText.ToString().Trim();
134 
135  //Make the duration "friendly"
136  int secs;
137  if (Int32.TryParse(duration, out secs))
138  {
139  duration = new TimeSpan(0, 0, secs).ToString();
140  }
141  else
142  {
143  duration = "UNSPECIFIED";
144  }
145 
146  entries.Add(new Entry(runnumber, title, start, duration, uamps, mev, username, rb));
147  }
148 
149  return true;
150  }
151  }
152 
153  return false;
154  }
155  }
156 }
157 
Boolean GetCurrentCycleData(String journalDir, String mainFileName, out List< Entry > entries)
Entry(String run, String title, String start, String duration, String uamps, String mev, String users, String rb)