-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessFullDirectory.py
181 lines (153 loc) · 4.47 KB
/
ProcessFullDirectory.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
"""
Author : Jeffrey Demieville
Date : 2023-06-20
Purpose: Test Dataset Generation
"""
import os
import sys
import argparse
import subprocess as sp
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description="ProcessFullDirectory",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-p",
"--path",
help="path containing level 0 data to process",
type=str,
required=True,
)
# Example: /iplant/home/shared/phytooracle/season_14_sorghum_yr_2022/level_0/scanner3DTop
parser.add_argument(
"-u",
"--user",
help="User account to use for data transfer",
type=str,
required=True,
)
# Example: jdemieville
parser.add_argument(
"-n",
"--number",
help="Number of measurements to include in the test dataset",
type=str,
required=True,
)
# Example: 90
parser.add_argument(
"-r",
"--random",
help="Choose files for the test dataset from a random position (default: middle of the scan)",
action="store_true",
required=False,
)
return parser.parse_args()
def get_file_list(data_path):
"""
List all files in path
Input:
- data_path: path to the input data on CyVerse data store
Output:
- List of files contained within data_path
"""
result = sp.run(f"ils {data_path}", stdout=sp.PIPE, shell=True)
files = result.stdout.decode("utf-8").split("\n")
return files
def generate_test_filename(infile):
"""
Generate a filename for the test dataset output.
Input:
- infile: input filename string
Output:
- outfile: output filename string
"""
split_filename = infile.split("-")
# split_filename[1] = "22" + split_filename[1][2:] #replace first two characters of year with 22
split_filename[3] = split_filename[3][:4] + "00" # replace hour component
split_filename[4] = "00" # replace minute component
split_filename[5] = "00" # replace second components
split_filename[6] = (
"000" + split_filename[6][3:-7]
) # replace millisecond component, remove extension
outfile = (
split_filename[0]
+ "-"
+ split_filename[1]
+ "-"
+ split_filename[2]
+ "-"
+ split_filename[3]
+ "-"
+ split_filename[4]
+ "-"
+ split_filename[5]
+ "-"
+ split_filename[6]
)
return outfile
def run_automator(input_file, username, output_filename, number, randomize):
"""
Run the automator
Input:
- input_file: full name of the input file, including extension
- username: username to use for file download and upload
- output_filename: name to use for the output test dataset, excluding extension
- number: number of directories to include in the test dataset
Output:
- Uploads test dataset to CyVerse datastore
"""
result = (
sp.run(
[
"./automator_random.sh",
"-p",
input_file,
"-u",
username,
"-t",
output_filename,
"-n",
number,
]
)
if randomize
else sp.run(
[
"./automator.sh",
"-p",
input_file,
"-u",
username,
"-t",
output_filename,
"-n",
number,
]
)
)
return result
def main():
print("getting args")
args = get_args()
print("getting file list")
files = get_file_list(args.path)
print("file list\n", files)
# Iterate through all dates within this season
for filename in files:
print("input filename: ", filename)
if filename.endswith(".tar.gz"):
filename = filename[2:]
print("corrected filename: ", filename)
outfile = generate_test_filename(filename)
print("outfile", outfile)
srcPath = ("/").join([args.path, filename])
print("srcPath", srcPath)
print("Randomize: ", args.random)
run_automator(srcPath, args.user, outfile, args.number, args.random)
# --------------------------------------------------
if __name__ == "__main__":
main()