From a35984325aad8cc415b592059dc0d065ab12eb3d Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Wed, 2 May 2018 14:21:29 -0400 Subject: [PATCH 1/2] Allowing Catalyst to make directories if needed when doing IO. --- Wrapping/Python/paraview/coprocessing.py | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Wrapping/Python/paraview/coprocessing.py b/Wrapping/Python/paraview/coprocessing.py index a60644e501..198ec9f0ab 100644 --- a/Wrapping/Python/paraview/coprocessing.py +++ b/Wrapping/Python/paraview/coprocessing.py @@ -11,6 +11,11 @@ from paraview import simple, servermanager from paraview.vtk.vtkPVVTKExtensionsCore import * import math +# If the user created a filename in a location that doesn't exist by default we'll +# make the directory for them. This can be changed though by setting createDirectoriesIfNeeded +# to False. +createDirectoriesIfNeeded = True + # ----------------------------------------------------------------------------- def IsInModulo(timestep, frequencyArray): """ @@ -203,6 +208,23 @@ class CoProcessor(object): else: ts = str(timestep).rjust(paddingamount, '0') writer.FileName = fileName.replace("%t", ts) + if '/' in writer.FileName and createDirectoriesIfNeeded: + oktowrite = [1.] + import vtk + comm = vtk.vtkMultiProcessController.GetGlobalController() + if comm.GetLocalProcessId() == 0: + import os + newDir = writer.FileName[0:writer.FileName.rfind('/')] + try: + os.makedirs(newDir) + except OSError: + if not os.path.isdir(newDir): + print ("ERROR: Cannot make directory for", writer.FileName, ". No data will be written.") + oktowrite[0] = 0. + comm.Broadcast(oktowrite, 1, 0) + if oktowrite[0] == 0: + # we can't make the directory so no reason to update the pipeline + return writer.UpdatePipeline(datadescription.GetTime()) def WriteImages(self, datadescription, rescale_lookuptable=False, @@ -267,6 +289,24 @@ class CoProcessor(object): if dirname: cinema_dirs.append(dirname) else: + if '/' in fname and createDirectoriesIfNeeded: + oktowrite = [1.] + import vtk + comm = vtk.vtkMultiProcessController.GetGlobalController() + if comm.GetLocalProcessId() == 0: + import os + newDir = fname[0:fname.rfind('/')] + try: + os.makedirs(newDir) + except OSError: + if not os.path.isdir(newDir): + print ("ERROR: Cannot make directory for", fname, ". No image will be output.") + oktowrite[0] = 0. + comm.Broadcast(oktowrite, 1, 0) + if oktowrite[0] == 0: + # we can't make the directory so no reason to update the pipeline + return + if image_quality is None and fname.endswith('png'): # for png quality = 0 means no compression. compression can be a potentially # very costly serial operation on process 0 -- GitLab From f46b0daa89f748460ed16d4e9d9e3c1dcfae9e1d Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Wed, 2 May 2018 14:22:12 -0400 Subject: [PATCH 2/2] Deal with '/' characters in Catalyst channel names. When writing out the dataset for channels with a '/' character in the name we just remove the '/' from the channel name when making the file to be written out to. This avoids having to create the subdirectory for the filename. --- CoProcessing/Catalyst/vtkCPXMLPWriterPipeline.cxx | 5 ++++- Examples/Catalyst/SampleScripts/allinputsgridwriter.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CoProcessing/Catalyst/vtkCPXMLPWriterPipeline.cxx b/CoProcessing/Catalyst/vtkCPXMLPWriterPipeline.cxx index a76241733d..5bd2b06c7f 100644 --- a/CoProcessing/Catalyst/vtkCPXMLPWriterPipeline.cxx +++ b/CoProcessing/Catalyst/vtkCPXMLPWriterPipeline.cxx @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -174,7 +175,7 @@ int vtkCPXMLPWriterPipeline::CoProcess(vtkCPDataDescription* dataDescription) for (unsigned int i = 0; i < dataDescription->GetNumberOfInputDescriptions(); i++) { - const char* inputName = dataDescription->GetInputDescriptionName(i); + std::string inputName = dataDescription->GetInputDescriptionName(i); vtkCPInputDataDescription* idd = dataDescription->GetInputDescription(i); vtkDataObject* grid = idd->GetGrid(); if (grid == nullptr) @@ -206,6 +207,8 @@ int vtkCPXMLPWriterPipeline::CoProcess(vtkCPDataDescription* dataDescription) vtkSMStringVectorProperty* fileName = vtkSMStringVectorProperty::SafeDownCast(writer->GetProperty("FileName")); + // If we have a / in the channel name we take it out of the filename we're going to write to + inputName.erase(std::remove(inputName.begin(), inputName.end(), '/'), inputName.end()); std::ostringstream o; if (this->Path.empty() == false) { diff --git a/Examples/Catalyst/SampleScripts/allinputsgridwriter.py b/Examples/Catalyst/SampleScripts/allinputsgridwriter.py index 7b89f771ed..8e34888d3a 100644 --- a/Examples/Catalyst/SampleScripts/allinputsgridwriter.py +++ b/Examples/Catalyst/SampleScripts/allinputsgridwriter.py @@ -41,6 +41,7 @@ def CreateCoProcessor(): print("Don't know how to create a writer for a ", grid.GetClassName()) if extension: + name = name.translate(None, '/') # Get rid of any slashes in the channel name coprocessor.RegisterWriter(writer, filename=name+'_%t'+extension, freq=outputfrequency) return Pipeline() -- GitLab