@@ -1265,7 +1265,15 @@ def save(self, filename: Union[str, Path]) -> None:
1265
1265
con = duckdb .connect (filename )
1266
1266
# TODO figure out how to save the x, y, z coordinates and other attributes later
1267
1267
df = ds .to_dataframe ().drop (columns = ["x" , "y" , "z" ]).reset_index () # noqa
1268
- duckdb .sql ("CREATE TABLE data AS SELECT * FROM df" , connection = con )
1268
+ duckdb .sql ("CREATE TABLE matched_data AS SELECT * FROM df" , connection = con )
1269
+
1270
+ attr_dict = {key : str (ds [key ].attrs ) for key in ds .data_vars }
1271
+ attr_df = pd .DataFrame (attr_dict .items (), columns = ["key" , "value" ]) # noqa
1272
+
1273
+ # attr_df["global", "key"] = str(ds.attrs)
1274
+
1275
+ duckdb .sql ("CREATE TABLE attrs AS SELECT * FROM attr_df" , connection = con )
1276
+
1269
1277
con .close ()
1270
1278
elif ext == ".nc" :
1271
1279
if self .gtype == "point" :
@@ -1304,18 +1312,20 @@ def load(filename: Union[str, Path]) -> "Comparer":
1304
1312
import duckdb
1305
1313
1306
1314
con = duckdb .connect (filename )
1307
- df = duckdb .sql ("SELECT * FROM data" , connection = con ).df ().set_index ("time" )
1315
+ df = (
1316
+ duckdb .sql ("SELECT * FROM matched_data" , connection = con )
1317
+ .df ()
1318
+ .set_index ("time" )
1319
+ )
1308
1320
1309
1321
# convert pandas dataframe to xarray dataset
1310
1322
ds = xr .Dataset .from_dataframe (df )
1311
1323
1312
- # set observation attribute
1313
- ds .Observation .attrs ["kind" ] = "observation"
1314
-
1315
- # set model attributes
1316
- for key in ds .data_vars :
1317
- if key != "Observation" :
1318
- ds [key ].attrs ["kind" ] = "model"
1324
+ attrs = duckdb .sql ("SELECT * FROM attrs" , connection = con ).df ()
1325
+ for row in attrs .iterrows ():
1326
+ key = row [1 ]["key" ]
1327
+ value = row [1 ]["value" ]
1328
+ ds [key ].attrs = eval (value )
1319
1329
1320
1330
# TODO figure out aux variables
1321
1331
0 commit comments