OpenSWMM Engine  6.0.0-alpha.3
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.3)
Loading...
Searching...
No Matches
ClimateFile.hpp
Go to the documentation of this file.
1
22
23#ifndef OPENSWMM_CLIMATE_FILE_HPP
24#define OPENSWMM_CLIMATE_FILE_HPP
25
26#include <string>
27#include <cstdio>
28#include <cmath>
29
30namespace openswmm {
31namespace climate {
32
33// ============================================================================
34// Enums
35// ============================================================================
36
44
46enum ClimateVar { TMIN = 0, TMAX = 1, EVAP = 2, WIND = 3 };
47
49enum WindFieldType { WDMV = 0, AWND = 1 };
50
52enum TempUnits { DEG_C10 = 0, DEG_C = 1, DEG_F = 2 };
53
54// ============================================================================
55// Daily climate record (output of a lookup)
56// ============================================================================
57
59 double tmin = NAN;
60 double tmax = NAN;
61 double evap = NAN;
62 double wind = NAN;
63};
64
65// ============================================================================
66// ClimateFileReader
67// ============================================================================
68
70public:
71 ClimateFileReader() = default;
73
76
84 bool open(const std::string& path, double start_oa_date, int unit_system,
85 int temp_units = -1);
86
88 void close();
89
95 bool getRecord(double oa_date, DailyClimateRecord& rec);
96
98 ClimateFileFormat format() const { return format_; }
99
101 bool isOpen() const { return file_ != nullptr; }
102
103private:
104 static constexpr double MISSING = -1.0e10;
105 static constexpr double MM_PER_INCH = 25.4;
106 static constexpr int MAX_LINE = 512;
107
108 // File state
109 std::FILE* file_ = nullptr;
111 int unit_system_ = 0; // 0=US, 1=SI
112
113 // Current month buffer: [variable][day], day 0 unused, 1-31 valid
114 double file_data_[4][32] = {};
115 int buf_year_ = -1;
116 int buf_month_ = -1;
117
118 // GHCND header-derived field positions
119 int field_pos_[4] = {-1, -1, -1, -1}; // column start for TMIN,TMAX,EVAP,WIND
120 int date_field_pos_ = 0;
121 int wind_type_ = WDMV;
122 int temp_units_ = DEG_C10;
123
124 // Saved line from month-boundary read-ahead
125 char saved_line_[MAX_LINE + 1] = {};
126 bool has_saved_line_ = false;
127
128 // Format detection
129 ClimateFileFormat detectFormat(const char* first_line);
130 bool isGhcndFormat(const char* line);
131
132 // Month buffering
133 bool bufferMonth(int year, int month);
134 void clearBuffer();
135
136 // Format-specific line parsers (populate file_data_ for one line)
137 void parseUserLine(const char* line);
138 void parseGhcndLine(const char* line);
139 void parseTD3200Line(const char* line);
140 void parseDLY0204Line(const char* line);
141
142 // Helpers
143 static void oaDateToYMD(double oa_date, int& y, int& m, int& d);
144 double convertTemp(double raw) const;
145 double convertEvap(double raw) const;
146 double convertWind(double raw, int wind_type) const;
147};
148
149} // namespace climate
150} // namespace openswmm
151
152#endif // OPENSWMM_CLIMATE_FILE_HPP
ClimateFileFormat format() const
Detected file format.
Definition ClimateFile.hpp:98
~ClimateFileReader()
Definition ClimateFile.cpp:24
ClimateFileReader & operator=(const ClimateFileReader &)=delete
void close()
Close file and release resources.
Definition ClimateFile.cpp:28
ClimateFileReader(const ClimateFileReader &)=delete
bool open(const std::string &path, double start_oa_date, int unit_system, int temp_units=-1)
Definition ClimateFile.cpp:38
bool isOpen() const
True if file is open and valid.
Definition ClimateFile.hpp:101
bool getRecord(double oa_date, DailyClimateRecord &rec)
Definition ClimateFile.cpp:462
@ 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:18
ClimateFileFormat
Definition ClimateFile.hpp:37
@ UNKNOWN
Definition ClimateFile.hpp:38
@ TD3200
NCDC TD3200 (NWS cooperative observer)
Definition ClimateFile.hpp:41
@ GHCND
NCDC Global Historical Climatology Network Daily.
Definition ClimateFile.hpp:40
@ DLY0204
Canadian DLY02/DLY04.
Definition ClimateFile.hpp:42
@ USER_PREPARED
StationID YYYY MM DD TMAX TMIN EVAP WIND.
Definition ClimateFile.hpp:39
TempUnits
Temperature unit encoding in GHCND.
Definition ClimateFile.hpp:52
@ DEG_C
Definition ClimateFile.hpp:52
@ DEG_F
Definition ClimateFile.hpp:52
@ DEG_C10
Definition ClimateFile.hpp:52
ClimateVar
Climate variable indices (matching legacy ClimateVarType)
Definition ClimateFile.hpp:46
@ TMAX
Definition ClimateFile.hpp:46
@ WIND
Definition ClimateFile.hpp:46
@ EVAP
Definition ClimateFile.hpp:46
@ TMIN
Definition ClimateFile.hpp:46
WindFieldType
Wind field type in GHCND.
Definition ClimateFile.hpp:49
@ AWND
Definition ClimateFile.hpp:49
@ WDMV
Definition ClimateFile.hpp:49
constexpr double MM_PER_INCH
Definition Climate.hpp:33
Definition NodeCoupling.cpp:15
double * y
Definition odesolve.c:28
Definition ClimateFile.hpp:58
double evap
Pan evaporation (in/day US, mm/day SI; NAN if missing)
Definition ClimateFile.hpp:61
double tmin
Minimum temperature (deg F)
Definition ClimateFile.hpp:59
double wind
Wind speed (mph; NAN if missing)
Definition ClimateFile.hpp:62
double tmax
Maximum temperature (deg F)
Definition ClimateFile.hpp:60