Open
Description
Here is the current code we use.
# la_iode.py
# ----------
# Interface between larray objects and IODE ws
from larray import *
import iode
def larray_to_ws(array, time='time', vars='vars'):
'''Replace the IODE variables in memory by the array contents
Parameters
----------
array : LArray
array with (exactly) 2 axes
- vars = IODE variable names
- time = time labels (yearly : 1990 1991...)
time : str, int or Axis
axis to use for the time.
vars : str, int or Axis
axis to use for the variables.
Examples
--------
Load an array from a .var file, modify its content and save it back to a
.var file.
>>> # Load an IODE .var file in an larray object [vars, time]
>>> surv = iode.wsloadvarpy("v00Mcj.var")
>>> surv.info
TODO
>>> # Modify some variables in the array
>>> surv['VF00'] = -0.123
>>> # Replace the IODE variable WS in memory by the larray contents
>>> larray_to_ws(surv)
>>> # Save the IODE ws in a .var file
>>> iode.wssavevar("toto.var")
Create an array from scratch and save it to a .var file.
>>> # Create an larray object (with axes vars and time)
>>> arr = ndrange('vars=X,Y;time=2000..2005')
>>> # Copy array into IODE memory
>>> larray_to_ws(arr)
>>> # Save the iode memory contents in a .var file
>>> iode.wssavevar("la.var")
'''
def iodeline(line):
"Execute a single IODE report line instruction."
rc = iode.reportline(line)
if rc != 0:
# GDM>
# * ideally this should be done in iode.reportline directly
# * ideally, we should raise different exceptions based on rc
raise Exception('report line failed to execute (code {}):\n{}'
.format(rc, line))
assert isinstance(array, LArray)
time = array.axes[time]
vars = array.axes[vars]
# converting to int to avoid .0 for float labels
smplfrom = str(int(time.i[0])) + 'Y1 ' # <-- GDM: is the space necessary?
smplto = str(int(time.i[-1])) + 'Y1'
iodeline("$WsClearVar")
iodeline("$WsSample {} {}".format(smplfrom, smplto))
# loop on vars
# GDM> It would be better if we could pass data directly by value
for name in vars:
cmd_parts = ['$DataUpdateVar {} {}'.format(name, smplfrom)]
cmd_parts += [repr(array[name, t]) for t in time]
cmd = ' '.join(cmd_parts)
iodeline(cmd)
TODO:
- combine_axes if more than 2 axes
- include iode.wssavevar(path)