Newer
Older

Karine PARRA
committed
from datetime import date
from source.simulation_parameters import *
params = [f'PRIVATE-TOKEN: {os.environ["NAOS_TOKEN"]}']
studies = get_curl_json('https://forge.naos-cluster.tech/api/v4/groups/594/projects', params)
style = {'description_width': '200px'}
layout = widgets.Layout(width='auto', height='30px')

Karine PARRA
committed
layout_dates = widgets.Layout(width='auto', height='30px')
selected_study = 0
loaded_study_datas = None
loaded_study_model_name = ""
loaded_study_model_id = None
loaded_study_sources = None
widget_sources_selected_study = widgets.Text(

Karine PARRA
committed
disabled=True,
style=style,

Karine PARRA
committed
widget_model_selected_study = widgets.Text(

Karine PARRA
committed
disabled=True,
style=style,

Karine PARRA
committed
widget_result = widgets.HTML(

Karine PARRA
committed
style=style, layout=layout)

Karine PARRA
committed
def select_end_date(change):
if change['type'] == 'change' and change['name'] == 'value':

Karine PARRA
committed
global end_date
end_date = change['new'].strftime('%Y-%m-%dT%H:%M:%S.%fZ')

Karine PARRA
committed
widget_end_date = widgets.DatePicker(
description='End date :',
disabled=False,
value=date.today(),
style=style,
layout=layout_dates)
widget_end_date.observe(select_end_date)
end_date = widget_end_date.value.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

Karine PARRA
committed
def select_start_date(change):
if change['type'] == 'change' and change['name'] == 'value':

Karine PARRA
committed
global start_date
start_date = change['new'].strftime('%Y-%m-%dT%H:%M:%S.%fZ')
widget_end_date.value = change['new'] + timedelta(days=10)

Karine PARRA
committed
widget_start_date = widgets.DatePicker(
description='Study start date :',
disabled=False,
value=date.today() - timedelta(days=10),
style=style,
layout=layout_dates)

Karine PARRA
committed
widget_start_date.observe(select_start_date)
start_date = widget_start_date.value.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

Karine PARRA
committed
def select_study(change):
if change['type'] == 'change' and change['name'] == 'value':
global selected_study
global loaded_study_datas
global loaded_study_model_name
global loaded_study_model_id
global loaded_study_sources
selected_study = change['new']
loaded_study_datas = get_curl_json(
f'https://forge.naos-cluster.tech/api/v4/projects/{selected_study}', params)

Karine PARRA
committed
loaded_study_model_name = loaded_study_datas['forked_from_project']['path']
loaded_study_model_id = loaded_study_datas['forked_from_project']['id']
loaded_study_sources = ast.literal_eval(get_curl_json(
f'https://giros-fleuve.jumeaux-numeriques.fr/catalog-api/gitlab/projects/{loaded_study_model_id}'
f'/repository/files/sources-forcages%2Ejson/raw?ref=main'))
widget_model_selected_study.value = loaded_study_model_name
widget_sources_selected_study.value = '["' + '", "'.join(loaded_study_sources) + '"]'
widget_sources_selected_study.disabled = False
generate_files_button.disabled = False
widget_studies = widgets.Dropdown(
options=[(study["name"], study["id"]) for study in studies],
description='Select a study :',
value=None,
style=style,
layout=layout)

Karine PARRA
committed
widget_studies.observe(select_study)

Karine PARRA
committed
generate_files_button = widgets.Button(
description="Update forcing files",
disabled=True,

Karine PARRA
committed
def gererate_files(b):
widget_result.value = "In progress...."

Karine PARRA
committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
try:
sources_variables = ast.literal_eval(widget_sources_selected_study.value)
naos_token = os.environ.get("NAOS_TOKEN")
write_in_naos = (os.environ.get("WRITE_IN_NAOS") == 'True')
parameters = SimulationParameters(
model=loaded_study_model_name,
project_id=selected_study,
sources=sources_variables,
start_date=start_date,
end_date=end_date,
naos_token=naos_token,
write_in_naos=write_in_naos,
)
widget_result.value = parameters.create_simulation()["result"]
except ValueError as err:
widget_result.value = f"Wrong format for sources. Please use : '[\"source1\", \"source2\"]. Error : {err}"
generate_files_button.on_click(gererate_files)
widgets_list = [widget_studies,
widget_model_selected_study,
widget_sources_selected_study,
widget_start_date,
widget_end_date,
generate_files_button,
widget_result]