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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 811 - (hide annotations) (download) (as text)
Thu Aug 10 17:08:16 2006 UTC (14 years, 1 month ago) by johnpye
File MIME type: text/x-csrc
File size: 4040 byte(s)
Added vim 'syntax=python' comments to many SConscript files.
Lots more work on the datareader extfn.
The testtmy.a4c file requires the file obtained here ('gzip -d' before use)
http://rredc.nrel.gov/solar/old_data/nsrdb/tmy2/unix/23161.tm2.Z
1 johnpye 811 /* ASCEND modelling environment
2     Copyright (C) 2006 Carnegie Mellon University
3 johnpye 801
4 johnpye 811 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 johnpye 801
9 johnpye 811 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 johnpye 801
14 johnpye 811 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 johnpye 801 */
19    
20 johnpye 811 #include "dr.h"
21     #include "tmy.h"
22 johnpye 801
23 johnpye 811 #include <utilities/config.h>
24     #include <general/ospath.h>
25     #include <utilities/ascMalloc.h>
26     #include <utilities/error.h>
27     #include <utilities/ascPanic.h>
28    
29 johnpye 809 /*------------------------------------------------------------------------------
30     FORWARD DECLARATIONS
31     */
32    
33    
34     /*------------------------------------------------------------------------------
35     API FUNCTIONS
36     */
37    
38 johnpye 801 /**
39     Create a data reader object, with the filename specified. The filename
40     will be searched for in a specified path, eg ASCENDLIBRARY.
41     */
42 johnpye 811 DataReader *datareader_new(const char *fn){
43 johnpye 801 DataReader *d;
44    
45     d = ASC_NEW(DataReader);
46     d->fn = fn;
47     d->fp = NULL;
48 johnpye 809 d->f = NULL;
49 johnpye 801 d->noutputs = 0;
50    
51     return d;
52     }
53    
54     /**
55 johnpye 809 Set data file format
56     @return 0 on success
57     */
58 johnpye 811 int datareader_set_file_format(DataReader *d, const DataReaderFileFormat format){
59 johnpye 809 switch(format){
60     case DATAREADER_FORMAT_TMY2:
61     d->headerfn=&datareader_tmy2_header;
62 johnpye 811 d->datafn=&datareader_tmy2_data;
63 johnpye 809 break;
64     default:
65     ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unknown file format specified");
66     return 1;
67     }
68     return 0;
69     }
70    
71 johnpye 801 /**
72     Initialise the datareader: open the file, check the number of columns, etc.
73     @return 0 on success
74     */
75     int datareader_init(DataReader *d){
76     d->fp = ospath_new(d->fn);
77     if(d->fp==NULL){
78     ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid filepath");
79     return 1;
80     }
81 johnpye 809
82 johnpye 811 d->f = ospath_fopen(d->fp,"r");
83     if(d->f == NULL){
84     ERROR_REPORTER_HERE(ASC_USER_ERROR,"Unable to open file '%s' for reading",d->fn);
85 johnpye 809 return 1;
86     }
87    
88 johnpye 811 if((*d->headerfn)(d)){
89 johnpye 809 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Error processing file header in '%s'",d->fn);
90 johnpye 811 fclose(d->f);
91 johnpye 809 return 1;
92     }
93    
94 johnpye 811 while(! (*d->eoffn)(d)){
95     if((*d->datafn)(d)){
96     ERROR_REPORTER_HERE(ASC_PROG_ERR,"Error reading file data in '%s'",d->fn);
97     fclose(d->f);
98     return 1;
99     }
100 johnpye 809 }
101     fclose(d->f);
102    
103     return 0;
104 johnpye 801 }
105    
106     /**
107     Shut down the data reader and deallocate any memory owned by it, then
108     free the memory at d.
109     */
110     int datareader_delete(DataReader *d){
111     if(d->fp){
112     ospath_free(d->fp);
113     d->fp = NULL;
114     }
115     if(d->f){
116     fclose(d->f);
117     d->f = NULL;
118     }
119     ASC_FREE(d);
120     return 0;
121     }
122    
123     /**
124     Return the number of inputs (independent variables) supplied in the
125     DataReader's current file. Can only be 1 at this stage.
126     */
127     int datareader_num_inputs(const DataReader *d){
128     return 1;
129     }
130    
131     /**
132     Return the number of outputs (dependent variables) found in the DataReader's
133     current file. Should be one or more.
134     */
135     int datareader_num_outputs(const DataReader *d){
136     return d->noutputs;
137 johnpye 811 }
138 johnpye 801
139 johnpye 809
140 johnpye 801 /**
141     Return an interpolated set of output values for the given input values.
142     This should be computed such that the output values are smooth in their
143     first derivatives.
144 johnpye 809
145     The required memory for the inputs and outputs must be allocated by the
146     caller, and indicated by the pointers 'inputs' and 'outputs'.
147    
148 johnpye 801 @see datareader_deriv
149     @TODO implement this
150     */
151     int datareader_func(DataReader *d, double *inputs, double *outputs){
152 johnpye 809 int i;
153     double t;
154    
155     t = inputs[0];
156 johnpye 811 i = 0;
157    
158     CONSOLE_DEBUG("Not implemented");
159     return 1;
160 johnpye 801 }
161    
162     /**
163     Return an interpolated set of output derivatives for the given input
164     values. These should be smooth.
165     @see datareader_func
166     @TODO implement this
167     */
168     int datareader_deriv(DataReader *d, double *inputs, double *jacobian){
169     CONSOLE_DEBUG("Not implemented");
170     return 1;
171     }
172    
173 johnpye 809

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