123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #include "stdafx.h"
- #include "OTSEDSSim.h"
- namespace OTSController {
- namespace
- {
- std::vector<CString> LoadTextFileToCStingList(CString a_strPathName, int a_nLine /*= -1*/)
- {
- // string list
- std::vector<CString> listStr;
- // load
- try
- {
- // open the file
- CStdioFile file;
- file.Open(a_strPathName, CFile::modeRead | CFile::shareDenyWrite);
- // read the file
- CString strLine;
- int nLine = 0;
- while (file.ReadString(strLine) && nLine != a_nLine)
- {
- // get a line
- // remove comments
- int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT);
- if (nCommentPos != -1)
- {
- // remove comments
- strLine = strLine.Left(nCommentPos);
- }
- // process the line
- strLine.Trim();
- // jump over empty lines
- if (strLine.IsEmpty())
- {
- continue;
- }
- listStr.push_back(strLine);
- }
- file.Close();
- }
- catch (CFileException* pe)
- {
- pe->Delete();
- return listStr;
- }
- // return string list
- return listStr;
- }
- // return "" if failed
- CString GetOSCommonDataPathName()
- {
- CString strPathName = _T(".\\");
- return strPathName;
- }
- // Determines whether is digit string.
- BOOL IsDigitString(LPCTSTR a_sValue)
- {
- CString strInt = a_sValue;
- strInt.Trim();
- if (strInt.IsEmpty())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string is an empty string."));
- return FALSE;
- }
- int nStart = 0;
- if (strInt[nStart] == _T('-'))
- {
- ++nStart;
- }
- // cycle through string and check each character if it is a digit
- for (; nStart < strInt.GetLength(); ++nStart)
- {
- if (!isdigit_t(strInt[nStart]))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string (%s) is not a digit string."), strInt);
- return FALSE;
- }
- }
- return TRUE;
- }
- // get system common data folder pathname
- // Strings to int.
- BOOL StringToInt(LPCTSTR a_sValue, int& a_nValue)
- {
- if (!IsDigitString(a_sValue))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("StringToInt: value string (%s) is not digit string."), a_sValue);
- return FALSE;
- }
- a_nValue = _ttoi(a_sValue);
- return TRUE;
- }
-
-
- }
- const CString SIMULATION_SPECTRUM_FILENAME = _T("SimSpectrum.txt");
- COTSEDSSim::COTSEDSSim()
- {
- m_bDoQuantification = FALSE;
- }
- COTSEDSSim::~COTSEDSSim()
- {
- }
- // initialization
- BOOL COTSEDSSim::Init()
- {
- // simulation, never be wrong
- return TRUE;
- }
-
- // collect spectrum at the given position (to controller buffer)
- BOOL COTSEDSSim::CollectSpectrum(DWORD a_nMilliseconds, const CPoint& a_oPoint)
- {
- // read simulation spectrum into buffer
- memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
- if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
- {
- // failed to call CollectASpectrumFromTxtFile method
- LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: failed to call CollectASpectrumFromTxtFile method."));
- }
- // always return TRUE
- return TRUE;
-
- }
- // collects spectrum (to controller buffer)
- BOOL COTSEDSSim::CollectSpectrum(DWORD a_nMilliseconds)
- {
-
- // read simulation spectrum into buffer
- memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
- if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
- {
- LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: failed to call CollectASpectrumFromTxtFile method."));
- }
- // always return TRUE
- return TRUE;
-
- }
- CString GetCompanySysDataPathName()
- {
- // get common data pathname string
- CString strCommonDataPathName = GetOSCommonDataPathName();
- if (strCommonDataPathName.IsEmpty())
- {
- // failed to get common data pathname string
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: failed to common data pathname string."));
- return _T("");
- }
- CString strCmpSysDataPath = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_SIMULATE_DATA + _T("\\");
- // return company system data pathname
- return strCmpSysDataPath;
- }
-
- // check if the file exists or not
- BOOL Exists(LPCTSTR a_sPath)
- {
- return ::PathFileExists(a_sPath) == TRUE;
- }
- // collects spectrum (to given buffer)
- BOOL COTSEDSSim::CollectSpectrum(DWORD a_nMilliseconds, long* a_pCounts, DWORD a_nBufferSize)
- {
- // input check
- ASSERT(a_pCounts);
- if (!a_pCounts)
- {
- // invalid input data buffer
- LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: invalid input data buffer."));
- return FALSE;
- }
- // read simulation spectrum into buffer
- memset(a_pCounts, 0, sizeof(long) * (long)a_nBufferSize);
- if (!CollectASpectrumFromTxtFile((DWORD*)a_pCounts, a_nBufferSize))
- {
- // failed to call CollectASpectrumFromTxtFile method
- LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::CollectSpectrum: failed to call CollectASpectrumFromTxtFile method."));
- }
- // always return TRUE
- return TRUE;
-
- }
- BOOL COTSEDSSim::CollectASpectrumFromTxtFile(DWORD* a_pCounts, DWORD a_nBufferSize)
- {
- // input check
- ASSERT(a_pCounts);
- if (!a_pCounts)
- {
- LogInfoTrace(__FILE__, __LINE__, _T("COTSEDSBase::CollectASpectrumFromTxtFile: invalid a_pCounts."));
- return FALSE;
- }
- // get simulation spectrum file name
- CString strOTSSysDataPath = GetCompanySysDataPathName();
- CString strSimSpectrumFilePathName = strOTSSysDataPath + SIMULATION_SPECTRUM_FILENAME;
- // check if the file exist
- if (!Exists(strSimSpectrumFilePathName))
- {
- // simulation spectrum file doesn't exist
- LogInfoTrace(__FILE__, __LINE__, _T("COTSEDSBase::CollectASpectrumFromTxtFile: simulation spectrum file doesn't exist."));
- return FALSE;
- }
- // load string lines from the file
- std::vector<CString > listLineStr = LoadTextFileToCStingList(strSimSpectrumFilePathName, (int)a_nBufferSize);
- // set spectrum data
- memset(a_pCounts, 0, sizeof(DWORD) * a_nBufferSize);
- for (int i = 0; i < (int)listLineStr.size() && i < (int)a_nBufferSize; ++i)
- {
- CString strValue = listLineStr[i];
- int nValue = 0;
- if (StringToInt(strValue, nValue))
- {
- a_pCounts[i] = (long)nValue;
- }
- }
- // ok, return TRUE
- return TRUE;
- }
-
- BOOL COTSEDSSim::StopXrayAcquistion()
- {
- return true;
- }
- // get live time
- float COTSEDSSim::GetLiveTime()
- {
- // is simulation?
- return (float)0.1;
- }
- BOOL COTSEDSSim::GetQuantificationMethods(std::vector<CString>& a_vMethods)
- {
- return TRUE;
- }
- BOOL COTSEDSSim::QuantifyXrayPoint(CPosXray* a_pXRayPoint, LPCTSTR a_sMethodName)
- {
- return TRUE;
- }
- BOOL COTSEDSSim::QuantifySpectrumFile(LPCTSTR a_sFilePathName, LPCTSTR a_sMethodName, CElementChemistriesList& a_listElementChemistry)
- {
- return TRUE;
- }
- BOOL COTSEDSSim::QuantifySpectrumOut(DWORD a_nMilliseconds, long* a_pCounts, DWORD a_nBufferSize, CElementChemistriesList& a_listElementChemistry)
- {
- return TRUE;
- }
- BOOL COTSEDSSim::GetXRayByPoints(std::vector<CPosXrayPtr>& a_vXRayPoints, const DWORD a_nXRayAQTime)
- {
- // read simulation spectrum into buffer
- memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
- if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
- {
- // failed to call CollectASpectrumFromTxtFile method
- LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::GetXRayByPoints: failed to call CollectASpectrumFromTxtFile method."));
- }
- int icount = 0;
- for (auto pXrayPoi : a_vXRayPoints)
- {
- std::vector<CString> eleList;
- for (size_t i = 0; i < pXrayPoi->GetElementQuantifyData().size(); i++)
- {
- eleList.push_back(pXrayPoi->GetElementQuantifyData()[i]->GetName());
- }
- pXrayPoi->SetXrayData(m_nRayData);
- CString sResult = "";
- CElementChemistriesList listElementQuantifyData;
- CElementPtr pElement = CElementPtr(new CElement());
- if (m_bDoQuantification/* && isFilter*/)
- {
- switch (icount % 7)
- {
- case 0://MnS
- sResult = _T("Quant=Al,K-Serie,19\nQuant=O,L-Serie,39\nQuant=Ca,L-Serie,20");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 1://FeO
- 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");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 3://Sulfide
- sResult = _T("Quant=C,K-Serie,1.057\nQuant=S,L-Serie,2.177\nQuant=Fe,L-Serie,84.592");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 4://Silicate
- 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");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 5://SiO2
- 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");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 6://AlO2
- sResult = _T("Quant=Al,K-Serie,40\nQuant=O,L-Serie,40\nQuant=Fe,L-Serie,20");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- //case 7:
- // sResult = _T("Quant=Al,K-Serie,1\nQuant=O,L-Serie,2\nQuant=Fe,L-Serie,2");
- // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- // break;
- //case 8:
- // sResult = _T("Quant=Fe,K-Serie,10\nQuant=C,L-Serie,30\nQuant=F,L-Serie,25");
- // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- // break;
- //case 9:
- // sResult = _T("Quant=O,K-Serie,40\nQuant=Si,L-Serie,40\nQuant=C,L-Serie,1");
- // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- // break;
- default://Not Identified
- sResult = _T("Quant=C,K-Serie,7.992\nQuant=Si,L-Serie,15.969\nQuant=Fe,L-Serie,69.805");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- }
- icount++;
- }
- }
-
- Sleep(500);
- // always return TRUE
- return TRUE;
-
-
- }
- BOOL COTSEDSSim::GetXRayByFeatures(std::vector<CPosXrayPtr>& a_listXRayPoints,
- std::vector<BrukerFeature>& a_vFeatures,
- const DWORD a_nXRayAQTime)
- {
- // read simulation spectrum into buffer
- memset(m_nRayData, 0, sizeof(DWORD) * (long)EDSConst::XANA_CHANNELS);
- if (!CollectASpectrumFromTxtFile(m_nRayData, (DWORD)EDSConst::XANA_CHANNELS))
- {
- // failed to call CollectASpectrumFromTxtFile method
- LogTrace(__FILE__, __LINE__, _T("COTSEDSSim::GetXRayByFeatures: failed to call CollectASpectrumFromTxtFile method."));
- }
- int icount = 0;
- for (auto pXrayPoi : a_listXRayPoints)
- {
- pXrayPoi->SetXrayData(m_nRayData);
- if (m_bDoQuantification)
- {
- CString sResult = "";
- CElementChemistriesList listElementQuantifyData;
- //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");
- //CString sResult = _T("Quant=O,K-Serie,28.5\nQuant=Fe,L-Serie,49.5");
- CElementPtr pElement = CElementPtr(new CElement());
- //listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- switch (icount % 7)
- {
- case 0://MnS
- sResult = _T("Quant=O,K-Serie,28.5\nQuant=S,L-Serie,10\nQuant=Mn,L-Serie,20\nQuant=Fe,L-Serie,9.5");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 1://FeO
- 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");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 3://Sulfide
- sResult = _T("Quant=C,K-Serie,1.057\nQuant=S,L-Serie,2.177\nQuant=Fe,L-Serie,84.592");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 4://Silicate
- 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");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 5://SiO2
- 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");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- case 6://AlO2
- sResult = _T("Quant=Al,K-Serie,40\nQuant=O,L-Serie,40\nQuant=Fe,L-Serie,20");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- //case 7:
- // sResult = _T("Quant=Al,K-Serie,1\nQuant=O,L-Serie,2\nQuant=Fe,L-Serie,2");
- // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- // break;
- //case 8:
- // sResult = _T("Quant=Fe,K-Serie,10\nQuant=C,L-Serie,30\nQuant=F,L-Serie,25");
- // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- // break;
- //case 9:
- // sResult = _T("Quant=O,K-Serie,40\nQuant=Si,L-Serie,40\nQuant=C,L-Serie,1");
- // listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- // pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- // break;
- default://Not Identified
- sResult = _T("Quant=C,K-Serie,7.992\nQuant=Si,L-Serie,15.969\nQuant=Fe,L-Serie,69.805");
- listElementQuantifyData = pElement->ExtractElementChemistrys(sResult);
- pXrayPoi->SetElementQuantifyData(listElementQuantifyData);
- break;
- }
- icount++;
- }
- }
- // always return TRUE
- return TRUE;
-
- }
-
- void COTSEDSSim::SetQuantification(BOOL a_bQuantification)
- {
- m_bDoQuantification = a_bQuantification;
- return;
- }
- BOOL COTSEDSSim::GetQuantification()
- {
- return TRUE;
- }
- BOOL COTSEDSSim::QuantifyXrays(std::vector<CPosXrayPtr>& a_vXRayParts)
- {
- return true;
- }
- // Get number of channels
- DWORD COTSEDSSim::GetNumberOfChannels(void)
- {
- return (DWORD)2000;
- }
- // Get the x-Ray data
- DWORD* COTSEDSSim::GetXRayData()
- {
- return m_nRayData;
- }
- BOOL COTSEDSSim::QuantifyXray(CPosXrayPtr& a_vXRayPart)
- {
- return true;
- }
- int COTSEDSSim::GetExpectCount()
- {
- return m_expectcount;
-
- }
- void COTSEDSSim::SetQuantificationParam(bool ifauto, CString knownelements)
- {
- }
- void COTSEDSSim::SetExpectCount(int expectcount)
- {
- m_expectcount = expectcount;
- }
-
- }
|