コード例 #1
0
ファイル: longtermMinMax.py プロジェクト: b7j/DRLMRepo
def produceTotal(obs):
    """
    Set up rios to apply images
    """
    controls = applier.ApplierControls()
    controls.setReferenceImage(obs.inputImages[0])
    controls.setOutputDriverName('HFA')

    infiles = applier.FilenameAssociations()
    infiles.images = obs.inputImages

    outfiles = applier.FilenameAssociations()

    if obs.stage == 'dix':
        if obs.fraction == 'bare':
            outStage = 'djl'
        elif obs.fraction == 'green':
            outStage = 'djm'        
        elif obs.fraction == 'dry':        
            outStage = 'djn'


    outfiles.image = qvf.changestage(qvf.changedate(os.path.basename(obs.inputImages[0]),obs.era),outStage)
    obs.outfile = outfiles.image

    otherargs = applier.OtherInputs()
    imginfo = fileinfo.ImageInfo(obs.inputImages[0])
    otherargs.coverNull = imginfo.nodataval[0]
    controls.setStatsIgnore(otherargs.coverNull)

    applier.apply(model, infiles, outfiles, otherargs, controls=controls)

    addHistory(obs)
コード例 #2
0
def recallList(sceneListTemp, recallStage):
    """
    Define and recall landsat images with stage
    """
    recallList = []
    for scene in sceneListTemp:
        scene = qvf.changestage(scene, recallStage)
        if not os.path.exists(scene) and qv.existsonfilestore(scene):
            recallList.append(scene)

    qv.recallToHere(recallList)
コード例 #3
0
def getStatVal(imageFile,longitude,latitude,winsize,statistic,site):
    """
    Caculates the statistics on the pixels in the window array
    """

    band1,band2,band3,band4,band5,band6,count = 'None','None','None','None','None','None','None'

    if imageFile != 'None' and imageFile != None:
        imageFile=qvf.changestage(imageFile,'tmp')
        temp = '%s_%s_%spix.tif' % (imageFile.split('.')[0],site.strip(),winsize)

        if not os.path.exists(temp):
            subsetRaster = getWindow(imageFile,longitude,latitude,winsize,site)
        else:
            subsetRaster = temp

        try:
            imgInfo = gdalcommon.info(subsetRaster)
            handle = gdal.Open(subsetRaster)

            for band in [1,2,3,4,5,6]:
                if handle != None:
                    bandHandle = handle.GetRasterBand(band)
                    bandArray = bandHandle.ReadAsArray()

                    maskedBand = ma.masked_values(bandArray, 0)
                    count = ma.count(maskedBand)

                    if statistic == 'mean': statVal = maskedBand.mean()
                    elif statistic == 'std': statVal = maskedBand.std()
                    else:
                        statVal = None

                    if band == 1:
                        band1 = statVal

                    elif band == 2:
                        band2 = statVal

                    elif band == 3:
                        band3 = statVal

                    elif band == 4:
                        band4 = statVal

                    elif band == 5:
                        band5 = statVal

                    elif band == 6:
                        band6 = statVal
        except:
            pass

    return band1, band2, band3, band4, band5, band6, count
コード例 #4
0
def applyMasks(processedList):
    """
    Do masking of output - create generic model to be used for all seasonal products.
    Uses cloud and shadow masks to mask image. Uses RSC derived masks if available
    and fmask if not.
    """

    cloudList = []
    shadowList = []
    sceneList =[]
    maskImageList = []
    processedListClipped = []

    for scene in processedList:
        processedListClipped.append(qvf.changestage(scene, 'tmp'))
        if not os.path.exists(qvf.changestage(scene, 'tmp')):

            fMaskCloudImage = qvf.changestage(scene, 'dgr')

            fMaskShadowImage = qvf.changestage(scene, 'dgs')
            if qv.existsonfilestore(fMaskCloudImage):
                cloudMaskImage = fMaskCloudImage
            if qv.existsonfilestore(fMaskShadowImage):
                shadowMaskImage = fMaskShadowImage

            if qv.existsonfilestore(cloudMaskImage) and qv.existsonfilestore(shadowMaskImage) and qv.existsonfilestore(scene):
                cloudList.append(cloudMaskImage)
                shadowList.append(shadowMaskImage)
                sceneList.append(scene)
                maskImageList.append([scene,cloudMaskImage,shadowMaskImage])


    if len(maskImageList)!=0:
        recallList(cloudList, qvf.stage(cloudList[0]))
        recallList(shadowList, qvf.stage(shadowList[0]))
        recallList(sceneList, qvf.stage(sceneList[0]))

    print 'Applying masks...'

    controls = applier.ApplierControls()
    controls.setStatsIgnore(0)

    for (image1,image2,image3) in maskImageList:
        tmpscene = qvf.changestage(image1, 'tmp')
        print tmpscene

        if not os.path.exists(tmpscene) and os.path.exists(image1) and os.path.exists(image2):
            infiles = applier.FilenameAssociations()
            infiles.image1 = image1
            infiles.image2 = image2
            infiles.image3 = image3
            outfiles = applier.FilenameAssociations()
            outfiles.outimage = tmpscene
            if not os.path.exists(outfiles.outimage):
                applier.apply(createMaskedImage, infiles, outfiles, controls=controls)
            if os.path.exists(outfiles.outimage):

                os.remove(image1)
                os.remove(image2)
                os.remove(image3)

    return  processedListClipped
コード例 #5
0
def searchDatabaseScenes(pointDict,timeLag,defStage,winsize):
    """
    Search database to find scenes which intersect field data points and were acquired
    within specified number of days of field work
    """

    keys = pointDict.keys()

    for key in keys:
        pointDict[key].image[1] = ImageDict()
        pointDict[key].image[2] = ImageDict()
        pointDict[key].image[3] = ImageDict()
        pointDict[key].image[4] = ImageDict()
        pointDict[key].image[5] = ImageDict()
        pointDict[key].image[6] = ImageDict()
        pointDict[key].image[7] = ImageDict()
        pointDict[key].image[8] = ImageDict()


    for stage in ['d%s' % defStage]:
        if stage == 'd%s' % defStage:
	    print defStage
            recallStage = 'd%s' % defStage 
            data_source = 'usgs'

        for key in keys:
            fieldDate = pointDict[key].obs_time.split(' ')[0]
            fieldDate = fieldDate.replace('-','')
            fieldDate = '%s-%s-%s' % (fieldDate[:4],fieldDate[4:6],fieldDate[6:])
            pointDict[key].obs_time = fieldDate

            con = metadb.connect(api=metadb.DB_API)
            cursor = con.cursor()

            query = """
                SELECT abs(cast('%s' as date) - f.cal_date),SLATSFilename(f.sat_type,f.scene_date)
                FROM FindFootprintsByPoint(%s, %s) as f, landsat_list
                WHERE landsat_list.scene_date = f.scene_date
                    AND abs(cast('%s' as date) - f.cal_date) < %s
                    AND landsat_list.product = 're'
                    AND data_source ='%s'




                    """ % (fieldDate,pointDict[key].longitude,pointDict[key].latitude,fieldDate,timeLag,data_source)
            try:
                cursor.execute(query)
                results = cursor.fetchall()

                results.sort()

                index=[1,2,3,4,5,6,7,8]

                if len(results) > 0:
                    for i in index:
                        try:
                            result = results[i-1]
                            image = qvf.changestage(result[1], '%s' % stage)

                            if qv.existsonfilestore(image) and pointDict[key].image[i].imageName is None:
                                pointDict[key].image[i].imageName = image
                                pointDict[key].image[i].timeLag = result[0]
                                pointDict[key].image[i].tmp3pix = qvf.changestage('%s_%s_%spix.tif' % (image.split('.')[0],pointDict[key].site.strip(),winsize),'tmp')
                            else:
                                pointDict[key].image[i].imageName = 'None'
                                pointDict[key].image[i].timeLag = 'None'
                                pointDict[key].image[i].tmp3pix = 'None'

                        except IndexError:
                            pointDict[key].image[i].imageName = 'None'
                            pointDict[key].image[i].timeLag = 'None'
                            pointDict[key].image[i].tmp3pix = 'None'

                            image='None'

                elif len(results) == 0:
                    image = 'None'
                    print pointDict[key].site,fieldDate,pointDict[key].longitude,pointDict[key].latitude

                else:
                    print 'Really bad problem.'

            except:
                print 'failed', query, pointDict[key].site

    return pointDict