OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
ClimateFile.hpp
Go to the documentation of this file.
1
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
38 UNKNOWN,
40 GHCND,
41 TD3200,
42 DLY0204
43};
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
81 bool open(const std::string& path, double start_oa_date, int unit_system);
82
84 void close();
85
91 bool getRecord(double oa_date, DailyClimateRecord& rec);
92
94 ClimateFileFormat format() const { return format_; }
95
97 bool isOpen() const { return file_ != nullptr; }
98
99private:
100 static constexpr double MISSING = -1.0e10;
101 static constexpr double MM_PER_INCH = 25.4;
102 static constexpr int MAX_LINE = 512;
103
104 // File state
105 std::FILE* file_ = nullptr;
107 int unit_system_ = 0; // 0=US, 1=SI
108
109 // Current month buffer: [variable][day], day 0 unused, 1-31 valid
110 double file_data_[4][32] = {};
111 int buf_year_ = -1;
112 int buf_month_ = -1;
113
114 // GHCND header-derived field positions
115 int field_pos_[4] = {-1, -1, -1, -1}; // column start for TMIN,TMAX,EVAP,WIND
116 int date_field_pos_ = 0;
117 int wind_type_ = WDMV;
118 int temp_units_ = DEG_C10;
119
120 // Saved line from month-boundary read-ahead
121 char saved_line_[MAX_LINE + 1] = {};
122 bool has_saved_line_ = false;
123
124 // Format detection
125 ClimateFileFormat detectFormat(const char* first_line);
126 bool isGhcndFormat(const char* line);
127
128 // Month buffering
129 bool bufferMonth(int year, int month);
130 void clearBuffer();
131
132 // Format-specific line parsers (populate file_data_ for one line)
133 void parseUserLine(const char* line);
134 void parseGhcndLine(const char* line);
135 void parseTD3200Line(const char* line);
136 void parseDLY0204Line(const char* line);
137
138 // Helpers
139 static void oaDateToYMD(double oa_date, int& y, int& m, int& d);
140 double convertTemp(double raw) const;
141 double convertEvap(double raw) const;
142 double convertWind(double raw, int wind_type) const;
143};
144
145} // namespace climate
146} // namespace openswmm
147
148#endif // OPENSWMM_CLIMATE_FILE_HPP
Definition ClimateFile.hpp:69
bool open(const std::string &path, double start_oa_date, int unit_system)
Definition ClimateFile.cpp:38
ClimateFileFormat format() const
Detected file format.
Definition ClimateFile.hpp:94
~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 isOpen() const
True if file is open and valid.
Definition ClimateFile.hpp:97
bool getRecord(double oa_date, DailyClimateRecord &rec)
Definition ClimateFile.cpp:457
ClimateFileFormat
Definition ClimateFile.hpp:37
@ TD3200
NCDC TD3200 (NWS cooperative observer)
@ GHCND
NCDC Global Historical Climatology Network Daily.
@ DLY0204
Canadian DLY02/DLY04.
@ USER_PREPARED
StationID YYYY MM DD TMAX TMIN EVAP WIND.
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
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