Skip to content

Commit

Permalink
Refs #5092. Improve the way MWRunFiles handles files.
Browse files Browse the repository at this point in the history
Improve the way current data files are manipulated in muon analysis.
  • Loading branch information
Robert-Whitley committed Apr 12, 2012
1 parent 52eda88 commit dcd1c32
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 134 deletions.
18 changes: 16 additions & 2 deletions Code/Mantid/Framework/API/src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,20 @@ namespace Mantid
{
std::string path = getFullPath(filenames[i] + *ext);
if (!path.empty())
return path;
{
//Check file actually exists.
try
{
if (Poco::File(path).exists() )
return path;
else
return("");
}
catch(...)
{
return("");
}
}
}
}

Expand Down Expand Up @@ -762,11 +775,12 @@ namespace Mantid
size_t nZero = run.size(); // zero padding
if (range[1].size() > nZero)
{
("Malformed range of runs: " + *h
throw std::invalid_argument("Malformed range of runs: " + *h
+ ". The end of string value is longer than the instrument's zero padding");
}
int runNumber = boost::lexical_cast<int>(run);
std::string runEnd = run;
// Adds zero padding to end of range.
runEnd.replace(runEnd.end() - range[1].size(), runEnd.end(), range[1]);

// Throw if runEnd contains something else other than a digit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ private slots:

/// is string a number?
bool isNumber(const std::string& s) const;

/// Check to see if file given is valid.
bool isValidFile(const QString & fileName);

/// Clear tables and front combo box
void clearTablesAndCombo();
Expand Down
171 changes: 43 additions & 128 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,8 @@ void MuonAnalysis::runLoadCurrent()
QString("does not seem to exist"));
return;
}
m_previousFilenames.clear();
m_previousFilenames.append(argusDAE);
inputFileChanged(m_previousFilenames);
m_uiForm.mwRunFiles->setUserInput(argusDAE);
m_uiForm.mwRunFiles->setText("CURRENT RUN");
return;
}

Expand Down Expand Up @@ -501,10 +500,8 @@ void MuonAnalysis::runLoadCurrent()
QString("does not seem to exist"));
return;
}

m_previousFilenames.clear();
m_previousFilenames.append(psudoDAE);
inputFileChanged(m_previousFilenames);
m_uiForm.mwRunFiles->setUserInput(psudoDAE);
m_uiForm.mwRunFiles->setText("CURRENT RUN");
return;
}

