def align_rasters(raster, alignraster, how=np.ma.mean, cxsize=None, cysize=None, masked=False):
ndv1, xsize1, ysize1, geot1, projection1, datatype1 = get_geo_info(raster)<EOL>ndv2, xsize2, ysize2, geot2, projection2, datatype2 = get_geo_info(alignraster)<EOL>if projection1.ExportToMICoordSys() == projection2.ExportToMICoordSys():<EOL><INDENT>blocksize = (np.round(geot2[<NUM_LIT:1>]/geot1[<NUM_LIT:1>]).astype(int), np.round(geot2[-<NUM_LIT:1>]/geot1[-<NUM_LIT:1>]).astype(int))<EOL>mraster = gdalnumeric.LoadFile(raster)<EOL>mraster = np.ma.masked_array(mraster, mask=mraster == ndv1, fill_value=ndv1)<EOL>mmin = mraster.min()<EOL>mraster = block_reduce(mraster, blocksize, func=how)<EOL>araster = gdalnumeric.LoadFile(alignraster)<EOL>araster = np.ma.masked_array(araster, mask=araster == ndv2, fill_value=ndv2)<EOL>amin = araster.min()<EOL>if geot1[<NUM_LIT:0>] <= geot2[<NUM_LIT:0>]:<EOL><INDENT>row3, mcol = map_pixel(geot2[<NUM_LIT:0>], geot2[<NUM_LIT:3>], geot1[<NUM_LIT:1>] *blocksize[<NUM_LIT:0>],<EOL>geot1[-<NUM_LIT:1>]*blocksize[<NUM_LIT:1>], geot1[<NUM_LIT:0>], geot1[<NUM_LIT:3>])<EOL>acol = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>row3, acol = map_pixel(geot1[<NUM_LIT:0>], geot1[<NUM_LIT:3>], geot2[<NUM_LIT:1>], geot2[-<NUM_LIT:1>], geot2[<NUM_LIT:0>], geot2[<NUM_LIT:3>])<EOL>mcol = <NUM_LIT:0><EOL><DEDENT>if geot1[<NUM_LIT:3>] <= geot2[<NUM_LIT:3>]:<EOL><INDENT>arow, col3 = map_pixel(geot1[<NUM_LIT:0>], geot1[<NUM_LIT:3>], geot2[<NUM_LIT:1>], geot2[-<NUM_LIT:1>], geot2[<NUM_LIT:0>], geot2[<NUM_LIT:3>])<EOL>mrow = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>mrow, col3 = map_pixel(geot2[<NUM_LIT:0>], geot2[<NUM_LIT:3>], geot1[<NUM_LIT:1>] *blocksize[<NUM_LIT:0>],<EOL>geot1[-<NUM_LIT:1>]*blocksize[<NUM_LIT:1>], geot1[<NUM_LIT:0>], geot1[<NUM_LIT:3>])<EOL>arow = <NUM_LIT:0><EOL><DEDENT>'''<STR_LIT>'''<EOL>mraster = mraster[mrow:, mcol:]<EOL>araster = araster[arow:, acol:]<EOL>if cxsize and cysize:<EOL><INDENT>araster = araster[:cysize, :cxsize]<EOL>mraster = mraster[:cysize, :cxsize]<EOL><DEDENT>else:<EOL><INDENT>rows = min(araster.shape[<NUM_LIT:0>], mraster.shape[<NUM_LIT:0>])<EOL>cols = min(araster.shape[<NUM_LIT:1>], mraster.shape[<NUM_LIT:1>])<EOL>araster = araster[:rows, :cols]<EOL>mraster = mraster[:rows, :cols]<EOL><DEDENT>if masked:<EOL><INDENT>mraster = np.ma.masked_array(mraster, mask=mraster < mmin, fill_value=ndv1)<EOL>araster = np.ma.masked_array(araster, mask=araster < amin, fill_value=ndv2)<EOL><DEDENT>geot = (max(geot1[<NUM_LIT:0>], geot2[<NUM_LIT:0>]), geot1[<NUM_LIT:1>]*blocksize[<NUM_LIT:0>], geot1[<NUM_LIT:2>],<EOL>min(geot1[<NUM_LIT:3>], geot2[<NUM_LIT:3>]), geot1[<NUM_LIT:4>], geot1[-<NUM_LIT:1>]*blocksize[<NUM_LIT:1>])<EOL>return (mraster, araster, geot)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL>return (-<NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>)<EOL><DEDENT>
Align two rasters so that data overlaps by geographical location
Usage:
(alignedraster_o, alignedraster_a, geot_a) = AlignRasters(raster, alignraster, how=np.mean)

where:
    raster: string with location of raster to be aligned

    alignraster: string with location of raster to which raster will be aligned

    how: function used to aggregate cells (if the rasters have different sizes)

It is assumed that both rasters have the same size