-
Notifications
You must be signed in to change notification settings - Fork 4
/
gee.py
302 lines (254 loc) · 8.88 KB
/
gee.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os
import subprocess
import argparse
import ee
from datetime import timedelta
#
# CONFIG
#
USER_ROOT=os.path.expanduser('~')
EE_CONFIG_PATH='{}/.config/earthengine'.format(USER_ROOT)
PEM_PATH='{}/.config/secret/privatekey.pem'.format(USER_ROOT)
NOISY=True
STATUS_PROPS=['description','state','id']
ALL='all'
OPENTASKS='opentasks'
FINISHED='finished'
CANCELLABLE=['READY','RUNNING','UNSUBMITTED']
COMPLETED=['COMPLETED','FAILED','CANCELLED','CANCEL_REQUESTED']
#
# INITIALIZATION/USERS
#
def init(user=None):
sa=os.environ.get('SERVICE_ACCOUNT')
if sa:
_out('init','SERVICE_ACCOUNT ({})'.format(sa))
ee.Initialize(ee.ServiceAccountCredentials(sa,PEM_PATH))
else:
if user: user_str='({})'.format(user)
else: user_str=''
_out('init','USER_ACCOUNT {}'.format(user_str))
ee.Initialize()
def current_user(state='CURRENT'):
os.system("echo 'gee.{}_USER:'".format(state))
os.system("cat {}/current_user.txt".format(EE_CONFIG_PATH))
def switch_user(user_name):
current_user('PREVIOUS')
os.system(
"rm {}/credentials-last".format(EE_CONFIG_PATH))
os.system(
"mv {path}/credentials {path}/credentials-last".format(
path=EE_CONFIG_PATH))
os.system(
"cp {path}/{user}/credentials {path}/credentials".format(
path=EE_CONFIG_PATH,
user=user_name))
os.system(
"echo '{}' > {}/current_user.txt".format(user_name,EE_CONFIG_PATH))
current_user('NEW')
#
# TASKS
#
def summary(task_id,detailed=True,props=None):
task_dict=get_status(task_id)
print "\t{}".format(_task_report(task_dict,props))
if detailed:
error_message=task_dict.get('error_message')
if error_message: print "\terror: {}".format(error_message)
tupdate = task_dict.get('update_timestamp_ms')
tstart = task_dict.get('start_timestamp_ms')
tcreate = task_dict.get('creation_timestamp_ms')
url = task_dict.get('output_url')
if tupdate:
if tstart: run_time = timedelta(milliseconds=tupdate-tstart)
else: run_time = "NOT RUNNING"
if tcreate: total_time = timedelta(milliseconds=tupdate-tcreate)
else: total_time = "NOT CREATED"
else:
run_time = "NO DATA"
total_time = "NO DATA"
if url: print "\toutput_url: {}".format(url[0])
print "\trun_time: {}".format(run_time)
print "\ttotal_time: {}".format(total_time)
def cancel(task_id=None,description=None,states=None,opentasks=False):
if task_id or description or states or opentasks:
if not states: states=OPENTASKS
task_list=get_tasks(
description=description,
task_id=task_id,
states=states,
opentasks=opentasks)
for task_dict in task_list:
task_id=task_dict.get('id')
ee.data.cancelTask(task_id)
_out('cancel',task_id)
else: _out('cancel','EMPTY REQUEST')
def tasks(n=25,task_id=None,description=None,states=None,
opentasks=False,props=None,return_list=False,print_tasks=True):
task_list=get_tasks(
description=description,
task_id=task_id,
states=states,
opentasks=opentasks)
task_reports=[]
for task_dict in task_list[:n]:
task_report=_task_report(task_dict,props)
if print_tasks: print task_report
task_reports.append(task_report)
if return_list: return task_reports
#
# Asset Manager
#
def asset_list(path,full_path=False):
project_root=os.environ.get('EE_PROJECT_ROOT')
if (not full_path) and project_root:
path='{}/{}'.format(project_root,path)
response=asset_cmd('ls',[path])
if response:
return response.split('\n')
else:
return False
def asset_mv(from_path,to_path,full_path=False):
project_root=os.environ.get('EE_PROJECT_ROOT')
if (not full_path) and project_root:
from_path='{}/{}'.format(project_root,from_path)
to_path='{}/{}'.format(project_root,to_path)
response=asset_cmd('mv',[from_path,to_path])
if response:
_out('asset_mv',response)
return False
else:
return True
def asset_cp(from_path,to_path,full_path=False):
project_root=os.environ.get('EE_PROJECT_ROOT')
if (not full_path) and project_root:
from_path='{}/{}'.format(project_root,from_path)
to_path='{}/{}'.format(project_root,to_path)
response=asset_cmd('cp',[from_path,to_path])
if response:
_out('asset_cp',response)
return False
else:
return True
def asset_cmd(cmd,args_list,grep=None):
if grep: args_list=args_list+['|','grep {}'.format(grep)]
(out,err)=subprocess.Popen(
['earthengine',cmd]+args_list,
stdout=subprocess.PIPE).communicate()
return (err or out).strip()
def asset_exists(path,full_path=False):
project_root=os.environ.get('EE_PROJECT_ROOT')
if (not full_path) and project_root:
path='{}/{}'.format(project_root,path)
root="/".join(path.split("/")[:-1])
assets=asset_list(root,True)
if assets: return path in assets
else: return False
#
# HELPERS
#
def get_status(task_id):
return ee.data.getTaskStatus(task_id)[0]
def get_tasks(task_id=None,description=None,states=None,opentasks=False):
if opentasks: states=CANCELLABLE
tasks=ee.data.getTaskList()
if states:
states=_get_states(states)
tasks=filter(lambda s: s['state'] in states,tasks)
if task_id:
tasks=filter(lambda s: task_id in s['id'],tasks)
if description:
tasks=filter(lambda s: description in s['description'],tasks)
return tasks
#
# INTERNAL
#
def _get_states(states):
if type(states) is str:
if states!=ALL:
if states==OPENTASKS:
states=CANCELLABLE
elif states==FINISHED:
states=COMPLETED
else:
states=states.split(',')
return states
def _out(trace_info,data):
if NOISY: print "gee.{}: {}".format(trace_info,data)
def _task_report(task_dict,props):
if not props: props=STATUS_PROPS
return " | ".join(map(lambda prop: str(task_dict.get(prop)),props))
#
# MAIN
#
def main():
parser=argparse.ArgumentParser(description='GEE HELPER')
subparsers=parser.add_subparsers()
# status
parser_status=subparsers.add_parser(
'status', help='GEE Task Status (consider using gee.summary)')
parser_status.add_argument('task_id',help='gee-task-id')
parser_status.set_defaults(func=_status)
# summary
parser_summary=subparsers.add_parser(
'summary', help='GEE Task Summary')
parser_summary.add_argument('task_id',help='gee-task-id')
parser_summary.add_argument(
'-d','--detailed',default='True',help='include details')
parser_summary.add_argument(
'-p','--props',help='comma seperated string of properties')
parser_summary.set_defaults(func=_summary)
# cancel
parser_cancel=subparsers.add_parser(
'cancel', help='Cancel Tasks')
parser_cancel.add_argument(
'-t','--task_id',help='any portion of gee-task-id')
parser_cancel.add_argument(
'-d','--description',help='any portion of gee-task-description')
parser_cancel.add_argument(
'-s','--states',help='commas seperated state names or one of all|opentasks')
parser_cancel.set_defaults(func=_cancel)
# tasks
parser_tasks=subparsers.add_parser('tasks', help='Prints summary of recent tasks')
parser_tasks.add_argument(
'-n','--num',default='50',
help='number of tasks to print')
parser_tasks.add_argument(
'-t','--task_id',
help='any portion of gee-task-id')
parser_tasks.add_argument(
'-d','--description',
help='any portion of gee-task-description')
parser_tasks.add_argument(
'-s','--states',
help='commas seperated state names or one of all|opentasks|finished')
parser_tasks.add_argument(
'-p','--props',
help='commas seperated property names to display')
parser_tasks.set_defaults(func=_tasks)
# user
parser_user=subparsers.add_parser('user', help='User Info')
parser_user.add_argument('-n','--new',help='new username')
parser_user.set_defaults(func=_user)
# run
args=parser.parse_args()
if (args.func.__name__)!='_user': init()
args.func(args)
def _status(args):
print get_status(args.task_id)
def _summary(args):
detailed=args.detailed.lower()=='true'
if args.props: props=args.props.split(',')
else: props=None
summary(args.task_id,detailed,props)
def _cancel(args):
cancel(args.task_id,args.description,args.states,False)
def _tasks(args):
if args.props: props=args.props.split(',')
else: props=None
tasks(int(args.num),args.task_id,args.description,args.states,False,props)
def _user(args):
if args.new: switch_user(args.new)
else: current_user()
if __name__ == "__main__":
main()