Newer
Older
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
#!/usr/bin/env python3
"""
Copy the SEVIRI files at whole hours to a subdirectory
"""
import os
import argparse
import glob
import shutil
# Source/destination directories and matching glob patterns
SOURCE_DEST_DIRS = {
"/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/" :
["/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/HOURLY",
"*00_flat.nc"],
"/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/SEVIRI-CLOUDS/" :
["/viper/nadir/nvap/IcelandEurope_1088_0088_1537x494/HOURLY/SEVIRI-CLOUDS/",
"*0000_reg2d.nc"]
}
def copy_files(dry_run=False):
"""
Copy files
"""
for d_key, v_val in SOURCE_DEST_DIRS.items():
source_files = glob.glob(os.path.join(d_key, v_val[1]))
dest_files = [os.path.join(v_val[0], os.path.basename(f)) for f in source_files]
if dry_run:
# Print some of the files that would be copied
print("Copying files from \n", source_files[:10], "to\n", dest_files[:10])
if not dry_run:
for src, dest in zip(source_files, dest_files):
try:
shutil.copyfile(src, dest)
except Exception as e_err:
# Things that can go wrong: no write permission,
# file has been deleted in the meantime etc.
print("ERROR: Failed to copy file {} to {}\n{}".format(src, dest, e_err))
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="copy_hourly",
description=("Copy SEVIRI files."),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-r', '--run_copy',
dest='run_copy',
help="Run the copying script",
action='store_true',
default=False)
parser.add_argument('-d', '--dry_run',
dest='dry_run',
help="Run the copying script but only show the files that would be copied",
action='store_true',
default=False)
args = parser.parse_args()
if args.run_copy:
copy_files(dry_run=args.dry_run)
else:
print("\nNothing to do, here is some help:\n")
parser.print_help()