Skip to content

Commit c7940b8

Browse files
committed
Add workaround to fix to_netcdf sometimes failing on filteredData
The failure is a result of an xarray bug that can occur after subsetting data that was itself loaded from netcdf. See pydata/xarray#6352 for the issue and pydata/xarray#7689 for the fix used to create the workaround.
1 parent a881f73 commit c7940b8

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

reconstruction/reconstruction/NetworkReconstructor.py

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
import xarray
55
from zipfile import ZipFile
66

7+
# Adapted from https://github.com/pydata/xarray/pull/7689
8+
def resetDataarrayEncoding(da):
9+
ds = da._to_temp_dataset()
10+
reset_variables = {k: v._replace(encoding={}) for k, v, in ds.variables.items()}
11+
ds = ds._replace(variables=reset_variables, encoding={})
12+
return da._from_temp_dataset(ds)
13+
714
class NetworkReconstructor:
815
def runPipeline(self, stages, allData={}, start=None, stopStage=None, dataOutFilePath=None):
916
started = True
@@ -32,6 +39,7 @@ def runPipeline(self, stages, allData={}, start=None, stopStage=None, dataOutFil
3239
for dim in dims:
3340
if isinstance(dataArray.get_index(dim), pandas.MultiIndex):
3441
dataArray = dataArray.reset_index(dim)
42+
dataArray = resetDataarrayEncoding(dataArray) # See https://github.com/pydata/xarray/issues/6352
3543
dataArray.to_netcdf(tempFile.name)
3644
dataOutZip.write(tempFile.name, arcname=dataName)
3745
tempFile.close()

0 commit comments

Comments
 (0)