Expand Down Expand Up @@ -951,12 +948,15 @@ void MuonAnalysis::inputFileChanged_MWRunFiles()
*/
void MuonAnalysis::handleInputFileChanges()
{
std::cout << "\n\n" << m_uiForm.mwRunFiles->getFirstFilename().toStdString() << "\n\n";

if ( m_uiForm.mwRunFiles->getText().isEmpty() )
return;

if ( !m_uiForm.mwRunFiles->isValid() )
{
QMessageBox::warning(this,"Mantid - MuonAnalysis", "Specified data file does not exist.");
{
QMessageBox::warning(this,"Mantid - MuonAnalysis", m_uiForm.mwRunFiles->getFileProblem() );
m_uiForm.mwRunFiles->setFileProblem("Error finding file. Reset to last working data.");
m_uiForm.mwRunFiles->setText(m_textToDisplay);
return;
}
Expand All @@ -965,35 +965,6 @@ void MuonAnalysis::handleInputFileChanges()
{
QStringList runFiles = m_uiForm.mwRunFiles->getFilenames();

if (runFiles.size() == m_previousFilenames.size())
{
bool same(true);
for (int i=0; i<runFiles.size(); ++i)
{
if (runFiles[i] != m_previousFilenames[i])
same = false;
}
if (same == true)
{
return;
}
}

// in case file is selected from browser button check that it actually exist
for (int i=0; i<runFiles.size(); ++i)
{
if (!(isValidFile(runFiles[i]) ) ) // If the file isn't valid...
{
if (m_previousFilenames.size() > 0) // If a previous file exists then reset the name.
{
m_uiForm.mwRunFiles->setText(m_textToDisplay);
}
else // Ohterwise set the text a blank string.
m_uiForm.mwRunFiles->setText("");
return;
}
}

m_previousFilenames.clear();
m_previousFilenames = runFiles;
m_textToDisplay = m_uiForm.mwRunFiles->getText();
Expand Down Expand Up @@ -1102,19 +1073,15 @@ void MuonAnalysis::inputFileChanged(const QStringList& files)
else if ((m_uiForm.instrSelector->currentText().toUpper().toStdString() != "ARGUS") && (m_uiForm.deadTimeType->currentIndex() == 2) )
{
QString deadTimeFile(m_uiForm.mwRunDeadTimeFile->getFirstFilename() );
if (isValidFile(deadTimeFile) )

try
{
try
{
getDeadTimeFromFile(deadTimeFile);
}
catch (std::exception&)
{
QMessageBox::information(this, "Mantid - MuonAnalysis", "A problem occurred while applying dead times.");
}
getDeadTimeFromFile(deadTimeFile);
}
catch (std::exception&)
{
QMessageBox::information(this, "Mantid - MuonAnalysis", "A problem occurred while applying dead times.");
}
else
QMessageBox::information(this, "Mantid - MuonAnalysis", "The dead time file could not be found. No dead times will be applied.");
}

// Make the options available
Expand Down Expand Up @@ -2430,18 +2397,9 @@ std::vector<int> MuonAnalysis::spectrumIDs(const std::string& str) const
*/
void MuonAnalysis::changeCurrentRun(std::string & workspaceGroupName)
{
bool isAuto(false);

for (size_t i=0; i<workspaceGroupName.size(); ++i)
{
if (workspaceGroupName[i] == '_')
{
isAuto = true;
break;
}
}
QString tempGroupName(QString::fromStdString(workspaceGroupName) );

if (isAuto)
if ( (tempGroupName.contains("auto") ) || (tempGroupName.contains("argus0000000") ) )
{
Workspace_sptr workspace_ptr = AnalysisDataService::Instance().retrieve(m_workspace_name);
MatrixWorkspace_sptr matrix_workspace = boost::dynamic_pointer_cast<MatrixWorkspace>(workspace_ptr);
Expand All @@ -2450,7 +2408,12 @@ void MuonAnalysis::changeCurrentRun(std::string & workspaceGroupName)
std::string runNumber = runDetails.getProperty("run_number")->value();
QString instname = m_uiForm.instrSelector->currentText().toUpper();

for (size_t i=runNumber.size(); i<8; ++i)
int zeroPadding(8);

if (instname == "ARGUS")
zeroPadding = 7;

for (size_t i=runNumber.size(); i<zeroPadding; ++i)
{
runNumber = '0' + runNumber;
}
Expand Down Expand Up @@ -3148,11 +3111,19 @@ void MuonAnalysis::changeRun(int amountToChange)
{
QString filePath("");
QString currentFile = m_uiForm.mwRunFiles->getFirstFilename();
if (currentFile.isEmpty())
if ( (currentFile.isEmpty() ) )//|| (currentFile == "CURRENT RUN") )
currentFile = m_previousFilenames[0];

QString run("");
int runSize(-1);

// If load current run get the correct run number.
if (currentFile.contains("auto") || currentFile.contains("argus0000000"))
{
separateMuonFile(filePath, currentFile, run, runSize);
currentFile = filePath + getGroupName() + ".nxs";
}

separateMuonFile(filePath, currentFile, run, runSize);

int fileExtensionSize(currentFile.size()-currentFile.find('.') );
Expand All @@ -3168,7 +3139,7 @@ void MuonAnalysis::changeRun(int amountToChange)

getFullCode(runSize, newRun);

if (m_textToDisplay.contains("\\") || m_textToDisplay.contains("/") )
if (m_textToDisplay.contains("\\") || m_textToDisplay.contains("/") || m_textToDisplay == "CURRENT RUN")
m_uiForm.mwRunFiles->setUserInput(filePath + currentFile + newRun);
else
m_uiForm.mwRunFiles->setUserInput(newRun);
Expand Down Expand Up @@ -3211,18 +3182,19 @@ void MuonAnalysis::separateMuonFile(QString &filePath, QString &currentFile, QSt
}

runSize = 0;

//Find where the run number ends
for (int i = firstRunDigit; i<currentFile.size(); i++)
if (! (firstRunDigit < 0) )
{
if(currentFile[i] == '.')
break;
if(currentFile[i].isDigit())
//Find where the run number ends
for (int i = firstRunDigit; i<currentFile.size(); i++)
{
++runSize;
if(currentFile[i] == '.')
break;
if(currentFile[i].isDigit())
{
++runSize;
}
}
}

run = currentFile.right(currentFile.size()-firstRunDigit);
run = run.left(runSize);
}
Expand Down Expand Up @@ -3456,62 +3428,5 @@ void MuonAnalysis::deadTimeFileSelected()
}


/**
* Validate the file path of the dead time table.
*/
bool MuonAnalysis::isValidFile(const QString & fileName)
{
if (fileName == "")
return false;

try
{
Poco::File l_path( fileName.toStdString() );
if ( !l_path.exists() )
{
QString tempFilename;
if (fileName.contains('.'))
{
tempFilename = fileName.left(fileName.find('.'));
}
Poco::File l_path( tempFilename.toStdString() );
if ( !l_path.exists() )
{
m_updating = true;
QMessageBox::warning(this,"Mantid - MuonAnalysis", tempFilename + " does not exist.");
return false;
}
}
}
catch(std::exception&)
{
//Specified a network drive.
QString tempFilename;
if (fileName.contains('.'))
{
tempFilename = fileName.left(fileName.find('.'));
}
Poco::File l_path( tempFilename.toStdString() );
try
{
if ( !l_path.exists() )
{
m_updating = true;
QMessageBox::warning(this,"Mantid - MuonAnalysis", tempFilename + " does not exist.");
return false;
}
}
catch (std::exception&)
{
m_updating = true;
QMessageBox::warning(this,"Mantid - MuonAnalysis", tempFilename + " does not exist.");
return false;
}
}
// Must be valid so return true
return true;
}


}//namespace MantidQT
}//namespace CustomInterfaces
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ namespace MantidQt
void setText(const QString & value);
/// flag a problem with the file the user entered, an empty string means no error
void setFileProblem(const QString & message);
/// Get file problem, empty string means no error.
QString getFileProblem();
/// Read settings from the given group
void readSettings(const QString & group);
/// Save settings in the given group
Expand Down
11 changes: 10 additions & 1 deletion Code/Mantid/MantidQt/MantidWidgets/src/MWRunFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ void MWRunFiles::setFileProblem(const QString & message)
refreshValidator();
}

/**
* Return the error.
* @returns A string explaining the error.
*/
QString MWRunFiles::getFileProblem()
{
return m_fileProblem;
}

/**
* Save settings to the given group
Expand All @@ -500,6 +508,7 @@ void MWRunFiles::saveSettings(const QString & group)

settings.endGroup();
}

/** Writes the total number of periods in a file to the NumEntries
* Qlabel
* @param number the number to write, if this is < 1 a ? will be displayed in it's place
Expand All @@ -511,6 +520,7 @@ void MWRunFiles::setNumberOfEntries(const int number)
m_uiForm.numEntries->setText("/"+total);
}
}

/**
* Set the file text. This is different to setText in that it emits findFiles, as well
* changing the state of the text box widget to "modified = true" which is a prerequisite
Expand Down Expand Up @@ -546,7 +556,6 @@ void MWRunFiles::findFiles()
}
}


/**
* Called when the file finding thread finishes. Inspects the result
* of the thread, and emits fileFound() if it has been successful.
Expand Down

0 comments on commit dcd1c32

Please sign in to comment.