# 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
        if data_params.spatialPoolingSize > 1:
            if anchor == 'topleft':
                poolOffs = 0.0
            elif anchor == 'bottomright':
                poolOffs = 1.0
            else:
                poolOffs = 0.5
            ind_y = data_params.spatialPoolingSize * (ind_y + poolOffs)
            ind_x = data_params.spatialPoolingSize * (ind_x + poolOffs)
        ind_y = min(ind_y + data_params.firstLat, data_params.lastLat)
        ind_x = min(ind_x + data_params.firstLon, data_params.lastLon)
    return (LAT_OFFS + ind_y * LAT_STEP, LON_OFFS + ind_x * LON_STEP)

def latlon2ind(lat, lon, data_params = None):
    ind = (int(round((lat - LAT_OFFS) / LAT_STEP)), int(round((lon - LON_OFFS) / LON_STEP)))
    if data_params is not None:
        ind[0] -= data_params.firstLat
        ind[1] -= data_params.firstLon
        if data_params.spatialPoolingSize > 1:
            ind_y //= data_params.spatialPoolingSize
            ind_x //= data_params.spatialPoolingSize
    return ind

def ind2time(t, data_params = None):
    start_year = data_params.firstYear if data_params is not None else YEAR_OFFS
    if start_year < YEAR_OFFS:
        start_year += YEAR_OFFS - 1
    return datetime.datetime(start_year, 1, 1, 1) + datetime.timedelta(seconds = t * 3600)

def time2ind(time, data_params = None):

# 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

        if data_params.spatialPoolingSize > 1:
            if anchor == 'topleft':
                poolOffs = 0.0
            elif anchor == 'bottomright':
                poolOffs = 1.0
            else:
                poolOffs = 0.5
            ind_y = data_params.spatialPoolingSize * (ind_y + poolOffs)
            ind_x = data_params.spatialPoolingSize * (ind_x + poolOffs)
        ind_y = min(ind_y + data_params.firstLat, data_params.lastLat)
        ind_x = min(ind_x + data_params.firstLon, data_params.lastLon)
    return (LAT_OFFS + ind_y * LAT_STEP, LON_OFFS + ind_x * LON_STEP)

def latlon2ind(lat, lon, data_params = None):
    ind = (int(round((lat - LAT_OFFS) / LAT_STEP)), int(round((lon - LON_OFFS) / LON_STEP)))
    if data_params is not None:
        ind[0] -= data_params.firstLat
        ind[1] -= data_params.firstLon
        if data_params.spatialPoolingSize > 1:
            ind_y //= data_params.spatialPoolingSize
            ind_x //= data_params.spatialPoolingSize
    return ind

def ind2time(t, data_params = None):
    start_year = data_params.firstYear if data_params is not None else YEAR_OFFS
    if start_year < YEAR_OFFS:
        start_year += YEAR_OFFS - 1
    return datetime.datetime(start_year, 1, 1, 1) + datetime.timedelta(seconds = t * 3600)

def time2ind(time, data_params = None):

    start_year = data_params.firstYear if data_params is not None else YEAR_OFFS