# Order a list of cat ids, launch AOP workflow for each one and download locally.

import json
import time

from gbdxtools import Interface

start_time = time.time()

catalog_ids = ['1030010034AFCE00',
               '103001003696B200',
               '105041001126A900',
               '1050410011360600',
               '1050410011360700']

print 'Order imagery from GBDX'
gbdx = Interface()
order_id = gbdx.ordering.order(catalog_ids) 

# check order status
while True:
    states, locations = zip(*[(order['state'],order['location']) 
                            for order in gbdx.ordering.status(order_id)])
    if any(state != 'delivered' for state in states):
        time.sleep(300)
    else:
        break    

print 'Elapsed time: {} min'.format(round((time.time() - start_time)/60))

# where in bucket/prefix to store my AOP'ed imagery
s3_location = 'kostas/yunnanearthquake2014'

# launch AOP workflows
print 'Launch AOP workflows'
workflow_ids = [gbdx.workflow.launch_aop_to_s3(location,
                                               s3_location,
                                               enable_acomp='true',
                                               enable_pansharpen='true') for location in locations]

# check workflow status
pending_workflow_ids = workflow_ids
while len(pending_workflow_ids) > 0:
    print 'Pending workflows', pending_workflow_ids
    for workflow_id in pending_workflow_ids:
        result = gbdx.workflow.status(workflow_id)
        if result['state'] == 'complete':
            pending_workflow_ids.remove(workflow_id)
    time.sleep(300)

print 'Elapsed time: {} min'.format(round((time.time() - start_time)/60))

# download
gbdx.s3.download(s3_location)

@@ -80,20 +80,8 @@
 y.%0A%0A
-import json%0A
 impo
@@ -329,17 +329,17 @@
 %5D%0A%0Aprint
- 
+(
 'Order i
@@ -355,16 +355,17 @@
 om GBDX'
+)
 %0Agbdx = 
@@ -419,17 +419,16 @@
 log_ids)
- 
 %0A%0A# chec
@@ -500,16 +500,17 @@
 state'%5D,
+ 
 order%5B'l
@@ -519,18 +519,19 @@
 ation'%5D)
- 
 %0A
+  
         
@@ -701,20 +701,16 @@
 reak
-    
 %0A%0Aprint
- 
+(
 'Ela
@@ -763,29 +763,32 @@
  start_time)
-/
+ / 
 60))
+)
 %0A%0A# where in
@@ -902,17 +902,17 @@
 ws%0Aprint
- 
+(
 'Launch 
@@ -925,16 +925,17 @@
 rkflows'
+)
 %0Aworkflo
@@ -1319,17 +1319,17 @@
    print
- 
+(
 'Pending
@@ -1361,16 +1361,17 @@
 flow_ids
+)
 %0A    for
@@ -1580,17 +1580,17 @@
 )%0A%0Aprint
- 
+(
 'Elapsed
@@ -1647,13 +1647,16 @@
 ime)
-/
+ / 
 60))
+)
 %0A%0A# 

fix formatting