#Anaxyrus quercicus Soybeans Max Dispersal Hot Spot Analysis

#Purpose: Assess overlap of areas with high suitability for Anaxyrus quercicus occurrence and high soybeans agricultural activity using ESRI optimized hot spot analysis tool. Compare how the hot spots change in the future under different climate scenarios.

#See Figure 1 for flow chart of geoprocessing steps

#############################################


#Hot Spot Analysis - Zonal Statistics to Hot Spot Analysis


#Zonal Stats as Table to Hot Spot Analysis - Soybeans

#Current Distribution
arcpy.ia.ZonalStatisticsAsTable(
    in_zone_data="AnaxQue_WBDHU12_Merge",
    zone_field="huc12",
    in_value_raster=r"e:\spatialanalysis\arcgis_pesticideanalysis\crophotspotanalysis\AnaxQue_CropHotSpotAnalysis.gdb\RastCalc_CurrentAnaxQue_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ZonalStat_CurrentAQ_SoybeansFreq",
    ignore_nodata="DATA",
    statistics_type="SUM",
    process_as_multidimensional="CURRENT_SLICE",
    percentile_values=[90],
    percentile_interpolation_type="AUTO_DETECT",
    circular_calculation="ARITHMETIC",
    circular_wrap_value=360
)

#Export zonal stats output to hard drive for records
arcpy.conversion.ExportTable(
    in_table="ZonalStat_CurrentAQ_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\zonalstat_currentaq_soybeansfreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='HUC12 "HUC12" true true false 12 Text 0 0,First,#,zonalstat_currentaq_soybeansfreq,HUC12,0,12;ZONE-CODE "ZONE-CODE" true true false 4 Long 0 0,First,#,zonalstat_currentaq_soybeansfreq,ZONE-CODE,-1,-1;COUNT "COUNT" true true false 8 Double 0 0,First,#,zonalstat_currentaq_soybeansfreq,COUNT,-1,-1;AREA "AREA" true true false 8 Double 0 0,First,#,zonalstat_currentaq_soybeansfreq,AREA,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,zonalstat_currentaq_soybeansfreq,SUM,-1,-1',
    sort_field=None
)

#Join Zonal Stats output to clipped HUC12 layer
arcpy.management.AddJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    in_field="huc12",
    join_table="ZonalStat_CurrentAQ_SoybeansFreq",
    join_field="HUC12",
    join_type="KEEP_ALL",
    index_join_fields="NO_INDEX_JOIN_FIELDS"
)

#Export clipped HUC12 layer with joined zonal stats
arcpy.conversion.ExportFeatures(
    in_features="AnaxQue_WBDHU12_Merge",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\HUC12_CurrentAQ_SoybeansFreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='areaacres "areaacres" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areaacres,-1,-1;areasqkm "areasqkm" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areasqkm,-1,-1;states "states" true true false 50 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.states,0,50;huc12 "huc12" true true false 12 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.huc12,0,12;name "name" true true false 120 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.name,0,120;hutype "hutype" true true false 1 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.hutype,0,1;humod "humod" true true false 30 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.humod,0,30;tohuc "tohuc" true true false 16 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.tohuc,0,16;COUNT "COUNT" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_CurrentAQ_SoybeansFreq.COUNT,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_CurrentAQ_SoybeansFreq.SUM,-1,-1',
    sort_field=None
)

#Select the features with NULL values for the COUNT fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_CurrentAQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="COUNT IS NULL",
    invert_where_clause=None
)

#Calculate COUNT field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_CurrentAQ_SoybeansFreq",
    field="COUNT",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_CurrentAQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NULL",
    invert_where_clause=None
)

#Calculate SUM field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_CurrentAQ_SoybeansFreq",
    field="SUM",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with NO NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_CurrentAQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NOT NULL",
    invert_where_clause=None
)

#Calculate Sum_Dens field - SUM/HUC12 Unit Area
arcpy.management.CalculateField(
    in_table="HUC12_CurrentAQ_SoybeansFreq",
    field="Sum_Dens",
    expression="!SUM! / !areasqkm!",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Remove joined Zonal Stats output from clipped HUC12 layer
arcpy.management.RemoveJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    join_name="ZonalStat_CurrentAQ_SoybeansFreq"
)

