Skip to content

Commit

Permalink
Create output directories if they don't already exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
rplzzz committed Aug 22, 2015
1 parent 5c5985f commit a8aaed1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/gcam/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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] != '/':
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/gcam/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a8aaed1

Please sign in to comment.