|
1 |
import sys, os
|
|
2 |
from optparse import OptionParser
|
|
3 |
import glob
|
|
4 |
import shutil
|
|
5 |
|
|
6 |
#Creates a shell script that can be used to run stage 2 and 3 of the climate layer scripts. It sets up all the
|
|
7 |
#input arguments for master_script based on provided arguments.
|
|
8 |
#Inputs:
|
|
9 |
# inLatLonList: A text file that includes lat_lon of the tiles you want to process,one per line in format 10.0_-120.0
|
|
10 |
# outputFolder: The path to the dir where you want to place the output files (e.g. /nobackupp4/climateLayers/output20Deg/reg4/
|
|
11 |
# outDirForShFiles: The directory where you want to place the shell script serial files.
|
|
12 |
# filePerDir: How many sh files to put in each subdirectory, this is useful if you want to run a subset.
|
|
13 |
# lowStatCount: What's the minimum station count you want to use
|
|
14 |
# higStatCount: The max station count you want to use
|
|
15 |
# scriptFn: The filename of the master script you want to use to run stage4.
|
|
16 |
# For example 'Rscript /climateCode/environmental-layers/climate/research/world/interpolation/master_script_09012014_normal.R'
|
|
17 |
#Output:
|
|
18 |
# Places number of files into directory.
|
|
19 |
|
|
20 |
def main():
|
|
21 |
usage = "usage: %prog [options] <inLatLonList> <outputFolder> <outDirForShFiles> <filesPerDir> <lowStatCount> <highStatCount> <scriptFn>\n"+\
|
|
22 |
"Prepare input for stage 4 "
|
|
23 |
parser = OptionParser(usage=usage)
|
|
24 |
opts,args=parser.parse_args()
|
|
25 |
if len(args)<2:
|
|
26 |
sys.exit(parser.print_help())
|
|
27 |
|
|
28 |
inFn = args[0]
|
|
29 |
outputFolder = args[1]
|
|
30 |
outDir = args[2]
|
|
31 |
fPer = int(args[3])
|
|
32 |
lowTresh = int(args[4])
|
|
33 |
hiTresh = int(args[5])
|
|
34 |
scriptFn = args[6]
|
|
35 |
|
|
36 |
inPtr = open(inFn,"r")
|
|
37 |
if inPtr is None:
|
|
38 |
print "Error: %s doesnt' exist" % inFn
|
|
39 |
lines = inPtr.readlines()
|
|
40 |
inPtr.close()
|
|
41 |
|
|
42 |
found = 0
|
|
43 |
notFound = 0
|
|
44 |
|
|
45 |
if os.path.exists(outDir) is False:
|
|
46 |
print "Warning: %s doesn't exists, creating" % outDir
|
|
47 |
os.mkdir(outDir)
|
|
48 |
|
|
49 |
wrap = "%s/wrapper.sh" % outDir
|
|
50 |
if os.path.exists(wrap) is False:
|
|
51 |
print "Warning: %s doesn't exists, creating" % wrap
|
|
52 |
wrapFn = os.path.abspath(os.path.dirname(sys.argv[0]))+"/wrapper.sh"
|
|
53 |
shutil.copyfile(wrapFn,wrap)
|
|
54 |
os.chmod(wrap,0750)
|
|
55 |
|
|
56 |
qs = "%s/serial_TEMPLATE.qsub" % outDir
|
|
57 |
if os.path.exists(qs) is False:
|
|
58 |
print "Warning: %s doesn't exists, creating" % qs
|
|
59 |
qsFn = os.path.abspath(os.path.dirname(sys.argv[0]))+"/serial_TEMPLATE.qsub"
|
|
60 |
shutil.copyfile(qsFn,qs)
|
|
61 |
os.chmod(wrap,0750)
|
|
62 |
|
|
63 |
rankCount = 0
|
|
64 |
subDirNum = (len(lines)/fPer) + 2
|
|
65 |
|
|
66 |
for i in range(0,subDirNum):
|
|
67 |
subDirName = "%s/dirSub_%d" % (outDir,i)
|
|
68 |
if os.path.exists(subDirName) is False:
|
|
69 |
print "Creating %s" % subDirName
|
|
70 |
os.mkdir(subDirName)
|
|
71 |
|
|
72 |
dirCounter = 0
|
|
73 |
|
|
74 |
shpFiles = []
|
|
75 |
for l in lines:
|
|
76 |
l = l.rstrip().split(",")
|
|
77 |
print l[0]
|
|
78 |
searchStr = "%s/subset/shapefiles/shp_%s*.shp" % (outputFolder,l[0])
|
|
79 |
|
|
80 |
shpFn = glob.glob(searchStr)
|
|
81 |
if len(shpFn) > 0:
|
|
82 |
print shpFn
|
|
83 |
print shpFn[0]
|
|
84 |
shpFn = shpFn[0].replace('.shp','')
|
|
85 |
print shpFn
|
|
86 |
print shpFn.split('_')[3]
|
|
87 |
numStat = 1
|
|
88 |
shpFiles.append([shpFn,numStat])
|
|
89 |
else:
|
|
90 |
notFound += 1
|
|
91 |
print "Error finding %s" % searchStr
|
|
92 |
|
|
93 |
sortedShpFiles = sorted(shpFiles,key=lambda x: x[1])
|
|
94 |
|
|
95 |
|
|
96 |
for s in sortedShpFiles:
|
|
97 |
numStat = s[1]
|
|
98 |
if (numStat < lowTresh):
|
|
99 |
print s[0],numStat
|
|
100 |
print "Not enough stations",numStat
|
|
101 |
continue
|
|
102 |
|
|
103 |
if (numStat > hiTresh):
|
|
104 |
print s[0],numStat
|
|
105 |
print "Too many stations"
|
|
106 |
continue
|
|
107 |
|
|
108 |
baseFn = os.path.basename(s[0])
|
|
109 |
print baseFn
|
|
110 |
baseSpl = baseFn.split('_')
|
|
111 |
ll = "%s_%s" % (baseSpl[1],baseSpl[2])
|
|
112 |
|
|
113 |
metTest = "%s/%s/met_stations_outfiles_obj_gam_CAI_%s.RData" % (outputFolder,ll,ll)
|
|
114 |
|
|
115 |
if os.path.exists(metTest) is False:
|
|
116 |
print "No met object"
|
|
117 |
continue
|
|
118 |
|
|
119 |
methodTest = "%s/%s/method_mod_obj_gam_CAI_dailyTmax%s.RData" % (outputFolder,ll,ll)
|
|
120 |
if os.path.exists(methodTest) is True:
|
|
121 |
print "Method object exists"
|
|
122 |
continue
|
|
123 |
|
|
124 |
|
|
125 |
outStr = "%s %s wgs84Grid %s %s %s %s/subset/mean_LST_%s_jun_6_wgs84_10deg.tif FALSE %s/%s/covar_obj_%s.RData %s/%s/met_stations_outfiles_obj_gam_CAI_%s.RData 10 4800 > %s/outLogs/%s_stage4.log 2> %s/outLogs/%s_stage4_err.log" % (scriptFn,ll,ll,outputFolder,s[0],outputFolder,ll,outputFolder,ll,ll,outputFolder,ll,ll,outputFolder,ll,outputFolder,ll)
|
|
126 |
print outStr
|
|
127 |
|
|
128 |
outFnSt = "%s/dirSub_%d/input_%d.in" % (outDir,dirCounter,rankCount)
|
|
129 |
outPtr = open(outFnSt,"w+")
|
|
130 |
if outPtr is None:
|
|
131 |
print "Error: Opening %s" % outFnSt
|
|
132 |
else:
|
|
133 |
outPtr.write("#!/bin/bash"+"\n")
|
|
134 |
outPtr.write(outStr+"\n")
|
|
135 |
outPtr.close()
|
|
136 |
if rankCount > fPer:
|
|
137 |
dirCounter += 1
|
|
138 |
rankCount = 0
|
|
139 |
else:
|
|
140 |
rankCount += 1
|
|
141 |
outPtr.close()
|
|
142 |
found += 1
|
|
143 |
|
|
144 |
print "Found %d" % found
|
|
145 |
print "Not found %d" % notFound
|
|
146 |
print "End"
|
|
147 |
|
|
148 |
if __name__ == '__main__':
|
|
149 |
main()
|
|
150 |
|
Setup files to run stage 4 and 5