-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregridRun.py
More file actions
97 lines (82 loc) · 3.61 KB
/
regridRun.py
File metadata and controls
97 lines (82 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Main running program for regridding using ESMF.
# Logan Karsten
# National Center for Atmospheric Research
# Research Applications Laboratory
import sys, os
from mpi4py import MPI
# Set path to include internal library
cwd = os.getcwd()
pathTmp = cwd + "/python"
sys.path.insert(0,pathTmp)
import sys, os
import argparse
from regridUtilities.argUtil import checkArgs
from regridUtilities.logMod import initLog, logErr, logMaster, logInfo
from regridUtilities.preProcMod import shpConvert
import datetime
MPI_COMM = MPI.COMM_WORLD
MPI_RANK = MPI_COMM.Get_rank()
def main(argv):
# Parse arguments passed in.
parser = argparse.ArgumentParser(description='Main program to regrid ' + \
'to either a modeling domain, or an ' + \
'an unstructured mesh grid.')
parser.add_argument('sourceDir', type=str, nargs='+',
help='Source directory containing data to be processed')
parser.add_argument('pattern', type=str, nargs='+',
help='Pattern of files to look for, I.E. - name.t12z')
parser.add_argument('inType', type=str, nargs='+',
help='Input type, either GRIB or NETCDF')
parser.add_argument('method', type=int, nargs='+',
help='Regrid method (1-2)')
# 1 = bilinear
# 2 = conservative 1st order
parser.add_argument('weightPath', type=str, nargs='+',
help='Weight file path')
# Specify path, even if file does not exist.
# It will be created if not found.
parser.add_argument('outPath', type=str, nargs='+',
help='Output NetCDF path')
# Specify output path to store NetCDF file.
parser.add_argument('--shpPath', nargs='?', help='Input polygon shapefile')
parser.add_argument('--geoPath', nargs='?', help='Input LSM geoGrid file.')
parser.add_argument('--shpUid', nargs='?', help='Unique Identifier for Shapefile.')
parser.add_argument('--nodeThresh', nargs='?', help='Number of nodes to cutoff.')
args = parser.parse_args()
# Ensure arguments provided by user are sane
if MPI_RANK == 0:
try:
checkArgs(args)
except:
print "ERROR: Improper arguments passed."
sys.exit(1)
# Set default node threshold. For any unstructured polygons with a number
# of vertices above this number, they will be broken up to reduce
# large ragged arrays.
if not parser.nodeThresh:
nodeThresh = 10000
# Initiate log file to store log messages on
pId = os.getpid()
cDate = datetime.datetime.now()
cDate = cDate.strftime('%Y_%m_%d_%H_%M_%S')
logDir = cwd + "/log"
initLog(pId,cDate,logDir)
# Establish temporary directory, which will hold temporary
# files during pre-processing, weight generation, and regridding
tmpDir = cwd + "/tmp"
# First check to see if weight file already exists. If it does,
# forego pre-processing and go straight to pre-processing source
# data for regridding.
if os.path.isfile(parser.weightPath):
try:
shpConvert(shpPath,shpUid,nodeThresh,tmpDir,MPI_RANK)
except:
logErr('Pre-Processing of shapefile failed. Program exiting.')
sys.exit(1)
else:
if parser.shpPath:
# Call routine to pre-process shp file
elif parser.geoPath:
# Call routine to pre-process geoGrid file
if __name__ == "__main__":
main(sys.argv[1:])