#1642 Batch : Replace folder for input files

This commit is contained in:
Bjørnar Grip Fjær 2017-06-28 20:10:06 +02:00
parent 70d3c501a4
commit 6dac618af0
5 changed files with 190 additions and 39 deletions

View File

@ -1305,6 +1305,7 @@ bool RiaApplication::parseArguments()
progOpt.registerOption("size", "<width> <height>", "Set size of the main application window.", cvf::ProgramOptions::MULTI_VALUE);
progOpt.registerOption("replaceCase", "[<caseId>] <newGridFile>", "Replace grid in <caseId> or first case with <newgridFile>. Repeat parameter for multiple replace operations.", cvf::ProgramOptions::MULTI_VALUE, cvf::ProgramOptions::COMBINE_REPEATED);
progOpt.registerOption("replaceSourceCases", "[<caseGroupId>] <gridListFile>", "Replace source cases in <caseGroupId> or first grid case group with the grid files listed in the <gridListFile> file. Repeat parameter for multiple replace operations.", cvf::ProgramOptions::MULTI_VALUE, cvf::ProgramOptions::COMBINE_REPEATED);
progOpt.registerOption("replacePropertiesFolder", "[<caseId>] <newPropertiesFolder>", "Replace the folder containing property files for an eclipse input case.", cvf::ProgramOptions::MULTI_VALUE);
progOpt.registerOption("multiCaseSnapshots", "<gridListFile>", "For each grid file listed in the <gridListFile> file, replace the first case in the project and save snapshot of all views.", cvf::ProgramOptions::SINGLE_VALUE);
progOpt.registerOption("help", "", "Displays help text.");
progOpt.registerOption("?", "", "Displays help text.");
@ -1468,6 +1469,30 @@ bool RiaApplication::parseArguments()
projectLoadAction = PLA_CALCULATE_STATISTICS;
}
if (cvf::Option o = progOpt.option("replacePropertiesFolder"))
{
if (projectModifier.isNull()) projectModifier = new RiaProjectModifier;
if (o.valueCount() == 1)
{
QString propertiesFolder = cvfqt::Utils::toQString(o.safeValue(0));
projectModifier->setReplacePropertiesFolderFirstOccurrence(propertiesFolder);
}
else
{
size_t optionIdx = 0;
while (optionIdx < o.valueCount())
{
const int caseId = o.safeValue(optionIdx++).toInt(-1);
QString propertiesFolder = cvfqt::Utils::toQString(o.safeValue(optionIdx++));
if (caseId != -1 && !propertiesFolder.isEmpty())
{
projectModifier->setReplacePropertiesFolder(caseId, propertiesFolder);
}
}
}
}
loadProject(projectFileName, projectLoadAction, projectModifier.p());
}

View File

