forked from wrf-model/WPS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
link_grib.py
executable file
·68 lines (60 loc) · 2.27 KB
/
link_grib.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
#!/usr/bin/env python
# WRF-CMake (https://github.com/WRF-CMake/wps).
# Copyright 2018 M. Riechert and D. Meyer. Licensed under the MIT License.
import os
import sys
import shutil
import glob
import string
import itertools
import argparse
def link(src_path, link_path):
assert os.path.isfile(src_path)
if os.path.exists(link_path) or os.path.islink(link_path):
os.remove(link_path)
try:
# Windows: requires admin rights, but not restricted to same drive
os.symlink(src_path, link_path)
except:
# Windows: does not require admin rights, but restricted to same drive
os.link(src_path, link_path)
def link_or_copy(src, dst):
try:
link(src, dst)
except:
# fall-back for Windows if hard/sym links couldn't be created
shutil.copy(src, dst)
def generate_gribfile_extensions():
letters = list(string.ascii_uppercase)
for a, b, c in itertools.product(letters, repeat=3):
yield a + b + c
def collect_input_files(paths):
input_files = []
for path in paths:
if os.path.isdir(path):
for filename in os.listdir(path):
input_files.append(os.path.join(path, filename))
else:
input_files.append(path)
return input_files
def link_grib_files(input_paths, output_dir):
os.makedirs(output_dir, exist_ok=True)
for path in glob.glob(os.path.join(output_dir, 'GRIBFILE.*')):
os.remove(path)
paths = collect_input_files(input_paths)
grib_exts = generate_gribfile_extensions()
for path in paths:
try:
ext = next(grib_exts)
except StopIteration:
print('RAN OUT OF GRIB FILE SUFFIXES!', file=sys.stderr)
sys.exit(1)
link_path = os.path.join(output_dir, 'GRIBFILE.' + ext)
link_or_copy(path, link_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='A tool that symlinks GRIB files to GRIBFILE.??? format.')
parser.add_argument('paths', metavar='path', nargs='+', help='GRIB file or folder with GRIB files')
parser.add_argument('-o', '--out', metavar='output_dir', help='Output folder (default is current folder)',
default=os.getcwd())
args = parser.parse_args()
link_grib_files(args.paths, args.out)