Saving and loading models#
This notebook introduces the concept of saving and loading .gwref files.
This notebook can be downloaded from the source code here.
Saving a model 💾#
You can save a gwrefpy model to a .gwref file using the save_project function of the Model class. This function saves the model, including all wells and fits, to a specified file.
We will first create a simple model with one observed well and one reference well.
import gwrefpy as gr
import numpy as np
import pandas as pd
# Creat the observed and reference wells
obs1 = gr.Well(name="Obs. well", is_reference=False)
obs1.add_timeseries(values_obs1)
ref1 = gr.Well(name="Ref. well", is_reference=True)
ref1.add_timeseries(values_ref1)
# Create the model and add the wells
model1 = gr.Model(name="IO Model")
model1.add_well(obs1)
model1.add_well(ref1)
We can now save the model to a .gwref file using the save_project function.
Note
The funciton will create the file in the current working directory if no path is specified.
# Save the model to a .gwref file
model1.save_project("my_model.gwref")
Model 'IO Model' saved to 'my_model.gwref'.
You can now find the my_model.gwref files in your working directory. You can also give a path with the filename and the fild will be saved to that path, e.g. "path//to//folder//my_model.gwref".
Warning
If the file already exists, a warning will be logged and the file will not be overwritten. You can set the overwrite variable to True to overwrite the existing file.
The funciton will not create any folders in the specified path if they do not exist.
# Save the model to a .gwref file, allowing overwriting
model1.save_project("my_model.gwref") # This will not overwrite the existing file and a warning will be logged
model1.save_project("my_model.gwref", overwrite=True) # This will overwrite the existing file
The file 'my_model.gwref' already exists. To overwrite the existing file set the argument 'overwrite' to True.
Model 'IO Model' saved to 'my_model.gwref'.
Loading Models 📦#
You can load a model from a .gwref file using the open_project function from the Model class. This function reads the model from the specified file and saves it to the Model object. The model will include all wells and fits that were saved in the file.
There are two ways to load a model:
Create a new
Modelobject and use theopen_projectmethod.Specify the
.gwreffile directly when creating a newModelobject.
# Method 1: Create a new Model object and load the project
loaded_model1 = gr.Model(name="Name will be overwritten")
loaded_model1.open_project("my_model.gwref")
Model 'IO Model' loaded from 'my_model.gwref'.
# Method 2: Directly load the project into a new Model object
loaded_model2 = gr.Model("my_model.gwref")
Model 'IO Model' loaded from 'my_model.gwref'.
This concludes this notebook on saving and loading models in gwrefpy.