iode.Variables.from_array

Variables.from_array(array: Array, time_axis_name: str = 'time', sep: str = '_')[source]

Copies the Array array into the IODE Variables database.

If the passed array has more than two dimensions, the non-time axes are grouped (with ‘sep’ as separator) to form the Variables names.

The time axis of the passed array can be of type string or int.

Parameters:
array: Array

Input Array object.

time_axis_name: str, optional

Name of the time axis in array. Assumed to be ‘time’ by default.

sep: str, optional

If the ‘array’ as more than two axes, the separator ‘sep’ is used to group labels of the non-time axes.

Warning

IODE and LArray don’t use the same constant to represent NaN values. When loading a LArray Array into the Variables database, the LArray NaN values (\(nan\)) are converted to IODE NaN values (\(NA\)).

Examples

>>> from iode import variables
>>> import larray as la
>>> import numpy as np
>>> variables.clear()
>>> len(variables)
0
  1. Filling an empty Variables database from a LArray Array

>>> regions_axis = la.Axis("region=VLA,WAL,BXL")
>>> code_axis = la.Axis("code=00..02")
>>> periods_axis = la.Axis("time=1960Y1..1970Y1")
>>> array = la.ndtest((regions_axis, code_axis, periods_axis), dtype=float)
>>> array
region  code\time  1960Y1  1961Y1  1962Y1  ...  1968Y1  1969Y1  1970Y1
   VLA         00     0.0     1.0     2.0  ...     8.0     9.0    10.0
   VLA         01    11.0    12.0    13.0  ...    19.0    20.0    21.0
   VLA         02    22.0    23.0    24.0  ...    30.0    31.0    32.0
   WAL         00    33.0    34.0    35.0  ...    41.0    42.0    43.0
   WAL         01    44.0    45.0    46.0  ...    52.0    53.0    54.0
   WAL         02    55.0    56.0    57.0  ...    63.0    64.0    65.0
   BXL         00    66.0    67.0    68.0  ...    74.0    75.0    76.0
   BXL         01    77.0    78.0    79.0  ...    85.0    86.0    87.0
   BXL         02    88.0    89.0    90.0  ...    96.0    97.0    98.0
>>> # load the IODE Variables from the Array object
>>> variables.from_array(array)
>>> len(variables)
9
>>> variables.names
['BXL_00', 'BXL_01', 'BXL_02', 'VLA_00', 'VLA_01', 'VLA_02', 'WAL_00', 'WAL_01', 'WAL_02']
>>> variables.sample
Sample("1960Y1:1970Y1")
>>> variables["VLA_00"]
Workspace: Variables
nb variables: 1
filename: ws
sample: 1960Y1:1970Y1
mode: LEVEL

 name       1960Y1  1961Y1  1962Y1  ...  1968Y1  1969Y1  1970Y1
VLA_00        0.00    1.00    2.00  ...    8.00    9.00   10.00

>>> variables["BXL_02"]
Workspace: Variables
nb variables: 1
filename: ws
sample: 1960Y1:1970Y1
mode: LEVEL

 name       1960Y1  1961Y1  1962Y1  ...  1968Y1  1969Y1  1970Y1
BXL_02       88.00   89.00   90.00  ...   96.00   97.00   98.00
  1. Updating an existing Variables database from a LArray Array

>>> # take a subset of the LArray Array
>>> array = array['1962Y1':'1968Y1']
>>> # change values
>>> array += 3.0
>>> # add a new code
>>> array = array.append(axis='code', value=array['02'] + 5.0, label='03')
>>> array
region  code\time  1962Y1  1963Y1  1964Y1  1965Y1  1966Y1  1967Y1  1968Y1
   VLA         00     5.0     6.0     7.0     8.0     9.0    10.0    11.0
   VLA         01    16.0    17.0    18.0    19.0    20.0    21.0    22.0
   VLA         02    27.0    28.0    29.0    30.0    31.0    32.0    33.0
   VLA         03    32.0    33.0    34.0    35.0    36.0    37.0    38.0
   WAL         00    38.0    39.0    40.0    41.0    42.0    43.0    44.0
   WAL         01    49.0    50.0    51.0    52.0    53.0    54.0    55.0
   WAL         02    60.0    61.0    62.0    63.0    64.0    65.0    66.0
   WAL         03    65.0    66.0    67.0    68.0    69.0    70.0    71.0
   BXL         00    71.0    72.0    73.0    74.0    75.0    76.0    77.0
   BXL         01    82.0    83.0    84.0    85.0    86.0    87.0    88.0
   BXL         02    93.0    94.0    95.0    96.0    97.0    98.0    99.0
   BXL         03    98.0    99.0   100.0   101.0   102.0   103.0   104.0
>>> # update the IODE Variables database
>>> variables.from_array(array)
>>> len(variables)
12
>>> variables.names
['BXL_00', 'BXL_01', 'BXL_02', 'BXL_03', ..., 'WAL_00', 'WAL_01', 'WAL_02', 'WAL_03']
>>> # note that the new variables '<region>_03' have been added with NA values 
>>> # for the periods present in the Variables sample but not in the Array
>>> variables
Workspace: Variables
nb variables: 12
filename: ws
sample: 1960Y1:1970Y1
mode: LEVEL

 name      1960Y1  1961Y1  1962Y1  1963Y1  1964Y1  1965Y1  1966Y1  1967Y1  1968Y1  1969Y1  1970Y1        
BXL_00      66.00   67.00   71.00   72.00   73.00   74.00   75.00   76.00   77.00   75.00   76.00        
BXL_01      77.00   78.00   82.00   83.00   84.00   85.00   86.00   87.00   88.00   86.00   87.00        
BXL_02      88.00   89.00   93.00   94.00   95.00   96.00   97.00   98.00   99.00   97.00   98.00        
BXL_03         na      na   98.00   99.00  100.00  101.00  102.00  103.00  104.00      na      na        
VLA_00       0.00    1.00    5.00    6.00    7.00    8.00    9.00   10.00   11.00    9.00   10.00        
...           ...     ...     ...     ...     ...     ...     ...     ...     ...     ...     ...        
VLA_03         na      na   32.00   33.00   34.00   35.00   36.00   37.00   38.00      na      na        
WAL_00      33.00   34.00   38.00   39.00   40.00   41.00   42.00   43.00   44.00   42.00   43.00        
WAL_01      44.00   45.00   49.00   50.00   51.00   52.00   53.00   54.00   55.00   53.00   54.00        
WAL_02      55.00   56.00   60.00   61.00   62.00   63.00   64.00   65.00   66.00   64.00   65.00        
WAL_03         na      na   65.00   66.00   67.00   68.00   69.00   70.00   71.00      na      na