diff --git a/src/mlair/time_series.py b/src/mlair/time_series.py new file mode 100644 index 0000000000000000000000000000000000000000..c04160ee70f0c0ebcf7ee05126313fcabd8d40e3 --- /dev/null +++ b/src/mlair/time_series.py @@ -0,0 +1,55 @@ +import matplotlib.pyplot as plt +import xarray as xr +import pandas as pd +import datetime as dt + +def plot(figsize=(6, 3), input_dir="static/data/MLAIR", output_dir="mlair/assets/generated_plots"): + + # get all these from outside!!! + species = "O3" + station_label = "Bielefeld-Ost" + station_code = "DENW067" + start_date = "2018-07-18" + forecast_length = "4days" + units = "ppb" + + # prepare input and output + hourly_start_date = (dt.datetime.strptime(start_date, '%Y-%m-%d') - dt.timedelta(days=4)).strftime('%Y-%m-%d') + hourly_end_date = (dt.datetime.strptime(start_date, '%Y-%m-%d') - dt.timedelta(days=1)).strftime('%Y-%m-%d') + file_s = f"{input_dir}/forecasts_{station_code}_test.nc" + dataset = xr.open_dataset(file_s) + d4_fc = dataset['__xarray_dataarray_variable__'].sel(index=start_date,type='nn').data + # plot the values at the middle of the day --> offset 12:00 + d4_fc_times = [ (dt.datetime.strptime(start_date, '%Y-%m-%d') + dt.timedelta(days=i)).strftime('%Y-%m-%d 12:00') for i in range(4) ] + df_fc = pd.DataFrame({'fc_time': d4_fc_times, 'value': d4_fc}) + # plot the values at the middle of the day --> give an hourly period + df_fc['fc_time'] = pd.to_datetime(df_fc['fc_time']).dt.to_period('H') + file_s = f"{input_dir}/{station_code}_no_no2_o3.nc" + dataset = xr.open_dataset(file_s) + d4_hourly = dataset['__xarray_dataarray_variable__'].sel(datetime=slice(hourly_start_date,hourly_end_date),variables=species.lower()).data[0] + d4_hourly_times = dataset['datetime'].sel(datetime=slice(hourly_start_date,hourly_end_date)).data + df_hourly = pd.DataFrame({'hourly_time': d4_hourly_times, 'value': d4_hourly}) + df_hourly['hourly_time'] = pd.to_datetime(df_hourly['hourly_time']) + output_file_name = f"mlair_{species}_{station_code}_{start_date}_{forecast_length}.png" + + # plot + plt.figure(figsize=figsize) + ax = df_hourly.plot.line(x="hourly_time", y= "value", marker="o", label='Beobachtungen') + df_fc.plot.line(x="fc_time", y= "value", linestyle=" ", marker="o", ax=ax, label='Vorhersagen') + plt.title(f'{species}: {station_label}') + plt.xlabel('Datum') + locs, labels = plt.xticks() + xticks = [locs[0] + i * 24 for i in range(8) ] + xlabels = [(dt.datetime.strptime(hourly_start_date, '%Y-%m-%d') + dt.timedelta(days=i)).strftime('%Y-%m-%d') for i in range(8) ] + plt.xticks(xticks, xlabels, rotation=45) + plt.ylabel(f'Konzentration ({units})') + ax.grid(True) + plt.tight_layout() + plt.savefig(f"{output_dir}/{output_file_name}") + + return output_file_name + + +if __name__ == "__main__": + plot(input_dir = "/home/s.schroeder/mlworkflowinterface/src/static/data/MLAIR", + output_dir = "/home/s.schroeder/mlworkflowinterface/src/mlair/assets/generated_plots") diff --git a/src/pages/dashboard.py b/src/pages/dashboard.py index 18c87a8c37b50d3431c917d031fa6758456e712e..37840732f3bc6eb3f107161afb86b28144ea1f60 100644 --- a/src/pages/dashboard.py +++ b/src/pages/dashboard.py @@ -21,6 +21,7 @@ from deployment_settings import ( UNICORE_BASE, UFTP_BASE, UNICORE_USER, UNICORE_PASSWORD ) +from mlair.time_series import plot as plot_ml_time_series # the following should be done with static files! APP_HOME = Path.cwd() @@ -287,12 +288,15 @@ def generate_ml_fcast_body(language_id=0): ] def generate_ml_fcast_output_body(language_id): + ts_plot_name = plot_ml_time_series() + ts_plot_path = str(APP_HOME.joinpath("assets", "generated_plots", ts_plot_name)) + return [ - dbc.Row(dbc.Label(f"{start_date_label[language_id]}: 17 June 2017, ozone, Nordrhein Westfalen")), + dbc.Row(dbc.Label(f"{start_date_label[language_id]}: 18.07.2018, Ozon, Nordrhein Westfalen")), dbc.Row([ dbc.Col(dbc.Label("station:"), width=3), dbc.Col( - dcc.Dropdown(value=station_info[0], + dcc.Dropdown(value=station_info[87], options=station_info, ), width=6 ), @@ -345,12 +349,17 @@ ml_fcast_result_modal = html.Div([ ], id="ml_fcast_result_modal_container") def generate_eurad_im_body(language_id=0): + disabled_days = [ dt.datetime.strptime('2017-02-21','%Y-%m-%d') + dt.timedelta(days=i) for i in range(510) ] return [ dbc.Row([ dbc.Col(dbc.Label(date_label[language_id]), width=3), - dbc.Col(dcc.DatePickerSingle(date=dt.date.today(), + dbc.Col(dcc.DatePickerSingle(date=dt.date(2017, 1, 1), display_format=date_format[language_id], - first_day_of_week=first_day_of_week[language_id])), + first_day_of_week=first_day_of_week[language_id], + min_date_allowed=dt.date(2017, 1, 1), + max_date_allowed=dt.date(2018, 9, 1), + disabled_days=disabled_days, + initial_visible_month=dt.date(2017, 1, 1))), dbc.Col(dbc.Label(f"{forecast_length_label[language_id]}:")), dbc.Col(dcc.Dropdown(value=forecast_length_options[language_id][0], options=forecast_length_options[language_id])) ], class_name="row mt-3"),