/[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 810 by johnpye, Mon Aug 7 14:49:46 2006 UTC revision 811 by johnpye, Thu Aug 10 17:08:16 2006 UTC
# Line 1  Line 1 
1  #include "dr.h"  /*  ASCEND modelling environment
2  #include <general/ospath.h>      Copyright (C) 2006 Carnegie Mellon University
 #include <utilities/ascMalloc.h>  
 #include <utilities/error.h>  
   
 /*------------------------------------------------------------------------------  
   DATA STRUCTURES (PRIVATE TO THIS FILE)  
 */  
 /**  
     Record where in the file the data for a particular time can be found,  
     for fast backtracking.  
 */  
 struct IndexPoint{  
     int pos;  
     double t;  
 };  
3    
4  /**      This program is free software; you can redistribute it and/or modify
5      Structure to hold the data for a particular data point after being loaded.      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  */  */
 struct DataPoint{  
     double t;  
     double *outputs;  
 };  
19    
20  /**  #include "dr.h"
21      Need some kind of structure here to hold spline data for a particular  #include "tmy.h"
     interval.  
 */  
22    
23  /**  #include <utilities/config.h>
24      Top-level structure for the data reader. Keeps track of the file we're  #include <general/ospath.h>
25      working with, the number of columns, etc.  #include <utilities/ascMalloc.h>
26  */  #include <utilities/error.h>
27  struct DataReader{  #include <utilities/ascPanic.h>
     const char *fn;  
     struct FilePath *fp;  
     FILE *f;  
     int noutputs;  
       
     InputFilterFn *iff;  
     DataPoint *data; /**< null terminated */  
 };  
28    
29  /*------------------------------------------------------------------------------  /*------------------------------------------------------------------------------
30    FORWARD DECLARATIONS    FORWARD DECLARATIONS
31  */  */
32    
 DataReaderReadFn datareader_tmy2_read;  
 DataReaderHeaderFn datareader_tmy2_header;  
33    
34  /*------------------------------------------------------------------------------  /*------------------------------------------------------------------------------
35    API FUNCTIONS    API FUNCTIONS
# Line 57  DataReaderHeaderFn datareader_tmy2_heade Line 39  DataReaderHeaderFn datareader_tmy2_heade
39      Create a data reader object, with the filename specified. The filename      Create a data reader object, with the filename specified. The filename
40      will be searched for in a specified path, eg ASCENDLIBRARY.      will be searched for in a specified path, eg ASCENDLIBRARY.
41  */        */      
42  int datareader_new(const char *fn){  DataReader *datareader_new(const char *fn){
43      DataReader *d;      DataReader *d;
44            
45      d = ASC_NEW(DataReader);      d = ASC_NEW(DataReader);
# Line 65  int datareader_new(const char *fn){ Line 47  int datareader_new(const char *fn){
47      d->fp = NULL;      d->fp = NULL;
48      d->f = NULL;      d->f = NULL;
49      d->noutputs = 0;      d->noutputs = 0;
     d->iff = NULL;  
50    
51      return d;      return d;
52  }  }
# Line 74  int datareader_new(const char *fn){ Line 55  int datareader_new(const char *fn){
55      Set data file format      Set data file format
56      @return 0 on success      @return 0 on success
57  */  */
58  int datareader_set_file_format(DataReader *d, const datareader_file_format_t &format);  int datareader_set_file_format(DataReader *d, const DataReaderFileFormat format){
59      switch(format){      switch(format){
60          case DATAREADER_FORMAT_TMY2:          case DATAREADER_FORMAT_TMY2:
61              d->headerfn=&datareader_tmy2_header;              d->headerfn=&datareader_tmy2_header;
62              d->linefn=&datareader_tmy2_read;              d->datafn=&datareader_tmy2_data;
63              break;              break;
64          default:          default:
65              ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unknown file format specified");              ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unknown file format specified");
# Line 87  int datareader_set_file_format(DataReade Line 68  int datareader_set_file_format(DataReade
68      return 0;      return 0;
69  }  }
70    
 /**  
     Assign an an on-file-open filter to the data reader. This will permit pre-processing  
     of data from the file, and reading of different formats, eg CSV, TDV, TMY,  
     fixed-width, etc.  
 */  
 int datareader_set_on_open_action(DataReader *d, DataHeaderFunction *iff){  
     d->iff = iff;  
     return 0;  
 }  
   
71  /**  /**
72      Initialise the datareader: open the file, check the number of columns, etc.      Initialise the datareader: open the file, check the number of columns, etc.
73      @return 0 on success      @return 0 on success
74  */  */
75  int datareader_init(DataReader *d){  int datareader_init(DataReader *d){
     FILE *f;  
   
76      d->fp = ospath_new(d->fn);      d->fp = ospath_new(d->fn);
77      if(d->fp==NULL){      if(d->fp==NULL){
78          ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid filepath");          ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid filepath");
79          return 1;          return 1;
80      }      }
81    
82      d->f = ospath_fopen(d->fp);      d->f = ospath_fopen(d->fp,"r");
83      if(d->f = NULL){      if(d->f == NULL){
84          ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unable to open file '%s'",d->fn);          ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unable to open file '%s' for reading",d->fn);
85          return 1;          return 1;
86      }      }
87    
88      if(datareader_process_header(d)){      if((*d->headerfn)(d)){
89          ERROR_REPORTER_HERE(ASC_PROG_ERR,"Error processing file header in '%s'",d->fn);          ERROR_REPORTER_HERE(ASC_PROG_ERR,"Error processing file header in '%s'",d->fn);
90            fclose(d->f);
91          return 1;          return 1;
92      }      }
93            
94      if(datareader_read_data(d)){      while(! (*d->eoffn)(d)){
95          ERROR_REPORTER_HERE(ASC_PROG,ERR,"Error reading file data in '%s'",d->fn);          if((*d->datafn)(d)){
96          return 1;              ERROR_REPORTER_HERE(ASC_PROG_ERR,"Error reading file data in '%s'",d->fn);
97                fclose(d->f);
98                return 1;
99            }
100      }      }
   
101      fclose(d->f);      fclose(d->f);
102    
103      return 0;      return 0;
# Line 162  int datareader_num_inputs(const DataRead Line 134  int datareader_num_inputs(const DataRead
134  */  */
135  int datareader_num_outputs(const DataReader *d){  int datareader_num_outputs(const DataReader *d){
136      return d->noutputs;      return d->noutputs;
137  }   d->f = NULL;  }
138    
139    
140  /**  /**
# Line 181  int datareader_func(DataReader *d, doubl Line 153  int datareader_func(DataReader *d, doubl
153      double t;      double t;
154    
155      t = inputs[0];      t = inputs[0];
156            i = 0;
157    
158        CONSOLE_DEBUG("Not implemented");
159        return 1;
160  }  }
161    
162  /**  /**
# Line 195  int datareader_deriv(DataReader *d, doub Line 170  int datareader_deriv(DataReader *d, doub
170      return 1;      return 1;
171  }  }
172    
 /*------------------------------------------------------------------------------  
   TMY2 READER FUNCTIONS  
       
     These functions implement a reader interface for meteorological data in the  
     TMY2 format as specified at http://rredc.nrel.gov/solar/pubs/tmy2/tab3-2.html  
 */  
173    
 /**  
     @return 0 on success  
 */  
 int datareader_tmy2_read(DataReader *d){  
     char wban[-2 + 6 +2];  
     char city[-8 +29 +2];  
     char zone[-34+36 +2];  
     char lathemi;  
     int latdeg, latmin;  
     char longhemi;  
     int longdeg, longmin;  
   
     fscanf(d->f,"%s %s %s %d %c %d %d %c %d %d %d"  
         ,wban,city,zone  
         ,lathemi,latdeg,latmin  
         ,longhemi,longdeg,longmin  
     );  
   
     double lat = latdeg + latmin/60;  
     if(lathemi=='S')lat=-lat;  
     double lng = longdeg + longmin/60;  
     if(longhemi=='E')lng=-lng;  
     CONSOLE_DEBUG( "TMY2 data for city '%s' (WBAN %s, time zone %s) at lat=%.3f, long=%.3f"  
         city, wban, zone, lat, lng  
     );  
 }  
   
 #define MEAS(N) int N; char N##_source; int N##_uncert  
   
 typedef struct Tmy2Point{  
     double t;  
     float G;  
     float G  
       
 /**  
     Read a line of data and store in d.  
     @return 0 on success  
 */  
 int datareader_tmy2_read(DataReader *d){  
     int year,month,day,hour;  
     int Iegh,Iedn ,Igh,Idn , Idh; // Irradiation  
     char Igh_source, Idn_source, Idh_source;  
     int Igh_uncert, Idn_uncert, Idh_uncert;  
   
     MEAS(Lgh); /* Global horiz illuminance / (100 lux) */  
     MEAS(Ldn); /* Direct normal illuminance / (100 lux) */  
     MEAS(Ldh); /* Diffuse horiz illuminance / (100 lux) */  
     MEAS(Lz); /* Zenith illuminance / (10 Cd/m2) */  
   
     MEAS(covtot); /* Total sky cover / tenths */  
     MEAS(covopq); /* Opaque sky cover / tenths */  
   
     MEAS(T); /* temperature / (0.1degC) */  
     MEAS(Tdew); /* dew point temperature / (0.1degC) */  
     MEAS(p); /* pressure / mbar */  
     MEAS(rh); /* rel humidity / % */  
     MEAS(wdir); /* wind dir, N=0, E=90,... */  
     MEAS(wvel); /* wind speed / (m/s) */  
     MEAS(vis); /* visibility / (100m) */  
     MEAS(ch); /* ceiling height / m (or special value) */  
       
     MEAS(rain); /* preciptable water / mm */  
     MEAS(aer); /* aerosol optical depth in thousandths (???) */  
     MEAS(snow); /* snow depth on the specified day / cm (999=missing data) */  
     MEAS(dsno); /* days since last snow (or special value) */  
   
     /* weather observations from Appendix B */  
     int obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail;  
   
     /* brace yourself for this one... */  
   
     fscanf(d->f,  
         /* 1 */ "%2d%2d%2d%2d" "%4d%4d" "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d"  
         /* 2 */ "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d" "%4d%1s%1d"  
         /* 3 */ "%2d%1s%1d" "2d%1s%1d" "%4d%1s%1d" "%4d%1s%1d" "%3d%1s%1d" "%4d%1s%1d"  
         /* 4 */ "%3d%1s%1d" "%3d%1s%1d" "%4d%1s%ld" "%5d%1s%1d"  
         /* 5 */ "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d"  
         /* 6 */ "%3d%1s%1d" "%3d%1s%1d" "%3d%1s%1d" "%2d%1s%1d"  
   
     /* 1 */  
         ,year,month,day,hour  
         ,Iegh,Iedn  
         ,Igh,Igh_source,Igh_uncert /* I values in Wh/m2 */  
         ,Idn,Idn_source,Idn_uncert  
         ,Idh,Idh_source,Idh_uncert  
     /* 2 */  
         ,Lgh,Lgh_source,Lgh_uncert /* L values in kCd/m2 */  
         ,Ldn,Ldn_source,Ldn_uncert  
         ,Ldh,Ldh_source,Ldh_uncert  
         ,Lz,Lz_source,Lz_uncert  
     /* 3 */  
         ,covtot, covtot_source, covtot_uncert  
         ,covopq, covopq_source, covopq_uncert  
         ,T, T_source, T_uncert  
         ,Tdew, Tdew_source, Tdew_uncert  
         ,rh,rh_source, rh_uncert  
         ,p, p_source, p_uncert  
     /* 4 */  
         ,wdir, wdir_source, wdir_uncert  
         ,wvel, wvel_source, wvel_uncert  
         ,vis, vis_source, vis_uncert  
         ,ch, ch_source, ch_uncert  
     /* 5 */      
         ,obs, storm, precip, drizz, snowtype, snowshower, sleet, fog, smog, hail  
     /* 6 */  
         ,rain, rain_source, rain_uncert  
         ,aer, aer_source, aer_uncert  
         ,snow, snow_source, snow_uncert  
         ,dsno, dsno_source, dsno_uncert  
     );  
   
     /*  
         for the moment, we only record global horizontal, direct normal,  
         ambient temperature, wind speed.  
     */  
   
     struct Tmy2Point tmy;  
     tmy.t = double(year  
     tmy.G0 = float(Igh);  
     tmy,  
       
       
 }  
       

Legend:
Removed from v.810  
changed lines
  Added in v.811

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