hcdump.cc 6.44 KB
Newer Older
1
#include <algorithm>
LEFEBVREJP email's avatar
LEFEBVREJP email committed
2
3
4
#include <iostream>
#include <string>

5
#include "radixbug/bug.hh"
LEFEBVREJP email's avatar
LEFEBVREJP email committed
6
7
8
#include "radixio/eafstream.hh"
#include "radixio/hcdump.hh"

9
10
namespace radix {
std::string HCDump::id() const { return mId; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
11

12
void HCDump::setId(const std::string &id) { mId = id; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
13

14
int HCDump::year() const { return mYear; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
15

16
void HCDump::setYear(int year) { mYear = year; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
17

18
int HCDump::month() const { return mMonth; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
19

20
void HCDump::setMonth(int month) { mMonth = month; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
21

22
int HCDump::day() const { return mDay; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
23

24
void HCDump::setDay(int day) { mDay = day; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
25

26
int HCDump::hour() const { return mHour; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
27

28
void HCDump::setHour(int hour) { mHour = hour; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
29

30
int HCDump::forecastHour() const { return mForecastHour; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
31

32
void HCDump::setForecastHour(int forecastHour) { mForecastHour = forecastHour; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
33

34
int HCDump::numLocations() const { return mNumLocations; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
35

36
void HCDump::setNumLocations(int numLocations) { mNumLocations = numLocations; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
37

38
39
const std::vector<HCDump::Location> &HCDump::startLocations() const {
  return mStartLocations;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
40
41
}

42
43
44
void HCDump::setStartLocations(
    const std::vector<HCDump::Location> &startLocations) {
  mStartLocations = startLocations;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
45
46
}

47
float HCDump::deltaLat() const { return mDeltaLat; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
48

49
void HCDump::setDeltaLat(float deltaLat) { mDeltaLat = deltaLat; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
50

51
float HCDump::deltaLon() const { return mDeltaLon; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
52

53
void HCDump::setDeltaLon(float deltaLon) { mDeltaLon = deltaLon; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
54

55
float HCDump::cLat() const { return mCLat; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
56

57
void HCDump::setCLat(float cLat) { mCLat = cLat; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
58

59
float HCDump::cLon() const { return mCLon; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
60

61
void HCDump::setCLon(float cLon) { mCLon = cLon; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
62

63
const std::vector<int> &HCDump::levels() const { return mLevels; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
64

65
void HCDump::setLevels(const std::vector<int> &levels) { mLevels = levels; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
66

67
68
const std::vector<std::string> &HCDump::pollutants() const {
  return mPollutants;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
69
70
}

71
72
void HCDump::setPollutants(const std::vector<std::string> &pollutants) {
  mPollutants = pollutants;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
73
74
}

75
76
const std::vector<HCDump::Time> &HCDump::startTimes() const {
  return mStartTimes;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
77
78
}

79
80
void HCDump::setStartTimes(const std::vector<Time> &startTimes) {
  mStartTimes = startTimes;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
81
82
}

83
const std::vector<HCDump::Time> &HCDump::endTimes() const { return mEndTimes; }
LEFEBVREJP email's avatar
LEFEBVREJP email committed
84

85
86
void HCDump::setEndTimes(const std::vector<Time> &endTimes) {
  mEndTimes = endTimes;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
87
88
}

89
90
91
const std::vector<std::vector<std::vector<std::vector<short>>>> &HCDump::xList()
    const {
  return mXList;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
92
93
}

94
95
96
void HCDump::setXList(
    const std::vector<std::vector<std::vector<std::vector<short>>>> &xList) {
  mXList = xList;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
97
98
}

99
100
101
const std::vector<std::vector<std::vector<std::vector<short>>>> &HCDump::yList()
    const {
  return mYList;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
102
103
}

104
105
106
void HCDump::setYList(
    const std::vector<std::vector<std::vector<std::vector<short>>>> &yList) {
  mYList = yList;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
107
108
}

109
110
111
const std::vector<std::vector<std::vector<std::vector<float>>>>
    &HCDump::concList() const {
  return mConcList;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
112
113
}

114
115
116
void HCDump::setConcList(
    const std::vector<std::vector<std::vector<std::vector<float>>>> &concList) {
  mConcList = concList;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
117
118
}

119
120
121
122
123
124
125
126
127
128
129
void HCDump::reload() {
  eafstream rstream(mPath.c_str(), std::ifstream::in | std::ifstream::binary);
  // big endian
  rstream.setReverseBytes(true);
  // read fortran header
  rstream.readHeader();
  mId = rstream.readString(4);
  int packing;
  rstream >> mYear >> mMonth >> mDay >> mHour >> mForecastHour >>
      mNumLocations >> packing;
  rstream.readFooter();
LEFEBVREJP email's avatar
LEFEBVREJP email committed
130

131
132
133
  mStartLocations.resize(mNumLocations);
  // for the number of locations
  for (int i = 0; i < mNumLocations; ++i) {
LEFEBVREJP email's avatar
LEFEBVREJP email committed
134
    rstream.readHeader();
135
136
137
    Location &l = mStartLocations[i];
    rstream >> l.year >> l.month >> l.day >> l.hour >> l.lat >> l.lon >> l.z >>
        l.minutes;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
138
    rstream.readFooter();
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  }

  rstream.readHeader();
  rstream >> mNumLat >> mNumLon >> mDeltaLat >> mDeltaLon >> mCLat >> mCLon;
  rstream.readFooter();

  rstream.readHeader();
  int numLevels;
  rstream >> numLevels;
  mLevels.resize(numLevels);
  for (int i = 0; i < numLevels; ++i) {
    rstream >> mLevels[i];
  }
  rstream.readFooter();

  rstream.readHeader();
  int numPollutants;
  rstream >> numPollutants;
  mPollutants.resize(numPollutants);
  for (int i = 0; i < numPollutants; ++i) {
    mPollutants[i] = rstream.readString(4);
  }
  rstream.readFooter();

  // clear previous values
  mXList.clear();
  mYList.clear();
  mConcList.clear();
  mStartTimes.clear();
  mEndTimes.clear();
  float total = 0.f;
  // infinite loop over observation times
  for (; rstream.good();) {
    Time time1, time2;
LEFEBVREJP email's avatar
LEFEBVREJP email committed
173
    rstream.readHeader();
174
175
176
    rstream >> time1.year >> time1.month >> time1.day >> time1.hour >>
        time1.minutes >> time1.forecast;
    mStartTimes.push_back(time1);
LEFEBVREJP email's avatar
LEFEBVREJP email committed
177
178
179
    rstream.readFooter();

    rstream.readHeader();
180
181
182
    rstream >> time2.year >> time2.month >> time2.day >> time2.hour >>
        time2.minutes >> time2.forecast;
    mEndTimes.push_back(time2);
LEFEBVREJP email's avatar
LEFEBVREJP email committed
183
184
    rstream.readFooter();

185
186
187
188
189
190
191
192
193
194
195
    mXList.push_back(std::vector<std::vector<std::vector<short>>>());
    mYList.push_back(std::vector<std::vector<std::vector<short>>>());
    mConcList.push_back(std::vector<std::vector<std::vector<float>>>());
    mXList[mXList.size() - 1].resize(numPollutants);
    mYList[mYList.size() - 1].resize(numPollutants);
    mConcList[mConcList.size() - 1].resize(numPollutants);
    for (int i = 0; i < numPollutants; ++i) {
      mXList[mXList.size() - 1][i].resize(numLevels);
      mYList[mYList.size() - 1][i].resize(numLevels);
      mConcList[mConcList.size() - 1][i].resize(numLevels);
      for (int j = 0; j < numLevels; ++j) {
LEFEBVREJP email's avatar
LEFEBVREJP email committed
196
        rstream.readHeader();
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        std::string pollutant;
        int z;
        int nxyp;
        if (packing) {
          pollutant = rstream.readString(4);
          rstream >> z >> nxyp;
          mXList[mXList.size() - 1][i][j].resize(nxyp);
          mYList[mYList.size() - 1][i][j].resize(nxyp);
          mConcList[mConcList.size() - 1][i][j].resize(nxyp);
          for (int np = 0; np < nxyp; ++np) {
            mXList[mXList.size() - 1][i][j][np]       = rstream.readShort();
            mYList[mYList.size() - 1][i][j][np]       = rstream.readShort();
            mConcList[mConcList.size() - 1][i][j][np] = rstream.readFloat();
          }
        }  // if packing is enabled
LEFEBVREJP email's avatar
LEFEBVREJP email committed
212
        rstream.readFooter();
213
214
215
216
217
        rstream.peek();  // peek ahead to check file integrity
      }                  // loop over num levels
    }                    // loop num pollutants
  }                      // infinite loop over output times
  radix_line("Total mass: " << total);
LEFEBVREJP email's avatar
LEFEBVREJP email committed
218
219
}

220
221
222
223
HCDump::HCDump(const std::string &path)
    : mPath(path) {
  reload();
}
224
}  // namespace radix