Skip to content

Commit

Permalink
Add second try for index path with instrument shortname
Browse files Browse the repository at this point in the history
If the path for the instrument index file was not found with instrument's name,
try again with instrument's shortname.
Solves case on IDAaaS where "ENGINX" (shortname) is the name of the folder
instead of "ENGIN-X" (name).
This commit can be reverted in the future if IDAaaS updates name to "ENGIN-X".
  • Loading branch information
GuiMacielPereira committed May 24, 2024
1 parent 645b183 commit b2e6a8f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Framework/API/inc/MantidAPI/ISISInstrumentDataCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidKernel/ConfigService.h"
#include <string>

namespace Mantid {
Expand All @@ -21,7 +22,8 @@ class MANTID_API_DLL ISISInstrumentDataCache {
std::string getFileParentDirectoryPath(const std::string &filename) const;

private:
std::pair<std::string, std::string> validateInstrumentAndNumber(const std::string &filename) const;
std::pair<Mantid::Kernel::InstrumentInfo, std::string> validateInstrumentAndNumber(const std::string &filename) const;
std::string makeIndexFilePath(const std::string &instrumentName) const;
std::pair<std::string, std::string> splitIntoInstrumentAndNumber(const std::string &filename) const;
std::string m_dataCachePath;
};
Expand Down
26 changes: 19 additions & 7 deletions Framework/API/src/ISISInstrumentDataCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ Mantid::Kernel::Logger g_log("ISISInstrumentDataCache");
std::string Mantid::API::ISISInstrumentDataCache::getFileParentDirectoryPath(const std::string &fileName) const {
g_log.debug() << "ISISInstrumentDataCache::getFileParentDirectoryPath(" << fileName << ")" << std::endl;

auto [instrName, runNumber] = validateInstrumentAndNumber(fileName);
auto [instrumentInfo, runNumber] = validateInstrumentAndNumber(fileName);
std::string instrName = instrumentInfo.name();

// Open index json file
std::string jsonPath = m_dataCachePath + "/" + instrName + "/" + instrName + "_index.json";
std::string jsonPath = makeIndexFilePath(instrName);
std::ifstream ifstrm{jsonPath};
if (!ifstrm) {
throw std::invalid_argument("Could not open index file: " + jsonPath);
if (!ifstrm.is_open()) { // Try again with shortname
instrName = instrumentInfo.shortName();
jsonPath = makeIndexFilePath(instrName);
ifstrm.open(jsonPath);
if (!ifstrm.is_open()) {
throw std::invalid_argument("Could not open index file: " + jsonPath);
}
}

// Read directory path from json file
Expand All @@ -39,7 +45,7 @@ std::string Mantid::API::ISISInstrumentDataCache::getFileParentDirectoryPath(con
return dirPath;
}

std::pair<std::string, std::string>
std::pair<Mantid::Kernel::InstrumentInfo, std::string>
Mantid::API::ISISInstrumentDataCache::validateInstrumentAndNumber(const std::string &fileName) const {

// Check if suffix eg. -add is present in filename
Expand All @@ -57,12 +63,18 @@ Mantid::API::ISISInstrumentDataCache::validateInstrumentAndNumber(const std::str
runNumber.erase(0, runNumber.find_first_not_of('0')); // Remove padding zeros

try { // Expand instrument name
instrName = FileFinder::Instance().getInstrument(instrName, false).name();
auto instrumentInfo = FileFinder::Instance().getInstrument(instrName, false);
return std::pair(instrumentInfo, runNumber);

} catch (const Kernel::Exception::NotFoundError &) {
throw std::invalid_argument("Instrument name not recognized.");
}
}

return std::pair(instrName, runNumber);
std::string Mantid::API::ISISInstrumentDataCache::makeIndexFilePath(const std::string &instrumentName) const {
g_log.debug() << "ISISInstrumentDataCache::makeIndexFilePath(" << instrumentName << ")" << std::endl;
std::string indexFilePath = m_dataCachePath + "/" + instrumentName + "/" + instrumentName + "_index.json";
return indexFilePath;
}

std::pair<std::string, std::string>
Expand Down

0 comments on commit b2e6a8f

Please sign in to comment.