Coordinator - better-wfp-00001 queue

This coordinator feeds the better-wfp-00001 data pipeline queue for the Sentinel-1 backscatter timeseries

  • First do the imports of the Python libraries required
In [1]:
import sys
import os

import owslib
from owslib.wps import monitorExecution
from owslib.wps import WebProcessingService
import json

import lxml.etree as etree

import cioppy

from shapely.wkt import loads
import getpass

import folium

from datetime import datetime, timedelta
import dateutil.parser

import requests

from nbconvert.preprocessors import ExecutePreprocessor, CellExecutionError
import nbformat as nbf
  • Read the data pipeline configuration information:
In [2]:
%store -r

nb_config = os.path.join('..', 'configuration.ipynb')

nb = nbf.read(nb_config, 4)

exec(nb['cells'][1]['source']) in globals(), locals()

app = dict([('artifact_id', app_artifact_id),
            ('version', app_version),
            ('repository', repository),
            ('community', community)])

app_process_id = '%s_%s_%s_%s' % (app['community'].replace('-', '_'),
                                  app['artifact_id'].replace('-', '_'),
                                  app['artifact_id'].replace('-', '_'),
                                  app['version'].replace('.', '_'))

trigger_queue = dict([('artifact_id', trigger_queue_artifact_id),
                      ('version', trigger_queue_version),
                      ('repository', repository),
                      ('folder', folder),
                      ('community', community)])

trigger_queue_process_id = '%s_%s_%s_%s' % (trigger_queue['community'].replace('-', '_'),
                                           trigger_queue['artifact_id'].replace('-', '_'),
                                           trigger_queue['artifact_id'].replace('-', '_'),
                                           trigger_queue['version'].replace('.', '_'))

print 'This notebook will create a coordinator for a queue to invoke the application %s with the trigger %s' % (app_process_id,
                                                                                                                trigger_queue_process_id)
This notebook will create a coordinator for a queue to invoke the application ec_better_ewf_wfp_01_01_01_ewf_wfp_01_01_01_1_16 with the trigger ec_better_tg_wfp_01_01_01_queue_tg_wfp_01_01_01_queue_0_9
  • Set the data transformation parameters
In [3]:
filter_size_x = '5'
filter_size_y = '5'
polarisation = 'VV'
geom = 'MULTIPOLYGON ( ((34.977154 10.632262, 32.722355 11.077297, 33.070683 12.826534, 35.340668 12.385893, 34.977154 10.632262)),((43.721626 4.577164,45.956501 5.041664,46.261715 3.533043,44.031803 3.064234,43.721626 4.577164)))'

wkt = loads(geom)[0].wkt
In [4]:
wkt
Out[4]:
'POLYGON ((34.977154 10.632262, 32.722355 11.077297, 33.070683 12.826534, 35.340668 12.385893, 34.977154 10.632262))'

Data selection parameters

In [5]:
series = 'https://catalog.terradue.com/sentinel1/description'
In [6]:
product_type = 'GRD'

Coordinator parameters