@ -21,6 +21,7 @@
#include "RimCaseCollection.h"
#include "RimEclipseCaseCollection.h"
#include "RimEclipseInputCase.h"
#include "RimEclipseResultCase.h"
#include "RimIdenticalGridCaseGroup.h"
#include "RimOilField.h"
@ -75,6 +76,25 @@ void RiaProjectModifier::setReplaceSourceCasesById(int caseGroupIdToReplace, std
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaProjectModifier::setReplacePropertiesFolderFirstOccurrence(QString newPropertiesFolder)
{
m_caseIdToPropertiesFolderMap[RiaProjectModifier::firstOccurrenceId()] = makeFilePathAbsolute(newPropertiesFolder);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaProjectModifier::setReplacePropertiesFolder(int caseIdToReplace, QString newPropertiesFolder)
{
if (caseIdToReplace >= 0)
{
m_caseIdToPropertiesFolderMap[caseIdToReplace] = makeFilePathAbsolute(newPropertiesFolder);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -90,6 +110,11 @@ bool RiaProjectModifier::applyModificationsToProject(RimProject* project)
replaceSourceCases(project);
}
if (m_caseIdToPropertiesFolderMap.size() > 0)
{
replacePropertiesFolder(project);
}
return true;
}
@ -167,6 +192,38 @@ void RiaProjectModifier::replaceCase(RimProject* project)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaProjectModifier::replacePropertiesFolder(RimProject* project)
{
std::vector<RimCase*> allCases;
project->allCases(allCases);
for (RimCase* rimCase : allCases)
{
RimEclipseInputCase* inputCase = dynamic_cast<RimEclipseInputCase*>(rimCase);
if (inputCase)
{
for (auto item : m_caseIdToPropertiesFolderMap)
{
int caseIdToReplace = item.first;
if (caseIdToReplace == RiaProjectModifier::firstOccurrenceId())
{
caseIdToReplace = firstInputCaseId(project);
}
if (caseIdToReplace == inputCase->caseId())
{
inputCase->updateAdditionalFileFolder(item.second);
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
/// Returns absolute path name to the specified file.
///
@ -236,6 +293,26 @@ int RiaProjectModifier::firstGroupId(RimProject* project)
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaProjectModifier::firstInputCaseId(RimProject * project)
{
std::vector<RimCase*> allCases;
project->allCases(allCases);
for (RimCase* rimCase : allCases)
{
RimEclipseInputCase* resultCase = dynamic_cast<RimEclipseInputCase*>(rimCase);
if (resultCase)
{
return resultCase->caseId();
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -47,21 +47,28 @@ public:
void setReplaceSourceCasesFirstOccurrence(std::vector<QString> newGridFileNames);
void setReplaceSourceCasesById(int caseGroupIdToReplace, std::vector<QString> newGridFileNames);
void setReplacePropertiesFolderFirstOccurrence(QString newPropertiesFolder);
void setReplacePropertiesFolder(int caseIdToReplace, QString newPropertiesFolder);
bool applyModificationsToProject(RimProject* project);
private:
void replaceSourceCases(RimProject* project);
void replaceCase(RimProject* project);
void replacePropertiesFolder(RimProject* project);
static QString makeFilePathAbsolute(QString relOrAbsolutePath);
static QString caseNameFromGridFileName(QString fullGridFilePathName);
static int firstCaseId(RimProject* project);
static int firstGroupId(RimProject* project);
static int firstInputCaseId(RimProject* project);
static int firstOccurrenceId();
private:
std::map<int, QString> m_caseIdToGridFileNameMap;
std::map<int, std::vector<QString> > m_groupIdToGridFileNamesMap;
std::map<int, QString> m_caseIdToPropertiesFolderMap;
};

View File

@ -42,6 +42,7 @@
#include "cafProgressInfo.h"
#include <QFileInfo>
#include <QDir>
CAF_PDM_SOURCE_INIT(RimEclipseInputCase, "RimInputReservoir");
//--------------------------------------------------------------------------------------------------
@ -53,12 +54,20 @@ RimEclipseInputCase::RimEclipseInputCase()
CAF_PDM_InitObject("RimInputCase", ":/EclipseInput48x48.png", "", "");
CAF_PDM_InitField(&m_gridFileName, "GridFileName", QString(), "Case File Name", "", "" ,"");
m_gridFileName.uiCapability()->setUiReadOnly(true);
CAF_PDM_InitFieldNoDefault(&m_additionalFileNames, "AdditionalFileNames", "Additional files", "", "" ,"");
m_additionalFileNames.uiCapability()->setUiReadOnly(true);
CAF_PDM_InitFieldNoDefault(&m_inputPropertyCollection, "InputPropertyCollection", "", "", "", "");
m_inputPropertyCollection = new RimEclipseInputPropertyCollection;
m_inputPropertyCollection->parentField()->uiCapability()->setUiHidden(true);
CAF_PDM_InitFieldNoDefault(&m_additionalFiles, "AdditionalFileNamesProxy", "Additional files", "", "", "");
m_additionalFiles.registerGetMethod(this, &RimEclipseInputCase::additionalFiles);
m_additionalFiles.uiCapability()->setUiReadOnly(true);
m_additionalFiles.xmlCapability()->setIOWritable(false);
CAF_PDM_InitFieldNoDefault(&m_additionalFilenames_OBSOLETE, "AdditionalFileNames", "Additional files", "", "" ,"");
m_additionalFilenames_OBSOLETE.uiCapability()->setUiReadOnly(true);
m_additionalFilenames_OBSOLETE.uiCapability()->setUiHidden(true);
m_additionalFilenames_OBSOLETE.xmlCapability()->setIOWritable(false);
}
@ -104,24 +113,24 @@ void RimEclipseInputCase::openDataFileSet(const QStringList& fileNames)
{
RiaPreferences* prefs = RiaApplication::instance()->preferences();
for (int i = 0; i < fileNames.size(); i++)
{
if (RifEclipseInputFileTools::openGridFile(fileNames[i], this->eclipseCaseData(), prefs->readerSettings->importFaults()))
{
m_gridFileName = fileNames[i];
for (int i = 0; i < fileNames.size(); i++)
{
if (RifEclipseInputFileTools::openGridFile(fileNames[i], this->eclipseCaseData(), prefs->readerSettings->importFaults()))
{
m_gridFileName = fileNames[i];
QFileInfo gridFileName(fileNames[i]);
QString caseName = gridFileName.completeBaseName();
QFileInfo gridFileName(fileNames[i]);
QString caseName = gridFileName.completeBaseName();
this->caseUserDescription = caseName;
this->caseUserDescription = caseName;
this->eclipseCaseData()->mainGrid()->setFlipAxis(flipXAxis, flipYAxis);
this->eclipseCaseData()->mainGrid()->setFlipAxis(flipXAxis, flipYAxis);
computeCachedData();
computeCachedData();
break;
}
}
break;
}
}
}
if (this->eclipseCaseData()->mainGrid()->gridPointDimensions() == cvf::Vec3st(0,0,0))
@ -129,29 +138,26 @@ void RimEclipseInputCase::openDataFileSet(const QStringList& fileNames)
return ; // No grid present
}
// Then read the properties possibly in the grid file
QStringList filesToRead;
for (int i = 0; i < fileNames.size(); i++)
std::vector<QString> filesToRead;
for (const QString& filename : fileNames)
{
size_t j;
bool exist = false;
for (j = 0; j < m_additionalFileNames().size(); j++)
bool exists = false;
for (const QString& currentFileName : additionalFiles())
{
if (m_additionalFileNames()[j] == fileNames[i])
if (filename == currentFileName)
{
exist = true;
exists = true;
break;
}
}
if (!exist)
if (!exists)
{
filesToRead.push_back(fileNames[i]);
filesToRead.push_back(filename);
}
}
for (int i = 0; i < filesToRead.size(); i++)
for (const QString& propertyFileName : filesToRead)
{
QString propertyFileName = filesToRead[i];
std::map<QString, QString> readProperties = RifEclipseInputFileTools::readProperties(propertyFileName, this->eclipseCaseData());
std::map<QString, QString>::iterator it;
@ -164,11 +170,6 @@ void RimEclipseInputCase::openDataFileSet(const QStringList& fileNames)
inputProperty->resolvedState = RimEclipseInputProperty::RESOLVED;
m_inputPropertyCollection->inputProperties.push_back(inputProperty);
}
if (propertyFileName != m_gridFileName)
{
m_additionalFileNames.v().push_back(propertyFileName);
}
}
}
@ -251,7 +252,11 @@ void RimEclipseInputCase::loadAndSyncronizeInputProperties()
// Then read the properties from all the files referenced by the InputReservoir
std::vector<QString> filenames = m_additionalFileNames;
std::vector<QString> filenames;
for (const RimEclipseInputProperty* inputProperty : m_inputPropertyCollection()->inputProperties())
{
filenames.push_back(inputProperty->fileName);
}
filenames.push_back(m_gridFileName);
size_t inputPropCount = this->m_inputPropertyCollection()->inputProperties.size();
@ -392,7 +397,7 @@ void RimEclipseInputCase::defineUiOrdering(QString uiConfigName, caf::PdmUiOrder
uiOrdering.add(&caseUserDescription);
uiOrdering.add(&caseId);
uiOrdering.add(&m_gridFileName);
uiOrdering.add(&m_additionalFileNames);
uiOrdering.add(&m_additionalFiles);
auto group = uiOrdering.addNewGroup("Case Options");
group->add(&activeFormationNames);
@ -422,8 +427,37 @@ void RimEclipseInputCase::updateFilePathsFromProjectPath(const QString& newProje
m_gridFileName = RimTools::relocateFile(m_gridFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
for (size_t i = 0; i < m_additionalFileNames().size(); i++)
for (RimEclipseInputProperty* inputProperty : m_inputPropertyCollection()->inputProperties())
{
m_additionalFileNames.v()[i] = RimTools::relocateFile(m_additionalFileNames()[i], newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
inputProperty->fileName = RimTools::relocateFile(inputProperty->fileName, newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimEclipseInputCase::updateAdditionalFileFolder(const QString& newFolder)
{
QDir newDir(newFolder);
for (RimEclipseInputProperty* inputProperty : m_inputPropertyCollection()->inputProperties())
{
if (inputProperty->fileName == m_gridFileName) continue;
QFileInfo oldFilePath(inputProperty->fileName);
QFileInfo newFilePath(newDir, oldFilePath.fileName());
inputProperty->fileName = newFilePath.absoluteFilePath();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<QString> RimEclipseInputCase::additionalFiles() const
{
std::vector<QString> additionalFiles;
for (const RimEclipseInputProperty* inputProperty : m_inputPropertyCollection()->inputProperties())
{
if (inputProperty->fileName == m_gridFileName) continue;
additionalFiles.push_back(inputProperty->fileName);
}
return additionalFiles;
}

View File

@ -23,6 +23,7 @@
#include "cafPdmChildField.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include "cafPdmProxyValueField.h"
#include "cvfBase.h"
#include "cvfObject.h"
@ -61,6 +62,11 @@ public:
virtual void updateFilePathsFromProjectPath(const QString& projectPath, const QString& oldProjectPath);
void updateAdditionalFileFolder(const QString& newFolder);
private:
std::vector<QString> additionalFiles() const;
protected:
virtual void defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) override;
@ -68,7 +74,9 @@ private:
cvf::ref<RifReaderInterface> createMockModel(QString modelName);
// Fields
caf::PdmField<std::vector<QString> > m_additionalFileNames;
caf::PdmField<QString> m_gridFileName;
caf::PdmField<QString> m_gridFileName;
caf::PdmProxyValueField< std::vector<QString> > m_additionalFiles;
// Obsolete fields
caf::PdmField<std::vector<QString> > m_additionalFilenames_OBSOLETE;
};