From a8aaed1ed3ddecd1b11510e7355c3da81036cf20 Mon Sep 17 00:00:00 2001 From: Robert Link Date: Sat, 22 Aug 2015 09:55:53 -0700 Subject: [PATCH] Create output directories if they don't already exist. --- src/gcam/modules.py | 17 +++++++++++++++-- src/gcam/util.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/gcam/modules.py b/src/gcam/modules.py index f488989..3e8cc98 100644 --- a/src/gcam/modules.py +++ b/src/gcam/modules.py @@ -518,6 +518,9 @@ def runmod(self): startmonth = 1 # Default is to start at the beginning of the year print '[HydroModule]: start month = %d' % startmonth + ## ensure that output directory exists + util.mkdir_if_noexist(outputdir) + ## get initial channel storage from historical hydrology ## module if available, or from self-parameters if not if 'historical-hydro' in self.cap_tbl: @@ -689,6 +692,9 @@ def runmod(self): startmonth = 1 # Default is January print '[HistoricalHydroModule]: start month = %d' % startmonth + ## ensure output directory exists + util.mkdir_if_noexist(outputdir) + if inputdir[-1] != '/': inputdir = inputdir + '/' if outputdir[-1] != '/': @@ -859,6 +865,10 @@ def runmod(self): rgnconfig = genparams['rgnconfig'] + ## ensure that output and temp directories exist + util.mkdir_if_noexist(outputdir) + util.mkdir_if_noexist(tempdir) + if 'inputdir' in self.params: inputdir = self.params['inputdir'] # static inputs, such as irrigation share and query files. else: @@ -994,7 +1004,7 @@ class NetcdfDemoModule(GcamModuleBase): forcing - forcing value (written into the output data as metadata) globalpop - 2050 global population (written into output data as metadata) pcGDP - 2050 per-capita GDP (written into output data as metadata -- currently not used anyhow) - outputdir - output directory + outfile - output file Module dependences: HydroModule, WaterDisaggregationModule @@ -1016,10 +1026,13 @@ def runmod(self): rcp = self.params['rcp'] pop = self.params['pop'] gdp = 10.0 # Dummy value; we didn't implement the GDP scenarios. - outfile = self.params['outfile'] + outfile = util.abspath(self.params['outfile']) mat2nc = util.abspath(self.params['mat2nc'],os.getcwd()) self.results['outfile'] = outfile + + ## ensure that the directory the output file is being written to exists + util.mkdir_if_noexist(os.path.dirname(outfile)) try: ## create a temporary file to hold the config diff --git a/src/gcam/util.py b/src/gcam/util.py index 4a9d807..80e1362 100644 --- a/src/gcam/util.py +++ b/src/gcam/util.py @@ -298,3 +298,37 @@ def abspath(filename,defaultpath=None, tag=None): return os.path.abspath(filename) else: return os.path.join(defaultpath,filename) + +def mkdir_if_noexist(dirname): + """Create a directory, if it doesn't exist already. + + This function will create the specified directory, along with any + intermediate directories, if they don't already exist. If the + directory already exists, nothing is done. If the directory + doesn't exist, and can't be created, an exception is raised. The + directory will be created with the default access mode of 0777, + which will be modified by the current umask in the usual way. + + Arguments: + dirname - Name of the directory to create + + Return value: none + + Exceptions: + OSError - The directory doesn't exist but can't be created. + Usually this means that either a non-directory + file type already exists at that name, or some + intermediate directory is not writeable by this UID. + + Limitations: If the directory already exists, this function + does not check to see if it is readable or + writeable by this UID. + """ + + try: + os.makedirs(dirname) + except OSError: + if os.path.isdir(dirname): + pass + else: + raise