def calc_monthly_average(basepath, filename, layers_by_month, epsg="3857"):
    print "-----Averages"

    if not os.path.exists(basepath):
        os.makedirs(basepath)

    for month in layers_by_month:
        output_path = basepath + "/" + filename + "_" + month + "_" + epsg + ".tif"

        data = None
        kwargs = None

        print "Processing: ", str(month)
        for f in layers_by_month[month]:
            print "Reading: ",  f
            r = rasterio.open(f)

            if data is None:
                data, kwargs = initialize_rasterio_raster(r, rasterio.float32)

            data = data + r.read_band(1).astype(float)

        data = data / len(layers_by_month[month])

        # writing
        print "Writing: ", output_path
        with rasterio.open(output_path, 'w', **kwargs) as dst:
            dst.write_band(1, data.astype(rasterio.float32))
def calc_variance(basepath, filename, layers_by_month, epsg="3857"):
    print "-----Variance"

    for month in layers_by_month:
        print month
        output_path = basepath + "/" + filename + "_" + month + "_" + epsg + ".tif"

        data = None
        kwargs = None

        if month == '07':
            print "Processing: ", str(month)
            for f in layers_by_month[month]:
                print "Reading: ",  f
                r = rasterio.open(f)

                if data is None:
                    data, kwargs = initialize_rasterio_raster(r, rasterio.float32)

                band_data = r.read_band(1).astype(float)

                sq = (band_data * band_data)

                # sum of squares
                data = data + sq

            # divide by n-1
            data = data / (len(layers_by_month[month]) - 1)

            # writing
            print "Writing: ", output_path
            with rasterio.open(output_path, 'w', **kwargs) as dst:
                dst.write_band(1, data.astype(rasterio.float32))
def calc_anomalies(basepath, layers, filename, epsg="3857"):
    print "-----Anomalies"

    for layer in layers:
        date = get_date_by_filename(layer)
        month = get_month_by_filename(layer)
        avg_path = basepath + "/avg/" + filename + "_" + month + "_" + epsg + ".tif"
        output_path = basepath + "/anomalies/" + filename + "_" + date + "_" + epsg + ".tif"

        print "Processing: ", layer, " ", avg_path
        r = rasterio.open(layer)
        r_avg = rasterio.open(avg_path)
        data, kwargs = initialize_rasterio_raster(r, rasterio.float32)
        r_band = r.read_band(1)
        r_avg_band = r_avg.read_band(1)

        nodata = 0
        index1 = (r_band != nodata)
        index2 = (r_avg_band != nodata)
        r_band = index1 * index2 *r_band
        r_avg_band = index1 * index2 * r_avg_band

        data = r_band.astype(float) - r_avg_band.astype(float)

        # writing
        print "Writing: ", output_path
        with rasterio.open(output_path, 'w', **kwargs) as dst:
            dst.write_band(1, data.astype(rasterio.float32))
def calc(basepath, layers, filename, epsg="3857"):
    print "-----Z-SCORE"
    for layer in layers:
        date = get_date_by_filename(layer)
        month = get_month_by_filename(layer)
        sd_path = basepath + "/sd/" + filename + "_" + month + "_" + epsg + ".tif"
        output_path = basepath + "/zscore/" + filename + "_" + date + "_" + epsg + ".tif"


        print "Processing: ", layer, " ", sd_path
        r = rasterio.open(layer)
        r_sd = rasterio.open(sd_path)
        data, kwargs = initialize_rasterio_raster(r, rasterio.float32)
        r_band = r.read_band(1)
        r_sd_band = r_sd.read_band(1)


        nodata = 0
        index1 = (r_band != nodata)
        index2 = (r_sd_band != nodata)
        r_band = index1 * index2 * r_band
        r_sd_band = index1 * index2 * r_sd_band

        data = (r_band.astype(float) / r_sd_band.astype(float))

        # writing
        print "Writing: ", output_path
        with rasterio.open(output_path, 'w', **kwargs) as dst:
            dst.write_band(1, data.astype(rasterio.float32))
def calc(basepath, output_path, layers, epsg="3857"):
    print "-----Anomaly DPY"


    for layer in layers:
        # print get_year_by_filename(layer)
        year = get_year_by_filename(layer)

        data = None
        month = get_month_by_filename(layer)
        filename = get_filename(layer)
        base_filename = get_base_filename(filename)
        yearPrev = str(int(year) - 1)

        layerPrev = basepath + "/" + base_filename + "_" + yearPrev + month + "_" + epsg + ".tif"

        print "Processing: ", layer, layerPrev

        try:
            print "Reading: ",  layer
            r = rasterio.open(layer)
            r_data = r.read_band(1).astype(float)

            print "Reading: ",  layerPrev
            r_prev = rasterio.open(layerPrev)
            r_prev_data = r_prev.read_band(1).astype(float)

            if data is None:
                data, kwargs = initialize_rasterio_raster(r, rasterio.float32)


            nodata = 0
            index1 = (r_data != nodata)
            index2 = (r_prev_data != nodata)
            r_data = index1 * index2 * r_data
            r_prev_data = index1 * index2 * r_prev_data
            data = r_data - r_prev_data

            # writing
            output_layer_path = output_path + "/" + filename + ".tif"
            print "Writing: ", output_layer_path
            with rasterio.open(output_layer_path, 'w', **kwargs) as dst:
                dst.write_band(1, data.astype(rasterio.float32))
        except Exception, e:
            print e