-
Notifications
You must be signed in to change notification settings - Fork 1
/
TMY3toTMY2_ansi.c
382 lines (288 loc) · 10.5 KB
/
TMY3toTMY2_ansi.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*************************************************************************\
National Renewable Energy Laboratory
Electric Systems Center
Program: TMY3toTMY2_ansi.c
Author: Steve Wilcox
Date: 2008-03-18
Description: Converts TMY3 files to TMY2
Usage:
Compile: gcc -o Tmy3toTMY2_ansi TMY3toTMY2_ansi.c
\**************************************************************************
Modifications
*************
Converted to ANSI C by Mary Anderberg 2008-05-22.
DP Chassin 2014-06-29:
- Changed #include <utility.h> to #include <stdlib.h>
- Added cast to long for 'horvunc' in fprintf to match format type
**************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define TMY3_FORMAT "%d/%d/%d,%d:%*d,%f,%f,%f,%d,%d,%f,%d,%d,%f,%d,%d,%f,%d,%d,%f,%d,%d,%f,%d,%d,%f,%d,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%f,%c,%d,%*f,%*c,%*d,%*f,%*f,%*c,%*d"
#define TMY2_FORMAT " %02d%02d%02d%02d%04d%04d%04d%c%1d%04d%c%1d%04d%c%1d%04d%c%1d%04d%c%1d%04d%c%1d%04d%c%1d%02d%c%1d%02d%c%1d%04d%c%1d%04d%c%1d%03d%c%1d%04d%c%1d%03d%c%1d%03d%c%1d%04d%c%ld%05d%c%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%03d%c%1d%03d%c%1d%03d%c%1d%02d%c%1d\n"
#define DEG2RAD 0.0174532925
#define VERSION "Version 1.0 (2008-03-18)"
#define MAX_PATHNAME_LEN 260
void process_file(char *in_fname, char *out_fname);
int rad_unc(int unc);
int met_unc(int flg);
char met_source(int flg);
char rad_source(int radflg, char metflg );
void quit_pgm(void);
void get_in_file(void);
void get_out_file(void);
int main (int argc, char *argv[])
{
char in_fname[MAX_PATHNAME_LEN],
out_fname[MAX_PATHNAME_LEN],
ok;
if (argc < 2)
{
fprintf(stderr,
"\n DATA USE DISCLAIMER AGREEMENT\n"
" (\"Agreement\")\n\n"
"This data and software (\"Data\") is provided by the National Renewable Energy\n"
"Laboratory (\"NREL\"), which is operated by the Midwest Research Institute\n"
"(\"MRI\") for the U.S. Department Of Energy (\"DOE\").\n\n"
"Access to and use of these Data shall impose the following obligations on the\n"
"user, as set forth in this Agreement. The user is granted the right, without\n"
"any fee or cost, to use, copy, modify, alter, enhance and distribute these\n"
"Data for any purpose whatsoever, provided that this entire notice appears in\n"
"all copies of the Data. Further, the user agrees to credit DOE/NREL/MRI in\n"
"any publication that results from the use of these Data. The names\n"
"DOE/NREL/MRI, however, may not be used in any advertising or publicity to\n"
"endorse or promote any products or commercial entities unless specific\n"
"written permission is obtained from DOE/NREL/MRI. The user also understands\n"
"that DOE/NREL/MRI is not obligated to provide the user with any support,\n"
"consulting, training or assistance of any kind with regard to the use of\n"
"these Data or to provide the user with any updates, revisions or new versions\n"
"of these Data.\n\n"
"Press <RETURN> to continue. . .");
ok = getchar();
fprintf(stderr,
"\rYOU AGREE TO INDEMNIFY DOE/NREL/MRI, AND ITS SUBSIDIARIES, AFFILIATES,\n"
"OFFICERS, AGENTS, AND EMPLOYEES AGAINST ANY CLAIM OR DEMAND, INCLUDING\n"
"REASONABLE ATTORNEYS' FEES, RELATED TO YOUR USE OF THESE DATA. THESE DATA\n"
"ARE PROVIDED BY DOE/NREL/MRI \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n"
"INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n"
"FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL\n"
"DOE/NREL/MRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR\n"
"ANY DAMAGES WHATSOEVER, INCLUDING BUT NOT LIMITED TO CLAIMS ASSOCIATED WITH\n"
"THE LOSS OF DATA OR PROFITS, WHICH MAY RESULT FROM AN ACTION IN CONTRACT,\n"
"NEGLIGENCE OR OTHER TORTIOUS CLAIM THAT ARISES OUT OF OR IN CONNECTION WITH\n"
"THE ACCESS, USE OR PERFORMANCE OF THESE DATA.\n\n");
fprintf(stderr, "usage: TMY3toTMY2_ansi <input file name>\n"
" Converts TMY3 format to TMY2 format\n"
" Output goes to the standard console output.\n"
" %s\n", VERSION);
exit(1);
}
strcpy(in_fname, argv[1]);
strcpy(out_fname, "");
process_file(in_fname, out_fname);
return 0;
}
void process_file(char *in_fname, char *out_fname)
{
int yr, mo, dy, hr,
gloflg, dirflg, difflg, gloiflg, diriflg, dififlg, zeniflg,
glounc, dirunc, difunc, gloiunc, diriunc, difiunc, zeniunc,
totunc, opqunc, drybunc, dewpunc, rhumunc, barounc,wspdunc,
wdirunc, horvunc, chgtunc, pwatunc, aodunc;
char site_id[20],
site_name[100],
site_state[3],
ns[2], ew[2],
io[2000], totflg, opqflg, drybflg, dewpflg, rhumflg, baroflg,
wspdflg, wdirflg, horvflg, chgtflg, pwatflg, aodflg;
float etr, etrn, glo, dir, dif, tot, opq, dryb, dewp, rhum, baro, wspd,
wdir, horv, chgt, pwat, aod, gloi, diri, difi, zeni,
lat, lon, elev, tzone;
FILE *infile, *outfile;
if (!*out_fname)
outfile = stdout;
else
{
if ((outfile = fopen(out_fname, "wt")) == NULL)
{
sprintf(io, "Cannot open output file <%s>.\n -- Processing halted.", out_fname);
}
}
if ((infile = fopen(in_fname, "rt")) == NULL)
{
sprintf(io, "Cannot open input file <%s>\n -- Processing halted.", in_fname);
}
else
{
fgets (io, 1999, infile); // Station header
/* scan the header */
sscanf(io, "%6s,\"%[^\"]\",%2s,%f,%f,%f,%f", site_id, site_name, site_state, &tzone, &lat, &lon, &elev);
*(site_name+22) = '\0'; // limit name to 22 characters
strcpy(ns, lat < 0 ? "S" : "N");
strcpy(ew, lon < 0 ? "W" : "E");
fprintf(outfile, "%6s %-22s %2s%4.0f %1s %02d%3d %1s %3d%3d%6.0f\n",
site_id, site_name, site_state, tzone,
ns, (int)fabs(lat), (int)((fabs(lat) - (int)fabs(lat)) * 60 + 0.5),
ew, (int)fabs(lon), (int)((fabs(lon) - (int)fabs(lon)) * 60 + 0.5),
elev);
fgets (io, 1999, infile); // purge column header
fgets (io, 1999, infile); // priming read
while (!feof(infile))
{
/* read the input line */
sscanf(io, TMY3_FORMAT,
&mo, &dy, &yr, &hr, &etr, &etrn,
&glo, &gloflg, &glounc, &dir, &dirflg, &dirunc, &dif, &difflg, &difunc,
&gloi, &gloiunc, &gloiflg, &diri, &diriunc, &diriflg, &difi, &difiunc, &dififlg, &zeni, &zeniunc, &zeniflg,
&tot, &totflg, &totunc, &opq, &opqflg, &opqunc,
&dryb, &drybflg, &drybunc, &dewp, &dewpflg, &dewpunc, &rhum, &rhumflg, &rhumunc, &baro, &baroflg, &barounc,
&wdir, &wdirflg, &wdirunc, &wspd, &wspdflg, &wspdunc, &horv, &horvflg, &horvunc, &chgt, &chgtflg, &chgtunc, &pwat, &pwatflg, &pwatunc, &aod, &aodflg, &aodunc);
/* covert to TMY2 units and bound field size for missing values */
if (dryb > -9000.0)
dryb *= 10.0;
else
dryb = -990.0;
if (dewp > -9000.0)
dewp *= 10.0;
else
dewp = -990.0;
if (rhum < -9000.0)
rhum = -99.0;
if (tot < -900.0)
tot = 99;
if (opq < -900.0)
opq = 99;
if (baro < -9000.0)
baro = -990.0;
if (wspd > -9000.0)
wspd *= 10.0;
else
wspd = -99.0;
if (wdir < -9000.0)
wdir = -99;
if (horv > -9000.0)
horv /= 100.0;
else
horv = 9999.0;
if (chgt < -9000.0)
chgt = 99999.0;
if (pwat > -9000.0)
pwat *= 10.0;
else
pwat = -99.0;
if (aod > -9000.0)
aod *= 1000.0;
else
aod = -99.0;
if (glo < -9000.0)
glo = -990.0;
if (dir < -9000.0)
dir = -990.0;
if (dif < -9000.0)
dif = -990.0;
if (gloi > -9000.0)
gloi /= 100.0;
if (diri > -9000.0)
diri /= 100.0;
if (difi > -9000.0)
difi /= 100.0;
if (zeni > -9000.0)
zeni /= 10.0;
/* output with flags and move on to next record */
fprintf(outfile, TMY2_FORMAT,
yr%100, mo, dy, hr, (int)etr, (int)etrn,
(int)glo, rad_source(gloflg, totflg > opqflg ? totflg : opqflg), rad_unc(glounc),
(int)dir, rad_source(dirflg, totflg > opqflg ? totflg : opqflg), rad_unc(dirunc),
(int)dif, rad_source(difflg, totflg > opqflg ? totflg : opqflg), rad_unc(difunc),
(int)gloi,'I', rad_unc(glounc),
(int)diri,'I', rad_unc(dirunc),
(int)difi,'I', rad_unc(difunc),
(int)zeni,'I', rad_unc(glounc),
(int)tot, totflg, totunc, (int)opq, opqflg, opqunc,
(int)(dryb), drybflg, drybunc, (int)dewp, dewpflg, dewpunc,
(int)rhum, rhumflg, rhumunc, (int)baro, baroflg, barounc,
(int)wdir, wdirflg, wdirunc, (int)wspd, wspdflg, wspdunc,
(int)(horv), horvflg, (long)horvunc, (int)chgt, chgtflg, chgtunc,
9,9,9,9,9,9,9,9,9,9,
(int)pwat, pwatflg, pwatunc,
(int)aod, aodflg, aodunc,
999, '?', 0,
99, '?', 0);
fgets(io, 1999, infile);
}
}
fclose(infile);
fclose(outfile);
}
/**************************************************************************\
Function rad_unc()
Maps percent uncertainty to TMY2 uncertainty categories
\**************************************************************************/
int rad_unc(int unc)
{
int uflg;
if (unc < 2.0)
uflg = 1;
else if (unc < 4.0)
uflg = 2;
else if (unc < 6.0)
uflg = 3;
else if (unc < 9.0)
uflg = 4;
else if (unc < 13.0)
uflg = 5;
else if (unc < 18.0)
uflg = 6;
else if (unc < 25.0)
uflg = 7;
else if (unc < 35.0)
uflg = 8;
else if (unc < 50.0)
uflg = 9;
else
uflg = 0;
return uflg;
}
int met_unc(int flg)
{
return (flg < 10 ? 7 : 8);
}
/**************************************************************************\
Function met_source()
Maps meteorological flags to TMY2 met source flags
\**************************************************************************/
char met_source(int flg)
{
char source;
if (flg == 99)
source = '?';
else if (flg < 10)
source = 'A';
else if (flg == 51)
source = 'B';
else if (flg == 52)
source = 'C';
else if (flg < 99)
source = 'E';
else
source = '?';
return source;
}
/**************************************************************************\
Function rad_source()
Maps radiation and meteorological flags to TMY2 source flags
\**************************************************************************/
char rad_source(int radflg, char metflg )
{
char source;
if (radflg == 2 || radflg == 3) // satellite
source = 'K';
else if (metflg == '?')
source = '?';
else if (metflg == 'A' || metflg == 'B') // METSTAT observed cloud
source = 'E';
else
source = 'F'; // METSTAT interpolated
return source;
}