Skip to content

Commit

Permalink
Ensure variable creation
Browse files Browse the repository at this point in the history
  • Loading branch information
HCookie committed Jan 6, 2025
1 parent 1187d78 commit 911e6a1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/anemoi/inference/outputs/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def _init(self, state):
self.longitude_var.long_name = "longitude"

self.vars = {}

for name in state["fields"].keys():
chunksizes = (1, values)

Expand All @@ -104,12 +105,34 @@ def _init(self, state):
self.n = 0
return self.ncfile

def ensure_variables(self, state):
values = len(state["latitudes"])

compression = {} # dict(zlib=False, complevel=0)

for name in state["fields"].keys():
if name in self.vars:
continue
chunksizes = (1, values)

while np.prod(chunksizes) > 1000000:
chunksizes = tuple(int(np.ceil(x / 2)) for x in chunksizes)

self.vars[name] = self.ncfile.createVariable(
name,
self.float_size,
("time", "values"),
chunksizes=chunksizes,
**compression,
)

def write_initial_state(self, state):
reduced_state = self.reduce(state)
self.write_state(reduced_state)

def write_state(self, state):
self._init(state)
self.ensure_variables(state)

step = state["date"] - self.reference_date
self.time_var[self.n] = step.total_seconds()
Expand Down

0 comments on commit 911e6a1

Please sign in to comment.