Ejemplo n.º 1
0
def cloudMask(date1, date2):
    # dateFns have completed therefore assume that, at least, the
    # quadscenes have been created in projectStorage
    # (i.e., sceneID1U*, sceneID1L*, sceneID2U*, and sceneID2L* directories are populuated)
    # eros_data may or may not be present
	# cloudMask dependencies are on files in tiffStorage, /lsfdata/eros_data/extractedTars/

    outBasename = date1.sceneID + "_" + date2.sceneID + '_Fmask.tif'
    wrs2Name=date1.sceneID[3:9]

    if not os.path.exists(date1.folder+"/"+date1.sceneID+"_MTLFmask.TIF"):
	   rasterAnalysis_GDAL.cloudMask(os.path.join(LSF.tiffsStorage, date1.sceneID[:-2]))
	   # create quads from the input scene
	   quadPaths = rasterAnalysis_GDAL.cropToQuad(os.path.join(LSF.tiffsStorage, date1.sceneID[:-2]), LSF.projectStorage, LSF.quadsFolder)
	   landsatFactTools_GDAL.writeQuadToDB(quadPaths)
	   # get cloud cover percentage for each quad
	   quadCCDict = landsatFactTools_GDAL.getQuadCCpercent(quadPaths)
	   # =========================================================================
	   # write input scene quads cloud cover percentage to the landsat_metadata table in the database
	   landsatFactTools_GDAL.writeQuadsCCtoDB(quadCCDict,date1.folder.replace('UR','').replace('UL','').replace('LR','').replace('LL',''))
    if not os.path.exists(date2.folder+"/"+date2.sceneID+"_MTLFmask.TIF"):
	   rasterAnalysis_GDAL.cloudMask(os.path.join(LSF.tiffsStorage, date2.sceneID[:-2]))
	   # create quads from the input scene
	   quadPaths = rasterAnalysis_GDAL.cropToQuad(os.path.join(LSF.tiffsStorage, date2.sceneID[:-2]), LSF.projectStorage, LSF.quadsFolder)
	   landsatFactTools_GDAL.writeQuadToDB(quadPaths)
	   # get cloud cover percentage for each quad
	   quadCCDict = landsatFactTools_GDAL.getQuadCCpercent(quadPaths)
	   # =========================================================================
	   # write input scene quads cloud cover percentage to the landsat_metadata table in the database
	   landsatFactTools_GDAL.writeQuadsCCtoDB(quadCCDict,date1.folder.replace('UR','').replace('UL','').replace('LR','').replace('LL',''))

    outputTiffName=os.path.join(LSF.outFMASKfolder,outBasename)
    if not os.path.exists(outputTiffName):
	   if os.path.exists(date1.folder+"/"+date1.sceneID+"_MTLFmask.TIF") and os.path.exists(date2.folder+"/"+date2.sceneID+"_MTLFmask.TIF"):
             qaTiffName=os.path.join(LSF.tiffsStorage, date1.sceneID[:-2], date1.sceneID[:-2]) + "_BQA.TIF"
             if os.path.exists(qaTiffName):
               cloud_mask_type='BQA'
             else:
               cloud_mask_type='FMASK'
             FmaskReclassedArray1 = date1.cloudMaskArray()
             FmaskReclassedArray2 = date2.cloudMaskArray()
             FmaskReclassedArray = FmaskReclassedArray1 * FmaskReclassedArray2
             FmaskReclassedArrayPlus1 = FmaskReclassedArray + 1
             shpName=os.path.join(LSF.quadsFolder, 'wrs2_'+ wrs2Name + date1.folder[-2:]+'.shp')
             LSFGeoTIFF.Unsigned8BitLSFGeoTIFF.fromArray(FmaskReclassedArrayPlus1, date1.geoTiffAtts).write(outputTiffName, shpName)
             landsatFactTools_GDAL.writeProductToDB(os.path.basename(outputTiffName),date1.sceneID,date2.sceneID,'CLOUD',date2.sceneID[9:16], 'CR',cloud_mask_type)
    # if the product's been created, should  be a row in products
    # note if the row is missing
    else:
	   resultRowExists = rowExists(outBasename, 'products', 'product_id')
	   if resultRowExists == False:
             print "No row for {} in products".format(outBasename)
def ndmi(date1, date2):
	outBasename = date1.sceneID + "_" + date2.sceneID+ '_percent_NDMI.tif'
	wrs2Name=date1.sceneID[3:9]
	outputTiffName=os.path.join(LSF.outNDMIfolder, outBasename )
	if not os.path.exists(outputTiffName):
	   ndmi1 = date1.ndmi("SR")
	   ndmi2 = date2.ndmi("SR")
	   ndmiChange = ndmi2 - ndmi1
	   ndmiPercentChange = ndmiChange / np.absolute(ndmi1)
	   ndmi1 = None
	   ndmi2 = None
	   ndmiPercentChange = np.multiply(100,ndmiPercentChange)
	   shpName=os.path.join(LSF.quadsFolder, 'wrs2_'+ wrs2Name + date1.folder[-2:]+'.shp')
	   LSFGeoTIFF.Unsigned8BitLSFGeoTIFF.fromArray(ndmiPercentChange, date1.geoTiffAtts).write(outputTiffName, shpName)
	   landsatFactTools_GDAL.writeProductToDB(os.path.basename(outputTiffName),date1.sceneID,date2.sceneID,'NDMI',date2.sceneID[9:16], 'CR')
	   ndmiPercentChange = None
	else:
	   resultRowExists = rowExists(outBasename, 'products', 'product_id')
	   if resultRowExists == False:
            print "No row for {} in products".format(outBasename)
