input(relation: 'data/Buildings_SE_Upolu.shp', name: 'exposure') as exposures_input
#input(relation: 'data/Buildings_SE_Upolu.shp', name: 'exposure', limit: 50) as exposures_input
 -> select({exposure, $choropleth }) # suppress surplus parameter warning
 -> select({exposure}) as exposures
 -> join(on: true) as exposures_join_hazards
 -> select({*, sample_closest(geometry: exposure, coverage: hazard) as hazard}) as "sample_hazard_layer"
 -> join(on: true) as exposures_join_areas
 -> select({*, sample_closest(geometry: exposure, coverage: area, buffer-distance: 1000) as area})
 -> select({*, probability: map(hazard, hv -> Samoa_Building_Fragility(exposure, hv))})
 # randomly assign a damage state based on the probabilities, i.e. likelihood of being in
 # one damage state and not others
 -> select({*, if_then_else(is_null(hazard), '0: Not damaged',
                            random_choice(['0: Not damaged', '1: Light', '2: Minor', '3: Moderate', '4: Severe', '5: Collapse'],
                              weights: [1.0 - probability.DS_1,
                                        probability.DS_1 - probability.DS_2,
                                        probability.DS_2 - probability.DS_3,
                                        probability.DS_3 - probability.DS_4,
                                        probability.DS_4 - probability.DS_5,
                                        probability.DS_5])) as DamageState})
 -> select({*}) as event_impact_table

input(value: bookmark('data/MaxEnv_All_Scenarios_50m.tif'), name: 'hazard') as hazards_input
 -> select({hazard as hazard}) as hazards
 -> exposures_join_hazards.rhs

input(relation: 'data/Samoa_constituencies.shp', name: 'area') as areas_input
 -> group({to_coverage(area) as area}) as areas
 -> exposures_join_areas.rhs

event_impact_table
 -> select({*}) as "report_event-impact"
 -> group(by: {area as Region},
          select: {
                Region,
                count(hazard) as Number_Exposed,
                count(exposure) as Total_buildings,
                count(DamageState != '0: Not damaged') as Damaged,
                mean(hazard) as Mean_Depth,
                bucket(pick: b -> b = DamageState,
                       select: {count(*)},
                       buckets: {None: '0: Not damaged', Light: '1: Light', Minor: '2: Minor', Moderate: '3: Moderate', Severe: '4: Severe', Collapse: '5: Collapse'}
                      ) as Damage
          }) as regional_impact
 -> select({ *, Region.Region as Region })
 -> sort([Region], direction: ['ASC'])
 -> save(name: 'summary', format: 'csv') as save_summary

 event_impact_table
 -> save(name: 'event-impact') as save_event_impact

regional_impact
  # make things slightly easier to access from the pandas dataframe
 -> select({ *, Damage.*, Number_Exposed as Exposed, Damage.Collapse.count as Collapsed })  as summary
 -> save(name: 'regional-impact', format: 'geojson') as save_regional_impact