#Optimized Hot Spot Analysis - current
arcpy.stats.OptimizedHotSpotAnalysis(
    Input_Features="HUC12_CurrentAQ_SoybeansFreq",
    Output_Features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\CurrentAQ_SoybeansFreq_OptHSA",
    Analysis_Field="Sum_Dens",
    Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_FISHNET_POLYGONS",
    Bounding_Polygons_Defining_Where_Incidents_Are_Possible=None,
    Polygons_For_Aggregating_Incidents_Into_Counts=None,
    Density_Surface=None,
    Cell_Size=None,
    Distance_Band=None
)

print('Current soybeans HSA complete!')


#



#ssp245 Distribution
arcpy.ia.ZonalStatisticsAsTable(
    in_zone_data="AnaxQue_WBDHU12_Merge",
    zone_field="huc12",
    in_value_raster=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\RastCalc_ssp245AnaxQue_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ZonalStat_ssp245AQ_SoybeansFreq",
    ignore_nodata="DATA",
    statistics_type="SUM",
    process_as_multidimensional="CURRENT_SLICE",
    percentile_values=[90],
    percentile_interpolation_type="AUTO_DETECT",
    circular_calculation="ARITHMETIC",
    circular_wrap_value=360
)

#Export zonal stats output to hard drive for records
arcpy.conversion.ExportTable(
    in_table="ZonalStat_ssp245AQ_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\zonalstat_ssp245aq_soybeansfreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='HUC12 "HUC12" true true false 12 Text 0 0,First,#,zonalstat_ssp245aq_soybeansfreq,HUC12,0,12;ZONE-CODE "ZONE-CODE" true true false 4 Long 0 0,First,#,zonalstat_ssp245aq_soybeansfreq,ZONE-CODE,-1,-1;COUNT "COUNT" true true false 8 Double 0 0,First,#,zonalstat_ssp245aq_soybeansfreq,COUNT,-1,-1;AREA "AREA" true true false 8 Double 0 0,First,#,zonalstat_ssp245aq_soybeansfreq,AREA,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,zonalstat_ssp245aq_soybeansfreq,SUM,-1,-1',
    sort_field=None
)

#Join Zonal Stats output to clipped HUC12 layer
arcpy.management.AddJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    in_field="huc12",
    join_table="ZonalStat_ssp245AQ_SoybeansFreq",
    join_field="HUC12",
    join_type="KEEP_ALL",
    index_join_fields="NO_INDEX_JOIN_FIELDS"
)

#Export clipped HUC12 layer with joined zonal stats
arcpy.conversion.ExportFeatures(
    in_features="AnaxQue_WBDHU12_Merge",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\HUC12_ssp245AQ_SoybeansFreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='areaacres "areaacres" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areaacres,-1,-1;areasqkm "areasqkm" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areasqkm,-1,-1;states "states" true true false 50 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.states,0,50;huc12 "huc12" true true false 12 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.huc12,0,12;name "name" true true false 120 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.name,0,120;hutype "hutype" true true false 1 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.hutype,0,1;humod "humod" true true false 30 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.humod,0,30;tohuc "tohuc" true true false 16 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.tohuc,0,16;COUNT "COUNT" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_ssp245AQ_SoybeansFreq.COUNT,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_ssp245AQ_SoybeansFreq.SUM,-1,-1',
    sort_field=None
)

#Select the features with NULL values for the COUNT fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp245AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="COUNT IS NULL",
    invert_where_clause=None
)

#Calculate COUNT field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_ssp245AQ_SoybeansFreq",
    field="COUNT",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp245AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NULL",
    invert_where_clause=None
)

#Calculate SUM field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_ssp245AQ_SoybeansFreq",
    field="SUM",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)


#Select the features with No NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp245AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NOT NULL",
    invert_where_clause=None
)

#Calculate Sum_Dens field - SUM/HUC12 Unit Area
arcpy.management.CalculateField(
    in_table="HUC12_ssp245AQ_SoybeansFreq",
    field="Sum_Dens",
    expression="!SUM! / !areasqkm!",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Remove joined Zonal Stats output from clipped HUC12 layer
arcpy.management.RemoveJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    join_name="ZonalStat_ssp245AQ_SoybeansFreq"
)

