Data_saver#

Hydrological_model_validator.Processing.Data_saver.convert_to_serializable(obj)[source]#

Recursively convert an object to a form compatible with JSON serialization.

Parameters:

obj (any) – The object to convert.

Returns:

obj_serializable

Return type:

JSON-compatible representation

Notes

Converts NumPy arrays, pandas DataFrames/Series, and xarray DataArrays/Datasets to JSON-friendly types. Fallbacks to string representation for unsupported objects.

Hydrological_model_validator.Processing.Data_saver.save_model_data(output_path, ModData_complete)[source]#

Save model data array to disk in user-selected formats.

This function saves the provided 3D model data array to the specified directory. The user is prompted to choose between MATLAB .mat format, NetCDF .nc format, or both. A warning is displayed regarding the absence of satellite mask application.

Parameters:
  • output_path (str or pathlib.Path) – Directory path where the output files will be saved.

  • ModData_complete (np.ndarray or xarray.DataArray) – 3D array of model data (e.g., dimensions [time, lat, lon]).

Raises:
  • TypeError – If input types are incorrect.

  • ValueError – If ModData_complete does not have 3 dimensions or if output_path is invalid.

Notes

It is recommended to apply the satellite mask via the Interpolator.m script before further analysis to ensure alignment with satellite data, especially when using Level 3 data.

Hydrological_model_validator.Processing.Data_saver.save_satellite_data(output_path, Sat_lon, Sat_lat, SatData_complete)[source]#

Save satellite longitude, latitude, and data arrays to disk in user-selected formats.

This function saves the provided satellite coordinate and data arrays to the specified directory, allowing the user to choose between MATLAB .mat format, NetCDF .nc format, .json format or all of the above. Longitude and latitude arrays must be 2D; the satellite data array must be 3D (time, lat, lon).

Parameters:
  • output_path (str or pathlib.Path) – Directory path where the output files will be saved.

  • Sat_lon (np.ndarray or xarray.DataArray) – 2D array representing satellite longitudes.

  • Sat_lat (np.ndarray or xarray.DataArray) – 2D array representing satellite latitudes.

  • SatData_complete (np.ndarray or xarray.DataArray) – 3D array of satellite data with dimensions (time, latitude, longitude).

Raises:
  • TypeError – If any input is not of the expected type.

  • ValueError – If input array dimensions are incorrect or if output_path is not a valid directory.

Notes

The user is prompted to select the desired file format(s) interactively.

Example

>>> save_satellite_data('./output', Sat_lon, Sat_lat, SatData_complete)
Choose a file format to save the data:
1. MAT-File (.mat)
2. NetCDF (.nc)
3. Both MAT and NetCDF
4. JSON (.json)
5. All of the above
Enter the number corresponding to your choice: 3
Saving data as a single .mat file...
Data saved as SatData_clean.mat
Saving the data as separate .nc files...
Sat_lon saved as Sat_lon.nc
Sat_lat saved as Sat_lat.nc
SatData_complete saved as SatData_complete.nc
✅ The requested data has been saved!
Hydrological_model_validator.Processing.Data_saver.save_to_netcdf(data_dict: Dict[str, ndarray | DataArray], output_path: str | Path) None[source]#

Save each variable from a dictionary of arrays or DataArrays as separate NetCDF files.

Parameters:
  • data_dict (dict) – Dictionary where keys are variable names and values are numpy arrays or xarray.DataArrays.

  • output_path (str or Path) – Directory path where NetCDF files will be saved.

Raises:
  • ValueError – If output_path is not a valid directory.

  • TypeError – If data items are not NumPy arrays or xarray DataArrays.

Example

>>> import numpy as np
>>> import xarray as xr
>>> data_dict = {
...     'temperature': np.random.rand(10, 5, 5),
...     'precipitation': xr.DataArray(np.random.rand(10, 5, 5))
... }
>>> save_to_netcdf(data_dict, "./output_data")
Hydrological_model_validator.Processing.Data_saver.save_variable_to_json(variable, output_path)[source]#

Save any Python variable to a JSON file in a serializable format.

Supports basic Python types, NumPy arrays, pandas DataFrames/Series, xarray DataArrays/Datasets, dictionaries, and nested combinations.

Parameters:
  • variable (any) – The Python object or data structure to save (e.g., dict, array, DataFrame, DataArray).

  • output_path (str or Path) – File path (must end in .json) where the data will be saved.

Raises:
  • ValueError – If the output_path does not end with ‘.json’.

  • TypeError – If the object cannot be serialized to JSON and no fallback is possible.

Example

>>> import numpy as np
>>> save_variable_to_json(np.array([[1, 2], [3, 4]]), "array_data.json")