-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertTmax.cpp
247 lines (210 loc) · 5.58 KB
/
convertTmax.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <ctime>
#include <string.h>
using namespace std;
bool processData(string, int, int, string, string, string);
void writeRecord(string, int);
void writeMissingMonth(int);
int ymd2day(int, int, int);
int str2int(string str);
double str2double(string str);
int days_in_month[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int NO_DATA = -99;
FILE *infFile;
FILE *out;
int tokenize(string str, string *arr) {
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
char *part = strtok(cstr, ",");
int i = 0;
while (part != 0) {
stringstream ss;
ss << part;
*(arr + i) = ss.str();
part = strtok(NULL, ",");
i++;
}
return i;
}
int main(int argc, char *argv[]) {
string COUNTRY;
string STATE;
string VARIABLE;
int START_YEAR;
int START_MONTH;
int END_YEAR;
int END_MONTH;
if (argc == 7) {
COUNTRY = argv[1];
//STATE = argv[2];
VARIABLE = argv[2];
START_YEAR = atoi(argv[3]);
START_MONTH = atoi(argv[4]);
END_YEAR = atoi(argv[5]);
END_MONTH = atoi(argv[6]);
} else {
cout << "Incorrect number of inputs" << endl;
exit(-1);
}
string line, country, state;
double lat, lon, elev;
int stn_id;
string stn_id_str;
string stn_name, date_start, date_end;
ifstream stnFile, stnFileName;
stnFile.open("./cmd/stationinfo.txt");
infFile = fopen("./cmd/Tmax/tmaxncdc.inf", "w");
out = fopen("./cmd/Tmax/tmaxncdc.daily", "w");
string data_path = "./cmd/climate/"; //should end with /
int num_stations = 0;
while (stnFile.good()) {
getline(stnFile, line);
//cout << line << endl;
if (line.substr(0, 2) == COUNTRY) {
//string parts[] = NULL; // = line.split(",");
string parts[10];
int count = tokenize(line, parts);
lat = atof(parts[2].c_str());
lon = atof(parts[1].c_str());
elev = atof(parts[3].c_str());
stn_id_str = parts[0].substr(5,6);
stn_id = atoi(stn_id_str.c_str());
/*
lat = atof((line.substr(13, 7)).c_str());
lon = atof((line.substr(21, 9)).c_str());
elev = atoi((line.substr(32, 5)).c_str());
stn_id_str = line.substr(5, 6);
stn_id = atoi(stn_id_str.c_str());
*/
cout << stn_id << " " << stn_id_str << endl;
if (stn_id == 0) {
//if conversion to numeric fails
for(int i = 0; i < stn_id_str.length(); i++)
{
char ch = stn_id_str[i];
//cout << ch << endl;
if (!(ch >= '0' && ch <= '9')) stn_id_str[i] = '0';
}
//cout << " Converted " << stn_id_str << endl;
}
stn_name = parts[4];
string dly_file = data_path + parts[0].c_str(); // data file has no extention .dly
bool foundData = processData(dly_file, START_YEAR, END_YEAR,
VARIABLE, stn_id_str, stn_name);
if (foundData) {
fprintf(infFile,
" %7.4f %8.4f %6.1f %-29s %6s %4s\n", lat,
lon, elev, stn_name.c_str(), stn_id_str.c_str(), VARIABLE.c_str());
num_stations++;
//cout << line << endl;
}
}
}
fclose(infFile);
fclose(out);
stnFile.close();
return 0;
}
bool processData(string filename, int year_start, int year_end, string variable,
string stn_id, string stn_name) {
ifstream in;
string line;
string stn_id_formatted_str = stn_id.substr(2, stn_id.length() - 2);
int stn_id_formatted_int;
stringstream ss0(stn_id_formatted_str);
ss0 >> stn_id_formatted_int;
in.open(filename.c_str());
if (!in.is_open()) {
cout << filename << " not found" << endl;
return false;
}
int year, month, tot_days = 0;
bool foundData = false;
//start reading the file
getline(in, line);
string ** datalines = new string*[year_end-year_start+1];
for (int i=0; i < year_end-year_start+1; i++) {
datalines[i] = new string[13];
for (int j=0; j < 13; j++) {
datalines[i][j] = "";
}
}
while (in.good())
{
string varname = line.substr(17, 4);
string str_year = line.substr(11, 4);
string str_month = line.substr(15, 2);
stringstream ss(str_year);
ss >> year;
stringstream ss1(str_month);
ss1 >> month;
if ((variable == varname) && (year >= year_start) && (year <= year_end))
{
//cout << year << " " << month << " " << line << endl;
datalines[year - year_start][month] = line;
foundData = true;
}
getline(in, line);
}
in.close();
if (foundData) {
string dataline = "";
for (year=year_start; year<=year_end; year++) {
fprintf(out, "%6d %-29s %4d\n ", stn_id_formatted_int, stn_name.c_str(), year);
for (month=1; month<13; month++) {
dataline = datalines[year-year_start][month];
if (dataline != "") {
writeRecord(dataline, month);
} else {
writeMissingMonth(month);
}
}
fprintf(out, "\n");
}
}
return foundData;
}
void writeMissingMonth(int month) {
for (int i = 0; i < days_in_month[month]; i++) {
fprintf(out, "%d ", NO_DATA);
}
}
void writeRecord(string line, int month) {
int length = line.length();
int num_vals = (length - 21) / 8;
int tot_days = days_in_month[month];
for (int j = 0; j < num_vals; j++) {
string token = line.substr(21 + j * 8, 8);
string val = token.substr(0, 5);
double tmax = 0;
stringstream ss(val);
ss >> tmax;
if (j == tot_days) {
//cout << "End of month reached " << j << endl;
break;
}
if (tmax >= 0.0) {
fprintf(out, "%.2f ", (tmax/10.) * 1.8 + 32.0);
} else {
fprintf(out, "%d ", -99);
}
}
}
int str2int(string str) {
int val = -999999;
stringstream ss(str);
ss >> val;
return val;
}
double str2double(string str) {
double val = -999999;
stringstream ss(str);
ss >> val;
return val;
}