Skip to content

Instantly share code, notes, and snippets.

@robkooper
Last active January 28, 2021 05:42
Show Gist options
  • Save robkooper/27ef3ff076713a10c2dda75c64e8c48e to your computer and use it in GitHub Desktop.
Save robkooper/27ef3ff076713a10c2dda75c64e8c48e to your computer and use it in GitHub Desktop.
example python code using the pecan api
import sys
import time
import requests
# pecan-api-1
# server = "http://141.142.216.73"
# pecan-api-2
server = "http://141.142.218.196"
# check server status
response = requests.get(f"{server}/api/ping")
response.raise_for_status()
# get server info
response = requests.get(f"{server}/api/status")
response.raise_for_status()
server_info = response.json()['host_details']
print(f"Connected to server with ID={server_info['hostid']}")
# get all models
response = requests.get(f"{server}/api/models/")
response.raise_for_status()
models = response.json()['models']
print(f"There are {len(models)} models")
# find sipnet model, r136
sipnet_id = None
for model in models:
if model['model_name'] == 'SIPNET' and model['revision'] == 'r136':
sipnet_id = model['model_id']
if not sipnet_id:
print("Could not find model SIPNET r136")
sys.exit(1)
# create workflow object
workflow = {
"pfts": {
"pft": {
"name": "temperate.coniferous"
}
},
"meta.analysis": {
"iter": 100,
"random.effects": "FALSE",
"threshold": 1.2,
"update": "AUTO"
},
"ensemble": {
"size": 10,
"variable": "NPP"
},
"model": {
"id": sipnet_id
},
"run": {
"site": {
"id": 772
},
"inputs": {
"met": {
"id": "5000000005"
}
},
"start.date": "2002-01-01 00:00:00",
"end.date": "2002-12-31 00:00:00",
"dbfiles": "pecan/dbfiles"
}
}
# run workflow
result = requests.post(f"{server}/api/workflows/", json=workflow)
result.raise_for_status()
workflow_id = result.json()['workflow_id']
print(f"Submitted workflow with ID={workflow_id}")
print(f" webpage = {server}/pecan/05-running.php?workflowid={workflow_id}&hostname=docker")
# get workflow info
result = requests.get(f"{server}/api/workflows/{workflow_id}")
result.raise_for_status()
print(result.json())
# wait for status (workflow starts)
result = requests.get(f"{server}/api/workflows/{workflow_id}/status")
while result.status_code != 200:
result = requests.get(f"{server}/api/workflows/{workflow_id}/status")
print("Workflow has started.")
# wait a bit and show status
time.sleep(10)
result = requests.get(f"{server}/api/workflows/{workflow_id}/status")
result.raise_for_status()
print(result.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment