# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# ImportAVL.py
# Created on: 2012-06-05 08:49:23.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy, datetime, os
def stringday(d):
        return str(d.month).rjust(2, "0") + str(d.day).rjust(2, "0")
# Local variables:
yesterday = datetime.date.today() - datetime.timedelta(1)
m = str(yesterday.month).rjust(2, "0")
d = str(yesterday.day).rjust(2, "0")
y = str(yesterday.year).rjust(2, "0")
gpsfile = "GPSArchive.dbo.ARC_%s%s%s"%(m, d, y)
gdbfile = "D:\\Crime\\TimeInHotspot.gdb\\ARC_%s%s%s"%(m, d, y)
if os.environ.get("TEMP") == None:
        temp = "c:\\temp"  
else:
        temp = os.environ.get("TEMP")

if os.environ.get("TMP") == None:
        temp = "/usr/tmp"		
else:
        temp = os.environ.get("TMP")  
Connection_File_Name = temp + os.sep + "pdsdw1.sde"
if not os.path.isfile(Connection_File_Name):
        arcpy.CreateDatabaseConnection_management(temp, "pdsdw1", "SQL_SERVER", "pdsdw1", "DATABASE_AUTH", "GPSAdmin", "gpsBcs#", "SAVE_USERNAME", "GPSArchive")
arcpy.env.workspace = Connection_File_Name
if arcpy.Exists(gpsfile) and not arcpy.Exists(gdbfile):
        arcpy.CopyRows_management(gpsfile, gdbfile)
arcpy.env.workspace = "D:\\Crime\\TimeInHotspot.gdb"
tables = arcpy.ListTables()
for table in tables:
        print table
        # Process: Make XY Event Layer
        if not arcpy.Exists("XY" + table):
                        xylayer = arcpy.MakeXYEventLayer_management(table, "rLongitude", "rLatitude", table + "_Layer", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision", "")
                        print xylayer
                # Process: Feature Class to Feature Class
                        fc = arcpy.FeatureClassToFeatureClass_conversion(xylayer, arcpy.env.workspace, "XY" + table, "", "szIpAddress \"szIpAddress\" true true false 15 Text 0 0 ,First,#,,szIpAddress,-1,-1;dtTimeStamp \"dtTimeStamp\" true true false 8 Date 0 0 ,First,#,,dtTimeStamp,-1,-1;szPriId \"szPriId\" true true false 15 Text 0 0 ,First,#,,szPriId,-1,-1;szSecId \"szSecId\" true true false 15 Text 0 0 ,First,#,,szSecId,-1,-1;rLatitude \"rLatitude\" true true false 8 Double 0 0 ,First,#,,rLatitude,-1,-1;rLongitude \"rLongitude\" true true false 8 Double 0 0 ,First,#,,rLongitude,-1,-1;iSpeed \"iSpeed\" true true false 2 Short 0 0 ,First,#,,iSpeed,-1,-1;iHeading \"iHeading\" true true false 2 Short 0 0 ,First,#,,iHeading,-1,-1;byStatus \"byStatus\" true true false 2 Short 0 0 ,First,#,,byStatus,-1,-1", "MAX_FILE_SIZE_256TB")
                        print fc
        arcpy.Delete_management(table)
bench = datetime.date.today()
diff = bench.weekday()
if (diff < 4):
		diff = diff + 7
startweek = bench + datetime.timedelta(-3 - diff)
preceedweek = startweek + datetime.timedelta(-1)
endweek = bench + datetime.timedelta(3 - diff)
startweekend = bench + datetime.timedelta(-bench.weekday() - 3)
preceedweekend = startweekend + datetime.timedelta(-1)
endweekend = bench + datetime.timedelta(-bench.weekday() - 1)
days = [startweek,preceedweek,endweek,startweekend,preceedweekend,endweekend]
[zstartweek,zpreceedweek,zendweek,zstartweekend,zpreceedweekend,zendweekend] = [stringday(x) for x in days]
zpreceedweek = "XYARC_" + zpreceedweek + str(preceedweek.year)
zpreceedweekend = "XYARC_" + zpreceedweekend + str(preceedweekend.year)
print zstartweek,zpreceedweek,zendweek,zstartweekend,zpreceedweekend,zendweekend
outputweek = "Week" + zstartweek + "_" + zendweek
outputweekend = "Week" + zstartweekend + "_" + zendweekend
destweek = "Database Connections\\sssgisdb1_police_sde_prod_sdedbo.sde\\police_sde_prod.SDEDBO.SIUC\\police_sde_prod.SDEDBO." + outputweek
destweekend = "Database Connections\\sssgisdb1_police_sde_prod_sdedbo.sde\\police_sde_prod.SDEDBO.SIUC\\police_sde_prod.SDEDBO." + outputweekend
print outputweek
print outputweekend
print destweek
print destweekend
if not arcpy.Exists(outputweek):
        files = []
        for dn in range(-6,1):
                day = endweek + datetime.timedelta(dn)
                m = str(day.month).rjust(2, "0")
                d = str(day.day).rjust(2, "0")
                y = str(day.year).rjust(2, "0")
                addfile = "D:\\Crime\\TimeInHotspot.gdb\\XYARC_%s%s%s"%(m, d, y)
                if arcpy.Exists(addfile):
                        files.append(addfile)
        print files
        arcpy.Merge_management(files, outputweek)
        arcpy.CopyFeatures_management(outputweek, destweek)
if not arcpy.Exists(outputweek.replace('Week','Pings')):
        #And then run FME for the week
        p = zpreceedweek + " " + outputweek
        command = 'C:\\apps\\FME\\fme.exe "C:\Users\pd3726.STLOUISCO\Documents\My FME Workspaces\TimeInHotspotNew.fmw" --FEATURE_TYPES "' + p + '"'
        print "Running This"
        print command
        os.system(command)
        print "Command executed."
if not arcpy.Exists(destweek.replace('Week','Pings')):
        print "Copying Week Pings to SDE."
        arcpy.CopyFeatures_management(outputweek.replace('Week','Pings'), destweek.replace('Week','Pings'))
        print "Copied"
if not arcpy.Exists(outputweekend):
        files = []
        for dn in range(-2,1):
                day = endweekend + datetime.timedelta(dn)
                m = str(day.month).rjust(2, "0")
                d = str(day.day).rjust(2, "0")
                y = str(day.year).rjust(2, "0")
                addfile = "D:\\Crime\\TimeInHotspot.gdb\\XYARC_%s%s%s"%(m, d, y)
                if arcpy.Exists(addfile):
                        files.append(addfile)
        print files
        arcpy.Merge_management(files, outputweekend)
        arcpy.CopyFeatures_management(outputweekend, destweekend)
if not arcpy.Exists(outputweekend.replace('Week','Pings')):
        #And then run FME for the week
        p = zpreceedweekend + " " + outputweekend
        command = 'C:\\apps\\FME\\fme.exe "C:\Users\pd3726.STLOUISCO\Documents\My FME Workspaces\TimeInHotspotNew.fmw" --FEATURE_TYPES "' + p + '"'
        print "Running This"
        print command
        os.system(command)
        print "Command executed."
if not arcpy.Exists(destweekend.replace('Week','Pings')):
        print "Copying Weekend Pings to SDE."
        arcpy.CopyFeatures_management(outputweekend.replace('Week','Pings'), destweekend.replace('Week','Pings'))
        print "Copied"
