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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 808 by johnpye, Thu Aug 3 01:48:05 2006 UTC revision 809 by johnpye, Mon Aug 7 14:48:55 2006 UTC
# Line 3  Line 3 
3  #include <utilities/ascMalloc.h>  #include <utilities/ascMalloc.h>
4  #include <utilities/error.h>  #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,      Record where in the file the data for a particular time can be found,
11      for fast backtracking.      for fast backtracking.
# Line 34  struct DataReader{ Line 37  struct DataReader{
37      struct FilePath *fp;      struct FilePath *fp;
38      FILE *f;      FILE *f;
39      int noutputs;      int noutputs;
40        
41      InputFilterFn *iff;      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      Create a data reader object, with the filename specified. The filename
58      will be searched for in a specified path, eg ASCENDLIBRARY.      will be searched for in a specified path, eg ASCENDLIBRARY.
# Line 47  int datareader_new(const char *fn){ Line 63  int datareader_new(const char *fn){
63      d = ASC_NEW(DataReader);      d = ASC_NEW(DataReader);
64      d->fn = fn;      d->fn = fn;
65      d->fp = NULL;      d->fp = NULL;
66        d->f = NULL;
67      d->noutputs = 0;      d->noutputs = 0;
68      d->iff = NULL;      d->iff = NULL;
69    
# Line 54  int datareader_new(const char *fn){ Line 71  int datareader_new(const char *fn){
71  }  }
72    
73  /**  /**
74      Assign an input filter to the data reader. This will permit pre-processing      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,      of data from the file, and reading of different formats, eg CSV, TDV, TMY,
93      fixed-width, etc.      fixed-width, etc.
94  */  */
95  int datareader_set_input_filter(DataReader *d, InputFilterFn *iff){  int datareader_set_on_open_action(DataReader *d, DataHeaderFunction *iff){
96      d->iff = iff;      d->iff = iff;
97      return 0;      return 0;
98  }  }
# Line 68  int datareader_set_input_filter(DataRead Line 102  int datareader_set_input_filter(DataRead
102      @return 0 on success      @return 0 on success
103  */  */
104  int datareader_init(DataReader *d){  int datareader_init(DataReader *d){
105        FILE *f;
106    
107      d->fp = ospath_new(d->fn);      d->fp = ospath_new(d->fn);
108      if(d->fp==NULL){      if(d->fp==NULL){
109          ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid filepath");          ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid filepath");
110          return 1;          return 1;
111      }      }
112      /**  
113          @TODO implement this      d->f = ospath_fopen(d->fp);
114      */      if(d->f = NULL){
115      CONSOLE_DEBUG("Not implemented");          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  /**  /**
# Line 110  int datareader_num_inputs(const DataRead Line 162  int datareader_num_inputs(const DataRead
162  */  */
163  int datareader_num_outputs(const DataReader *d){  int datareader_num_outputs(const DataReader *d){
164      return d->noutputs;      return d->noutputs;
165  }  }   d->f = NULL;
166    
167    
168  /**  /**
169      Return an interpolated set of output values for the given input values.      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      This should be computed such that the output values are smooth in their
171      first derivatives.      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      @see datareader_deriv
177      @TODO implement this      @TODO implement this
178  */  */
179  int datareader_func(DataReader *d, double *inputs, double *outputs){  int datareader_func(DataReader *d, double *inputs, double *outputs){
180      CONSOLE_DEBUG("Not implemented");      int i;
181      return 1;      double t;
182    
183        t = inputs[0];
184        
185  }  }
186    
187  /**  /**
# Line 135  int datareader_deriv(DataReader *d, doub Line 195  int datareader_deriv(DataReader *d, doub
195      return 1;      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    /**
235        Read a line of data and store in d.
236        @return 0 on success
237    */
238    int datareader_tmy2_read(DataReader *d){
239        int year,month,day,hour;
240        int Iegh,Iedn ,Igh,Idn , Idh; // Irradiation
241        char Igh_source, Idn_source, Idh_source;
242        int Igh_uncert, Idn_uncert, Idh_uncert;
243    
244        MEAS(Lgh); /* Global horiz illuminance / (100 lux) */
245        MEAS(Ldn); /* Direct normal illuminance / (100 lux) */
246        MEAS(Ldh); /* Diffuse horiz illuminance / (100 lux) */
247        MEAS(Lz); /* Zenith illuminance / (10 Cd/m2) */
248    
249        MEAS(covtot); /* Total sky cover / tenths */
250        MEAS(covopq); /* Opaque sky cover / tenths */
251    
252        MEAS(T); /* temperature / (0.1degC) */
253        MEAS(Tdew); /* dew point temperature / (0.1degC) */
254        MEAS(p); /* pressure / mbar */
255        MEAS(rh); /* rel humidity / % */
256        MEAS(wdir); /* wind dir, N=0, E=90,... */
257        MEAS(wvel); /* wind speed / (m/s) */
258        MEAS(vis); /* visibility / (100m) */
259        MEAS(ch); /* ceiling height / m (or special value) */
260        
261        MEAS(rain); /* preciptable water / mm */
262        MEAS(aer); /* aerosol optical depth in thousandths (???) */
263        MEAS(snow); /* snow depth on the specified day / cm (999=missing data) */
264        MEAS(dsno); /* days since last snow (or special value) */
265    
266        /* weather observations from Appendix B */
267        int obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail;
268    
269        /* brace yourself for this one... */
270    
271        fscanf(d->f,
272            /* 1 */ "%2d%2d%2d%2d" "%4d%4d" "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d"
273            /* 2 */ "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d"
274            /* 3 */ "%2d%1s%1d" "2d%1s%1d" "%4d%1s%1d" "%4d%1s%1d" "%3d%1s%1d" "%4d%1s%1d"
275            /* 4 */ "%3d%1s%1d" "%3d%1s%1d" "%4d%1s%ld" "%5d%1s%1d"
276            /* 5 */ "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d"
277            /* 6 */ "%3d%1s%1d" "%3d%1s%1d" "%3d%1s%1d" "%2d%1s%1d"
278    
279        /* 1 */
280            ,year,month,day,hour
281            ,Iegh,Iedn
282            ,Igh,Igh_source,Igh_uncert /* I values in Wh/m2 */
283            ,Idn,Idn_source,Idn_uncert
284            ,Idh,Idh_source,Idh_uncert
285        /* 2 */
286            ,Lgh,Lgh_source,Lgh_uncert /* L values in kCd/m2 */
287            ,Ldn,Ldn_source,Ldn_uncert
288            ,Ldh,Ldh_source,Ldh_uncert
289            ,Lz,Lz_source,Lz_uncert
290        /* 3 */
291            ,covtot, covtot_source, covtot_uncert
292            ,covopq, covopq_source, covopq_uncert
293            ,T, T_source, T_uncert
294            ,Tdew, Tdew_source, Tdew_uncert
295            ,rh,rh_source, rh_uncert
296            ,p, p_source, p_uncert
297        /* 4 */
298            ,wdir, wdir_source, wdir_uncert
299            ,wvel, wvel_source, wvel_uncert
300            ,vis, vis_source, vis_uncert
301            ,ch, ch_source, ch_uncert
302        /* 5 */    
303            ,obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail
304        /* 6 */
305            ,rain, rain_source, rain_uncert
306            ,aer, aer_source, aer_uncert
307            ,snow, snow_source, snow_uncert
308            ,dsno, dsno_source, dsno_uncert
309        );
310    }
311            

Legend:
Removed from v.808  
changed lines
  Added in v.809

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