#Optimized Hot Spot Analysis - ssp245
arcpy.stats.OptimizedHotSpotAnalysis(
    Input_Features="HUC12_ssp245AQ_SoybeansFreq",
    Output_Features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ssp245AQ_SoybeansFreq_OptHSA",
    Analysis_Field="Sum_Dens",
    Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_FISHNET_POLYGONS",
    Bounding_Polygons_Defining_Where_Incidents_Are_Possible=None,
    Polygons_For_Aggregating_Incidents_Into_Counts=None,
    Density_Surface=None,
    Cell_Size=None,
    Distance_Band=None
)

print('HSA complete!')


#



#ssp370 Distribution
arcpy.ia.ZonalStatisticsAsTable(
    in_zone_data="AnaxQue_WBDHU12_Merge",
    zone_field="huc12",
    in_value_raster=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\RastCalc_ssp370AnaxQue_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ZonalStat_ssp370AQ_SoybeansFreq",
    ignore_nodata="DATA",
    statistics_type="SUM",
    process_as_multidimensional="CURRENT_SLICE",
    percentile_values=[90],
    percentile_interpolation_type="AUTO_DETECT",
    circular_calculation="ARITHMETIC",
    circular_wrap_value=360
)

#Export zonal stats output to hard drive for records
arcpy.conversion.ExportTable(
    in_table="ZonalStat_ssp370AQ_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\zonalstat_ssp370aq_soybeansfreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='HUC12 "HUC12" true true false 12 Text 0 0,First,#,zonalstat_ssp370aq_soybeansfreq,HUC12,0,12;ZONE-CODE "ZONE-CODE" true true false 4 Long 0 0,First,#,zonalstat_ssp370aq_soybeansfreq,ZONE-CODE,-1,-1;COUNT "COUNT" true true false 8 Double 0 0,First,#,zonalstat_ssp370aq_soybeansfreq,COUNT,-1,-1;AREA "AREA" true true false 8 Double 0 0,First,#,zonalstat_ssp370aq_soybeansfreq,AREA,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,zonalstat_ssp370aq_soybeansfreq,SUM,-1,-1',
    sort_field=None
)

#Join Zonal Stats output to clipped HUC12 layer
arcpy.management.AddJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    in_field="huc12",
    join_table="ZonalStat_ssp370AQ_SoybeansFreq",
    join_field="HUC12",
    join_type="KEEP_ALL",
    index_join_fields="NO_INDEX_JOIN_FIELDS"
)

#Export clipped HUC12 layer with joined zonal stats
arcpy.conversion.ExportFeatures(
    in_features="AnaxQue_WBDHU12_Merge",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\HUC12_ssp370AQ_SoybeansFreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='areaacres "areaacres" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areaacres,-1,-1;areasqkm "areasqkm" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areasqkm,-1,-1;states "states" true true false 50 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.states,0,50;huc12 "huc12" true true false 12 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.huc12,0,12;name "name" true true false 120 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.name,0,120;hutype "hutype" true true false 1 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.hutype,0,1;humod "humod" true true false 30 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.humod,0,30;tohuc "tohuc" true true false 16 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.tohuc,0,16;COUNT "COUNT" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_ssp370AQ_SoybeansFreq.COUNT,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_ssp370AQ_SoybeansFreq.SUM,-1,-1',
    sort_field=None
)

#Select the features with NULL values for the COUNT fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp370AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="COUNT IS NULL",
    invert_where_clause=None
)

#Calculate COUNT field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_ssp370AQ_SoybeansFreq",
    field="COUNT",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp370AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NULL",
    invert_where_clause=None
)

#Calculate SUM field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_ssp370AQ_SoybeansFreq",
    field="SUM",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with No NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp370AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NOT NULL",
    invert_where_clause=None
)

#Calculate Sum_Dens field - SUM/HUC12 Unit Area
arcpy.management.CalculateField(
    in_table="HUC12_ssp370AQ_SoybeansFreq",
    field="Sum_Dens",
    expression="!SUM! / !areasqkm!",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Remove joined Zonal Stats output from clipped HUC12 layer
arcpy.management.RemoveJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    join_name="ZonalStat_ssp370AQ_SoybeansFreq"
)

