#=========================================================================================================
def Process_MRT(Package,year):
    import os, time
    from subprocess import Popen

    hdfpath = Package + '/Datos/NASA_Datos/MODIS/' + year
    param = open(Package + '/Ejecutables/MRT/Blank_Params.prm','r')
    data = param.read()
    data = data.replace('INPUT_FILENAME = ','INPUT_FILENAME = ' + hdfpath + '/MOD10A1.A' + year +'001.h11v11.005.2008257004812.hdf')
    data = data.replace('OUTPUT_FILENAME = ','OUTPUT_FILENAME = ' + hdfpath + '/MOD10A1.A' + year +'001.h11v11.005.2008257004812.tif')
    data = data.replace('/','\\')

    param2= open(Package.replace('/','\\') + r'\Ejecutables\MRT\Temp_MRT_Params.prm','w+')
    param2.write(data)

    print 'Process_MRT: Created parameters files for MODIS Reprojection tool!'

    # delete any existing batch file
    if os.path.isfile(Package + '/Ejecutables/MRT/bin/mrtbatch.bat'):
        os.remove(Package + '/Ejecutables/MRT/bin/mrtbatch.bat')

    # print 'Process_MRT: Please coppy and paste the following text into the command prompt window!'
    print 'Process_MRT: Running Modis Reprojection tool!'
    # print 'Process_MRT: This may take a while!'
    
    # run the batch script
    
    runbatch1 = 'java -jar '+ Package + '/Ejecutables/MRT/bin/MRTBatch.jar -d ' + Package + '/Datos/NASA_Datos/MODIS/' + year + ' -p ' + Package + '/Ejecutables/MRT/Temp_MRT_Params.prm'
    runbatch2 = Package +'\\Ejecutables\\run_mrt.bat'
    runbatch3 = Package +'\\Ejecutables\\mrtbatch.bat'
    runbatch1 = runbatch1.replace('/','\\')
    runbatch2 = runbatch2.replace('/','\\')
    runbatch3 = runbatch3.replace('/','\\')

    # create the batch file to run the other batch file, this seems silly but its first way to consistently make it work that we found.
    run_mrt = open(runbatch2,'w+')
    run_mrt.write(runbatch1)
    run_mrt.close()
    time.sleep(2)

    #actually runs the text in variable runbatch1 which is writen in the file runbatch2. that batch file creates runbatch3, then it runs runbatch3.

    print 'Process_MRT: Configuration files have been generated!'
    print 'Process_MRT: Please run the following batch files to launch MRT'
    print '    1:  ' + runbatch2 
    print '    2:  ' + runbatch3


#=========================================================================================================
def Process_MODIS(Package,Basin,Year):
    
    # assembling the list of file addresses 
    Dir = Package + '/Datos/Cuencas/'+ Basin + '/Datos_Intermedia/MODIS/' + Year
    ModisData = Package + '/Datos/NASA_Datos/MODIS/' + Year + '/prm'
    Shapefile = Package + '/Datos/Cuencas/' + Basin + '/Parametros/Shapefile/Shape.shp'

    # open meta.txt file and read out the value of the clip box.
    # meta file must be created during basin characterization by coppying the extents of basin shapefile.
    meta = open(Package + '/Datos/Cuencas/'+ Basin + '/Meta.txt','r')
    ClipBox = meta.read().split(':')[1].replace('Basin_Area','')
    print 'Process_MODIS: ClipBox set to ' + ClipBox

    # import modules and create temporary folders
    import arcpy, os
    arcpy.env.workspace = ModisData
    arcpy.env.overwriteOutput = True

    if not os.path.exists(Dir + '/temp'):
        os.makedirs(Dir + '/temp')

    # Grab a list of all the HDFS in the folder meeting the META criteria of the basin
    Tiffs = arcpy.ListFiles("*Fractional_Snow_Cover.tif")
    print 'Process_MODIS: Disocvered ' + str(len(Tiffs)) +' tifs'

    for filename in Tiffs:
        print 'Process_MODIS: Clipping and formating ' + filename
        
        clip_tif      = Dir + '/temp/clipped.tif'
        resampled_tif = Dir + '/RS_' + filename[:-4] + '.tif'
        
        arcpy.Clip_management(filename, ClipBox, clip_tif, Shapefile, "255", "ClippingGeometry")
        arcpy.Resample_management(clip_tif, resampled_tif, '29.10712091 29.10712091' , "NEAREST")

    print 'Process_MODIS: Finished processing year ' + str(Year)
    
#=========================================================================================================
def Process_TRMM(Package,Basin,Year,zoneField):

    # assembling the list of file addresses 
    TRMM_path= Package + '/Datos/NASA_Datos/TRMM/' + Year
    inZoneData = Package + '/Datos/Cuencas/' + Basin + '/Parametros/Shapefile/HighShape.shp'
    OutputTRMM = Package + '/Datos/Cuencas/' +Basin + '/Datos_Intermedia/TRMM/TRMM_Precip' + Year +'.dbf'
    OutputTRMMpath = Package + '/Datos/Cuencas/' +Basin + '/Datos_Intermedia/TRMM'

    #import modules and manage temporary folders
    import arcpy, arcgisscripting, sys, os, csv, string, shutil
    from arcpy import env
    from arcpy.sa import *
    from dbfpy import dbf
    
    arcpy.env.overwriteOutput = True

    IntermediateOutput = TRMM_path + '/temp'

    # make sure the TRMM output path is there
    if not os.path.exists(OutputTRMMpath):
        os.makedirs(OutputTRMMpath)

    # If a temp data folder already exists, delete its contents and recreate it. 
    if os.path.exists(IntermediateOutput):
        shutil.rmtree(IntermediateOutput)
        os.makedirs(IntermediateOutput)
    else:

    # If no temporary folder exists, NetCDFs are converted to tiffs for the first time.
        os.makedirs(IntermediateOutput)
        arcpy.env.workspace = TRMM_path
        arcpy.env.overwriteOutput = True

        NCfiles = arcpy.ListFiles("*.nc")

        for filename in NCfiles:
            print 'Process_TRMM: Converting netCDF file ' + filename + ' to Raster'
            inNCfiles = arcpy.env.workspace + "/" + filename
            fileroot = filename[0:(len(filename)-3)]
            outRasterLayer = TRMM_path + "/" + fileroot
            arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "r", "longitude", "latitude", "r", "", "", "BY_VALUE")   
            arcpy.CopyRaster_management("r", outRasterLayer + ".tif", "", "", "", "NONE", "NONE", "")

        print 'Process_TRMM: Finished creating TIFs!'


    # Execute zonal statistics functions on selected basin and store output as a dbf.
    arcpy.env.workspace = TRMM_path
    TIFfiles = arcpy.ListFiles("*.tif")
    
    try:
        for filename in TIFfiles:
            fileroot = filename[0:(len(filename)-4)]
            print "Process_TRMM: Calculating zonal statistics on " + filename
            
            inValueRaster = arcpy.env.workspace + "/" + filename
            outTable = IntermediateOutput + "/" + fileroot + '.dbf'
            arcpy.CheckOutExtension("Spatial")
            outZstat = ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster, outTable, "NODATA", "ALL")
            
        print 'Process_TRMM: Finished calculating zonal statistics!'

    except:
        print 'Error: Process_TRMM: Error encountered while calculating zonal statistics!'
        print 'Ensure that shapefile HighShape.shp is in the WSGS 1984 geographic projection'
        print 'And is stored in the correct directory under basin parameters.'

    # Create csvs from DBFs
    arcpy.env.workspace = IntermediateOutput
    DBFfiles = arcpy.ListFiles("*.dbf")

    try:
        for filename in DBFfiles:
            fileroot = filename[0:(len(filename)-4)]
            
            # replace the '.' with '_', because the merge function is apparently very finicky with inputs.
            csv_fn = IntermediateOutput + '/' + string.replace(fileroot, '.', '_') + '.csv'
            
            inDBFfiles = arcpy.env.workspace + '/' + filename   
            with open(csv_fn,'wb') as csvfile:
                in_db = dbf.Dbf(inDBFfiles)
                out_csv = csv.writer(csvfile)
                names = []
                for field in in_db.header.fields:
                    names.append(field.name)
                out_csv.writerow(names)
                for rec in in_db:
                    out_csv.writerow(rec.fieldData)
                in_db.close()

        print 'Process_TRMM: Finished conversion to CSVs!'
    except:
        print 'Error: Process_TRMM: Error encountered while creating CSVs from dbfs!'

        
    # Merge CSVs together and print a bunch of progress items.
    arcpy.env.workspace = IntermediateOutput
    CSVfiles = arcpy.ListFiles("*.csv")

    print 'Process_TRMM: Creating Output file at ' + OutputTRMM
    print 'Process_TRMM: This may take up to 15 minutes' 
    arcpy.env.workspace = IntermediateOutput
    CSVfiles = arcpy.ListFiles("*.csv")
    arcpy.Merge_management(CSVfiles, OutputTRMM)

    print 'Process_TRMM: Output file created for year ' + Year
