Model discovery and model exploration
Explore the models available and get the details of running and getting results from a model
niel ae8ap8 sapre describe_inputs get_external_inputs get_result_names get_model_result_by_name get_variable_data

# coding=utf-8
"""
category: tutorial
title: Model discovery and model exploration
description: Explore the models available and get the details of running and getting results from a model
keywords: niel, ae8ap8, sapre, describe_inputs, get_external_inputs, 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="Model discovery and model exploration", default_server_id="ext_rest_server")


"""
Find models with category 'radiation'
A dictionary is returned with model name as keys.
"""
with_radiation = nom_client.list_models(categories=["radiation"])
print(list(with_radiation.keys()))


"""
Find models with category 'effects'
"""
with_effects = nom_client.list_models(categories=["effects"])
print(list(with_effects.keys()))


"""
Find models with category 'radiation' AND 'effects'
"""
print(with_radiation.keys() & with_effects.keys())


"""
Explore the NIEL model

List the model inputs and their defaults
"""
niel_model = nom_client.get_model(model_name='niel')
print(niel_model.describe_inputs())

# Lets set the damageCurve to Huhtinen Si
niel_model.set_params(damageCurve=4)

# The NIEL model requires some external inputs to be satisfied before we can run it

pprint(niel_model.get_external_inputs())

"""
Lets give the NIEL model a trapped particle spectrum using the ae8ap8 model. Similar investigation of the ae8ap8 model
requires us to give the ae8ap8 model a trajectory
"""
sapre_model = nom_client.get_model(model_name="sapre")
sapre_result = nom_client.run_model(model=sapre_model)

ae8ap8_model = nom_client.get_model(model_name="ae8ap8")
ae8ap8_model.set_external_input(external_input_name="trajectory", external_input=sapre_result)
ae8ap8_result = nom_client.run_model(model=ae8ap8_model)


""" 
OK, now we can run the NIEL model after we add the trapped particle spectrum external input
Run the model
"""
niel_model.set_external_input(external_input_name="trappedParticleSpectrum", external_input=ae8ap8_result)
niel_results = nom_client.run_model(model=niel_model)
print(niel_results)


"""
List all of the model results from this model. This can be used to get the approriate model result 
from the results returned by the model
"""
print(niel_model.get_result_names())


# Lets get the 'shielded_trapped_proton_spectrum'
niel_result_data = niel_results.get_model_result_by_name(result_name="shielded_trapped_proton_spectrum")


# List all the data variable names for the 'shielded_trapped_proton_spectrum' result
pprint(niel_model.get_result_data_names(result_name="shielded_trapped_proton_spectrum"))

# Lets get the 'integral_fluence' data
integral_flux = niel_result_data.get_variable_data(variable_name="integral_fluence")

print(integral_flux)