# Path: rvic/core/log.py
# LOG_NAME = 'rvic'
# 
# Path: rvic/core/variables.py
# class Point(object):
#     '''
#     Creates a point class for intellegently storing coordinate information
#     '''
# 
#     def __init__(self, lat=-9999.9, lon=-9999.9, domx=-9999, domy=-9999,
#                  routx=-9999, routy=-9999, name=None, cell_id=-9999):
#         '''Defines x and y variables'''
#         self.lat = lat
#         self.lon = lon
#         self.domx = domx
#         self.domy = domy
#         self.routx = routx
#         self.routy = routy
#         self.cell_id = cell_id
# 
#         if name:
#             self.name = name
#         else:
#             self.name = 'outlet_{0:3.4f}_{1:3.4f}'.format(self.lat, self.lon)
# 
#         return
# 
#     def __str__(self):
#         return ('Point({0}, lat:{1:3.4f}, lon:{2:3.4f}, y:{3:d}, '
#                 'x:{4:d})'.format(self.name, self.lat, self.lon, self.domy,
#                                   self.domx))
# 
#     def __repr__(self):
#         return ('    -- Point --    \n'
#                 'name:\t{0}\n'
#                 'lat:\t{1:3.4f}\n'
#                 'lon:\t{2:3.4f}\n'
#                 'domy:\t{3:d}\n'
#                 'domx:\t{4:d}\n'
#                 'routy:\t{5:d}\n'
#                 'routx:\t{6:d}\n'.format(self.name, self.lat, self.lon,
#                                          self.domy, self.domx,
#                                          self.routy, self.routx))
# 
# Path: rvic/core/share.py
# FILLVALUE_I = default_fillvals[NC_INT]
# 
# Path: rvic/core/pycompat.py
# def iteritems(d):
# def itervalues(d):
# def iteritems(d):
# def itervalues(d):


import os
import re
import numpy as np
import logging
from .log import LOG_NAME
from .variables import Point
from .share import FILLVALUE_I
from .pycompat import iteritems, pyrange
# -------------------------------------------------------------------- #
# Adjust Domain Bounds
def move_domain(dom_data, new_dom_data, outlets):

    # ---------------------------------------------------------------- #
    # Create lookup arrays (size of dom_data but contains mapping to new_dom)
    if dom_data['cord_lons'].ndim == 1:
        new_y = np.zeros(len(dom_data['cord_lats']),
                         dtype=np.int16) - FILLVALUE_I
        new_x = np.zeros(len(dom_data['cord_lons']),
                         dtype=np.int16) - FILLVALUE_I
    else:
        raise ValueError('Grids must be regular and coordinate variables must \
                         have only 1 dimension to move domain to smaller size')
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    # Check that lons/lats in new_domain match those in original_domain
    xi = np.searchsorted(dom_data['cord_lons'], new_dom_data['cord_lons'])
    yi = np.searchsorted(dom_data['cord_lats'], new_dom_data['cord_lats'])
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    # Fill in the lookup arrays with the correct value
    new_y[yi] = np.arange(len(yi), dtype=np.int16)
    new_x[xi] = np.arange(len(xi), dtype=np.int16)
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    # Adjust locations

# Path: rvic/core/log.py
# LOG_NAME = 'rvic'
# 
# Path: rvic/core/variables.py
# class Point(object):
#     '''
#     Creates a point class for intellegently storing coordinate information
#     '''
# 
#     def __init__(self, lat=-9999.9, lon=-9999.9, domx=-9999, domy=-9999,
#                  routx=-9999, routy=-9999, name=None, cell_id=-9999):
#         '''Defines x and y variables'''
#         self.lat = lat
#         self.lon = lon
#         self.domx = domx
#         self.domy = domy
#         self.routx = routx
#         self.routy = routy
#         self.cell_id = cell_id
# 
#         if name:
#             self.name = name
#         else:
#             self.name = 'outlet_{0:3.4f}_{1:3.4f}'.format(self.lat, self.lon)
# 
#         return
# 
#     def __str__(self):
#         return ('Point({0}, lat:{1:3.4f}, lon:{2:3.4f}, y:{3:d}, '
#                 'x:{4:d})'.format(self.name, self.lat, self.lon, self.domy,
#                                   self.domx))
# 
#     def __repr__(self):
#         return ('    -- Point --    \n'
#                 'name:\t{0}\n'
#                 'lat:\t{1:3.4f}\n'
#                 'lon:\t{2:3.4f}\n'
#                 'domy:\t{3:d}\n'
#                 'domx:\t{4:d}\n'
#                 'routy:\t{5:d}\n'
#                 'routx:\t{6:d}\n'.format(self.name, self.lat, self.lon,
#                                          self.domy, self.domx,
#                                          self.routy, self.routx))
# 
# Path: rvic/core/share.py
# FILLVALUE_I = default_fillvals[NC_INT]
# 
# Path: rvic/core/pycompat.py
# def iteritems(d):
# def itervalues(d):
# def iteritems(d):
# def itervalues(d):


# Path: rvic/core/convert.py
import os
import re
import numpy as np
import logging
from .log import LOG_NAME
from .variables import Point
from .share import FILLVALUE_I
from .pycompat import iteritems, pyrange

# -------------------------------------------------------------------- #
# Adjust Domain Bounds
def move_domain(dom_data, new_dom_data, outlets):

    # ---------------------------------------------------------------- #
    # Create lookup arrays (size of dom_data but contains mapping to new_dom)
    if dom_data['cord_lons'].ndim == 1:
        new_y = np.zeros(len(dom_data['cord_lats']),
                         dtype=np.int16) - FILLVALUE_I
        new_x = np.zeros(len(dom_data['cord_lons']),
                         dtype=np.int16) - FILLVALUE_I
    else:
        raise ValueError('Grids must be regular and coordinate variables must \
                         have only 1 dimension to move domain to smaller size')
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    # Check that lons/lats in new_domain match those in original_domain
    xi = np.searchsorted(dom_data['cord_lons'], new_dom_data['cord_lons'])
    yi = np.searchsorted(dom_data['cord_lats'], new_dom_data['cord_lats'])
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    # Fill in the lookup arrays with the correct value
    new_y[yi] = np.arange(len(yi), dtype=np.int16)
    new_x[xi] = np.arange(len(xi), dtype=np.int16)
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    # Adjust locations

    for cell_id, outlet in iteritems(outlets):