def raster_average_mk2(rasterobject_list, outras): # this function improves the previous version in that no value data is considered from arcpy.sa import Con, SetNull, CellStatistics n = len(rasterobject_list) # get mask rastermask_list = list() for each in rasterobject_list: eachmask = Con(each > 32760, 1, 0) rastermask_list.append(eachmask) sum_mask = CellStatistics(rastermask_list, "SUM") # flip values and set null for mask # only do this for pixels having more than 6 NoData ## sum_mask = Con(sum_mask>0, None, 1) sum_mask = SetNull(sum_mask > 6, 1) # it doesn't honor mask outras_mask = r"C:\mask_temp.tif" sum_mask.save(outras_mask) # average, only operate on those valid values arcpy.env.mask = outras_mask # average avg_raster = CellStatistics(rasterobject_list, "MEAN", "DATA") avg_raster.save(outras) # clear mask arcpy.env.mask = None
def night_image_mean(): """ 从多年的night images生成一张均值image的栅格文件 """ env.workspace = "G:\yang\ozone_process/night_images" out_raster_name = "night_mean.tif" if not os.path.exists(os.path.join(env.workspace, out_raster_name)): night_images = get_night_images() out_rasters = night_images_extract(night_images) out_raster = CellStatistics(out_rasters, "MEAN", "DATA") out_raster.save(out_raster_name) return os.path.join(env.workspace, out_raster_name)
def evi_year_mean_stat(): years = YEARS for year in years: evi_raster_paths = glob.glob( os.path.join(EVI_RASTER_SUBDIR, "US{}*.250m_16_days_EVI.tif".format(str(year)))) output_raster_path = "evi_{}_mean_stat.tif".format(str(year)) if os.path.exists(os.path.join(env.workspace, output_raster_path)): print("{} has been created before".format(output_raster_path)) continue print("CellStatistics is working for {}".format(str(year))) evi_year_stat = CellStatistics(evi_raster_paths, "MAXIMUM", "NODATA") evi_year_stat.save(output_raster_path)