forked from CMCC-Foundation/OceanAnalyticsLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockup.py
executable file
·88 lines (64 loc) · 2.25 KB
/
mockup.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
#!/usr/bin/env python
import os
import glob
import shutil
import sys
import check_json
wdir = "./indir"
def get_args():
import argparse
parse = argparse.ArgumentParser(description="Mockup method")
parse.add_argument('dirID', type=str, help="D4Science directory ID")
return parse.parse_args()
def filter_list(list_file, output_type):
"""
This function filters the input list and return a new list with the files that have
the type of files desired
@param list_file: list containing (id, file_name) pairs
@param output_type: file type you want to download from list_file
@return: list that contains only files of desired type
"""
llist = list()
for file in list_file:
if output_type in file[1]:
llist.append(file)
break # remove if you want to download all files
return llist
def make_indir(dirID):
"""
This function downloads the first file founded in directory with id = dirID
@dirID: D4Science directory ID from which download a file
"""
import json
import storagehubfacilitypython
if os.path.isdir(wdir):
print(wdir + ' already exists, please remove it')
return
os.mkdir("indir")
# GET CONTENT OF input_datasets/test_med_rea16 or input_datasets/appo
print("START ItemChildren")
try:
myshfo = storagehubfacilitypython.StorageHubFacility(operation="ItemChildren", ItemId=dirID)
myshfo.main()
except Exception as e:
print(e, file=sys.stderr)
raise Exception('Download error')
mobj = json.load(open('outFile'))
complete_list = check_json.get_id(mobj) # list of (id, name_file) pairs
filtered_list = filter_list(complete_list, '.nc')
# download
for x in filtered_list:
myshfo = storagehubfacilitypython.StorageHubFacility(operation="Download", ItemId=x[0],
localFile=wdir + "/" + x[1])
myshfo.main()
def main():
args = get_args()
dirId = args.dirID
make_indir(dirId)
# move only the first file in wdir and rename to output.nc
for file in glob.glob(wdir + "/*.nc"):
print(file)
shutil.move(file, "output.nc")
break
if __name__ == '__main__':
main()