# 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
        self._time_ord = time_ord        # Days since basetime
        self._caseid = caseid            # Case ID and prefix for outfiles
        self._fincl = list(fincl)        # Fields to include in history file
        self._mfilt = mfilt              # Maximum number of time samples
        self._ndens = ndens
        if self._ndens == 1:             # Output file precision
            self._ncprec = NC_FLOAT
        else:
            self._ncprec = NC_DOUBLE
        self._nhtfrq = nhtfrq            # Write frequency
        self._avgflag = avgflag          # Average Flag (A,I,X,M)
        self._outtype = outtype          # Outfile type (grid, array)
        self._count = 0
        self.files_count = 0
        self._file_format = file_format
        self._calendar = calendar
        self._out_dir = out_dir
        self._glob_ats = glob_ats

        self.__get_rvar(rvar)           # Get the initial rvar fields
        self._grid_shape = grid_area.shape
        self._out_data = {}

        # ------------------------------------------------------------ #
        # calculate the step size for each out_data timestep (units=days)
        if self._nhtfrq > 0:
            # If some number of timesteps
            self._out_data_stepsize = self._nhtfrq * self._dt / SECSPERDAY
        elif self._nhtfrq < 0:
            # If some number hours

# 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

        self._time_ord = time_ord        # Days since basetime
        self._caseid = caseid            # Case ID and prefix for outfiles
        self._fincl = list(fincl)        # Fields to include in history file
        self._mfilt = mfilt              # Maximum number of time samples
        self._ndens = ndens
        if self._ndens == 1:             # Output file precision
            self._ncprec = NC_FLOAT
        else:
            self._ncprec = NC_DOUBLE
        self._nhtfrq = nhtfrq            # Write frequency
        self._avgflag = avgflag          # Average Flag (A,I,X,M)
        self._outtype = outtype          # Outfile type (grid, array)
        self._count = 0
        self.files_count = 0
        self._file_format = file_format
        self._calendar = calendar
        self._out_dir = out_dir
        self._glob_ats = glob_ats

        self.__get_rvar(rvar)           # Get the initial rvar fields
        self._grid_shape = grid_area.shape
        self._out_data = {}

        # ------------------------------------------------------------ #
        # calculate the step size for each out_data timestep (units=days)
        if self._nhtfrq > 0:
            # If some number of timesteps
            self._out_data_stepsize = self._nhtfrq * self._dt / SECSPERDAY
        elif self._nhtfrq < 0:
            # If some number hours

            self._out_data_stepsize = -1 * self._nhtfrq / HOURSPERDAY