|
| 1 | +package hazardproviders |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/USACE/go-consequences/geography" |
| 10 | + "github.com/USACE/go-consequences/hazards" |
| 11 | +) |
| 12 | + |
| 13 | +type jsonArrivalDepthDurationMultiHazardProvider struct { |
| 14 | + arrivals []time.Time |
| 15 | + depthCRs []cogReader |
| 16 | + durations []float64 |
| 17 | + process HazardFunction |
| 18 | +} |
| 19 | + |
| 20 | +type ADDProperties struct { |
| 21 | + Year float64 `json:"year"` |
| 22 | + Month float64 `json:"month"` |
| 23 | + Day float64 `json:"day"` |
| 24 | + Depth float64 `json:"depth"` |
| 25 | + Depthgrid string `json:"depthgrid"` |
| 26 | + Duration float64 `json:"duration"` |
| 27 | + Xmin float64 `json:"xmin"` |
| 28 | + Xmax float64 `json:"xmax"` |
| 29 | + Ymin float64 `json:"ymin"` |
| 30 | + Ymax float64 `json:"ymax"` |
| 31 | +} |
| 32 | + |
| 33 | +type ADDEvents struct { |
| 34 | + Events []ADDProperties `json:"events"` |
| 35 | +} |
| 36 | + |
| 37 | +func InitADDMHP(fp string) (jsonArrivalDepthDurationMultiHazardProvider, error) { |
| 38 | + fmt.Println("Connecting to: " + fp) |
| 39 | + c, err := os.ReadFile(fp) |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + |
| 44 | + var events ADDEvents |
| 45 | + json.Unmarshal(c, &events) |
| 46 | + |
| 47 | + arrivalTimes := make([]time.Time, len(events.Events)) |
| 48 | + durations := make([]float64, len(events.Events)) |
| 49 | + depthCRs := make([]cogReader, len(events.Events)) |
| 50 | + |
| 51 | + for i, e := range events.Events { |
| 52 | + at := time.Date(int(e.Year), time.Month(e.Month), int(e.Day), 0, 0, 0, 0, time.UTC) |
| 53 | + cr, err := initCR(e.Depthgrid) |
| 54 | + if err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | + |
| 58 | + arrivalTimes[i] = at |
| 59 | + durations[i] = e.Duration |
| 60 | + depthCRs[i] = cr |
| 61 | + } |
| 62 | + |
| 63 | + return jsonArrivalDepthDurationMultiHazardProvider{ |
| 64 | + arrivals: arrivalTimes, |
| 65 | + depthCRs: depthCRs, |
| 66 | + durations: durations, |
| 67 | + process: ArrivalDepthAndDurationHazardFunction(), |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (j jsonArrivalDepthDurationMultiHazardProvider) Close() { |
| 72 | + for _, c := range j.depthCRs { |
| 73 | + c.Close() |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (j jsonArrivalDepthDurationMultiHazardProvider) Hazard(l geography.Location) (hazards.HazardEvent, error) { |
| 78 | + var hm hazards.ArrivalDepthandDurationEventMulti |
| 79 | + for i, cr := range j.depthCRs { |
| 80 | + d, err := cr.ProvideValue(l) |
| 81 | + if err != nil { |
| 82 | + return hm, err |
| 83 | + } |
| 84 | + hd := hazards.HazardData{ |
| 85 | + Depth: d, |
| 86 | + Velocity: 0, |
| 87 | + ArrivalTime: j.arrivals[i], |
| 88 | + Erosion: 0, |
| 89 | + Duration: j.durations[i], |
| 90 | + WaveHeight: 0, |
| 91 | + Salinity: false, |
| 92 | + Qualitative: "", |
| 93 | + } |
| 94 | + var h hazards.HazardEvent |
| 95 | + h, err = j.process(hd, h) |
| 96 | + if err != nil { |
| 97 | + panic(err) |
| 98 | + } |
| 99 | + hm.Append(h.(hazards.ArrivalDepthandDurationEvent)) |
| 100 | + } |
| 101 | + return &hm, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (j jsonArrivalDepthDurationMultiHazardProvider) HazardBoundary() (geography.BBox, error) { |
| 105 | + // We'll probably want to do something different here. |
| 106 | + // Probably allow user to define study bbox with a |
| 107 | + // shapefile/geojson/directly entering bbox coords |
| 108 | + return j.depthCRs[0].GetBoundingBox() |
| 109 | +} |
0 commit comments