# Path: pysims/translators/utils/fillgaps.py
# def fill(data, time, ref, varname):
#     var = masked_array(data)
# 
#     for i in range(len(data)):
#         isfill = var[i] > 1e10
#         var[i] = masked_where(var[i] > 1e10, var[i]) # remove fill values
#         var[i] = var[i].astype(float) # convert to float
# 
#         if isfill.sum():
#             # if 100. * isfill.sum() / var[i].size > 1.:
#             #     raise Exception('More than one percent of values for variable %s are masked!' % varname)
#             if varname in ['RAIN', 'rain']:
#                 var[i, isfill] = 0. # fill with zeros
#             else:
#                 days = array([int((ref + timedelta(int(t))).strftime('%j')) for t in time])
#                 fdays = days[isfill]
#                 varave = zeros(len(fdays))
#                 for j in range(len(fdays)):
#                     ave = var[i, days == fdays[j]].mean()
#                     varave[j] = ave if not is_masked(ave) else 1e20 
#                 var[i, isfill] = varave # fill with daily average
# 
#     return var


import datetime
import os
import re
import stat
import traceback
import warnings
from netCDF4 import Dataset
from collections import OrderedDict
from numpy import empty, array, concatenate, savetxt, zeros, intersect1d, inf, ones, append, resize, \
    VisibleDeprecationWarning
from ..utils.co2 import CO2
from ..utils.fillgaps import fill
from ..utils.dewpoint import dewpoint
from .. import translator
                                     ('vp',   ['vap', 'vapr', 'vap']),
                                     ('tas',  ['tas']),
                                     ('ps',   ['ps'])])
            unit_names = array([['mj/m^2', 'mj/m2', 'mjm-2', 'mjm-2day-1', 'mjm-2d-1', 'mj/m^2/day', 'mj/m2/day'],
                                ['oc', 'degc', 'degreesc', 'c'], ['oc', 'degc', 'degreesc', 'c'], ['mm', 'mm/day'],
                                ['m/s', 'ms-1'], ['oc', 'degc', 'degreesc', 'c'],
                                ['%'], ['kgkg-1', 'kg/kg'], ['mb'], ['oc', 'degc', 'c'], ['mb']])
            unit_names2 = array(['MJ/m^2', 'oC', 'oC', 'mm', 'm/s', 'oC', '%', 'kgkg-1', 'mb', 'oC', 'mb'])
            
            var_keys = var_lists.keys()
            var_names = array(var_keys)
            nt = len(time)
            nv = len(var_names)
            alldata = empty((nv, ns, nt))
            found_var = zeros(nv, dtype=bool)
            
            for i in range(nv):
                var_name = var_names[i]
                var_list = var_lists[var_name]
            
                for v in var_list:
                    matchvar = isin(v, variables)
                    if not matchvar:
                        continue
            
                    matchvar = matchvar[0]
                    if matchvar not in vlist:
                        continue
            
                    alldata[i] = infile.variables[matchvar][:].squeeze()

# Path: pysims/translators/utils/fillgaps.py
# def fill(data, time, ref, varname):
#     var = masked_array(data)
# 
#     for i in range(len(data)):
#         isfill = var[i] > 1e10
#         var[i] = masked_where(var[i] > 1e10, var[i]) # remove fill values
#         var[i] = var[i].astype(float) # convert to float
# 
#         if isfill.sum():
#             # if 100. * isfill.sum() / var[i].size > 1.:
#             #     raise Exception('More than one percent of values for variable %s are masked!' % varname)
#             if varname in ['RAIN', 'rain']:
#                 var[i, isfill] = 0. # fill with zeros
#             else:
#                 days = array([int((ref + timedelta(int(t))).strftime('%j')) for t in time])
#                 fdays = days[isfill]
#                 varave = zeros(len(fdays))
#                 for j in range(len(fdays)):
#                     ave = var[i, days == fdays[j]].mean()
#                     varave[j] = ave if not is_masked(ave) else 1e20 
#                 var[i, isfill] = varave # fill with daily average
# 
#     return var


# Path: pysims/translators/apsim79/psims2met.py
import datetime
import os
import re
import stat
import traceback
import warnings
from netCDF4 import Dataset
from collections import OrderedDict
from numpy import empty, array, concatenate, savetxt, zeros, intersect1d, inf, ones, append, resize, \
    VisibleDeprecationWarning
from ..utils.co2 import CO2
from ..utils.fillgaps import fill
from ..utils.dewpoint import dewpoint
from .. import translator

                                     ('vp',   ['vap', 'vapr', 'vap']),
                                     ('tas',  ['tas']),
                                     ('ps',   ['ps'])])
            unit_names = array([['mj/m^2', 'mj/m2', 'mjm-2', 'mjm-2day-1', 'mjm-2d-1', 'mj/m^2/day', 'mj/m2/day'],
                                ['oc', 'degc', 'degreesc', 'c'], ['oc', 'degc', 'degreesc', 'c'], ['mm', 'mm/day'],
                                ['m/s', 'ms-1'], ['oc', 'degc', 'degreesc', 'c'],
                                ['%'], ['kgkg-1', 'kg/kg'], ['mb'], ['oc', 'degc', 'c'], ['mb']])
            unit_names2 = array(['MJ/m^2', 'oC', 'oC', 'mm', 'm/s', 'oC', '%', 'kgkg-1', 'mb', 'oC', 'mb'])
            
            var_keys = var_lists.keys()
            var_names = array(var_keys)
            nt = len(time)
            nv = len(var_names)
            alldata = empty((nv, ns, nt))
            found_var = zeros(nv, dtype=bool)
            
            for i in range(nv):
                var_name = var_names[i]
                var_list = var_lists[var_name]
            
                for v in var_list:
                    matchvar = isin(v, variables)
                    if not matchvar:
                        continue
            
                    matchvar = matchvar[0]
                    if matchvar not in vlist:
                        continue
            
                    alldata[i] = infile.variables[matchvar][:].squeeze()

                    alldata[i] = fill(alldata[i], time, ref, var_name)