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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 810 - (show annotations) (download) (as text)
Mon Aug 7 14:49:46 2006 UTC (13 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 7994 byte(s)
woops, missed a bit
1 #include "dr.h"
2 #include <general/ospath.h>
3 #include <utilities/ascMalloc.h>
4 #include <utilities/error.h>
5
6 /*------------------------------------------------------------------------------
7 DATA STRUCTURES (PRIVATE TO THIS FILE)
8 */
9 /**
10 Record where in the file the data for a particular time can be found,
11 for fast backtracking.
12 */
13 struct IndexPoint{
14 int pos;
15 double t;
16 };
17
18 /**
19 Structure to hold the data for a particular data point after being loaded.
20 */
21 struct DataPoint{
22 double t;
23 double *outputs;
24 };
25
26 /**
27 Need some kind of structure here to hold spline data for a particular
28 interval.
29 */
30
31 /**
32 Top-level structure for the data reader. Keeps track of the file we're
33 working with, the number of columns, etc.
34 */
35 struct DataReader{
36 const char *fn;
37 struct FilePath *fp;
38 FILE *f;
39 int noutputs;
40
41 InputFilterFn *iff;
42 DataPoint *data; /**< null terminated */
43 };
44
45 /*------------------------------------------------------------------------------
46 FORWARD DECLARATIONS
47 */
48
49 DataReaderReadFn datareader_tmy2_read;
50 DataReaderHeaderFn datareader_tmy2_header;
51
52 /*------------------------------------------------------------------------------
53 API FUNCTIONS
54 */
55
56 /**
57 Create a data reader object, with the filename specified. The filename
58 will be searched for in a specified path, eg ASCENDLIBRARY.
59 */
60 int datareader_new(const char *fn){
61 DataReader *d;
62
63 d = ASC_NEW(DataReader);
64 d->fn = fn;
65 d->fp = NULL;
66 d->f = NULL;
67 d->noutputs = 0;
68 d->iff = NULL;
69
70 return d;
71 }
72
73 /**
74 Set data file format
75 @return 0 on success
76 */
77 int datareader_set_file_format(DataReader *d, const datareader_file_format_t &format);
78 switch(format){
79 case DATAREADER_FORMAT_TMY2:
80 d->headerfn=&datareader_tmy2_header;
81 d->linefn=&datareader_tmy2_read;
82 break;
83 default:
84 ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unknown file format specified");
85 return 1;
86 }
87 return 0;
88 }
89
90 /**
91 Assign an an on-file-open filter to the data reader. This will permit pre-processing
92 of data from the file, and reading of different formats, eg CSV, TDV, TMY,
93 fixed-width, etc.
94 */
95 int datareader_set_on_open_action(DataReader *d, DataHeaderFunction *iff){
96 d->iff = iff;
97 return 0;
98 }
99
100 /**
101 Initialise the datareader: open the file, check the number of columns, etc.
102 @return 0 on success
103 */
104 int datareader_init(DataReader *d){
105 FILE *f;
106
107 d->fp = ospath_new(d->fn);
108 if(d->fp==NULL){
109 ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid filepath");
110 return 1;
111 }
112
113 d->f = ospath_fopen(d->fp);
114 if(d->f = NULL){
115 ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unable to open file '%s'",d->fn);
116 return 1;
117 }
118
119 if(datareader_process_header(d)){
120 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Error processing file header in '%s'",d->fn);
121 return 1;
122 }
123
124 if(datareader_read_data(d)){
125 ERROR_REPORTER_HERE(ASC_PROG,ERR,"Error reading file data in '%s'",d->fn);
126 return 1;
127 }
128
129 fclose(d->f);
130
131 return 0;
132 }
133
134 /**
135 Shut down the data reader and deallocate any memory owned by it, then
136 free the memory at d.
137 */
138 int datareader_delete(DataReader *d){
139 if(d->fp){
140 ospath_free(d->fp);
141 d->fp = NULL;
142 }
143 if(d->f){
144 fclose(d->f);
145 d->f = NULL;
146 }
147 ASC_FREE(d);
148 return 0;
149 }
150
151 /**
152 Return the number of inputs (independent variables) supplied in the
153 DataReader's current file. Can only be 1 at this stage.
154 */
155 int datareader_num_inputs(const DataReader *d){
156 return 1;
157 }
158
159 /**
160 Return the number of outputs (dependent variables) found in the DataReader's
161 current file. Should be one or more.
162 */
163 int datareader_num_outputs(const DataReader *d){
164 return d->noutputs;
165 } d->f = NULL;
166
167
168 /**
169 Return an interpolated set of output values for the given input values.
170 This should be computed such that the output values are smooth in their
171 first derivatives.
172
173 The required memory for the inputs and outputs must be allocated by the
174 caller, and indicated by the pointers 'inputs' and 'outputs'.
175
176 @see datareader_deriv
177 @TODO implement this
178 */
179 int datareader_func(DataReader *d, double *inputs, double *outputs){
180 int i;
181 double t;
182
183 t = inputs[0];
184
185 }
186
187 /**
188 Return an interpolated set of output derivatives for the given input
189 values. These should be smooth.
190 @see datareader_func
191 @TODO implement this
192 */
193 int datareader_deriv(DataReader *d, double *inputs, double *jacobian){
194 CONSOLE_DEBUG("Not implemented");
195 return 1;
196 }
197
198 /*------------------------------------------------------------------------------
199 TMY2 READER FUNCTIONS
200
201 These functions implement a reader interface for meteorological data in the
202 TMY2 format as specified at http://rredc.nrel.gov/solar/pubs/tmy2/tab3-2.html
203 */
204
205 /**
206 @return 0 on success
207 */
208 int datareader_tmy2_read(DataReader *d){
209 char wban[-2 + 6 +2];
210 char city[-8 +29 +2];
211 char zone[-34+36 +2];
212 char lathemi;
213 int latdeg, latmin;
214 char longhemi;
215 int longdeg, longmin;
216
217 fscanf(d->f,"%s %s %s %d %c %d %d %c %d %d %d"
218 ,wban,city,zone
219 ,lathemi,latdeg,latmin
220 ,longhemi,longdeg,longmin
221 );
222
223 double lat = latdeg + latmin/60;
224 if(lathemi=='S')lat=-lat;
225 double lng = longdeg + longmin/60;
226 if(longhemi=='E')lng=-lng;
227 CONSOLE_DEBUG( "TMY2 data for city '%s' (WBAN %s, time zone %s) at lat=%.3f, long=%.3f"
228 city, wban, zone, lat, lng
229 );
230 }
231
232 #define MEAS(N) int N; char N##_source; int N##_uncert
233
234 typedef struct Tmy2Point{
235 double t;
236 float G;
237 float G
238
239 /**
240 Read a line of data and store in d.
241 @return 0 on success
242 */
243 int datareader_tmy2_read(DataReader *d){
244 int year,month,day,hour;
245 int Iegh,Iedn ,Igh,Idn , Idh; // Irradiation
246 char Igh_source, Idn_source, Idh_source;
247 int Igh_uncert, Idn_uncert, Idh_uncert;
248
249 MEAS(Lgh); /* Global horiz illuminance / (100 lux) */
250 MEAS(Ldn); /* Direct normal illuminance / (100 lux) */
251 MEAS(Ldh); /* Diffuse horiz illuminance / (100 lux) */
252 MEAS(Lz); /* Zenith illuminance / (10 Cd/m2) */
253
254 MEAS(covtot); /* Total sky cover / tenths */
255 MEAS(covopq); /* Opaque sky cover / tenths */
256
257 MEAS(T); /* temperature / (0.1degC) */
258 MEAS(Tdew); /* dew point temperature / (0.1degC) */
259 MEAS(p); /* pressure / mbar */
260 MEAS(rh); /* rel humidity / % */
261 MEAS(wdir); /* wind dir, N=0, E=90,... */
262 MEAS(wvel); /* wind speed / (m/s) */
263 MEAS(vis); /* visibility / (100m) */
264 MEAS(ch); /* ceiling height / m (or special value) */
265
266 MEAS(rain); /* preciptable water / mm */
267 MEAS(aer); /* aerosol optical depth in thousandths (???) */
268 MEAS(snow); /* snow depth on the specified day / cm (999=missing data) */
269 MEAS(dsno); /* days since last snow (or special value) */
270
271 /* weather observations from Appendix B */
272 int obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail;
273
274 /* brace yourself for this one... */
275
276 fscanf(d->f,
277 /* 1 */ "%2d%2d%2d%2d" "%4d%4d" "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d"
278 /* 2 */ "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d"
279 /* 3 */ "%2d%1s%1d" "2d%1s%1d" "%4d%1s%1d" "%4d%1s%1d" "%3d%1s%1d" "%4d%1s%1d"
280 /* 4 */ "%3d%1s%1d" "%3d%1s%1d" "%4d%1s%ld" "%5d%1s%1d"
281 /* 5 */ "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d"
282 /* 6 */ "%3d%1s%1d" "%3d%1s%1d" "%3d%1s%1d" "%2d%1s%1d"
283
284 /* 1 */
285 ,year,month,day,hour
286 ,Iegh,Iedn
287 ,Igh,Igh_source,Igh_uncert /* I values in Wh/m2 */
288 ,Idn,Idn_source,Idn_uncert
289 ,Idh,Idh_source,Idh_uncert
290 /* 2 */
291 ,Lgh,Lgh_source,Lgh_uncert /* L values in kCd/m2 */
292 ,Ldn,Ldn_source,Ldn_uncert
293 ,Ldh,Ldh_source,Ldh_uncert
294 ,Lz,Lz_source,Lz_uncert
295 /* 3 */
296 ,covtot, covtot_source, covtot_uncert
297 ,covopq, covopq_source, covopq_uncert
298 ,T, T_source, T_uncert
299 ,Tdew, Tdew_source, Tdew_uncert
300 ,rh,rh_source, rh_uncert
301 ,p, p_source, p_uncert
302 /* 4 */
303 ,wdir, wdir_source, wdir_uncert
304 ,wvel, wvel_source, wvel_uncert
305 ,vis, vis_source, vis_uncert
306 ,ch, ch_source, ch_uncert
307 /* 5 */
308 ,obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail
309 /* 6 */
310 ,rain, rain_source, rain_uncert
311 ,aer, aer_source, aer_uncert
312 ,snow, snow_source, snow_uncert
313 ,dsno, dsno_source, dsno_uncert
314 );
315
316 /*
317 for the moment, we only record global horizontal, direct normal,
318 ambient temperature, wind speed.
319 */
320
321 struct Tmy2Point tmy;
322 tmy.t = double(year
323 tmy.G0 = float(Igh);
324 tmy,
325
326
327 }
328

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