def calcTCI_os(cur_filename, lta_max_filename, lta_min_filename, dst_filename, gdal_path):
    # arcpy-free version
    # calculate Temperature Condition Index
    # TCI = 100 x (LST_max - LST)/(LST_max - LST_min)

    with rasterio.open(cur_filename) as cur_r:
#        cur_band = cur_r.read(1, masked=True)
        cur_band = cur_r.read(1, masked=False)
        profile = cur_r.profile.copy()
        print cur_r.nodatavals
        with rasterio.open(lta_max_filename) as lta_max_r:
            lta_max_a = lta_max_r.read(1, masked=False)
            with rasterio.open(lta_min_filename) as lta_min_r:
                lta_min_a = lta_min_r.read(1, masked=False)
                numerator=(lta_max_a - cur_band)
                denominator = (lta_max_a - lta_min_a)
                res2 = np.where(np.logical_or(cur_band == cur_r.nodatavals[0],
                                             np.logical_or(lta_max_a == lta_max_r.nodatavals[0],
                                             lta_min_a == lta_min_r.nodatavals[0])),
                               cur_r.nodatavals[0], 100*(numerator/denominator))

                profile.update(dtype=rasterio.float32)
                dst_f = "{0}.tmp{1}".format(os.path.splitext(dst_filename)[0], os.path.splitext(dst_filename)[1])
                with rasterio.open(dst_f, 'w', **profile) as dst:
                    dst.write(res2.astype(rasterio.float32), 1)
                rasterUtils.setRasterNoDataValues(dst_f, dst_filename, gdal_path, -9999, None, 'Float32', True)

                    # del newd_f
                # del res
                # del res2
    return 0
def calcVCI_os(cur_filename, evi_max_filename, evi_min_filename, dst_filename, gdal_path=None):
    # arcpy-free version
    # calculate Vegetation Condition Index
    # VCI = 100 x (EVI - EVI_min)/(EVI_max - EVI_min)

    with rasterio.open(cur_filename) as cur_r:
        cur_band = cur_r.read(1, masked=False)
        profile = cur_r.profile.copy()
        print cur_r.nodatavals
        with rasterio.open(evi_max_filename) as evi_max_r:
            evi_max_a = evi_max_r.read(1, masked=False)
            with rasterio.open(evi_min_filename) as evi_min_r:
                evi_min_a = evi_min_r.read(1, masked=False)
                numerator = (cur_band - evi_min_a)
                denominator = (evi_max_a - evi_min_a)
                denominator = np.where(denominator == 0, cur_r.nodatavals, denominator)
                res2 = np.where(np.logical_or(cur_band == cur_r.nodatavals[0],
                                              np.logical_or(evi_max_a == evi_max_r.nodatavals[0],
                                                            evi_min_a == evi_min_r.nodatavals[0])),
                                cur_r.nodatavals[0], 100.0 * (numerator.astype(np.float32) / denominator.astype(np.float32)))
                profile.update(dtype=rasterio.float32)
                dst_f = "{0}.tmp{1}".format(os.path.splitext(dst_filename)[0], os.path.splitext(dst_filename)[1])
                with rasterio.open(dst_f, 'w', **profile) as dst:
                    dst.write(res2.astype(rasterio.float32), 1)
                rasterUtils.setRasterNoDataValues(dst_f, dst_filename, gdal_path, -9999, None, 'Float32', True)

    return 0
def calcVHI_os(vci_filename, tci_filename, dst_filename, shpfile, gdal_path=None):
    # arcpy-free version
    # calculate Vegetation Health Index
    # VHI = 0.5 x (VCI + TCI)
#    calcVHI_gdal_calculations(vci_filename, tci_filename, dst_filename, shpfile)

    with rasterio.open(vci_filename) as vci_r:
        vci_a = vci_r.read(1, masked=False)
        # print vci_r.nodatavals
        with rasterio.open(tci_filename) as tci_r:
            tci_a = tci_r.read(1, masked=False)
            profile = tci_r.profile.copy()
            vci_a2 = None
            tci_a2 = None
            # print "initial"
            # print np.max(vci_a)
            # print np.min(vci_a)
            # print np.mean(vci_a)
            if tci_a.shape > vci_a.shape:
                # only use smaller area
                tci_a2 = np.resize(tci_a, vci_a.shape)
                vci_a2 = vci_a
            elif vci_a.shape > tci_a.shape:
                vci_a2 = np.resize(vci_a, tci_a.shape)
                tci_a2 = tci_a
            else:
                vci_a2 = vci_a
                tci_a2 = tci_a

            tci_a2[np.isnan(tci_a2)] = tci_r.nodatavals
            vci_a2[np.isnan(vci_a2)] = vci_r.nodatavals


            vhi = np.zeros(tci_a.shape, np.float32)
            tmp = 0.5*(tci_a2 + vci_a2)
#            vhi = np.where(tci_a == tci_r.nodatavals[0], -9999, 0.5) # * (tci_a2 + vci_a2))
            vhi = np.where(np.logical_and(vci_a2 == vci_r.nodatavals[0],
                                          tci_a2 == tci_r.nodatavals[0]),
                           vci_r.nodatavals,
                           np.where(vci_a2 == vci_r.nodatavals[0], tci_a2,
                                    np.where(tci_a2 == tci_r.nodatavals[0], vci_a2,
                                             np.where(np.logical_and(vci_a2 == 0.0, tci_a2 == 0.0), 0.0,
                                                      np.where(vci_a2 == 0.0, tci_a2,
                                                               np.where(tci_a2 == 0.0, vci_a2,
                                                                        (tci_a2 + vci_a2)*0.5)))
                                             )))
            profile.update(dtype=rasterio.float32)
            dst_f = "{0}.tmp{1}".format(os.path.splitext(dst_filename)[0], os.path.splitext(dst_filename)[1])
            with rasterio.open(dst_f, 'w', **profile) as dst:
                dst.write(vhi.astype(rasterio.float32), 1)
            rasterUtils.setRasterNoDataValues(dst_f, dst_filename, gdal_path, -9999, None, 'Float32', True)

    return 0
예제 #4
0
def calcAverageOfDayNight_os(dayFile, nightFile, avgFile, gdal_path):
    print "calcAverage: ", dayFile, nightFile
    dst_filename = "{0}.tmp{1}".format(os.path.splitext(avgFile)[0], os.path.splitext(avgFile)[1])
    with rasterio.open(dayFile) as day_r:
        profile = day_r.profile.copy()
#        profile.update(dtype=rasterio.uint32)
        profile.update(NoData=0)
        day_a = day_r.read(1, masked=False)
        with rasterio.open(nightFile) as night_r:
            night_a = night_r.read(1, masked=False)
            dst_r2 = np.where(np.logical_and(day_a==0, night_a==0), 0,
                              np.where(night_a==0, day_a,
                                       np.where(day_a==0, night_a, (day_a + night_a)/2)))
            with rasterio.open(dst_filename, 'w', **profile) as dst:
                dst.write(dst_r2.astype(rasterio.uint16), 1)
            rasterUtils.setRasterNoDataValues(dst_filename, avgFile, gdal_path, -9999, None, 'Int32', True)