Spaces:
Sleeping
Sleeping
File size: 5,426 Bytes
6ee917b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | import Foundation
protocol DailyVariableCalculatable {
associatedtype Variable
var aggregation: DailyAggregation<Variable> { get }
}
enum DailyAggregation<WeatherVariable> {
case none
case max(WeatherVariable)
case min(WeatherVariable)
case mean(WeatherVariable)
case sum(WeatherVariable)
case radiationSum(WeatherVariable)
case precipitationHours(WeatherVariable)
case dominantDirection(velocity: WeatherVariable, direction: WeatherVariable)
case dominantDirectionComponents(u: WeatherVariable, v: WeatherVariable)
/// Return 0, 1 or 2 weahter variables which should be prefetched
var variables: (WeatherVariable?, WeatherVariable?) {
switch self {
case .none:
return (nil, nil)
case .max(let weatherVariable):
return (weatherVariable, nil)
case .min(let weatherVariable):
return (weatherVariable, nil)
case .mean(let weatherVariable):
return (weatherVariable, nil)
case .sum(let weatherVariable):
return (weatherVariable, nil)
case .radiationSum(let weatherVariable):
return (weatherVariable, nil)
case .precipitationHours(let weatherVariable):
return (weatherVariable, nil)
case .dominantDirection(let velocity, let direction):
return (velocity, direction)
case .dominantDirectionComponents(let u, let v):
return (u, v)
}
}
}
/*extension GenericReaderMixable {
func getDaily<V: DailyVariableCalculatable, Units: ApiUnitsSelectable>(variable: V, params: Units, time timeDaily: TimerangeDt) throws -> DataAndUnit? where V.Variable == MixingVar {
let time = timeDaily.with(dtSeconds: 3600)
fatalError()
}
func prefetchDaily<V: DailyVariableCalculatable>(variables: [V], time timeDaily: TimerangeDt) throws where V.Variable == MixingVar {
fatalError()
}
}*/
extension GenericReaderMulti {
func getDaily<V: DailyVariableCalculatable, Units: ApiUnitsSelectable>(variable: V, params: Units, time timeDaily: TimerangeDtAndSettings) throws -> DataAndUnit? where V.Variable == Variable {
let time = timeDaily.with(dtSeconds: 3600)
switch variable.aggregation {
case .none:
return nil
case .max(let variable):
guard let data = try get(variable: variable, time: time)?.convertAndRound(params: params) else {
return nil
}
return DataAndUnit(data.data.max(by: 24), data.unit)
case .min(let variable):
guard let data = try get(variable: variable, time: time)?.convertAndRound(params: params) else {
return nil
}
return DataAndUnit(data.data.min(by: 24), data.unit)
case .mean(let variable):
guard let data = try get(variable: variable, time: time)?.convertAndRound(params: params) else {
return nil
}
return DataAndUnit(data.data.mean(by: 24), data.unit)
case .sum(let variable):
guard let data = try get(variable: variable, time: time)?.convertAndRound(params: params) else {
return nil
}
return DataAndUnit(data.data.sum(by: 24), data.unit)
case .radiationSum(let variable):
guard let data = try get(variable: variable, time: time)?.convertAndRound(params: params) else {
return nil
}
// 3600s only for hourly data of source
return DataAndUnit(data.data.map({$0*0.0036}).sum(by: 24).round(digits: 2), .megajoulePerSquareMetre)
case .precipitationHours(let variable):
guard let data = try get(variable: variable, time: time)?.convertAndRound(params: params) else {
return nil
}
return DataAndUnit(data.data.map({$0 > 0.001 ? 1 : 0}).sum(by: 24), .hours)
case .dominantDirection(velocity: let velocity, direction: let direction):
guard let speed = try get(variable: velocity, time: time)?.data,
let direction = try get(variable: direction, time: time)?.data else {
return nil
}
// vector addition
let u = zip(speed, direction).map(Meteorology.uWind).sum(by: 24)
let v = zip(speed, direction).map(Meteorology.vWind).sum(by: 24)
return DataAndUnit(Meteorology.windirectionFast(u: u, v: v), .degreeDirection)
case .dominantDirectionComponents(u: let u, v: let v):
guard let u = try get(variable: u, time: time)?.data,
let v = try get(variable: v, time: time)?.data else {
return nil
}
return DataAndUnit(Meteorology.windirectionFast(u: u.sum(by: 24), v: v.sum(by: 24)), .degreeDirection)
}
}
func prefetchData<V: DailyVariableCalculatable>(variables: [V], time timeDaily: TimerangeDtAndSettings) throws where V.Variable == Variable {
let time = timeDaily.with(dtSeconds: 3600)
for variable in variables {
if let v0 = variable.aggregation.variables.0 {
try prefetchData(variable: v0, time: time)
}
if let v1 = variable.aggregation.variables.1 {
try prefetchData(variable: v1, time: time)
}
}
}
}
|