# Copyright 2022 - Barcelona Supercomputing Center
# Authors: Etienne Tourigny, Rodrigo Martín Posada
# MIT License
import logging
import numpy as np

import pyoasis
from pyoasis import OASIS

class OasisOutputVar:
    def __init__(self, conf, partition, fileIn_vars ):
        self._conf = conf
        print(str(conf))
        self._id = conf['id']
        self._partition = partition
        self._fileIn_vars = fileIn_vars
        self._oasis_var = pyoasis.Var(self._id, partition, OASIS.OUT)
        self._send_field = None

    def add_file_input_var(self, varname, var):
        self._fileIn_vars[varname] = var

    def file_input_vars(self):
        return self._fileIn_vars

    def update(self,time):
        logging.debug('OasisOutputVar::update() var {} at time {}'.format(self._id,time))
        # accumulate values from each fileIn_var linked to this oasisIn_var
        self._send_field = 0.
        for (varname,fileIn_var) in self._fileIn_vars.items():
            self._send_field += fileIn_var.send_field()
            logging.debug('accumulating {} from fileIn var {} for oasisOut var {} total= {}'.format(str(fileIn_var.send_field), varname, self._id, str(self._send_field)))

    def put(self, time):
        # send the data
        logging.debug('OasisOutputVar::put() var {} at time {}'.format(self._id,time))
        self._oasis_var.put(time, self._send_field)

