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
86public:
87 ClimateFileReader() = default;
89
92
100 bool open(const std::string& path, double start_oa_date, int unit_system,
101 int temp_units = -1);
102
104 void close();
105
111 bool getRecord(double oa_date, DailyClimateRecord& rec);
112
114 ClimateFileFormat format() const { return format_; }
115
117 bool isOpen() const { return file_ != nullptr; }
118
119private:
120 static constexpr double MISSING = -1.0e10;
121 static constexpr double MM_PER_INCH = 25.4;
122 static constexpr int MAX_LINE = 512;
123
124 // File state
125 std::FILE* file_ = nullptr;
127 int unit_system_ = 0; // 0=US, 1=SI
128
129 // Current month buffer: [variable][day], day 0 unused, 1-31 valid
130 double file_data_[4][32] = {};
131 int buf_year_ = -1;
132 int buf_month_ = -1;
133
134 // GHCND header-derived field positions
135 int field_pos_[4] = {-1, -1, -1, -1}; // column start for TMIN,TMAX,EVAP,WIND
136 int date_field_pos_ = 0;
137 int wind_type_ = WDMV;
138 int temp_units_ = DEG_C10;
139
140 // Saved line from month-boundary read-ahead
141 char saved_line_[MAX_LINE + 1] = {};
142 bool has_saved_line_ = false;
143
144 // Format detection
145 ClimateFileFormat detectFormat(const char* first_line);
146 bool isGhcndFormat(const char* line);
147
148 // Month buffering
149 bool bufferMonth(int year, int month);
150 void clearBuffer();
151
152 // Format-specific line parsers (populate file_data_ for one line)
153 void parseUserLine(const char* line);
154 void parseGhcndLine(const char* line);
155 void parseTD3200Line(const char* line);
156 void parseDLY0204Line(const char* line);
157
158 // Helpers
159 static void oaDateToYMD(double oa_date, int& y, int& m, int& d);
160 double convertTemp(double raw) const;
161 double convertEvap(double raw) const;
162 double convertWind(double raw, int wind_type) const;
163};
164
165} // namespace climate
166} // namespace openswmm
167
168#endif // OPENSWMM_CLIMATE_FILE_HPP
ClimateFileFormat format() const
Detected file format.
Definition ClimateFile.hpp:114
~ClimateFileReader()
Definition ClimateFile.cpp:40
ClimateFileReader & operator=(const ClimateFileReader &)=delete
void close()
Close file and release resources.
Definition ClimateFile.cpp:44
ClimateFileReader(const ClimateFileReader &)=delete
bool open(const std::string &path, double start_oa_date, int unit_system, int temp_units=-1)
Definition ClimateFile.cpp:54
bool isOpen() const
True if file is open and valid.
Definition ClimateFile.hpp:117
bool getRecord(double oa_date, DailyClimateRecord &rec)
Definition ClimateFile.cpp:478
@ DEG_C10
Temperature in tenths of a degree Celsius.
Definition climate.c:118
@ WDMV
Wind speed.
Definition climate.c:106
#define MISSING
Missing value code.
Definition consts.h:136
Definition Climate.cpp:34
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:49
Definition NodeCoupling.cpp:16
double * y
Definition odesolve.c:28
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