Newer
Older
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prerequisite:\n",
"\n",
"Run the following command in a terminal before launching the notebook:\n",
"\n",
"`conda install -y --freeze-installed --file requirements.txt`\n",
"\n",
"NB : make sure to be in the folder where you have the requirements.txt file\n"
]
},
"source": [
"import requests\n",
"import json\n",
"import sys\n",
"import os\n",
"import xml.etree.ElementTree as ET\n",
"import time\n",
"from wpsToolsBox import wpsTB\n",
"import glob\n",
"import numpy as np\n",
"import configparser\n",
"\n",
"try:\n",
" import wget\n",
"except :\n",
" !pip install wget\n",
" import wget\n",
" \n",
"try:\n",
" import rasterio\n",
"except:\n",
" !pip install rasterio\n",
" import rasterio\n"
]
},
{
"cell_type": "code",
"#Load the file that contains auth values\n",
"config = configparser.ConfigParser()\n",
"config.read('/projects/.maap/auth.ini')\n",
"#Retrieve auth values\n",
"user_id = config['auth']['user_id']\n",
"email = config['auth']['email']\n",
"password = config['auth']['password']\n",
"#Set env variables\n",
"os.environ[\"EMAIL\"] = email\n",
"os.environ[\"PASSWORD\"] = password"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"#Load the file that contains maap values\n",
"config = configparser.ConfigParser()\n",
"config.read('/projects/.maap/maap.ini')\n",
"#Retrieve maap values\n",
"copa_backend_url = config['maap']['copa_backend_url']\n",
"CLIENT_ID = config['maap']['client_id']\n",
"url_token = config['maap']['url_token']\n",
"url_gravitee_s3 = config['maap']['url_gravitee_s3']"
]
},
{
"cell_type": "code",
"response = requests.post(url_token, data=data={'client_id': CLIENT_ID, 'username': email, 'password': password, \"grant_type\": \"password\", \"scope\": \"profile\"})\n",
"oauth_token = data['access_token']"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"wps = wpsTB(oauth_token, copa_backend_url, user_id)"
]
},
{
"cell_type": "code",
"source": [
"wps.wps_processes()"
]
},
{
"cell_type": "code",
"source": [
"wps.wps_process_info('resample')"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"dir_path = '/projects/data/resample/'\n",
"\n",
"if not os.path.exists(dir_path):\n",
" os.makedirs(dir_path)"
]
},
{
"cell_type": "code",
"!maap-s3.py login $EMAIL $PASSWORD"
"metadata": {},
"source": [
"!maap-s3.py list 'maap-scientific-data/shared/demo/ratio'"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"while len(glob.glob('/projects/data/resample/ratio.tiff')) == 0:\n",
" !maap-s3.py download 'maap-scientific-data/shared/demo/ratio.tiff' '/projects/data/resample/ratio.tiff'\n",
" time.sleep(5)\n",
" \n",
"ratio_path = '/projects/data/resample/ratio.tiff'\n",
"dir_path = '/projects/data/resample/'\n",
"path_s3 = 'maap-scientific-data/shared/demo/ratio.tiff'"
"url = url_gravitee_s3+path_s3\n",
"\n",
"response = requests.get(url, headers = {'Authorization': 'Bearer '+oauth_token}, allow_redirects=False)\n",
"time.sleep(5)\n",
"print(response.text)\n",
"url_ratio = response.headers['Location']\n",
"print('url ratio :', url_ratio)"
]
},
{
"cell_type": "code",
"dirname = os.getcwd()\n",
"xml_file = os.path.join(dirname, 'xml_file/wps_resample.xml')\n",
"xml_file_out = os.path.join(dirname, 'resample_ratio.xml')\n",
"\n",
"wps.add_value(xml_file, 'size', '2', xml_file_out)\n",
"wps.add_value(xml_file_out, 'input_image', url_ratio, xml_file_out)\n",
"wps.add_value(xml_file_out, 'output', dir_path, xml_file_out)"
]
},
{
"cell_type": "code",
"source": [
"jobID = wps.wps_execute_process(xml_file_out, 1)"
]
},
{
"cell_type": "code",
"source": [
"print(\"--- GetStatus---\")\n",
"#FIXME retrieve the id of the user\n",
"url = copa_backend_url + 'wps/getStatus?userId=' + user_id\n",
" \n",
"body = { \"jobID\": jobID[0] }\n",
"response = requests.post(url, headers = {'Authorization': 'Bearer '+oauth_token}, json = body)\n",
"\n",
"parsed = json.loads(response.text)\n",
"\n",
"print(json.dumps(parsed, indent=2, sort_keys=True))"
]
},
{
"cell_type": "code",
"source": [
"print(\"--- GetResult---\")\n",
"#FIXME retrieve the id of the user\n",
"url = copa_backend_url + 'wps/'+ jobID[0] +'?userId=' + user_id\n",
"response = requests.get(url, headers = {'Authorization': 'Bearer '+oauth_token})\n",
"\n",
"print(response.text)"
]
},
{
"cell_type": "code",
"source": [
"response = wps.wps_result(jobID[0])\n",
"\n",
"url_ratio_resample = wps.get_url(response, 'ratio_resample.npy')\n",
"print(\"Url ratio_resample.npy :\", url_ratio_resample)\n",
"\n",
"ratio_resample_path = wget.download(url_ratio_resample, '/projects/data/resample/ratio_resample.npy')\n",
"\n"
"source": [
"ratio_resample_arr = np.load(ratio_resample_path, allow_pickle=True)\n",
"ratio_arr = rasterio.open(ratio_path).read(1)\n",
"\n",
"print(\"ratio shape :\", ratio_arr.shape)\n",
"print(\"ratio resample shape :\", ratio_resample_arr.shape)"
]
},
{
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(13,13))\n",
"ax1.imshow(ratio_arr, vmin=0, vmax=0.8)\n",
"ax1.set_title('ratio')\n",
"ax2.imshow(ratio_resample_arr, vmin=0, vmax=0.8)\n",
"ax2.set_title('ratio resample (x2)')"
]
},
{
"cell_type": "code",
"source": [
"print(\"---DeleteJob---\")\n",
"url = copa_backend_url + 'wps/dismiss?userId=' + user_id\n",
"body = { \"jobID\": jobID[0]}\n",
"#body = { \"jobID\": '62054cac310b5900011f25f0'}\n",
"response = requests.post(url, headers = {'Authorization': 'Bearer '+oauth_token}, json = body)\n",
"print(response.text)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Maap",
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
}
},
"nbformat": 4,
"nbformat_minor": 4
}