#Optimized Hot Spot Analysis - ssp370
arcpy.stats.OptimizedHotSpotAnalysis(
    Input_Features="HUC12_ssp370AQ_SoybeansFreq",
    Output_Features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ssp370AQ_SoybeansFreq_OptHSA",
    Analysis_Field="Sum_Dens",
    Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_FISHNET_POLYGONS",
    Bounding_Polygons_Defining_Where_Incidents_Are_Possible=None,
    Polygons_For_Aggregating_Incidents_Into_Counts=None,
    Density_Surface=None,
    Cell_Size=None,
    Distance_Band=None
)

print('HSA complete!')

#



#ssp585 Distribution
arcpy.ia.ZonalStatisticsAsTable(
    in_zone_data="AnaxQue_WBDHU12_Merge",
    zone_field="huc12",
    in_value_raster=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\RastCalc_ssp585AnaxQue_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ZonalStat_ssp585AQ_SoybeansFreq",
    ignore_nodata="DATA",
    statistics_type="SUM",
    process_as_multidimensional="CURRENT_SLICE",
    percentile_values=[90],
    percentile_interpolation_type="AUTO_DETECT",
    circular_calculation="ARITHMETIC",
    circular_wrap_value=360
)

#Export zonal stats output to hard drive for records
arcpy.conversion.ExportTable(
    in_table="ZonalStat_ssp585AQ_SoybeansFreq",
    out_table=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\zonalstat_ssp585aq_soybeansfreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='HUC12 "HUC12" true true false 12 Text 0 0,First,#,zonalstat_ssp585aq_soybeansfreq,HUC12,0,12;ZONE-CODE "ZONE-CODE" true true false 4 Long 0 0,First,#,zonalstat_ssp585aq_soybeansfreq,ZONE-CODE,-1,-1;COUNT "COUNT" true true false 8 Double 0 0,First,#,zonalstat_ssp585aq_soybeansfreq,COUNT,-1,-1;AREA "AREA" true true false 8 Double 0 0,First,#,zonalstat_ssp585aq_soybeansfreq,AREA,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,zonalstat_ssp585aq_soybeansfreq,SUM,-1,-1',
    sort_field=None
)

#Join Zonal Stats output to clipped HUC12 layer
arcpy.management.AddJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    in_field="huc12",
    join_table="ZonalStat_ssp585AQ_SoybeansFreq",
    join_field="HUC12",
    join_type="KEEP_ALL",
    index_join_fields="NO_INDEX_JOIN_FIELDS"
)

#Export clipped HUC12 layer with joined zonal stats
arcpy.conversion.ExportFeatures(
    in_features="AnaxQue_WBDHU12_Merge",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\HUC12_ssp585AQ_SoybeansFreq",
    where_clause="",
    use_field_alias_as_name="NOT_USE_ALIAS",
    field_mapping='areaacres "areaacres" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areaacres,-1,-1;areasqkm "areasqkm" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.areasqkm,-1,-1;states "states" true true false 50 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.states,0,50;huc12 "huc12" true true false 12 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.huc12,0,12;name "name" true true false 120 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.name,0,120;hutype "hutype" true true false 1 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.hutype,0,1;humod "humod" true true false 30 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.humod,0,30;tohuc "tohuc" true true false 16 Text 0 0,First,#,AnaxQue_WBDHU12_Merge,AnaxQue_WBDHU12_Merge.tohuc,0,16;COUNT "COUNT" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_ssp585AQ_SoybeansFreq.COUNT,-1,-1;SUM "SUM" true true false 8 Double 0 0,First,#,AnaxQue_WBDHU12_Merge,ZonalStat_ssp585AQ_SoybeansFreq.SUM,-1,-1',
    sort_field=None
)

#Select the features with NULL values for the COUNT fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp585AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="COUNT IS NULL",
    invert_where_clause=None
)

#Calculate COUNT field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_ssp585AQ_SoybeansFreq",
    field="COUNT",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp585AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NULL",
    invert_where_clause=None
)