def swir(date1, date2):
	outBasename = date1.sceneID + "_" + date2.sceneID + '_percent_SWIR.tif'
	wrs2Name=date1.sceneID[3:9]
	outputTiffName=os.path.join(LSF.outSWIRfolder, outBasename)
	if not os.path.exists(outputTiffName):
	   swir1 = date1.SurfaceReflectance(date1.swir2,"swir2")
	   swir2 = date2.SurfaceReflectance(date2.swir2,"swir2")
	   swir = np.subtract(swir2,swir1)
	   swirPercentChange = swir / np.absolute(swir1)
	   swir = None
	   swir1 = None
	   swir2 = None
	   swirPercentChange = np.multiply(100,swirPercentChange)
   	   shpName=os.path.join(LSF.quadsFolder, 'wrs2_'+ wrs2Name + date1.folder[-2:]+'.shp')
	   LSFGeoTIFF.Unsigned8BitLSFGeoTIFF.fromArray(swirPercentChange, date1.geoTiffAtts).write(outputTiffName, shpName)
	   landsatFactTools_GDAL.writeProductToDB(os.path.basename(outputTiffName),date1.sceneID,date2.sceneID,'SWIR',date2.sceneID[9:16], 'CR')
	   swirPercentChange = None
    # if the product's been created, should  be a row in products
    # note if the row is missing
	else:
	   resultRowExists = rowExists(outBasename, 'products', 'product_id')
	   if resultRowExists == False:
            print "No row for {} in products".format(outBasename)
     rasterAnalysis_GDAL.runFmask(date2.folder.replace('UR','').replace('UL','').replace('LR','').replace('LL',''),fmaskShellCall)
     # create quads from the input scene
     quadPaths = rasterAnalysis_GDAL.cropToQuad(date1.folder.replace('UR','').replace('UL','').replace('LR','').replace('LL',''),projectStorage,quadsFolder)
     landsatFactTools_GDAL.writeQuadToDB(quadPaths)
     # get cloud cover percentage for each quad
     # write input scene quads cloud cover percentage to the landsat_metadata table in the database
     landsatFactTools_GDAL.readAndWriteQuadCC(quadPaths,date1.folder.replace('UR','').replace('UL','').replace('LR','').replace('LL',''))
 if os.path.exists(date1.folder+"/"+date1.sceneID+"_MTLFmask.TIF") and os.path.exists(date2.folder+"/"+date2.sceneID+"_MTLFmask.TIF"):
     FmaskReclassedArray1 = date1.reclassFmask()
     FmaskReclassedArray2 = date2.reclassFmask()
     FmaskReclassedArray = FmaskReclassedArray1 * FmaskReclassedArray2
     outputTiffName=os.path.join(outFMASKfolder,outBasename + '_Fmask.tif')
     shpName=os.path.join(quadsFolder, 'wrs2_'+ wrs2Name + date1.folder[-2:]+'.shp')
     LSFGeoTIFF.Unsigned8BitLSFGeoTIFF.fromArray(FmaskReclassedArray, date1.geoTiffAtts).write(outputTiffName, shpName)
     print "writeProductToDB: "+os.path.basename(outputTiffName)+" ,"+date1.sceneID+" ,"+date2.sceneID+" ,"+'CLOUD'+" ,"+date2.sceneID[9:16]+'Analysis Source'+" ,"+'LCV'
     landsatFactTools_GDAL.writeProductToDB(os.path.basename(outputTiffName),date1.sceneID,date2.sceneID,'CLOUD',date2.sceneID[9:16], 'LCV')
 else:
     raise RuntimeError("Apparently the fmask file doesn't exist")
 # =========================================================================
 # NDVI
 ndvi1 = date1.ndvi("SR")
 ndvi2 = date2.ndvi("SR")
 ndviChange = ndvi2 - ndvi1
 ndviPercentChange = ndviChange / np.absolute(ndvi1)
 ndvi1 = None
 ndvi2 = None
 ndviPercentChange = np.multiply(100,ndviPercentChange)
 outputTiffName=os.path.join(outNDVIfolder, outBasename + '_percent_NDVI.tif')
 shpName=os.path.join(quadsFolder, 'wrs2_'+ wrs2Name + date1.folder[-2:]+'.shp')
 LSFGeoTIFF.Unsigned8BitLSFGeoTIFF.fromArray(ndviPercentChange, date1.geoTiffAtts).write(outputTiffName, shpName)
 print "writeProductToDB: "+os.path.basename(outputTiffName)+" ,"+date1.sceneID+" ,"+date2.sceneID+" ,"+'NDVI'+" ,"+date2.sceneID[9:16]+'Analysis Source'+" ,"+'LCV'