Created
January 7, 2020 16:45
-
-
Save segabor/41156749bbece0d01f88aa9747e4deb1 to your computer and use it in GitHub Desktop.
Transform time series data saved into csv file to influxdb import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
datumsec | T | Td | Tmin12 | Tmax12 | p | pmsl | pch | ptend | rr6 | rr12 | rr24 | VV | ww | W1 | W2 | sun | sunday | N | dd | ff | ffx | ffmin6 | ffmean6 | ffmax6 | ff6 | ffx6 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1561939200 | 190 | 162 | 10029 | 10194 | 1 | 2 | 0 | 12 | 10 | ||||||||||||||||||
1561942800 | 182 | 158 | 10026 | 10191 | -1 | 7 | 11 | 20 | |||||||||||||||||||
1561946400 | 175 | 155 | 10023 | 10188 | -6 | 7 | 11 | 20 | |||||||||||||||||||
1561950000 | 172 | 154 | 10020 | 10186 | -9 | 7 | 11 | 20 | |||||||||||||||||||
1561953600 | 181 | 154 | 10017 | 10182 | -9 | 7 | 12 | 20 | |||||||||||||||||||
1561957200 | 203 | 159 | 10015 | 10179 | -8 | 7 | 12 | 20 | |||||||||||||||||||
1561960800 | 230 | 163 | 172 | 10014 | 10176 | -6 | 7 | 0 | 51480 | 16 | 20 | ||||||||||||||||
1561964400 | 256 | 170 | 10016 | 10177 | -1 | 7 | 16 | 10 | |||||||||||||||||||
1561968000 | 268 | 170 | 10016 | 10176 | 1 | 2 | 20 | 20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
guard let handle = FileHandle(forReadingAtPath: "./lorinc_ogimet.csv") else { | |
fatalError("Could not open file") | |
} | |
defer { | |
handle.closeFile() | |
} | |
let content = handle.readDataToEndOfFile() | |
guard let csv = String(data: content, encoding: .ascii) else { | |
fatalError("") | |
} | |
let table = csv.components(separatedBy: "\r\n") | |
let headers = table[0].components(separatedBy: ",").dropFirst() | |
let rows = table.dropFirst() | |
rows.filter{ !$0.isEmpty }.forEach { row in | |
let cells = row.components(separatedBy: ",") | |
let timestamp = cells[0] | |
let fields = zip(headers, cells.dropFirst()).compactMap { (hdr: String, val: String) in | |
return val.isEmpty ? nil : "\(hdr)=\(val)" | |
}.joined(separator: ",") | |
print("weather,station=lorinc \(fields) \(timestamp)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment