Open simmsa opened 2 months ago
@hivanov-nrel, we should consider this a starting point for xarray conversion. The overall goal is provide a way to use xarray objects created by MHKiT in MHKiT-MATLAB.
Overall there seem to be a few approaches:
I started with approach 1, attempting to convert xarray to a dictionary, then convert that to json, then decode the json into a struct in MATLAB. This approach is valid, but is going to be suboptimal for a few reasons:
To implement approach 2 xarray has the to_numpy, and to_dict methods that we could leverage. An idea for a matlab struct would be:
data
: we can get data but what dimension do we use?attrs
: to_dict attrs
coords
: to_dict coords
dims
: to_dict dims
data_vars
: to_dict data_vars
xr
: python xarray objectThe trick with approach 2 is that in some cases we have to alter the data to display it to the MATLAB user. For one dimensional data this is easy, but for multidimensional data we would require user input. This might be confusing for the end user, but it forces them to better understand the underlying data.
Approach 3 could work as well, but may be confusing to MATLAB users. We would probably have to wrap a subset of xarray functionality inside of MATLAB functions. This could expose all of the power of xarray with minimal type conversion.
Approach 2 is presenting itself as a good compromise and probably the preferred path forward, but I'd like to get your feedback.
@jmcvey3 directed us to these two functions in MHKiT-Python that perform most of the conversion that we need (Thank you!):
We may need to make these functions more generic, and be careful about timestamp conversion. In MHKiT-Python we could break out the xarray -> dict conversion, and the dict -> xarray conversion so we can pass a dictionary into MATLAB which can be converted to a struct and pass back a struct for conversion to xarray.
We should probably test xarray here and include the final functions in mhkit_python_utils. If this modified solution meets our needs we can add it to MHKiT-Python.
Create a standardized method for converting xarray objects into a matlab structure and converting them back into xarray objects.