-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheyenneSummaJob.py
executable file
·118 lines (97 loc) · 3.58 KB
/
cheyenneSummaJob.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os, shutil
import subprocess
# User-provided info
summaexe = '/glade/p/work/manab/fcast/newsumma/summa/bin/summa.exe'
masterdir = '/glade/p/work/manab/fcast/PNW/'
summafilemanname = 'summa_fileManager_new.txt'
pbstemplatename = 'template_pbs.txt'
restartflag = '-r never' #Options: [y,m,d,e,never]
logdname = 'log'
jobdname = 'joblists'
pbsdname = 'pbsscripts'
strtGRU = 1
endGRU = 11723
lenGRU = 10 #GRU Length of each run
numcores = 36 #Number of cores requested in each node
def concatDir(dname):
'''
Concatenates Master dir and another dname (directory name)
'''
filename = masterdir + dname
return(filename)
def purgeDir(folder):
'''
Purges contents of a directory
'''
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
def createJobs(strtGRU, endGRU, lenGRU):
'''
Creates a list of SUMMA jobs to be run
'''
runCommandList = []
for num in range(strtGRU, endGRU, lenGRU):
if (num + lenGRU < endGRU):
runCommand = [summaexe, '-g', str(num), str(lenGRU), restartflag, '-m',
summafileman, '>', os.path.join(logdir, str(num))]
runCommand = " ".join(runCommand)
runCommandList.append(runCommand)
else: #Ensuring that the last SUMMA job has correct length
runCommand = [summaexe, '-g', str(num), str(endGRU-num+1), restartflag, '-m', summafileman]
runCommand = " ".join(runCommand)
runCommandList.append(runCommand)
return(runCommandList)
def createFilenames(dir, prestring, ext):
'''
Creates names of joblists and pbsscripts
'''
listlen = numcores * lenGRU #Number of jobs in each joblist
listnum = endGRU//listlen + (endGRU % listlen > 0) #Total number of joblists required
listname = []
for num in range(0, listnum):
filename = os.path.join(dir, prestring + str(num))
filename = filename + ext
listname.append(filename)
return(listname)
def writeJobLists(runCommandList, joblist, numcores):
'''
Write jobs to joblist text files
'''
for x,y in enumerate(range(0, len(runCommandList), numcores)):
jobchunk = runCommandList[y : y + numcores]
with open(joblist[x], 'w') as file_handler:
for item in jobchunk:
file_handler.write("{}\n".format(item))
def pbsScripts(joblist):
'''
Creates PBS scripts for each joblist
'''
for p in range(0, len(joblist)):
with open(pbstemplate, "rt") as fin:
with open(pbslist[p], "wt") as fout:
for line in fin:
fout.write(line.replace('columbiaTest_NUMBER', 'PNW_SM_' + str(p)).
replace('JOBLIST', joblist[p]))
def submitCheyenne(pbslist):
for count, value in enumerate(pbslist):
subprocess.run(["qsub", value])
if __name__ == '__main__':
logdir = concatDir(logdname)
jobdir = concatDir(jobdname)
pbsdir = concatDir(pbsdname)
pbstemplate = concatDir(pbstemplatename)
summafileman = concatDir(summafilemanname)
purgeDir(logdir)
purgeDir(jobdir)
purgeDir(pbsdir)
runCommandList = createJobs(strtGRU, endGRU, lenGRU)
joblist = createFilenames(jobdir, 'summa_joblist_', '')
pbslist = createFilenames(pbsdir, 'pbs_', '.sh')
writeJobLists(runCommandList, joblist, numcores)
pbsScripts(joblist)
submitCheyenne(pbslist)