# Path: rvic/core/time_utility.py
# def ord_to_datetime(time, units, calendar='standard'):
#     '''
#     netCDF4.num2date yields a fake datetime object, this function converts
#     converts that back to a real datetime object
#     '''
#     # this is the netCDF4.datetime object
#     # if the calendar is standard, t is already a real datetime object
#     if type(time) == np.ndarray:
#         if len(time) == 1:
#             time = time[0]
#         else:
#             raise ValueError('time variable is a numpy array with length > 1')
#     t = num2date(time, units, calendar=calendar)
#     if calendar in ['proleptic_gregorian', 'standard', 'gregorian']:
#         return t
#     else:
#         return datetime(t.year, t.month, t.day, t.hour, t.minute, t.second)
# 
# Path: rvic/core/log.py
# LOG_NAME = 'rvic'
# 
# Path: rvic/core/share.py
# SECSPERDAY = HOURSPERDAY * SECSPERHOUR
# 
# HOURSPERDAY = 24.
# 
# TIMEUNITS = 'days since ' + REFERENCE_STRING  # do not change (MUST BE DAYS)!
# 
# Path: rvic/core/share.py
# NC_INT = 'i4'
# 
# NC_FLOAT = 'f4'
# 
# NC_CHAR = 'S1'
# 
# Path: rvic/core/share.py
# NC_DOUBLE = 'f8'
# 
# WATERDENSITY = 1000.
# 
# MONTHSPERYEAR = 12
# 
# Path: rvic/core/pycompat.py
# def iteritems(d):
#     return iter(d.items())


import os
import numpy as np
from netCDF4 import Dataset, date2num, num2date, stringtochar
from datetime import datetime
from .time_utility import ord_to_datetime
from logging import getLogger
from .log import LOG_NAME
from .share import SECSPERDAY, HOURSPERDAY, TIMEUNITS
from .share import NC_INT, NC_FLOAT, NC_CHAR
from .share import NC_DOUBLE, WATERDENSITY, MONTHSPERYEAR
from .pycompat import iteritems
from . import share
        time[:] = self._out_times[:self._out_data_i]
        for key, val in iteritems(share.time):
            if val:
                setattr(time, key, val.encode())
        time.calendar = self._calendar.encode()

        if self._avgflag != 'I':
            f.createDimension('nv', 2)

            time.bounds = 'time_bnds'

            time_bnds = f.createVariable('time_bnds', self._ncprec,
                                         ('time', 'nv',), **self.ncvaropts)
            time_bnds[:, :] = self._out_time_bnds[:self._out_data_i]
        # ------------------------------------------------------------ #

        # ------------------------------------------------------------ #
        # Setup Coordinate Variables
        if self._grid_lons.ndim > 1:
            coords = ('yc', 'xc',)

            # Grid is not regular
            xc = f.createDimension('xc', self._grid_lons.shape[1])
            yc = f.createDimension('yc', self._grid_lons.shape[0])

            xc = f.createVariable('xc', self._ncprec, coords, **self.ncvaropts)
            yc = f.createVariable('yc', self._ncprec, coords, **self.ncvaropts)
            xc[:, :] = self._grid_lons
            yc[:, :] = self._grid_lats


# Path: rvic/core/time_utility.py
# def ord_to_datetime(time, units, calendar='standard'):
#     '''
#     netCDF4.num2date yields a fake datetime object, this function converts
#     converts that back to a real datetime object
#     '''
#     # this is the netCDF4.datetime object
#     # if the calendar is standard, t is already a real datetime object
#     if type(time) == np.ndarray:
#         if len(time) == 1:
#             time = time[0]
#         else:
#             raise ValueError('time variable is a numpy array with length > 1')
#     t = num2date(time, units, calendar=calendar)
#     if calendar in ['proleptic_gregorian', 'standard', 'gregorian']:
#         return t
#     else:
#         return datetime(t.year, t.month, t.day, t.hour, t.minute, t.second)
# 
# Path: rvic/core/log.py
# LOG_NAME = 'rvic'
# 
# Path: rvic/core/share.py
# SECSPERDAY = HOURSPERDAY * SECSPERHOUR
# 
# HOURSPERDAY = 24.
# 
# TIMEUNITS = 'days since ' + REFERENCE_STRING  # do not change (MUST BE DAYS)!
# 
# Path: rvic/core/share.py
# NC_INT = 'i4'
# 
# NC_FLOAT = 'f4'
# 
# NC_CHAR = 'S1'
# 
# Path: rvic/core/share.py
# NC_DOUBLE = 'f8'
# 
# WATERDENSITY = 1000.
# 
# MONTHSPERYEAR = 12
# 
# Path: rvic/core/pycompat.py
# def iteritems(d):
#     return iter(d.items())


# Path: rvic/core/history.py
import os
import numpy as np
from netCDF4 import Dataset, date2num, num2date, stringtochar
from datetime import datetime
from .time_utility import ord_to_datetime
from logging import getLogger
from .log import LOG_NAME
from .share import SECSPERDAY, HOURSPERDAY, TIMEUNITS
from .share import NC_INT, NC_FLOAT, NC_CHAR
from .share import NC_DOUBLE, WATERDENSITY, MONTHSPERYEAR
from .pycompat import iteritems
from . import share

        time[:] = self._out_times[:self._out_data_i]
        for key, val in iteritems(share.time):
            if val:
                setattr(time, key, val.encode())
        time.calendar = self._calendar.encode()

        if self._avgflag != 'I':
            f.createDimension('nv', 2)

            time.bounds = 'time_bnds'

            time_bnds = f.createVariable('time_bnds', self._ncprec,
                                         ('time', 'nv',), **self.ncvaropts)
            time_bnds[:, :] = self._out_time_bnds[:self._out_data_i]
        # ------------------------------------------------------------ #

        # ------------------------------------------------------------ #
        # Setup Coordinate Variables
        if self._grid_lons.ndim > 1:
            coords = ('yc', 'xc',)

            # Grid is not regular
            xc = f.createDimension('xc', self._grid_lons.shape[1])
            yc = f.createDimension('yc', self._grid_lons.shape[0])

            xc = f.createVariable('xc', self._ncprec, coords, **self.ncvaropts)
            yc = f.createVariable('yc', self._ncprec, coords, **self.ncvaropts)
            xc[:, :] = self._grid_lons
            yc[:, :] = self._grid_lats


            for key, val in iteritems(share.xc):