{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ESGF CWT subset example\n", "\n", "taken from esgf-api example:\n", "https://github.com/ESGF/esgf-compute-api/blob/master/examples/getting_started.ipynb\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib.wps import WebProcessingService" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get a new token from: https://aims2.llnl.gov/\n", "\n", "Login with your ESGF OpendID (LLNL works)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# token\n", "access_token = 'TOKEN'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# api_key should be in the HTTP headers\n", "client = WebProcessingService('https://aims2.llnl.gov/wps/', verify=True, headers={'COMPUTE-TOKEN': access_token})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### GetCapabilities\n", "https://aims2.llnl.gov/wps/?service=WPS&version=1.0.0&request=GetCapabilities" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for p in client.processes:\n", " print(p.identifier)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DescribeProcess\n", "https://aims2.llnl.gov/wps/?service=WPS&version=1.0.0&request=DescribeProcess&identifier=CDAT.subset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "proc = client.describeprocess(\n", " 'CDAT.subset'\n", ")\n", "proc.identifier" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for inpt in proc.dataInputs:\n", " print(inpt.identifier, inpt.dataType)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Domain" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib_esgfwps import Domain, Dimension" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d0 = Domain(dict(\n", " time=Dimension(0, 2000, crs='values'),\n", " lat=Dimension(-90, 0, crs='values'),\n", "))\n", "print(d0.json)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib_esgfwps import Variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# (122, 240, 480)\n", "data_url = 'http://aims3.llnl.gov/thredds/dodsC/css03_data/CMIP6/CMIP/NASA-GISS/GISS-E2-1-G/historical/r10i1p1f1/Amon/tas/gn/v20180830/tas_Amon_GISS-E2-1-G_historical_r10i1p1f1_gn_185001-190012.nc'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v0 = Variable(uri=data_url, var_name='tas')\n", "print(v0.json)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib_esgfwps import Operation\n", "op = Operation(name=proc.identifier, domain=d0, input=[v0])\n", "op.json" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib_esgfwps import Variables, Domains, Operations\n", "inputs = []\n", "inputs.append(('domain', Domains([d0])))\n", "inputs.append(('variable', Variables([v0])))\n", "inputs.append(('operation', Operations([op])))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for inp in inputs:\n", " print(inp[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute\n", "https://aims2.llnl.gov/wps/?service=WPS&version=1.0.0&request=Execute&identifier=CDAT.subset&api_key=TOKEN&DataInputs=domain={};variable={};operation={}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib.wps import SYNC, ASYNC\n", "exec = client.execute(proc.identifier, inputs=inputs, mode=ASYNC)\n", "print(exec.statusLocation)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO sync option is not working\n", "\n", "from owslib.wps import monitorExecution\n", "monitorExecution(exec)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"status: {}, progress: {}/100, message: {}\".format(exec.status, exec.percentCompleted, exec.statusMessage))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for output in exec.processOutputs:\n", " print(output.identifier, output.reference or output.data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot output from json document" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO: fix outputs\n", "import json\n", "output = json.loads(exec.processOutputs[0].data[0])\n", "output['uri']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "ds = xr.open_dataset(output['uri'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "ds.tas.isel(time=0).plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Show Output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from owslib_esgfwps import Outputs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# *TODO*: returns a list of str instead of dict\n", "# https://github.com/ESGF/esgf-compute-wps/issues/214\n", "\n", "outputs = Outputs.from_owslib(exec.processOutputs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# *TODO*: returns a list of str instead of dict\n", "\n", "outputs = Outputs.from_owslib(exec.processOutputs)" ] }, { "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }