/[ascend]/trunk/models/johnpye/datareader/tmy.c
ViewVC logotype

Contents of /trunk/models/johnpye/datareader/tmy.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2071 - (show annotations) (download) (as text)
Fri Aug 28 02:42:47 2009 UTC (11 years, 1 month ago) by jose
File MIME type: text/x-csrc
File size: 6550 byte(s)
MERGE Jose's branch [2274]:[2584] into trunk
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//**
19 @file
20 Data Reader implementation for the TMY2 format.
21
22 These functions implement a reader interface for meteorological data in the
23 TMY2 format as specified at http://rredc.nrel.gov/solar/pubs/tmy2/tab3-2.html
24 *//*
25 by John Pye, Aug 2006
26 */
27
28 #include <stdio.h>
29 #include <math.h>
30 /* #include <libradtran/sun.h> */
31 #include "sun.h"
32
33 #include <ascend/utilities/ascMalloc.h>
34 #include <ascend/utilities/error.h>
35
36 #include "tmy.h"
37
38
39 typedef struct Tmy2Point_struct{
40 double t;
41 float I;
42 float Ibn;
43 float Id;
44 float T;
45 float v_wind;
46
47 } Tmy2Point;
48
49 #define DATA(D) ((Tmy2Point *)(D->data))[D->i]
50
51 /**
52 @return 0 on success
53 */
54 int datareader_tmy2_header(DataReader *d){
55 char wban[-2 + 6 +2];
56 char city[-8 +29 +2];
57 char state[-31+32+2];
58 int zone;
59 char lathemi;
60 int latdeg, latmin;
61 char longhemi;
62 int longdeg, longmin;
63 int elev;
64
65 fscanf(d->f,"%s %s %s %d"
66 " %c %d %d"
67 " %c %d %d"
68 " %d"
69 ,wban,city,state,&zone
70 ,&lathemi,&latdeg,&latmin
71 ,&longhemi,&longdeg,&longmin
72 ,&elev
73 );
74
75 double lat = latdeg + latmin/60;
76 if(lathemi=='S')lat=-lat;
77 double lng = longdeg + longmin/60;
78 if(longhemi=='E')lng=-lng;
79 CONSOLE_DEBUG( "TMY2 data for city '%s' (WBAN %s, time zone %+d) at lat=%.3f, long=%.3f, elev=%d m"
80 , city, wban, zone, lat, lng, elev
81 );
82 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"TMY2 data is for %s, %s",city, state);
83
84 d->i = 0;
85 d->ndata=8760;
86 d->data = ASC_NEW_ARRAY(Tmy2Point,d->ndata);
87 return 0;
88 }
89
90 int datareader_tmy2_eof(DataReader *d){
91 if(feof(d->f)){
92 CONSOLE_DEBUG("REACHED END OF FILE");
93 d->ndata=d->i;
94 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Read %d rows",d->ndata);
95 return 1;
96 }
97
98 /* set the number of inputs and max outputs */
99 d->ninputs = 1;
100 /*
101 @TODO change the amount of data retrieved from a TMY2 file
102 */
103 d->nmaxoutputs = 5;
104
105 return 0;
106 }
107
108 #define MEAS(N) int N; char N##_source; int N##_uncert
109 #define READ(N) &N, &N##_source, &N##_uncert
110
111 /**
112 Read a line of data and store in d.
113 @return 0 on success
114 */
115 int datareader_tmy2_data(DataReader *d){
116 /* static int lastmonth=-1;
117 static int lastday=-1; */
118 int res = 0;
119 Tmy2Point *tmy;
120 int year,month,day,hour;
121 int Iegh,Iedn; // Irradiation
122 MEAS(Igh); /* Global horizontal irradiation in the specified interval / (Wh/m2) */
123 MEAS(Idn); /* Direct normal irradiation in the specified interval / (Wh/m2) */
124 MEAS(Idh); /* Diffuse horizontal irradiation in the specified interval / (Wh/m2) */
125
126 MEAS(Lgh); /* Global horiz illuminance / (100 lux) */
127 MEAS(Ldn); /* Direct normal illuminance / (100 lux) */
128 MEAS(Ldh); /* Diffuse horiz illuminance / (100 lux) */
129 MEAS(Lz); /* Zenith illuminance / (10 Cd/m2) */
130
131 MEAS(covtot); /* Total sky cover / tenths */
132 MEAS(covopq); /* Opaque sky cover / tenths */
133
134 MEAS(T); /* temperature / (0.1degC) */
135 MEAS(Tdew); /* dew point temperature / (0.1degC) */
136 MEAS(p); /* pressure / mbar */
137 MEAS(rh); /* rel humidity / % */
138 MEAS(wdir); /* wind dir, N=0, E=90,... */
139 MEAS(wvel); /* wind speed / (m/s) */
140 MEAS(vis); /* visibility / (100m) */
141 MEAS(ch); /* ceiling height / m (or special value) */
142
143 MEAS(rain); /* preciptable water / mm */
144 MEAS(aer); /* aerosol optical depth in thousandths (???) */
145 MEAS(snow); /* snow depth on the specified day / cm (999=missing data) */
146 MEAS(dsno); /* days since last snow (or special value) */
147
148 /* weather observations from Appendix B */
149 int obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail;
150
151 /* brace yourself for this one... */
152
153 res = fscanf(d->f,
154 /* 1 */ "%2d%2d%2d%2d" "%4d%4d" "%4d%1c%1d" "%4d%1c%1d" "%4d%1c%1d" /* =15 */
155 /* 2 */ "%4d%1c%1d" "%4d%1c%1d" "%4d%1c%1d" "%4d%1c%1d" /* +12=27 */
156 /* 3 */ "%2d%1c%1d" "%2d%1c%1d" "%4d%1c%1d" "%4d%1c%1d" "%3d%1c%1d" "%4d%1c%1d" /* +18=45 */
157 /* 4 */ "%3d%1c%1d" "%3d%1c%1d" "%4d%1c%1d" "%5d%1c%1d" /* +12=57 */
158 /* 5 */ "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d" /* +10=67 */
159 /* 6 */ "%3d%1c%1d" "%3d%1c%1d" "%3d%1c%1d" "%2d%1c%1d" /* +12=79 */
160 " " /* to ensure that we move to the start of the next line, else end of file */
161
162 /* 1 */,&year, &month, &day, &hour, &Iegh, &Iedn, READ(Igh), READ(Idn), READ(Idh) /* I values in Wh/m2 */
163 /* 2 */,READ(Lgh), READ(Ldn), READ(Ldh), READ(Lz) /* L values in kCd/m2 */
164 /* 3 */,READ(covtot), READ(covopq), READ(T), READ(Tdew), READ(rh), READ(p)
165 /* 4 */,READ(wdir), READ(wvel), READ(vis), READ(ch)
166 /* 5 */,&obs, &storm, &precip, &drizz, &snowtype, &snowshower, &sleet, &fog, &smog, &hail
167 /* 6 */,READ(rain), READ(aer), READ(snow), READ(dsno)
168 );
169
170 if(res!=79){
171 CONSOLE_DEBUG("Bad input data in data row %d (read %d items OK) (%d/%d/%d %2d:00",d->i,res,day,month,year,hour);
172 return 1;
173 }
174
175 /*
176 if(month!=lastmonth || day!=lastday){
177 CONSOLE_DEBUG("Reading data for %d/%d",day,month);
178 lastmonth=month;
179 lastday=day;
180 }
181 */
182
183 /*
184 for the moment, we only record global horizontal, direct normal,
185 ambient temperature, wind speed.
186 */
187 #define DATA(D) ((Tmy2Point *)(D->data))[D->i]
188 tmy = &DATA(d);
189 tmy->t = ((day_of_year_specific(day,month,year) - 1)*24.0 + hour)*3600.0;
190 tmy->I = Igh; /* average W/m2 for the hour in question */
191 tmy->Ibn = Idn; /* normal beam radiation */
192 tmy->Id = Idh;
193 tmy->T = 0.1*T + 273.15; /* temperature */
194 tmy->v_wind = wvel;
195
196 #if 0
197 if(d->i < 20){
198 CONSOLE_DEBUG("ROW %d: year %d, month %d, day %d, hour %d (t = %.0f), Iegh %d, Iedn %d, Igh (%c) = %d --> I = %f"
199 , d->i, year, month, day, hour, tmy->t, Iegh, Iedn, Igh_source, Igh, tmy->I
200 );
201 }
202 #endif
203
204 d->i++;
205 return 0;
206 }
207
208
209
210
211 int datareader_tmy2_time(DataReader *d, double *t){
212 *t = DATA(d).t;
213 return 0;
214 }
215
216 int datareader_tmy2_vals(DataReader *d, double *v){
217
218 v[0]=DATA(d).I;
219 v[1]=DATA(d).Ibn;
220 v[2]=DATA(d).Id;
221 v[3]=DATA(d).T;
222 v[4]=DATA(d).v_wind;
223
224 /*CONSOLE_DEBUG("At t=%f h, T = %lf, I = %f J/m2"
225 ,(DATA(d).t/3600.0),DATA(d).T, DATA(d).I);*/
226
227 return 0;
228 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22