OTSEDSSim.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include "stdafx.h"
  2. #include "OTSEDSSim.h"
  3. namespace OTSController {
  4. namespace
  5. {
  6. std::vector<CString> LoadTextFileToCStingList(CString a_strPathName, int a_nLine /*= -1*/)
  7. {
  8. // string list
  9. std::vector<CString> listStr;
  10. // load
  11. try
  12. {
  13. // open the file
  14. CStdioFile file;
  15. file.Open(a_strPathName, CFile::modeRead | CFile::shareDenyWrite);
  16. // read the file
  17. CString strLine;
  18. int nLine = 0;
  19. while (file.ReadString(strLine) && nLine != a_nLine)
  20. {
  21. // get a line
  22. // remove comments
  23. int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT);
  24. if (nCommentPos != -1)
  25. {
  26. // remove comments
  27. strLine = strLine.Left(nCommentPos);
  28. }
  29. // process the line
  30. strLine.Trim();
  31. // jump over empty lines
  32. if (strLine.IsEmpty())
  33. {
  34. continue;
  35. }
  36. listStr.push_back(strLine);
  37. }
  38. file.Close();
  39. }
  40. catch (CFileException* pe)
  41. {
  42. pe->Delete();
  43. return listStr;
  44. }
  45. // return string list
  46. return listStr;
  47. }
  48. // return "" if failed
  49. CString GetOSCommonDataPathName()
  50. {
  51. CString strPathName = _T(".\\");
  52. return strPathName;
  53. }
  54. // Determines whether is digit string.
  55. BOOL IsDigitString(LPCTSTR a_sValue)
  56. {
  57. CString strInt = a_sValue;
  58. strInt.Trim();
  59. if (strInt.IsEmpty())
  60. {
  61. LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string is an empty string."));
  62. return FALSE;
  63. }
  64. int nStart = 0;
  65. if (strInt[nStart] == _T('-'))
  66. {
  67. ++nStart;
  68. }
  69. // cycle through string and check each character if it is a digit
  70. for (; nStart < strInt.GetLength(); ++nStart)
  71. {
  72. if (!isdigit_t(strInt[nStart]))
  73. {
  74. LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string (%s) is not a digit string."), strInt);
  75. return FALSE;
  76. }
  77. }
  78. return TRUE;
  79. }
  80. // get system common data folder pathname
  81. // Strings to int.
  82. BOOL StringToInt(LPCTSTR a_sValue, int& a_nValue)
  83. {
  84. if (!IsDigitString(a_sValue))
  85. {
  86. LogErrorTrace(__FILE__, __LINE__, _T("StringToInt: value string (%s) is not digit string."), a_sValue);
  87. return FALSE;
  88. }
  89. a_nValue = _ttoi(a_sValue);
  90. return TRUE;
  91. }
  92. }
  93. const CString SIMULATION_SPECTRUM_FILENAME = _T("SimSpectrum.txt");
  94. COTSEDSSim::COTSEDSSim()
  95. {
  96. m_bDoQuantification = FALSE;
  97. }
  98. COTSEDSSim::~COTSEDSSim()
  99. {
  100. }
  101. // initialization
  102. BOOL COTSEDSSim::Init()
  103. {
  104. // simulation, never be wrong
  105. return TRUE;
  106. }
  107. // collect spectrum at the given position (to controller buffer)
  108. BOOL COTSEDSSim::CollectSpectrum(DWORD a_nMilliseconds, const CPoint& a_oPoint)
  109. {
  110. // read simulation spectrum into buffer
  111. memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
  112. if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
  113. {
  114. // failed to call CollectASpectrumFromTxtFile method
  115. LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: failed to call CollectASpectrumFromTxtFile method."));
  116. }
  117. // always return TRUE
  118. return TRUE;
  119. }
  120. // collects spectrum (to controller buffer)
  121. BOOL COTSEDSSim::CollectSpectrum(DWORD a_nMilliseconds)
  122. {
  123. // read simulation spectrum into buffer
  124. memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
  125. if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
  126. {
  127. LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: failed to call CollectASpectrumFromTxtFile method."));
  128. }
  129. // always return TRUE
  130. return TRUE;
  131. }
  132. CString GetCompanySysDataPathName()
  133. {
  134. // get common data pathname string
  135. CString strCommonDataPathName = GetOSCommonDataPathName();
  136. if (strCommonDataPathName.IsEmpty())
  137. {
  138. // failed to get common data pathname string
  139. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: failed to common data pathname string."));
  140. return _T("");
  141. }
  142. CString strCmpSysDataPath = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_SIMULATE_DATA + _T("\\");
  143. // return company system data pathname
  144. return strCmpSysDataPath;
  145. }
  146. // check if the file exists or not
  147. BOOL Exists(LPCTSTR a_sPath)
  148. {
  149. return ::PathFileExists(a_sPath) == TRUE;
  150. }
  151. // collects spectrum (to given buffer)
  152. BOOL COTSEDSSim::CollectSpectrum(DWORD a_nMilliseconds, long* a_pCounts, DWORD a_nBufferSize)
  153. {
  154. // input check
  155. ASSERT(a_pCounts);
  156. if (!a_pCounts)
  157. {
  158. // invalid input data buffer
  159. LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: invalid input data buffer."));
  160. return FALSE;
  161. }
  162. // read simulation spectrum into buffer
  163. memset(a_pCounts, 0, sizeof(long) * (long)a_nBufferSize);
  164. if (!CollectASpectrumFromTxtFile((DWORD*)a_pCounts, a_nBufferSize))
  165. {
  166. // failed to call CollectASpectrumFromTxtFile method
  167. LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: failed to call CollectASpectrumFromTxtFile method."));
  168. }
  169. // always return TRUE
  170. return TRUE;
  171. }
  172. BOOL COTSEDSSim::CollectASpectrumFromTxtFile(DWORD* a_pCounts, DWORD a_nBufferSize)
  173. {
  174. // input check
  175. ASSERT(a_pCounts);
  176. if (!a_pCounts)
  177. {
  178. LogInfoTrace(__FILE__, __LINE__, _T("COTSEDSBase::CollectASpectrumFromTxtFile: invalid a_pCounts."));
  179. return FALSE;
  180. }
  181. // get simulation spectrum file name
  182. CString strOTSSysDataPath = GetCompanySysDataPathName();
  183. CString strSimSpectrumFilePathName = strOTSSysDataPath + SIMULATION_SPECTRUM_FILENAME;
  184. // check if the file exist
  185. if (!Exists(strSimSpectrumFilePathName))
  186. {
  187. // simulation spectrum file doesn't exist
  188. LogInfoTrace(__FILE__, __LINE__, _T("COTSEDSBase::CollectASpectrumFromTxtFile: simulation spectrum file doesn't exist."));
  189. return FALSE;
  190. }
  191. // load string lines from the file
  192. std::vector<CString > listLineStr = LoadTextFileToCStingList(strSimSpectrumFilePathName, (int)a_nBufferSize);
  193. // set spectrum data
  194. memset(a_pCounts, 0, sizeof(DWORD) * a_nBufferSize);
  195. for (int i = 0; i < (int)listLineStr.size() && i < (int)a_nBufferSize; ++i)
  196. {
  197. CString strValue = listLineStr[i];
  198. int nValue = 0;
  199. if (StringToInt(strValue, nValue))
  200. {
  201. a_pCounts[i] = (long)nValue;
  202. }
  203. }
  204. // ok, return TRUE
  205. return TRUE;
  206. }
  207. BOOL COTSEDSSim::StopXrayAcquistion()
  208. {
  209. return true;
  210. }
  211. // get live time
  212. float COTSEDSSim::GetLiveTime()
  213. {
  214. // is simulation?
  215. return (float)0.1;
  216. }
  217. BOOL COTSEDSSim::GetQuantificationMethods(std::vector<CString>& a_vMethods)
  218. {
  219. return TRUE;
  220. }
  221. BOOL COTSEDSSim::QuantifyXrayPoint(CPosXray* a_pXRayPoint, LPCTSTR a_sMethodName)
  222. {
  223. return TRUE;
  224. }
  225. BOOL COTSEDSSim::QuantifySpectrumFile(LPCTSTR a_sFilePathName, LPCTSTR a_sMethodName, CElementChemistriesList& a_listElementChemistry)
  226. {
  227. return TRUE;
  228. }
  229. BOOL COTSEDSSim::QuantifySpectrumOut(DWORD a_nMilliseconds, long* a_pCounts, DWORD a_nBufferSize, CElementChemistriesList& a_listElementChemistry)
  230. {
  231. return TRUE;
  232. }
  233. BOOL COTSEDSSim::GetXRayByPoints(std::vector<CPosXrayPtr>& a_vXRayPoints, const DWORD a_nXRayAQTime)
  234. {
  235. // read simulation spectrum into buffer
  236. memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
  237. if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
  238. {
  239. // failed to call CollectASpectrumFromTxtFile method
  240. LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::GetXRayByPoints: failed to call CollectASpectrumFromTxtFile method."));
  241. }
  242. int icount = 0;
  243. for (auto pXrayPoi : a_vXRayPoints)
  244. {
  245. std::vector<CString> eleList;
  246. for (size_t i = 0; i < pXrayPoi->GetElementQuantifyData().size(); i++)
  247. {
  248. eleList.push_back(pXrayPoi->GetElementQuantifyData()[i]->GetName());
  249. }
  250. pXrayPoi->SetXrayData(m_nRayData);
  251. CString sResult = "";
  252. CElementChemistriesList listElementQuantifyData;
  253. CElementPtr pElement = CElementPtr(new CElement());
  254. if (m_bDoQuantification/* && isFilter*/)
  255. {
  256. switch (icount % 7)
  257. {
  258. case 0://MnS
  259. sResult = _T("Quant=Al,K-Serie,19\nQuant=O,L-Serie,39\nQuant=Ca,L-Serie,20");
  260. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  261. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  262. break;
  263. case 1://FeO
  264. sResult = _T("Quant=O,K-Serie,4.84\nQuant=Al,L-Serie,1.47\nQuant=S,L-Serie,1.6\nQuant=Si,L-Serie,2.42\nQuant=Mn,L-Serie,2.97\nQuant=Fe,L-Serie,80.361");
  265. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  266. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  267. break;
  268. case 3://Sulfide
  269. sResult = _T("Quant=C,K-Serie,1.057\nQuant=S,L-Serie,2.177\nQuant=Fe,L-Serie,84.592");
  270. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  271. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  272. break;
  273. case 4://Silicate
  274. sResult = _T("Quant=O,K-Serie,9.75\nQuant=Al,L-Serie,1.08\nQuant=Si,L-Serie,5.261\nQuant=Mn,L-Serie,11.204\nQuant=Fe,L-Serie,50.403");
  275. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  276. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  277. break;
  278. case 5://SiO2
  279. sResult = _T("Quant=C,K-Serie,12.563\nQuant=O,L-Serie,1.906\nQuant=Si,L-Serie,26.126\nQuant=Fe,L-Serie,54.61");
  280. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  281. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  282. break;
  283. case 6://AlO2
  284. sResult = _T("Quant=Al,K-Serie,40\nQuant=O,L-Serie,40\nQuant=Fe,L-Serie,20");
  285. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  286. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  287. break;
  288. //case 7:
  289. // sResult = _T("Quant=Al,K-Serie,1\nQuant=O,L-Serie,2\nQuant=Fe,L-Serie,2");
  290. // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  291. // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  292. // break;
  293. //case 8:
  294. // sResult = _T("Quant=Fe,K-Serie,10\nQuant=C,L-Serie,30\nQuant=F,L-Serie,25");
  295. // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  296. // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  297. // break;
  298. //case 9:
  299. // sResult = _T("Quant=O,K-Serie,40\nQuant=Si,L-Serie,40\nQuant=C,L-Serie,1");
  300. // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  301. // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  302. // break;
  303. default://Not Identified
  304. sResult = _T("Quant=C,K-Serie,7.992\nQuant=Si,L-Serie,15.969\nQuant=Fe,L-Serie,69.805");
  305. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  306. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  307. break;
  308. }
  309. icount++;
  310. }
  311. }
  312. Sleep(500);
  313. // always return TRUE
  314. return TRUE;
  315. }
  316. BOOL COTSEDSSim::GetXRayByFeatures(std::vector<CPosXrayPtr>& a_listXRayPoints,
  317. std::vector<BrukerFeature>& a_vFeatures,
  318. const DWORD a_nXRayAQTime)
  319. {
  320. // read simulation spectrum into buffer
  321. memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
  322. if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
  323. {
  324. // failed to call CollectASpectrumFromTxtFile method
  325. LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::GetXRayByFeatures: failed to call CollectASpectrumFromTxtFile method."));
  326. }
  327. int icount = 0;
  328. for (auto pXrayPoi : a_listXRayPoints)
  329. {
  330. pXrayPoi->SetXrayData(m_nRayData);
  331. if (m_bDoQuantification)
  332. {
  333. CString sResult = "";
  334. CElementChemistriesList listElementQuantifyData;
  335. //CString sResult = _T("Quant=O,K-Serie,28.5\nQuant=Al,L-Serie,26.4\nQuant=S,L-Serie,1.6\nQuant=Ca,L-Serie,15.8\nQuant=Fe,L-Serie,9.5");
  336. //CString sResult = _T("Quant=O,K-Serie,28.5\nQuant=Fe,L-Serie,49.5");
  337. CElementPtr pElement = CElementPtr(new CElement());
  338. //listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  339. switch (icount % 7)
  340. {
  341. case 0://MnS
  342. sResult = _T("Quant=O,K-Serie,28.5\nQuant=S,L-Serie,10\nQuant=Mn,L-Serie,20\nQuant=Fe,L-Serie,9.5");
  343. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  344. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  345. break;
  346. case 1://FeO
  347. sResult = _T("Quant=O,K-Serie,4.84\nQuant=Al,L-Serie,1.47\nQuant=S,L-Serie,1.6\nQuant=Si,L-Serie,2.42\nQuant=Mn,L-Serie,2.97\nQuant=Fe,L-Serie,80.361");
  348. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  349. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  350. break;
  351. case 3://Sulfide
  352. sResult = _T("Quant=C,K-Serie,1.057\nQuant=S,L-Serie,2.177\nQuant=Fe,L-Serie,84.592");
  353. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  354. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  355. break;
  356. case 4://Silicate
  357. sResult = _T("Quant=O,K-Serie,9.75\nQuant=Al,L-Serie,1.08\nQuant=Si,L-Serie,5.261\nQuant=Mn,L-Serie,11.204\nQuant=Fe,L-Serie,50.403");
  358. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  359. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  360. break;
  361. case 5://SiO2
  362. sResult = _T("Quant=C,K-Serie,12.563\nQuant=O,L-Serie,1.906\nQuant=Si,L-Serie,26.126\nQuant=Fe,L-Serie,54.61");
  363. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  364. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  365. break;
  366. case 6://AlO2
  367. sResult = _T("Quant=Al,K-Serie,40\nQuant=O,L-Serie,40\nQuant=Fe,L-Serie,20");
  368. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  369. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  370. break;
  371. //case 7:
  372. // sResult = _T("Quant=Al,K-Serie,1\nQuant=O,L-Serie,2\nQuant=Fe,L-Serie,2");
  373. // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  374. // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  375. // break;
  376. //case 8:
  377. // sResult = _T("Quant=Fe,K-Serie,10\nQuant=C,L-Serie,30\nQuant=F,L-Serie,25");
  378. // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  379. // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  380. // break;
  381. //case 9:
  382. // sResult = _T("Quant=O,K-Serie,40\nQuant=Si,L-Serie,40\nQuant=C,L-Serie,1");
  383. // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  384. // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  385. // break;
  386. default://Not Identified
  387. sResult = _T("Quant=C,K-Serie,7.992\nQuant=Si,L-Serie,15.969\nQuant=Fe,L-Serie,69.805");
  388. listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
  389. pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
  390. break;
  391. }
  392. icount++;
  393. }
  394. }
  395. // always return TRUE
  396. return TRUE;
  397. }
  398. void COTSEDSSim::SetQuantification(BOOL a_bQuantification)
  399. {
  400. m_bDoQuantification = a_bQuantification;
  401. return;
  402. }
  403. BOOL COTSEDSSim::GetQuantification()
  404. {
  405. return TRUE;
  406. }
  407. BOOL COTSEDSSim::QuantifyXrays(std::vector<CPosXrayPtr>& a_vXRayParts)
  408. {
  409. return true;
  410. }
  411. // Get number of channels
  412. DWORD COTSEDSSim::GetNumberOfChannels(void)
  413. {
  414. return (DWORD)2000;
  415. }
  416. // Get the x-Ray data
  417. DWORD* COTSEDSSim::GetXRayData()
  418. {
  419. return m_nRayData;
  420. }
  421. BOOL COTSEDSSim::QuantifyXray(CPosXrayPtr& a_vXRayPart)
  422. {
  423. return true;
  424. }
  425. int COTSEDSSim::GetExpectCount()
  426. {
  427. return m_expectcount;
  428. }
  429. void COTSEDSSim::SetQuantificationParam(bool ifauto, CString knownelements)
  430. {
  431. }
  432. void COTSEDSSim::SetExpectCount(int expectcount)
  433. {
  434. m_expectcount = expectcount;
  435. }
  436. }