loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
import requests

# Call the Amentum Atmosphere API to calculate total mass density using both NRLMSISE and JB2008 atmospheric model
url = "https://atmosphere.amentum.space/api/"

for endpoint in ["nrlmsise00", "jb2008"]:

  # assume midnight at Greenwich, arbitrary date
  payload = {
      'year' : 2019,
      'month' : 6,
      'day' : 16,
      'geodetic_latitude' : 51.47879,
      'geodetic_longitude' : 0,
      'altitude' : 100, # km
      'utc' : 0, # hours
  }

  # Only nrlmsise00 requires specification of indices, JB2008 fetches them automatically
  if endpoint == "nrlmsise00":
    payload.update(
      {
      'f107a' : 150, # nominal vals suggested by NRLMSISE-00 for alt <= 80 km 
      'f107' : 150, # SFU
      'ap' : 4,
      }
    )

  try:
    # API is called here, passing query parameters as inputs
    response = requests.get(url+endpoint, params=payload)
    # extract JSON payload of response as Python dictionary
    json_payload = response.json()
    # raise an Exception if we encoutnered any HTTP error codes like 404 
    response.raise_for_status()
  except requests.exceptions.ConnectionError as e: 
    # handle any typo errors in url or endpoint, or just patchy internet connection
    print(e)
  except requests.exceptions.HTTPError as e:  
    # handle HTTP error codes in the response
    print(e, json_payload['error'])
  except requests.exceptions.RequestException as e:  
    # general error handling
    print(e, json_payload['error'])
  else:
    # pretty print the result
    print(f"Response for {endpoint} atmospheric model")
    print(json.dumps(json_payload, indent=4, sort_keys=True))
Fetching token