OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
ClimateFile.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
38
39#ifndef OPENSWMM_CLIMATE_FILE_HPP
40#define OPENSWMM_CLIMATE_FILE_HPP
41
42#include <string>
43#include <cstdio>
44#include <cmath>
45
46namespace openswmm {
47namespace climate {
48
49// ============================================================================
50// Enums
51// ============================================================================
52
60
62enum ClimateVar { TMIN = 0, TMAX = 1, EVAP = 2, WIND = 3 };
63
65enum WindFieldType { WDMV = 0, AWND = 1 };
66
68enum TempUnits { DEG_C10 = 0, DEG_C = 1, DEG_F = 2 };
69
70// ============================================================================
71// Daily climate record (output of a lookup)
72// ============================================================================
73
75 double tmin = NAN;
76 double tmax = NAN;
77 double evap = NAN;
78 double wind = NAN;
79};
80
81// ============================================================================
82// ClimateFileReader
83// ============================================================================
84
91public:
95 ClimateFileReader() = default;
99
106
120 bool open(const std::string& path, double start_oa_date, int unit_system, int temp_units = -1);
121
125 void close();
126
139 bool getRecord(double oa_date, DailyClimateRecord& rec);
140
145 void setDayOffset(double days) { day_offset_ = days; }
146
150 ClimateFileFormat format() const { return format_; }
151
155 bool isOpen() const { return file_ != nullptr; }
156
157private:
158 static constexpr double MISSING = -1.0e10;
159 static constexpr double MM_PER_INCH = 25.4;
160 static constexpr int MAX_LINE = 512;
161
162 // File state
163 std::FILE* file_ = nullptr;
165 int unit_system_ = 0; // 0=US, 1=SI
166
167 // Current month buffer: [variable][day], day 0 unused, 1-31 valid
168 double file_data_[4][32] = {};
169 int buf_year_ = -1;
170 int buf_month_ = -1;
171 double day_offset_ = 0.0; // see setDayOffset()
172
173 // GHCND header-derived field positions
174 int field_pos_[4] = {-1, -1, -1, -1}; // column start for TMIN,TMAX,EVAP,WIND
175 int date_field_pos_ = 0;
176 int wind_type_ = WDMV;
177 int temp_units_ = DEG_C10;
178
179 // Saved line from month-boundary read-ahead
180 char saved_line_[MAX_LINE + 1] = {};
181 bool has_saved_line_ = false;
182
183 // Format detection
189 ClimateFileFormat detectFormat(const char* first_line);
196 bool isGhcndFormat(const char* line);
197
198 // Month buffering
207 bool bufferMonth(int year, int month);
210 void clearBuffer();
211
212 // Format-specific line parsers (populate file_data_ for one line)
220 void parseUserLine(const char* line);
228 void parseGhcndLine(const char* line);
236 void parseTD3200Line(const char* line);
243 void parseDLY0204Line(const char* line);
244
245 // Helpers
252 static void oaDateToYMD(double oa_date, int& y, int& m, int& d);
257 double convertTemp(double raw) const;
262 double convertEvap(double raw) const;
268 double convertWind(double raw, int wind_type) const;
269};
270
271} // namespace climate
272} // namespace openswmm
273
274#endif // OPENSWMM_CLIMATE_FILE_HPP
ClimateFileReader()=default
Construct a closed reader with no cached month and zero day offset. Initial temperature encoding is t...
ClimateFileFormat format() const
Read the recognized first-line format without further IO.
Definition ClimateFile.hpp:150
~ClimateFileReader()
Close the owned stream, ignoring fclose status. Caller first ends all concurrent/borrowed use; no fin...
Definition ClimateFile.cpp:41
ClimateFileReader & operator=(const ClimateFileReader &)=delete
Disallow copy assignment of owned stream/cached cursor state.
void close()
Close the stream and invalidate cached month and saved read-ahead line. Ignores fclose status....
Definition ClimateFile.cpp:45
ClimateFileReader(const ClimateFileReader &)=delete
Disallow copying the uniquely owned file/cursor state. No callable copy operation is provided.
bool open(const std::string &path, double start_oa_date, int unit_system, int temp_units=-1)
Close any previous stream, open a file and detect its first-line format.
Definition ClimateFile.cpp:55
bool isOpen() const
Test ownership of a currently open stream.
Definition ClimateFile.hpp:155
void setDayOffset(double days)
Store the date shift used by subsequent lookups.
Definition ClimateFile.hpp:145
bool getRecord(double oa_date, DailyClimateRecord &rec)
Fetch the day at a shifted serial date from the current/next month buffer.
Definition ClimateFile.cpp:539
@ DEG_C10
Temperature in tenths of a degree Celsius.
Definition climate.c:119
@ WDMV
Wind speed.
Definition climate.c:108
#define MISSING
Missing value code.
Definition consts.h:142
Definition Rk2RuntimeSources.hpp:19
ClimateFileFormat
Definition ClimateFile.hpp:53
@ UNKNOWN
Definition ClimateFile.hpp:54
@ TD3200
NCDC TD3200 (NWS cooperative observer)
Definition ClimateFile.hpp:57
@ GHCND
NCDC Global Historical Climatology Network Daily.
Definition ClimateFile.hpp:56
@ DLY0204
Canadian DLY02/DLY04.
Definition ClimateFile.hpp:58
@ USER_PREPARED
StationID YYYY MM DD TMAX TMIN EVAP WIND.
Definition ClimateFile.hpp:55
TempUnits
Temperature unit encoding in GHCND.
Definition ClimateFile.hpp:68
@ DEG_C
Definition ClimateFile.hpp:68
@ DEG_F
Definition ClimateFile.hpp:68
@ DEG_C10
Definition ClimateFile.hpp:68
ClimateVar
Climate variable indices (matching legacy ClimateVarType)
Definition ClimateFile.hpp:62
@ TMAX
Definition ClimateFile.hpp:62
@ WIND
Definition ClimateFile.hpp:62
@ EVAP
Definition ClimateFile.hpp:62
@ TMIN
Definition ClimateFile.hpp:62
WindFieldType
Wind field type in GHCND.
Definition ClimateFile.hpp:65
@ AWND
Definition ClimateFile.hpp:65
@ WDMV
Definition ClimateFile.hpp:65
constexpr double MM_PER_INCH
Definition Climate.hpp:50
Definition ExchangeLaw.hpp:60
double * y
Definition odesolve.c:37
Definition ClimateFile.hpp:74
double evap
Pan evaporation (in/day US, mm/day SI; NAN if missing)
Definition ClimateFile.hpp:77
double tmin
Minimum temperature (deg F)
Definition ClimateFile.hpp:75
double wind
Wind speed (mph; NAN if missing)
Definition ClimateFile.hpp:78
double tmax
Maximum temperature (deg F)
Definition ClimateFile.hpp:76