Exploring model results
Explore the results returned by models
sapre sapphire-total-fluence get_result_names get_model_result_by_name get_variable_data

# coding=utf-8
"""
category: tutorial
title: Exploring model results
description: Explore the results returned by models
keywords: sapre, sapphire-total-fluence, get_result_names, get_model_result_by_name, get_variable_data
"""
from pprint import pprint

from nom_client.nom_client import NoMClient


"""
Initialise the NoM Client with a project name (optional) and the configuation data.
"""
nom_client = NoMClient(project_name="Exploring model results")


"""
Run the SAPPHIRE Total Fluence model
"""
trajectory_model = nom_client.get_model(model_name="trajectory")
trajectory_model.set_params(orbitSpecificationCode=205)
trajectory_result = nom_client.run_model(model=trajectory_model)

sapphire_model = nom_client.get_model(model_name="sapphire-total-fluence")
sapphire_model.set_external_input(external_input_name="trajectory", external_input=trajectory_result)
sapphire_result = nom_client.run_model(model=sapphire_model)


"""
The result object 'sapphire_result' is an instance of a 'ModelRun' class AND also a dictionary of 'NumericModelResult' classes.
The results contained within this ModelRun are documented on the model documentation pages
"""

# To list the available results from the model
print(sapphire_model.get_result_names())

# Or get the result names from the result object
print(sapphire_result.get_result_names())

# Or list the keys of the results dictionary
print(sapphire_result.keys())

# Loop over the model results
print("\n--- Model results ---")
for result_name, result in sapphire_result.items():
    print(result)

# Lets take a look at the 'full_ion_spectrum' result

print("\n--- full_ion_spectrum ---")

# Get the result using the 'get_model_result_by_name' method
# The 'full_ion_spectrum' is a NumericModelResult object. See online documentation for API description.
full_ion_spectrum = sapphire_result.get_model_result_by_name(result_name="full_ion_spectrum")
print(full_ion_spectrum)

# ... or access the result directly
full_ion_spectrum = sapphire_result["full_ion_spectrum"]
print(full_ion_spectrum)

# Lets list the variables within the 'full_ion_spectrum' result.
print("\n--- List the variables within the 'full_ion_spectrum' result' ---")
print(full_ion_spectrum.get_variable_names())


# Loop over the variables
print("\n--- Loop over the variables in the 'full_ion_spectrum' result' ---")
for variable_name, variable in full_ion_spectrum.items():
    print(variable)

# Grab the variables
energy_variable = full_ion_spectrum['energy']
species_variable = full_ion_spectrum['species']
integral_fluence_variable = full_ion_spectrum['integral_fluence']

"""
Looking at the details of the variables using 'get_metadata()'

'energy' is row-varying with a dimensionality of {'cols': 1, 'rows': 81}
'species' is column varying (not row-varying) with a dimensionality of {'cols': 92, 'rows': 1}
'integral_fluence' is row-varying with a dimensionality of {'cols': 92, 'rows': 81}
"""

print("\n--- 'energy' variable ---\n")
pprint(energy_variable.get_metadata())
print("\n--- 'species' variable ---\n")
pprint(species_variable.get_metadata())
print("\n--- 'integral_fluence' variable ---\n")
pprint(integral_fluence_variable.get_metadata())


# To get the actual data values we use the model result object method 'get_variable_data'
# To get the entire 'integral_fluence' dataset, energy vs. species

integral_fluence_data = full_ion_spectrum.get_variable_data(variable_name="integral_fluence")

"""
You can get more specific data by supplying additional parameters, in the case of 'integral fluence' 
we can specify any of its axes values, i.e. 'energy' or 'species'

We can print out the values for 'species' and then slice the fluence data further into a vector of values over energy
"""
species_data = full_ion_spectrum.get_variable_data(variable_name="species")
print("\n--- species data ---\n")
print(species_data)

# Get integral fluence data for species 'H'
species_data_H = full_ion_spectrum.get_variable_data(variable_name="integral_fluence", species="H")
print("\n--- species data (species: H)---\n")
print(species_data_H)

# Get all energy data
energy_data = full_ion_spectrum.get_variable_data(variable_name="energy")
print("\n--- energy data ---\n")
print(energy_data)

"""
Similarly we can specify species and an energy to reduce our data to a scalar value for that energy and species
"""
species_data_H_e = full_ion_spectrum.get_variable_data(variable_name="integral_fluence", species="H", energy=1.0e-01)
print("\n--- species data (energy: 1.0e-01, species: H)---\n")
print(species_data_H_e)