# 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
        # b0 is first timestep of next period
        # b1 is end of last timestep of next period

        b0 = self._time_ord
        self._begtime = b0

        if self._nhtfrq == 0:
            if self._timestamp.month == 12:
                b1 = date2num(datetime(self._timestamp.year + 1, 2, 1),
                              TIMEUNITS, calendar=self._calendar)
            else:
                b1 = date2num(datetime(self._timestamp.year,
                                       self._timestamp.month + 1, 1),
                              TIMEUNITS, calendar=self._calendar)

        else:
            b1 = b0 + self._out_data_stepsize
        # ------------------------------------------------------------ #

        # ------------------------------------------------------------ #
        # Get the number of timesteps and datestamp for the next write
        # next_ord is the ord_time when the write will happen
        self._update_count = int(round((b1 - b0) / (self._dt / SECSPERDAY)))
        # ------------------------------------------------------------ #

        # ------------------------------------------------------------ #
        # Get next file names and timeord
        if self._avgflag == 'I':
            self._write_ord = b1
            self.filename = num2date(

# 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

        # b0 is first timestep of next period
        # b1 is end of last timestep of next period

        b0 = self._time_ord
        self._begtime = b0

        if self._nhtfrq == 0:
            if self._timestamp.month == 12:
                b1 = date2num(datetime(self._timestamp.year + 1, 2, 1),
                              TIMEUNITS, calendar=self._calendar)
            else:
                b1 = date2num(datetime(self._timestamp.year,
                                       self._timestamp.month + 1, 1),
                              TIMEUNITS, calendar=self._calendar)

        else:
            b1 = b0 + self._out_data_stepsize
        # ------------------------------------------------------------ #

        # ------------------------------------------------------------ #
        # Get the number of timesteps and datestamp for the next write
        # next_ord is the ord_time when the write will happen
        self._update_count = int(round((b1 - b0) / (self._dt / SECSPERDAY)))
        # ------------------------------------------------------------ #

        # ------------------------------------------------------------ #
        # Get next file names and timeord
        if self._avgflag == 'I':
            self._write_ord = b1
            self.filename = num2date(

                b1, TIMEUNITS,