From aff8205d503aa5742f57eee21f41c8575250a576 Mon Sep 17 00:00:00 2001 From: Ed Armstrong Date: Wed, 3 Jul 2024 16:29:42 -0700 Subject: [PATCH] Add files via upload MCC API tutorial --- mcc_api/metadata_compliance_checker_API.ipynb | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 mcc_api/metadata_compliance_checker_API.ipynb diff --git a/mcc_api/metadata_compliance_checker_API.ipynb b/mcc_api/metadata_compliance_checker_API.ipynb new file mode 100644 index 00000000..119c39bd --- /dev/null +++ b/mcc_api/metadata_compliance_checker_API.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# MCC - API POST\n", + "\n", + "Template for making API calls to Metadata Compliance Checker (MCC) using a local netCDF or HDF file, and outputing the response to JSON" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## MCC Endpoints\n", + "\n", + "PROD - https://mcc.podaac.earthdatacloud.nasa.gov/\n", + "\n", + "UAT - https://mcc.podaac.uat.earthdatacloud.nasa.gov/mcc\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "import requests\n", + "import sys\n", + "\n", + "# Choose VENUE: UAT or PROD\n", + "mcc_env = 'UAT'\n", + "mcc_env = 'PROD'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## MCC Dictionary - Environments\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "parameters" + ] + }, + "outputs": [], + "source": [ + "url_dict = {\n", + " 'UAT': \"https://mcc.podaac.uat.earthdatacloud.nasa.gov/mcc\",\n", + " 'PROD': \"https://mcc.podaac.earthdatacloud.nasa.gov/\"\n", + "}\n", + "\n", + "mcc_host = url_dict.get(mcc_env)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Print\n", + "print(mcc_env, \" - \" , mcc_host)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## MCC - API POST query example (for a local file)\n", + "\n", + "**See https://mcc.podaac.earthdatacloud.nasa.gov/mcc/about_api for a description of the query parameters (accessed via payload{} dictionary in this example)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "url = mcc_host + \"/check\"\n", + "\n", + "payload = {\n", + "'ACDD':'on',\n", + "'ACDD-version':'1.3',\n", + "'CF':'on',\n", + "'CF-version':'1.7',\n", + "'response':'json',\n", + "}\n", + "\n", + "\n", + "# set the path and filename to upload to MCC\n", + "dirname = \"my_local_path_to_the_file\"\n", + "filename = \"myFile.nc\"\n", + "\n", + "# working examples . . . comment out, modify or remove these two lines as needed\n", + "dirname = \"./data/\"\n", + "filename = \"ascat_20210101_000900_metopa_73696_eps_o_coa_3202_ovw.l2.nc\"\n", + "\n", + "\n", + "files=[\n", + " ('file-upload',\n", + " (\n", + " filename, \n", + " open(dirname+filename,'rb'),'application/octet-stream'\n", + "\n", + " )\n", + " )\n", + "]\n", + "\n", + "headers = {}\n", + "\n", + "# Ping the API; format the response in json; pretty print the json response\n", + "response = requests.request(\"POST\", url, headers=headers, data=payload, files=files)\n", + "json_resp = json.loads(response.text)\n", + "json_resp_formatted = json.dumps(json_resp, indent=2)\n", + "print(json_resp_formatted)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "celltoolbar": "Tags", + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}