def Precip(watershed_poly): # Local variables precipchago = r'D:\Jackson\StorageCapacity\ToolData\precipchago' precip_raster = Raster(precipchago) prec_table = 'prec_table' prec_table_avg = 'prec_table_avg' if precipchago == '#' or not precipchago: precipchago = "precipchago" # provide a default value if unspecified # Process arcpy.AddMessage("Extract precipitation values from raster to table") arcpy.ExtractValuesToTable_ga(watershed_poly, precip_raster, prec_table, "", "true") arcpy.AddField_management(watershed_poly, "Prec_ID", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED") arcpy.CalculateField_management(watershed_poly, "Prec_ID", 1, "PYTHON", "") arcpy.AddMessage("Determine Average precipitation across watershed") arcpy.Statistics_analysis(prec_table, prec_table_avg, "Value MEAN", "") arcpy.AddField_management(prec_table_avg, "Av_Prec", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED") arcpy.AddField_management(prec_table_avg, "ID") arcpy.CalculateField_management(prec_table_avg, "ID", 1, "PYTHON") arcpy.CalculateField_management(prec_table_avg, "Av_Prec", "!MEAN_Value!", "PYTHON", "") arcpy.JoinField_management(watershed_poly, "Prec_ID", prec_table_avg, "ID", "Av_Prec")
def extractValues(): '''Extract values of daily rainfall data to csv table based on household point shapefile. Add option to output table with names of input rasters ''' env.workspace = pathlist[0] # Set workplace for CHIRPS daily rasters raslist = arcpy.ListRasters() # create raster list of all rasters outname = "CHIRPS_tanz.dbf" arcpy.ExtractValuesToTable_ga(pathlist[2] + "BalancedGeoVars.shp", raslist, pathlist[1] + outname, pathlist[1] + "rasnames.dbf", "FALSE") print "Completed extracting values on", outname
def calcstats(mydir): lTIFs = [ ] # Create a blank list that would be populated by input geotiff files later for path, subdirs, files in os.walk(mydir): arcpy.AddMessage("\n" + 'Processing Folder ' + mydir + "\n") for name in files: if fnmatch(name, pattern): TIF = os.path.join(path, name) lTIFs.append(TIF) # Loop through each raster file and calculate statistics for tif in lTIFs: tifpath, tifname = os.path.split(tif) # Split filenames and paths tbloutfield = tifname.split('.')[ 0] # Get output csv filname without extension tbloutfield = tbloutfield.replace('prate_', '') # Strip _pt from name outcsv = tbloutfield + '.csv' tbloutfield = "d" + tbloutfield[ 1:] # Replace first char of date(year) by "d" to overrule a restriction ################################################################### ## Definition of variables related to Point Shapefile Processing ## ################################################################### ptout = tif.replace( '.tif', '.dbf') # Full name & Path of temp output point shp ############################################################## ## Start process to calulate statistics for point shapefile ## ############################################################## # Skip if output table already exists if not os.path.exists(ptout): arcpy.AddMessage('Processing ' + tifname) try: arcpy.ExtractValuesToTable_ga(ptshp, tif, ptout, "", "") fmap = '{} \\\"'.format( tbloutfield ) + tbloutfield + "\\\" " + "true true false 19 Double 0 0 ,First,#," + ptout + ",Value,-1,-1;SrcID_Feat \\\"SrcID_Feat\\\" true true false 10 Long 0 10 ,First,#," + ptout + ",SrcID_Feat,-1,-1" arcpy.TableToTable_conversion(ptout, tifpath, outcsv, "", fmap, "") # Define local variables for calculating statistics if ptinter == True: arcpy.Delete_management(ptout) except: arcpy.AddMessage('Error in processing ' + tifname) else: arcpy.AddMessage('Skipping ' + tifname + " (Already Exists)") del ptout, tif, tbloutfield, fmap, outcsv lTIFs = []
def Find_Slope(watershed_poly): # Local variables watershed_dem = 'watershed_dem' slope1 = 'slope1' slope_table = 'slope_table' slope_stats = 'slope_stats' # Extract slope raster by watershed polygon arcpy.AddMessage("clip DEM to watershed polygon") arcpy.gp.ExtractByMask_sa(filled_dem, watershed_poly, watershed_dem) arcpy.AddMessage("Calculate slope for watershed") arcpy.gp.Slope_sa(watershed_dem, slope1, "PERCENT_RISE", "1") arcpy.ExtractValuesToTable_ga(watershed_poly, slope1, slope_table) arcpy.AddMessage("Find average slope across the watershed") arcpy.Statistics_analysis(slope_table, slope_stats, "Value MEAN") arcpy.AddField_management(slope_stats, "Slope_Avg", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.CalculateField_management(slope_stats, "Slope_Avg", "!MEAN_Value!", "PYTHON", "") arcpy.AddMessage("Join field to polygon") arcpy.AddField_management(slope_stats, "Prec_ID") arcpy.CalculateField_management(slope_stats, "Prec_ID", 1, "PYTHON") arcpy.JoinField_management(watershed_poly, "Prec_ID", slope_stats, "OBJECTID", "Slope_Avg")
# Mask Hillshade with Cloudless LST pixels masked_hillshade_raster = Con( Raster( "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/merged_" + the_date + ".tif"), Raster( "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/hillshade_" + the_date + ".tif")) masked_hillshade_raster.save( "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/masked_hillshade_" + the_date + ".tif") # Extract Values to Table (surface temp) arcpy.ExtractValuesToTable_ga( "C:/Users/eric-/Desktop/MGST_Final/MGST_Final/all_stations_online_XYTableT2/all_stations_online_XYTableT2.shp", "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/merged_" + the_date + ".tif", "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/" + the_date + ".txt", "", "") # Extract Values to Table (masked hillshade status) arcpy.ExtractValuesToTable_ga( "C:/Users/eric-/Desktop/MGST_Final/MGST_Final/all_stations_online_XYTableT2/all_stations_online_XYTableT2.shp", "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/masked_hillshade_" + the_date + ".tif", "C:/Users/eric-/Desktop/MGST_Final/ECOSTRESS_processing/GeoRef/output/" + the_date + "_shadestatus.txt", "", "") except: pass ################## # Step THREE: Establish the "hot" and "cold" spots for eight groups (see 'ex' variable in code block)