forked from manmeet3591/python_class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_links
More file actions
executable file
·122 lines (98 loc) · 4.25 KB
/
create_links
File metadata and controls
executable file
·122 lines (98 loc) · 4.25 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
"""create links for a CESM archive source directory to
a user specified destination directory.
"""
from __future__ import print_function
import sys
# check the system python version and require 2.7.x or greater
if sys.hexversion < 0x02070000:
print(70 * "*")
print("ERROR: {0} requires python >= 2.7.x. ".format(sys.argv[0]))
print("It appears that you are running python {0}".format(
".".join(str(x) for x in sys.version_info[0:3])))
print(70 * "*")
sys.exit(1)
#
# built-in modules
#
import argparse
import glob
import os
import shutil
# -------------------------------------------------------------------------------
# commandline_options - parse any command line options
# -------------------------------------------------------------------------------
def commandline_options():
"""Process the command line arguments.
"""
parser = argparse.ArgumentParser(
description='Create symbolic links between CESM source archive directory component monthly history files and a specified link directory for a range of specified years. Also copy the log files for ocean timeseries diagnostics.')
parser.add_argument('-sourcedir', '--sourcedir', required=True, nargs=1,
help='source archive directory')
parser.add_argument('-linkdir', '--linkdir', required=True, nargs=1,
help='link directory')
parser.add_argument('-startyear', '--startyear', required=True, nargs=1,
help='start year')
parser.add_argument('-endyear', '--endyear', required=True, nargs=1,
help='start year')
options = parser.parse_args()
return options
# -------------------------------------------------------------------------------
# main
# -------------------------------------------------------------------------------
def main(options):
""" main
"""
comps = {'atm':'cam.h0','ice':'cice.h','lnd':'clm2.h0','ocn':'pop.h','rof':'mosart.h0'}
logs = ['cesm.log','cpl.log','ocn.log']
months = ['01','02','03','04','05','06','07','08','09','10','11','12']
years = []
# create the years list
syear = int(options.startyear[0])
eyear = int(options.endyear[0])
while syear <= eyear:
years.append(str(syear).zfill(4))
syear += 1
# check if sourcedir exists
try:
os.path.isdir(options.sourcedir[0])
except:
raise OSError('{0} does not exist. Exiting...'.format(options.sourcedir[0]))
# check if linkdir exists
if not os.path.exists(options.linkdir[0]):
os.makedirs(options.linkdir[0])
else:
raise OSError('{0} already exists. Exiting...'.format(options.linkdir[0]))
# loop through the components, years and months
for comp in comps:
srcdir = '{0}/{1}/hist'.format(options.sourcedir[0], comp)
lkdir = '{0}/{1}/hist'.format(options.linkdir[0], comp)
if not os.path.exists(lkdir):
os.makedirs(lkdir)
else:
raise OSError('{0} already exists. Exiting...'.format(lkdir))
for year in years:
for month in months:
filepath = srcdir.split('/')
filename = '{0}.{1}.{2}-{3}.nc'.format(filepath[-3],comps[comp],year,month)
try:
os.symlink(os.path.join(srcdir, filename), os.path.join(lkdir, filename))
except:
raise OSError('Unable to create symlink {0}/{1} -> {2}/{3}. Exiting...'.format(srcdir, filename, lkdir, filename))
# copy the cesm, cpl, and ocn logs files as those files may be compressed and write permissions may be prohibitive
if not os.path.exists('{0}/logs'.format(options.linkdir[0])):
os.makedirs('{0}/logs'.format(options.linkdir[0]))
else:
raise OSError('{0}/logs already exists. Exiting...'.format(options.linkdir[0]))
for log in logs:
for file in glob.glob(r'{0}/logs/{1}.*.gz'.format(options.sourcedir[0],log)):
shutil.copy(file,'{0}/logs'.format(options.linkdir[0]))
#===================================
if __name__ == "__main__":
options = commandline_options()
try:
status = main(options)
sys.exit(status)
except Exception as error:
print(str(error))
sys.exit(1)