89 const std::string &date,
90 const std::string &searchTerm,
91 const std::vector<std::string> &fileFormats,
92 const std::string &dirHint) {
94 if (searchTerm ==
"_Definition")
95 fileType =
"instrument file";
96 else if (searchTerm ==
"_Parameters")
97 fileType =
"parameter file";
99 fileType = searchTerm +
" file";
101 g_log.
debug() <<
"Looking for " << fileType <<
" for " << instrumentName <<
" that is valid on '" << date <<
"'\n";
103 std::string instrument;
105 instrument = Kernel::ConfigService::Instance().getInstrument(instrumentName).name();
107 instrument = instrumentName;
113 const std::vector<std::string> &configDirs = Kernel::ConfigService::Instance().getInstrumentDirectories();
114 std::vector<std::string> directoryNames;
115 if (!dirHint.empty()) {
116 directoryNames.push_back(dirHint);
117 directoryNames.insert(directoryNames.end(), configDirs.begin(), configDirs.end());
119 directoryNames = configDirs;
123 const std::vector<std::string> matchingFiles =
125 std::string foundFile;
126 if (!matchingFiles.empty()) {
127 foundFile = matchingFiles[0];
128 g_log.
debug() <<
"The " << fileType <<
" selected is " << foundFile <<
'\n';
130 g_log.
debug() <<
"No " << fileType <<
" found\n";
216 const std::string ext =
".xml";
218 boost::algorithm::ierase_all(filename, ext);
220 const std::string suffixSeperator(
"_Definition");
225 if (
auto sepPos = boost::algorithm::ifind_first(filename, suffixSeperator)) {
226 prefix = std::string(filename.begin(), sepPos.begin());
227 suffix = std::string(sepPos.end(), filename.end());
232 std::filesystem::path directoryPath(dir);
235 std::string fullPathParamIDF = (directoryPath / (prefix +
"_Parameters" + suffix + ext)).string();
237 if (std::filesystem::exists(fullPathParamIDF)) {
238 return fullPathParamIDF;
241 fullPathParamIDF = (directoryPath / (prefix +
"_Parameters" + ext)).string();
242 if (std::filesystem::exists(fullPathParamIDF)) {
243 return fullPathParamIDF;
265 const std::vector<std::string> &fileFormats,
266 const std::vector<std::string> &directoryNames,
267 const std::string &date) {
271 g_log.
debug() <<
"No date specified, using current date and time.\n";
272 const std::string now = Types::Core::DateAndTime::getCurrentTime().toISO8601String();
278 std::stringstream ss;
280 for (
size_t i = 0; i < fileFormats.size(); ++i) {
283 ss << fileFormats[i];
286 const std::string allFileFormats = ss.str();
288 const boost::regex regex(prefix +
".*\\." + allFileFormats, boost::regex_constants::icase);
296 d = DateAndTime(normalisedDate);
297 }
catch (
const std::invalid_argument &) {
301 <<
"' as ISO8601; using current time for instrument file lookup.\n";
302 d = DateAndTime::getCurrentTime();
305 DateAndTime refDate(
"1899-01-01 23:59:00");
307 DateAndTime refDateGoodFile(
"1899-01-01 23:59:00");
311 std::multimap<DateAndTime, std::string, std::greater<DateAndTime>> matchingFiles;
312 bool foundFile =
false;
313 std::string mostRecentFile;
314 for (
const auto &directoryName : directoryNames) {
317 for (
const auto &dir_entry : std::filesystem::directory_iterator(directoryName)) {
319 const auto &filePath = dir_entry.path();
320 if (!std::filesystem::is_regular_file(filePath))
323 const std::string l_filenamePart = filePath.filename().string();
324 if (regex_match(l_filenamePart, regex)) {
325 const std::string pathName = filePath.string();
326 g_log.
debug() <<
"Found file: '" << pathName <<
"'\n";
328 std::string validFrom, validTo;
330 g_log.
debug() <<
"File '" << pathName <<
" valid dates: from '" << validFrom <<
"' to '" << validTo <<
"'\n";
335 DateAndTime to, from;
337 if (validFrom.length() > 0) {
339 from.setFromISO8601(normFrom);
343 }
catch (
const std::invalid_argument &) {
344 g_log.
debug() <<
"Could not parse valid-from='" << validFrom <<
"' in '" << pathName
345 <<
"'; treating as lowest priority.\n";
349 if (validTo.length() > 0) {
351 to.setFromISO8601(normTo);
353 to.setFromISO8601(
"2100-01-01T00:00:00");
355 }
catch (
const std::invalid_argument &) {
356 g_log.
debug() <<
"Could not parse valid-to='" << validTo <<
"' in '" << pathName
357 <<
"'; treating as lowest priority.\n";
358 to.setFromISO8601(
"2100-01-01T00:00:00");
361 if (from <=
d &&
d <= to) {
363 matchingFiles.insert(std::pair<DateAndTime, std::string>(from, pathName));
366 if (!foundFile && (from >= refDate)) {
368 mostRecentFile = pathName;
375 std::vector<std::string> pathNames;
376 if (!matchingFiles.empty()) {
377 pathNames.reserve(matchingFiles.size());
379 std::transform(matchingFiles.begin(), matchingFiles.end(), std::back_inserter(pathNames),
380 [](
const auto &elem) { return elem.second; });
382 pathNames.emplace_back(std::move(mostRecentFile));
395 std::string &outValidTo) {
399 myContentHandler conHand;
400 pParser.setContentHandler(&conHand);
403 pParser.parse(IDFfilename);
404 }
catch (
const DummyException &e) {
405 outValidFrom = e.m_validFrom;
406 outValidTo = e.m_validTo;
static std::string getFilenameByInstrumentDateAndSearchTerm(const std::string &instrumentName, const std::string &date, const std::string &searchTerm, const std::vector< std::string > &fileFormats, const std::string &dirHint="")
This method returns a file name which finds a file which contains the given instrument name + search ...
static std::vector< std::string > getResourceFilenames(const std::string &prefix, const std::vector< std::string > &fileFormats, const std::vector< std::string > &directoryNames, const std::string &date)
Utility to retrieve a resource file (IDF, Parameters, ..)