Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Updated type names and improved documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaximilian committed Dec 25, 2019
1 parent 8b186b7 commit 1cb89b7
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Sources/MetobsKit/Models/Forecast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

import Foundation

/// A `Forecast` is a collection of `Value`s for a set of `Parameter`s
public struct Forecast: Codable {
/// A timestamp for when the `Forecast` is valid
public let validTime: String
public let parameters: [Parameter]

/// An array of `Value` instances
public let parameters: [Value]
}
5 changes: 5 additions & 0 deletions Sources/MetobsKit/Models/Geometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@

import Foundation

/// A typealias for an array of `Double`
public typealias GeometryCoordinate = [Double]

/// A `Geometry` representation
public struct Geometry: Codable {
/// The type of `Geometry`
public let type: String

/// A number of `GeometryCoordinates` that represent the `Geometry` for the given type
public let coordinates: [GeometryCoordinate]
}
76 changes: 76 additions & 0 deletions Sources/MetobsKit/Models/Level.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Level.swift
//
// Copyright (c) 2019 Maximilian Wendel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import Foundation

/// A `Level` representing the measurement's base-level
public enum Level: String {
/// Above sea level
case seaLevel = "hmsl"

/// Above ground level
case groundLevel = "hl"

/// Above unknown level
case unknownLevel
}

/// An extension adding `Codable` protocol conformance to `Level`
extension Level: Codable {
// MARK: Initializer

/// Initialize a new `Level` using `Decoder`
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

/// Attempt to decode received value
guard let value = try? container.decode(String.self) else {
self = .unknownLevel
return
}

/// Map raw value to enum representation
switch value.lowercased() {
case "hmsl":
self = .seaLevel
case "hl":
self = .groundLevel
default:
self = .unknownLevel
}
}

// MARK: Public methods

/// Encode `Level` using `Encoder`
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

do {
try container.encode(self.rawValue)
} catch {
try container.encode("unknown")
}
}
}
8 changes: 8 additions & 0 deletions Sources/MetobsKit/Models/Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@

import Foundation

/// An `Observation` is a collection of `Forecast` instances
public struct Observation: Codable {
/// A timestamp for when the `Forecast` was approved
public let approvedTime: String

/// A timestamp for when the `Forecast` was created or updated
public let referenceTime: String

/// A `Geometry` represenation for where the `Forecast` is valid
public let geometry: Geometry

/// An array of `Forecast` instances that reflect the overall `Forecast` over time
public let timeSeries: [Forecast]
}
141 changes: 135 additions & 6 deletions Sources/MetobsKit/Models/Parameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,139 @@

import Foundation

public struct Parameter: Codable {
public let name: String
public let levelType: String
public let level: Int
public let unit: String
public let values: [Double]
/// A `Parameter` type
/// - Note: See https://opendata.smhi.se/apidocs/metfcst/parameters.html
public enum Parameter: String {
/// Air pressure
case airPressure = "msl"

/// Air temperature
case airTemperature

/// Horizontal visibility
case visibility

/// Wind direction
case windDirection

/// Wind speed
case windSpeed

/// Relative humidity
case relativeHumidity

/// Thunder probability
case thunderProbability

/// Mean value of total cloud cover
case cloudCoverTotal

/// Mean value of low level cloud cover
case cloudCoverLow

/// Mean value of medium level cloud cover
case cloudCoverMedium

/// Mean value of high level cloud cover
case cloudCoverHigh

/// Wind gust speed
case windGustSpeed

/// Minimum precipitation intensity
case precipitationIntensityMin

/// Maximum precipitation intensity
case precipitationIntensityMax

/// Percent of precipitation in frozen form
case frozenPrecipitation

/// Precipitation category
case precipitationCategory

/// Mean precipitation intensity
case precipitationIntensityMean

/// Median precipitation intensity
case precipitationIntensityMedian

/// Weather symbol
case weatherSymbol

/// Unknown parameter
case unknown
}

/// An extension adding `Codable` protocol conformance to `Parameter`
extension Parameter: Codable {
// MARK: Initializer

/// Initialize a new `Parameter` using `Decoder`
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

/// Attempt to decode received value
guard let value = try? container.decode(String.self) else {
self = .unknown
return
}

/// Map raw value to enum representation
switch value.lowercased() {
case "msl":
self = .airPressure
case "t":
self = .airTemperature
case "vis":
self = .visibility
case "wd":
self = .windDirection
case "ws":
self = .windSpeed
case "r":
self = .relativeHumidity
case "tstm":
self = .thunderProbability
case "tcc_mean":
self = .cloudCoverTotal
case "lcc_mean":
self = .cloudCoverLow
case "mcc_mean":
self = .cloudCoverMedium
case "hcc_mean":
self = .cloudCoverHigh
case "gust":
self = .windGustSpeed
case "pmin":
self = .precipitationIntensityMin
case "pmax":
self = .precipitationIntensityMax
case "spp":
self = .frozenPrecipitation
case "pcat":
self = .precipitationCategory
case "pmean":
self = .precipitationIntensityMean
case "pmedian":
self = .precipitationIntensityMedian
case "wsymb2":
self = .weatherSymbol
default:
self = .unknown
}
}

// MARK: Public methods

/// Encode `Parameter` using `Encoder`
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

do {
try container.encode(self.rawValue)
} catch {
try container.encode("unknown")
}
}
}
43 changes: 43 additions & 0 deletions Sources/MetobsKit/Models/Value.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Value.swift
//
// Copyright (c) 2019 Maximilian Wendel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import Foundation

/// A `Value` value representation for a `Forecast` parameter
public struct Value: Codable {
/// A `Parameter` type representing the underlying value's type
public let name: Parameter

/// A `Level` representing the measurement's reference distance
public let levelType: Level

/// The distance above the `Level`
public let level: Int

/// The unit the value can be measured in
public let unit: String

/// An array of raw parameter values
public let values: [Double]
}

0 comments on commit 1cb89b7

Please sign in to comment.