Skip to content

Commit ee59684

Browse files
author
Logan Karsten
committed
Update 7/31/2016
1 parent 6b31e1b commit ee59684

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

python/regridUtilities/argUtil.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# to ensure they make sense.
33

44
import os
5+
from regridUtilities.logMod import logErr, logInfo, logMaster
56

67
def checkArgs(parser):
78
# Check to make sure input directory exists
@@ -42,6 +43,11 @@ def checkArgs(parser):
4243
if not parser.shpPath:
4344
print "ERROR: One must specify either a polygon shapefil or geoGrid file"
4445
raise
46+
if parser.shpPath:
47+
if parser.geoPath:
48+
print "ERROR: One must choose either regridding to a geoGrid domain or \
49+
unstructured mesh"
50+
raise
4551
if parser.geoPath:
4652
if len(parser.geoPath) == 0:
4753
print "ERROR: geoPath not specified"
@@ -50,3 +56,29 @@ def checkArgs(parser):
5056
if len(parser.shpPath) == 0:
5157
print "ERROR: shpPath not specified"
5258
raise
59+
if not parser.shpUid:
60+
print "ERROR: shpUid not specified"
61+
raise
62+
if len(parser.shpUid) == 0:
63+
print "ERROR: shpUid not specified"
64+
raise
65+
66+
def parseShpPath(shpPath,outPath):
67+
# Utility function to decompose shapefile path, and return an output
68+
# NetCDF path to be used in pre-processing
69+
logInfo('Parsing shapefile path for pre-processing.')
70+
parse1 = shpPath.split('/')
71+
shpFile = parse1[len(parse1)-1]
72+
73+
parse2 = shpFile.split('.')
74+
if len(parse2) != 2:
75+
logErr('Unexpected shapefile path format')
76+
raise
77+
78+
segOut = ''
79+
for i in range(0,len(parse1)-1):
80+
segOut = segOut + parse1[i] + '/'
81+
82+
outFile = parse2[0] + '_ESMF_UNSTRUC.nc'
83+
outPath = segOut + outFile
84+

python/regridUtilities/logMod.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def initLog(pId,cDate,logDir):
1010
# Create log file path
1111
logPath = logDir + "/REGRID_JOB_RANK" + str(MPI_RANK) + "_PID" + \
1212
str(pId) + "_" + cDate + ".LOG"
13-
print logPath
1413
logging.basicConfig(filename=logPath,level=logging.DEBUG)
1514

1615
logging.info('Program initialized from rank: ' + str(MPI_RANK))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This is the pre-processing module file. Several sub-routines
2+
# are contained within here to handle pre-processing shapefiles
3+
# representing unstructured meshes and geoGrid files representing
4+
# the LSM modeling domain.
5+
6+
from regridUtilities.logMod import logErr, logInfo, logMaster
7+
8+
def shpConvert(shpPath,shpUid,nodeThres,tmpDir,rank):
9+
from utools.prep.prep_shapefiles import convert_to_esmf_format
10+
from regridUtilities.argUtil import parseShpPath
11+
12+
try:
13+
parseShpPath(shpPath,unstrucPath)
14+
except:
15+
raise
16+
17+
logMaster('Initializing pre-processing of unstructured mesh')
18+
convert_to_esmf_format(esmf_format, source, source_uid, \
19+
node_threshold=node_threshold)

regridRun.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Research Applications Laboratory
66

77
import sys, os
8+
from mpi4py import MPI
89

910
# Set path to include internal library
1011
cwd = os.getcwd()
@@ -14,10 +15,12 @@
1415
import sys, os
1516
import argparse
1617
from regridUtilities.argUtil import checkArgs
17-
from regridUtilities.logMod import initLog
18-
#from regridUtilities.preProc import shpConvert
18+
from regridUtilities.logMod import initLog, logErr, logMaster, logInfo
19+
from regridUtilities.preProcMod import shpConvert
1920
import datetime
20-
import logging
21+
22+
MPI_COMM = MPI.COMM_WORLD
23+
MPI_RANK = MPI_COMM.Get_rank()
2124

2225
def main(argv):
2326
# Parse arguments passed in.
@@ -43,22 +46,52 @@ def main(argv):
4346
# Specify output path to store NetCDF file.
4447
parser.add_argument('--shpPath', nargs='?', help='Input polygon shapefile')
4548
parser.add_argument('--geoPath', nargs='?', help='Input LSM geoGrid file.')
49+
parser.add_argument('--shpUid', nargs='?', help='Unique Identifier for Shapefile.')
50+
parser.add_argument('--nodeThresh', nargs='?', help='Number of nodes to cutoff.')
4651

4752
args = parser.parse_args()
4853

4954
# Ensure arguments provided by user are sane
50-
try:
51-
checkArgs(args)
52-
except:
53-
print "ERROR: Improper arguments passed."
54-
sys.exit(1)
55+
if MPI_RANK == 0:
56+
try:
57+
checkArgs(args)
58+
except:
59+
print "ERROR: Improper arguments passed."
60+
sys.exit(1)
61+
62+
# Set default node threshold. For any unstructured polygons with a number
63+
# of vertices above this number, they will be broken up to reduce
64+
# large ragged arrays.
65+
if not parser.nodeThresh:
66+
nodeThresh = 10000
5567

5668
# Initiate log file to store log messages on
5769
pId = os.getpid()
5870
cDate = datetime.datetime.now()
5971
cDate = cDate.strftime('%Y_%m_%d_%H_%M_%S')
6072
logDir = cwd + "/log"
61-
initLog(pId,cDate,logDir)
73+
initLog(pId,cDate,logDir)
74+
75+
# Establish temporary directory, which will hold temporary
76+
# files during pre-processing, weight generation, and regridding
77+
tmpDir = cwd + "/tmp"
78+
79+
# First check to see if weight file already exists. If it does,
80+
# forego pre-processing and go straight to pre-processing source
81+
# data for regridding.
82+
if os.path.isfile(parser.weightPath):
83+
try:
84+
shpConvert(shpPath,shpUid,nodeThresh,tmpDir,MPI_RANK)
85+
except:
86+
logErr('Pre-Processing of shapefile failed. Program exiting.')
87+
sys.exit(1)
88+
else:
89+
if parser.shpPath:
90+
91+
# Call routine to pre-process shp file
92+
elif parser.geoPath:
93+
# Call routine to pre-process geoGrid file
94+
6295

6396
if __name__ == "__main__":
6497
main(sys.argv[1:])

0 commit comments

Comments
 (0)