In [7]:
coordinator_name = 'co_%s_validation_queue1' % data_pipeline
My desired product parameters 'S1B_IW_GRDH_1SDV_20180425T151539_20180425T151604_010642_0136BA_441C' {'enddate': '2018-04-25T15:16:04.7582520Z', 'startdate': '2018-04-25T15:15:39.7599990Z', 'updated': '2018-04-25T22:30:02.1587710+00:00', 'dct:modified=2018-04-25T22:30:02.1587710Z/2018-04-25T22:30:02.1587711Z' ('update', '2017-10-12T11:05:06.319990Z/2017-10-12T11:05:06.320000Z' {'enddate': '2017-05-24T15:15:59.7999500Z', 'startdate': '2017-05-24T15:15:34.8016780Z', 'updated': '2017-05-25T17:28:02.0466360+00:00',
In [8]:
start_queue = '${coord:formatTime(coord:dateOffset(coord:nominalTime(), -0, \'DAY\'), "yyyy-MM-dd")}T11:05:06.319990Z'
stop_queue = '${coord:formatTime(coord:dateOffset(coord:nominalTime(), -0, \'DAY\'), "yyyy-MM-dd")}T11:05:06.320000Z'
In [9]:
co_trigger_queue_process_id = 'coordinator_%s' % trigger_queue_process_id

print (co_trigger_queue_process_id)
coordinator_ec_better_tg_wfp_01_01_01_queue_tg_wfp_01_01_01_queue_0_9
In [10]:
coordinator_date_start = '2017-10-12T00:00Z'
coordinator_date_stop = '2017-10-12T23:50Z'
coordinator_period = '0 0 * * *'

Common Parameters

In [11]:
tg_quotation = 'No'
recovery = 'No'
_T2Username = data_pipeline

Visual check on the AOI

In [12]:
lat = (loads(wkt).bounds[3]+loads(wkt).bounds[1])/2
lon = (loads(wkt).bounds[2]+loads(wkt).bounds[0])/2

zoom_start = 8

m = folium.Map(location=[lat, lon], zoom_start=zoom_start)

radius = 4
folium.CircleMarker(
    location=[lat, lon],
    radius=radius,
    color='#FF0000',
    stroke=False,
    fill=True,
    fill_opacity=0.6,
    opacity=1,
    popup='{} pixels'.format(radius),
    tooltip='I am in pixels',
).add_to(m)

locations = []

locations.append([t[::-1] for t in list(loads(wkt).exterior.coords)])

folium.PolyLine(
    locations=locations,
    color='#FF0000',
    weight=2,
    tooltip='',
).add_to(m)

m.save(os.path.join('maps', '%s_search.html' % data_pipeline))

m

Check data transformation application

In [13]:
wps_url_apps = '%s/zoo-bin/zoo_loader.cgi' % apps_deployer

wps = WebProcessingService(wps_url_apps, verbose=False, skip_caps=False)

found_process = False

message = "The process %s is not deployed" % app_process_id

for index, elem in enumerate(wps.processes):

    if elem.identifier == app_process_id:
        message = "The process %s is deployed" % app_process_id
        found_process = True

print message

if not found_process:
    raise Exception(message)
The process ec_better_ewf_wfp_01_01_01_ewf_wfp_01_01_01_1_16 is deployed

Check trigger coordinator

In [14]:
wps_url_triggers = '%s/zoo-bin/zoo_loader.cgi' % trigger_deployer

wps = WebProcessingService(wps_url_triggers, verbose=False, skip_caps=False)

found_process = False

message = "The queue coordinator process %s is not deployed" % co_trigger_queue_process_id

for index, elem in enumerate(wps.processes):

    if elem.identifier == co_trigger_queue_process_id:
        message = "The queue coordinator process %s is deployed" % co_trigger_queue_process_id
        found_process = True

print message

if not found_process:
    raise Exception(message)
The queue coordinator process coordinator_ec_better_tg_wfp_01_01_01_queue_tg_wfp_01_01_01_queue_0_9 is deployed
In [15]:
trigger_deployer
Out[15]:
'https://ec-better-triggers-deployer.terradue.com'

Feed the queue

In [16]:
process = wps.describeprocess(co_trigger_queue_process_id)

print process.title

print process.abstract
WFP-01-01-01 Trigger - Queue Coordinator
Coordinator: Trigger for the WFP-01-01-01 Sentinel-1 backscatter timeseries data pipeline - Queue
In [17]:
for data_input in process.dataInputs:
    print data_input.identifier
series
data_pipeline
wps_url
process_id
tg_quotation
api_key
update
geom
product_type
filterSizeX
filterSizeY
polarisation
wkt
t2_coordinator_date_start
t2_coordinator_date_stop
t2_coordinator_period
t2_coordinator_name
quotation
_T2Username

Define the input parameters

In [19]:
start_queue
Out[19]:
'${coord:formatTime(coord:dateOffset(coord:nominalTime(), -0, \'DAY\'), "yyyy-MM-dd")}T11:05:06.319990Z'
In [20]:
mode = 'Queue'

inputs = [('series', series),
          ('data_pipeline', data_pipeline),
          ('wps_url', wps_url_apps),
          ('process_id', app_process_id),
          ('update', '%s/%s' % (start_queue, stop_queue)),
          ('geom',  wkt.replace(' ', '%20').replace(',', '%2C')),
          ('product_type', product_type),
          ('tg_quotation', tg_quotation),
          ('api_key', datapipeline_api_key),
          ('filterSizeX', filter_size_x),
          ('filterSizeY', filter_size_y),
          ('polarisation', polarisation),
          ('wkt', wkt),
          ('t2_coordinator_date_start', coordinator_date_start),
          ('t2_coordinator_date_stop', coordinator_date_stop),
          ('t2_coordinator_period', coordinator_period),
          ('t2_coordinator_name', coordinator_name),
          ('quotation', tg_quotation),
          ('_T2Username', data_pipeline)]

Submit the coordinator request

In [21]:
execution = owslib.wps.WPSExecution(url=wps_url_triggers)

execution_request = execution.buildRequest(co_trigger_queue_process_id,
                                           inputs,
                                           output=[('coordinatorIds', False)])

execution_response = execution.submitRequest(etree.tostring(execution_request, pretty_print=True))

execution.parseResponse(execution_response)

execution.statusLocation

monitorExecution(execution)

if not execution.isSucceded():

    raise Exception('Coordinator %s creation failed' % co_trigger_queue_process_id)
In [22]:
coordinator_id = str(json.loads(execution.processOutputs[0].data[0])['coordinatorsId'][0]['oozieId'])
In [23]:
coordinator_id
Out[23]:
'0008058-181221095105003-oozie-oozi-C'

** DANGER ZONE **

In [ ]:
answer = raw_input('Are you sure you want to kill the coordinator %s (YES I DO to confirm)?' % coordinator_id)

if answer == 'YES I DO':
    r = requests.put('%s:11000/oozie/v1/job/%s?user.name=oozie&action=kill' % (production_centre, coordinator_id))
    if r.status_code:
        print 'Coordinator %s killed' % coordinator_id