# Path: maxdiv/libmaxdiv_wrapper.py
# class detection_t(Structure):
# class pointwise_proposal_params_t(Structure):
# class erph_params_t(Structure):
# class embedding_params_t(Structure):
# class detrending_params_t(Structure):
# class projection_params_t(Structure):
# class preproc_params_t(Structure):
# class maxdiv_params_t(Structure):
# class _LibMaxDiv(object):
#     def __init__(self, library):
#     def _register_func(self, funcName, paramtypes, paramflags, errcheck = None):
#     def _errcheck_compile_pipeline(result, func, args):
# def _search_libmaxdiv():
# def maxdiv(X, method = 'gaussian_cov', num_intervals = 1, proposals = 'dense', **kwargs):
# def maxdiv_exec(X, params, num_intervals = 1):
# def maxdiv_score_intervals(X, params, intervals):
#         X = X.reshape((1, len(X)))
#                     X = preproc.local_linear_regression(X)
#                     X = preproc.deseasonalize_ft(X)
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])
# 
# Path: maxdiv/maxdiv_util.py
# def IoU(start1, len1, start2, len2):
#     """ Computes the intersection over union of two intervals starting at start1 and start2 with lengths len1 and len2. """
#     intersection = max(0, min(start1 + len1, start2 + len2) - max(start1, start2))
#     return float(intersection) / (len1 + len2 - intersection)


import sys, os.path
import datetime, csv
from ctypes import *
from maxdiv.libmaxdiv_wrapper import maxdiv_params_t, maxdiv_params_p, detection_t, detection_p
from maxdiv.maxdiv_util import IoU
COASTDAT_DESEAS_OLS_DAY     = 1
COASTDAT_DESEAS_OLS_YEAR    = 2    
COASTDAT_DESEAS_ZSCORE_DAY  = 3
COASTDAT_DESEAS_ZSCORE_YEAR = 4


class coastdat_params_t(Structure):
    _fields_ = [('variables', c_char_p),
                ('firstYear', c_uint),
                ('lastYear', c_uint),
                ('firstLat', c_uint),
                ('lastLat', c_uint),
                ('firstLon', c_uint),
                ('lastLon', c_uint),
                ('spatialPoolingSize', c_uint),
                ('deseasonalization', c_int)]

coastdat_params_p = POINTER(coastdat_params_t)
c_uint_p = POINTER(c_uint)


libcoastdat = CDLL('./maxdiv_coastdat.so')

coastdat_default_params = CFUNCTYPE(c_void_p, coastdat_params_p)(('coastdat_default_params', libcoastdat), ((1, 'data_params'),))

coastdat_dump = CFUNCTYPE(c_int, coastdat_params_p, c_char_p)(('coastdat_dump', libcoastdat), ((1, 'data_params'), (1, 'dump_file')))

coastdat_maxdiv = CFUNCTYPE(c_int, maxdiv_params_p, coastdat_params_p, detection_p, c_uint_p)(
                            ('coastdat_maxdiv', libcoastdat), ((1, 'params'), (1, 'data_params'), (1, 'detection_buf'), (1, 'detection_buf_size')))


# Path: maxdiv/libmaxdiv_wrapper.py
# class detection_t(Structure):
# class pointwise_proposal_params_t(Structure):
# class erph_params_t(Structure):
# class embedding_params_t(Structure):
# class detrending_params_t(Structure):
# class projection_params_t(Structure):
# class preproc_params_t(Structure):
# class maxdiv_params_t(Structure):
# class _LibMaxDiv(object):
#     def __init__(self, library):
#     def _register_func(self, funcName, paramtypes, paramflags, errcheck = None):
#     def _errcheck_compile_pipeline(result, func, args):
# def _search_libmaxdiv():
# def maxdiv(X, method = 'gaussian_cov', num_intervals = 1, proposals = 'dense', **kwargs):
# def maxdiv_exec(X, params, num_intervals = 1):
# def maxdiv_score_intervals(X, params, intervals):
#         X = X.reshape((1, len(X)))
#                     X = preproc.local_linear_regression(X)
#                     X = preproc.deseasonalize_ft(X)
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])
# 
# Path: maxdiv/maxdiv_util.py
# def IoU(start1, len1, start2, len2):
#     """ Computes the intersection over union of two intervals starting at start1 and start2 with lengths len1 and len2. """
#     intersection = max(0, min(start1 + len1, start2 + len2) - max(start1, start2))
#     return float(intersection) / (len1 + len2 - intersection)


# Path: experiments/coastdat/maxdiv_coastdat_utils.py
import sys, os.path
import datetime, csv
from ctypes import *
from maxdiv.libmaxdiv_wrapper import maxdiv_params_t, maxdiv_params_p, detection_t, detection_p
from maxdiv.maxdiv_util import IoU

COASTDAT_DESEAS_OLS_DAY     = 1
COASTDAT_DESEAS_OLS_YEAR    = 2    
COASTDAT_DESEAS_ZSCORE_DAY  = 3
COASTDAT_DESEAS_ZSCORE_YEAR = 4


class coastdat_params_t(Structure):
    _fields_ = [('variables', c_char_p),
                ('firstYear', c_uint),
                ('lastYear', c_uint),
                ('firstLat', c_uint),
                ('lastLat', c_uint),
                ('firstLon', c_uint),
                ('lastLon', c_uint),
                ('spatialPoolingSize', c_uint),
                ('deseasonalization', c_int)]

coastdat_params_p = POINTER(coastdat_params_t)
c_uint_p = POINTER(c_uint)


libcoastdat = CDLL('./maxdiv_coastdat.so')

coastdat_default_params = CFUNCTYPE(c_void_p, coastdat_params_p)(('coastdat_default_params', libcoastdat), ((1, 'data_params'),))

coastdat_dump = CFUNCTYPE(c_int, coastdat_params_p, c_char_p)(('coastdat_dump', libcoastdat), ((1, 'data_params'), (1, 'dump_file')))

coastdat_maxdiv = CFUNCTYPE(c_int, maxdiv_params_p, coastdat_params_p, detection_p, c_uint_p)(
                            ('coastdat_maxdiv', libcoastdat), ((1, 'params'), (1, 'data_params'), (1, 'detection_buf'), (1, 'detection_buf_size')))


coastdat_maxdiv_dump = CFUNCTYPE(c_int, maxdiv_params_p, c_char_p, detection_p, c_uint_p)(