Skip to content
Snippets Groups Projects
Commit dd8579d2 authored by Lemaire Louis's avatar Lemaire Louis
Browse files

[feature] Add save_csv function

Save mean soil/crop data from a nyears_data dictionnary
parent 26109a33
No related branches found
No related tags found
2 merge requests!32[feature] New module sky_model with Reinhart sky discretization scheme, water balance correction in GrasSim,!29Resolve "[feature] Save time series of mean variables in a csv file"
import pandas as pd
import numpy as np
import os
def save_csv(filename, nyears_data, variables):
"""
Save the mean values of the variables in the nyears_data dictionary to a CSV file.
Args:
filename: The name of the CSV file to save.
filename type: str
nyears_data: A dictionary containing the data for each year.
nyears_data type: dict
variables: A list of variables to save.
variables type: list
"""
records = []
for year in nyears_data:
missing_var = []
for var in variables:
if var in nyears_data[year]: # Ensure the variable exists in the dictionary
for date, array in nyears_data[year][var].items():
mean_value = np.mean(array)
records.append((date, var, mean_value))
else:
missing_var.append(var)
if missing_var:
print(f'Warning: variable(s) {missing_var} not in nyears_data for year {year}')
# Create a DataFrame
df = pd.DataFrame(records, columns=['Date', 'Variable', 'Mean'])
df['Date'] = pd.to_datetime(df['Date']) # Convert to datetime
df = df.pivot(index='Date', columns='Variable', values='Mean') # Reshape to have variables as columns
# Save to CSV
filepath = os.path.join('OUTPUTS', filename)
df.to_csv(filepath)
print('saved to', filepath)
......@@ -13,6 +13,7 @@ import pickle
from MODULES.user_support_tools import PASE_Logger
from MODULES.DATA_MANAGEMENT.yaml_inputs_provider import YAML_Inputs_provider, Inputs_aggregator
from MODULES.DATA_MANAGEMENT.weather_data_provider import Weather_data
from MODULES.DATA_MANAGEMENT.output.save_csv import save_csv
from MODULES.PHOTOVOLTAICS.configuration import PV_Configuration_3D
from MODULES.ENVIRONMENT.light import Sun_positions_sampled, Sun_positions, Light
from MODULES.ENVIRONMENT.light import show_light_map2, Ray_casting_scene
......@@ -200,6 +201,8 @@ Soil_plot, Crop_plot = run_crop_simu(crop_config, option_2D, WD.nyears_daily_dat
L.daily_irr_spat,
Loc_1)
save_csv('mean_data.csv', Crop_plot.nyears_data, ['Biomass', 'Dry_yield'])
show_light_map2(L.sourcepoints[:,:-1],
Crop_plot[0].nyears_data['2008']['BM'][datetime.strptime('2008-10-10 00:00:00', '%Y-%m-%d %H:%M:%S')]/100,
PV_1_3Dconfig.PV_central_PD,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment