'''
Name: avi_load_coverage_into_fgdb_tool.py

Overview: Loads an AVI coverage into an AVI feature class contained in FGDB.

Description:
    This script is intended to facilitae the transision of moving
    storage of AVI data from its current coverage format to one that
    uses a File Geodatabase feature class.

    The current model for dealing with AVI data is to store and
    maintain spatial data in an ArcInfo coverage and the associated
    attributes in an INFO table. This script will join the attributes
    to the coverage and then populate an existing AVI feature class
    contained in a File Geodatabase.

Notes:
    The AVI feature class must have a standard AVI 2.1 dataset
    with field naming conventions matching this standard. This
    dataset can be defined using othr tools contained in the 
    AVI Geodatabase toolbox.
    
Author: Doug Crane
        Sept, 2009

Modifications:

'''

logChn = None

import pdb
import os
import sys

import arcpy

from srd.srd_exception import *
import srd.srd_logging as srd_logging
import srd.srd_misc as srd_misc

from avi_schema import *

DEBUG = 0
#--------------------------------------------------------------------------
if __name__ == '__main__':

    arcpy.env.overwriteOutput = True
    # If using geographic features then may be best to modify these.
    arcpy.env.XYTolerance = .001
    arcpy.env.XYResolution = .0001
    arcpy.gp.logHistory = False

    if DEBUG:
        # Modify arguments to simulate those passed from toolbox.
        # You will have to modify these based on your scripts requirements.
        sys.argv = [sys.argv[0], \
        r'X:\prjs\F11\chris_verf11', \
        r'X:\prjs\F11\F11.gdb\AVI']
        print('\n***\n*** IN DEBUG MODE\n***')
        arcpy.AddMessage('\n***\n*** IN DEBUG MODE\n***')
        
    try:

        logFilePath = srd_misc.getLogFilePath(sys.argv[0])
        logChn = srd_logging.SRD_Log(logFilePath)
                    
        sourceCoverage = arcpy.GetParameterAsText(0)
        # Must add on polygon identifier.
        sourcePolygons = '%s\\polygon' % sourceCoverage
        
        destinationAVI = arcpy.GetParameterAsText(1)

        #-------------------------------------------------------------
        # Set the log file.
        if logFilePath:
            try:
                # Parameters to tool will be written to log file in case of problems.
                params = arcpy.GetParameterInfo()
                hdrList = ['Arguments:']
                idx = 0
                for param in params:
                    hdrList.append('Name: %s, Type: %s, Value: %s' % (param.name, param.parameterType, arcpy.GetParameterAsText(idx)))
                    idx += 1
                logChn.stampHeader(hdrList)
                arcpy.AddMessage('\n*** Log information will be written to: %s\n' % logFilePath)
                
                logChn.stampHeader(hdrList)
                arcpy.AddMessage('\n*** Log information will be written to: %s\n' % logFilePath)
            except:
                pass

        # Build the FGDB including domains.
        aviSchemaObj = AVI_Schema()
        aviSchemaObj.loadAVICoverageToGeodatabase(sourcePolygons, destinationAVI)
        
    except SRD_Exception as e:
        if logChn:
            logChn.logError(e)
            logChn.logError(srd_misc.exceptionDetails(sys.exc_info()[2]))
        else:
            arcpy.AddMessage(srd_misc.exceptionDetails(sys.exc_info()[2]))
    except Exception as e:
        if logChn:
            logChn.logError(e)
            logChn.logError(srd_misc.exceptionDetails(sys.exc_info()[2]))
        else:
            arcpy.AddMessage(srd_misc.exceptionDetails(sys.exc_info()[2]))
    else:
        logChn.logMsg('\n*** %s completed normally\n' % sys.argv[0])
    finally:
        arcpy.ResetEnvironments()
        if logChn:
            logChn.closeLog()
                
