Skip to content
Snippets Groups Projects
Scalability Demonstration-checkpoint.ipynb 10.2 KiB
Newer Older
algodeveloper1@free.fr's avatar
up
algodeveloper1@free.fr committed
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import sys\n",
    "import os\n",
    "import xml.etree.ElementTree as ET\n",
    "import time\n",
    "import sys\n",
    "\n",
    "sys.path.insert(0, '/projects/owslib')\n",
    "from owslib.wps import WebProcessingService\n",
    "\n",
    "oauth_token = '4e911402-fb22-4e3c-aa25-8d96caf96768'\n",
    "copa_backend_url = \"https://gravitee-gateway.val.esa-maap.org/\"\n",
    "\n",
    "user_id='37275'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- GET CAPABILITIES---\n",
      "--- Service identification ---\n",
      "title:  WPS COPA layer\n",
      "type:  WPS 2.0.0\n"
     ]
    }
   ],
   "source": [
    "wps = WebProcessingService(copa_backend_url + 'wps/capabilities', \\\n",
    "                           headers = {'Authorization': 'Bearer ' + oauth_token}, version =\"2.0.0\")\n",
    "\n",
    "print(\"--- GET CAPABILITIES---\")\n",
    "print(\"--- Service identification ---\")\n",
    "print('title: ',wps.identification.title)\n",
    "print('type: ',wps.identification.type, wps.identification.version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Service Provider ---\n",
      "provider:  COPA Application caroline.ball@capgemini.com https://copa-backend-val.biomass-maap.com/\n"
     ]
    }
   ],
   "source": [
    "print(\"--- Service Provider ---\")\n",
    "print('provider: ',wps.provider.name,wps.provider.contact.email,wps.provider.url) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Service Operations ---\n",
      "getCapabilities\n",
      "execute\n",
      "getResult\n"
     ]
    }
   ],
   "source": [
    "print(\"--- Service Operations ---\")\n",
    "for operation in wps.operations:\n",
    "    print(operation.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Service Processes ---\n",
      "there are 11 processes available for execution\n",
      "5fe1c9ca6d8d6d0001b0e230 readl2 None\n",
      "5ff3301e91db790001274258 read This is a project description\n",
      "5fff1388493f610001f5178d read-s2 Read Sentinel 2 data\n",
      "5fff1388493f610001f5178b geocoding Geocodes an image using georeferencing information from a reference image\n",
      "5fff1388493f610001f5178a classification-mask Classify image using a vegetation, water and urban mask\n",
      "5fff1388493f610001f5178c s2index Compute Sentinel 2 index\n",
      "5fff1388493f610001f5178e resample Resample image\n",
      "600e8ca13d255d0001aef7c5 reads2 None\n",
      "600ef3b64f4c6300018f2c54 readstwo Read the tile\n",
      "6011904643325f0001558fa5 classificationmask Classify image using a vegetation, water and urban mask\n",
      "6011904643325f0001558fa6 indexstwo Compute Sentinel 2 index (NDVI, NDWI, NDBI, ...)\n"
     ]
    }
   ],
   "source": [
    "print(\"--- Service Processes ---\")\n",
    "print(\"there are \"+str(len(wps.processes))+\" processes available for execution\")\n",
    "for process in wps.processes:\n",
    "    print(process.identifier, process.title, process.abstract)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- EXECUTE---\n",
      "--- EXECUTE---\n",
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n",
      "<StatusInfo xmlns:xlin=\"http://www.w3.org/1999/xlink\" xmlns:se=\"http://www.opengis.net/se/2.0/core\" xmlns:fes=\"http://www.opengis.net/fes/2.1\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:wps=\"http://www.opengis.net/wps/2.0\" xmlns:ns2=\"http://www.w3.org/2005/Atom\" xmlns:wms=\"http://www.opengis.net/wms\" xmlns:ns3=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n",
      "    <wps:JobID>601d7a173d255d000167b452</wps:JobID>\n",
      "    <wps:Status>ACCEPTED</wps:Status>\n",
      "</StatusInfo>\n",
      "\n",
      "\n",
      "--- EXECUTE---\n",
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n",
      "<StatusInfo xmlns:xlin=\"http://www.w3.org/1999/xlink\" xmlns:se=\"http://www.opengis.net/se/2.0/core\" xmlns:fes=\"http://www.opengis.net/fes/2.1\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:wps=\"http://www.opengis.net/wps/2.0\" xmlns:ns2=\"http://www.w3.org/2005/Atom\" xmlns:wms=\"http://www.opengis.net/wms\" xmlns:ns3=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n",
      "    <wps:JobID>601d7a183d255d000167b454</wps:JobID>\n",
      "    <wps:Status>ACCEPTED</wps:Status>\n",
      "</StatusInfo>\n",
      "\n",
      "\n",
      "--- EXECUTE---\n",
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n",
      "<StatusInfo xmlns:xlin=\"http://www.w3.org/1999/xlink\" xmlns:se=\"http://www.opengis.net/se/2.0/core\" xmlns:fes=\"http://www.opengis.net/fes/2.1\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:wps=\"http://www.opengis.net/wps/2.0\" xmlns:ns2=\"http://www.w3.org/2005/Atom\" xmlns:wms=\"http://www.opengis.net/wms\" xmlns:ns3=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n",
      "    <wps:JobID>601d7a1a3d255d000167b456</wps:JobID>\n",
      "    <wps:Status>ACCEPTED</wps:Status>\n",
      "</StatusInfo>\n",
      "\n",
      "\n",
      "--- EXECUTE---\n",
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n",
      "<StatusInfo xmlns:xlin=\"http://www.w3.org/1999/xlink\" xmlns:se=\"http://www.opengis.net/se/2.0/core\" xmlns:fes=\"http://www.opengis.net/fes/2.1\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:wps=\"http://www.opengis.net/wps/2.0\" xmlns:ns2=\"http://www.w3.org/2005/Atom\" xmlns:wms=\"http://www.opengis.net/wms\" xmlns:ns3=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n",
      "    <wps:JobID>601d7a1c3d255d000167b458</wps:JobID>\n",
      "    <wps:Status>ACCEPTED</wps:Status>\n",
      "</StatusInfo>\n",
      "\n",
      "\n",
      "--- EXECUTE---\n",
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n",
      "<StatusInfo xmlns:xlin=\"http://www.w3.org/1999/xlink\" xmlns:se=\"http://www.opengis.net/se/2.0/core\" xmlns:fes=\"http://www.opengis.net/fes/2.1\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:wps=\"http://www.opengis.net/wps/2.0\" xmlns:ns2=\"http://www.w3.org/2005/Atom\" xmlns:wms=\"http://www.opengis.net/wms\" xmlns:ns3=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n",
      "    <wps:JobID>601d7a1d3d255d000167b45a</wps:JobID>\n",
      "    <wps:Status>ACCEPTED</wps:Status>\n",
      "</StatusInfo>\n",
      "\n",
      "\n",
      "--- EXECUTE---\n",
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n",
      "<StatusInfo xmlns:xlin=\"http://www.w3.org/1999/xlink\" xmlns:se=\"http://www.opengis.net/se/2.0/core\" xmlns:fes=\"http://www.opengis.net/fes/2.1\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:wps=\"http://www.opengis.net/wps/2.0\" xmlns:ns2=\"http://www.w3.org/2005/Atom\" xmlns:wms=\"http://www.opengis.net/wms\" xmlns:ns3=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n",
      "    <wps:JobID>601d7a1f3d255d000167b45c</wps:JobID>\n",
      "    <wps:Status>ACCEPTED</wps:Status>\n",
      "</StatusInfo>\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def wps_execute(user_id, algo_wps_xml):\n",
    "    \n",
    "    print(\"--- EXECUTE---\")\n",
    "    \n",
    "    url = copa_backend_url + 'wps?userId=' + user_id\n",
    "    tree = ET.parse(algo_wps_xml)\n",
    "    \n",
    "    response = requests.post(url, headers = \\\n",
    "                             {'Authorization': 'Bearer '+oauth_token, 'Content-Type': 'application/xml'},  \\\n",
    "                             data=ET.tostring(tree.getroot()))\n",
    "    print(response.text)\n",
    "    tree = ET.fromstring(response.text)\n",
    "    \n",
    "    return tree.find('{http://www.opengis.net/wps/2.0}JobID').text\n",
    "\n",
    "\n",
    "read_xml = '/projects/scalability-s2-jupyter/wps_readstwo.xml'\n",
    "\n",
    "print(\"--- EXECUTE---\")\n",
    "\n",
    "i = 0\n",
    "b = 6\n",
    "\n",
    "time1 = time.time()\n",
    "job = []\n",
    "\n",
    "################read##############\n",
    "\n",
    "while i < b:\n",
    "    jobId = wps_execute(user_id, read_xml)\n",
    "    job.append(jobId)\n",
    "    time.sleep(1)\n",
    "    i += 1\n",
    "i = 0\n",
    "\n",
    "while len(job) != 0:\n",
    "    \n",
    "    for Id in job:\n",
    "        \n",
    "        url = copa_backend_url + 'wps/getStatus?userId=' + user_id\n",
    "        body = { \"jobID\": Id }\n",
    "        response = requests.post(url, headers = {'Authorization': 'Bearer '+oauth_token},  json = body)\n",
    "        parsed = json.loads(response.text)\n",
    "        if parsed[\"status\"] == \"SUCCEEDED\":\n",
    "            job.remove(Id)\n",
    "        \n",
    "        \n",
    "    time.sleep(2)\n",
    "    \n",
    "time2 = time.time()\n",
    "final_time = (time2 - time1)\n",
    "print('execution time was', final_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}