#Calculate SUM field - replace NULL values with 0
arcpy.management.CalculateField(
    in_table="HUC12_ssp585AQ_SoybeansFreq",
    field="SUM",
    expression="0",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Select the features with No NULL values for the SUM fields
arcpy.management.SelectLayerByAttribute(
    in_layer_or_view="HUC12_ssp585AQ_SoybeansFreq",
    selection_type="NEW_SELECTION",
    where_clause="SUM IS NOT NULL",
    invert_where_clause=None
)

#Calculate Sum_Dens field - SUM/HUC12 Unit Area
arcpy.management.CalculateField(
    in_table="HUC12_ssp585AQ_SoybeansFreq",
    field="Sum_Dens",
    expression="!SUM! / !areasqkm!",
    expression_type="PYTHON3",
    code_block="",
    field_type="DOUBLE",
    enforce_domains="NO_ENFORCE_DOMAINS"
)

#Remove joined Zonal Stats output from clipped HUC12 layer
arcpy.management.RemoveJoin(
    in_layer_or_view="AnaxQue_WBDHU12_Merge",
    join_name="ZonalStat_ssp585AQ_SoybeansFreq"
)


#Optimized Hot Spot Analysis - ssp585
arcpy.stats.OptimizedHotSpotAnalysis(
    Input_Features="HUC12_ssp585AQ_SoybeansFreq",
    Output_Features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\ssp585AQ_SoybeansFreq_OptHSA",
    Analysis_Field="Sum_Dens",
    Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_FISHNET_POLYGONS",
    Bounding_Polygons_Defining_Where_Incidents_Are_Possible=None,
    Polygons_For_Aggregating_Incidents_Into_Counts=None,
    Density_Surface=None,
    Cell_Size=None,
    Distance_Band=None
)

print('HSA completed!')

#########################################################



#Hot Spot Analysis Comparison - Soybeans

#current versus ssp245
arcpy.stats.HotSpotAnalysisComparison(
    in_hot_spot_1="CurrentAQ_SoybeansFreq_OptHSA",
    in_hot_spot_2="ssp245AQ_SoybeansFreq_OptHSA",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\Currentssp245AnaxQue_SoybeansFreq_HSAComp",
    num_neighbors=8,
    num_perms=499,
    weighting_method="FUZZY",
    similarity_weights="-3 -3 1;3 3 1;-3 -2 0.71;3 2 0.71;-3 -1 0.55;3 1 0.55;-2 -2 1;2 2 1;-2 -1 0.78;2 1 0.78;-1 -1 1;1 1 1;0 0 1",
    in_weights_table=None,
    exclude_nonsig_features="NO_EXCLUDE"
)

#current versus ssp370
arcpy.stats.HotSpotAnalysisComparison(
    in_hot_spot_1="CurrentAQ_SoybeansFreq_OptHSA",
    in_hot_spot_2="ssp370AQ_SoybeansFreq_OptHSA",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\Currentssp370AnaxQue_SoybeansFreq_HSAComp",
    num_neighbors=8,
    num_perms=499,
    weighting_method="FUZZY",
    similarity_weights="-3 -3 1;3 3 1;-3 -2 0.71;3 2 0.71;-3 -1 0.55;3 1 0.55;-2 -2 1;2 2 1;-2 -1 0.78;2 1 0.78;-1 -1 1;1 1 1;0 0 1",
    in_weights_table=None,
    exclude_nonsig_features="NO_EXCLUDE"
)

#current versus ssp585
arcpy.stats.HotSpotAnalysisComparison(
    in_hot_spot_1="CurrentAQ_SoybeansFreq_OptHSA",
    in_hot_spot_2="ssp585AQ_SoybeansFreq_OptHSA",
    out_features=r"E:\SpatialAnalysis\ArcGIS_PesticideAnalysis\CropHotSpotAnalysis\AnaxQue_CropHotSpotAnalysis.gdb\Currentssp585AnaxQue_SoybeansFreq_HSAComp",
    num_neighbors=8,
    num_perms=499,
    weighting_method="FUZZY",
    similarity_weights="-3 -3 1;3 3 1;-3 -2 0.71;3 2 0.71;-3 -1 0.55;3 1 0.55;-2 -2 1;2 2 1;-2 -1 0.78;2 1 0.78;-1 -1 1;1 1 1;0 0 1",
    in_weights_table=None,
    exclude_nonsig_features="NO_EXCLUDE"
)

print('HSA comparisons complete!')
