gdrsclib
datastruct.h
Go to the documentation of this file.
1 /***************************************************
2 Apache License Version 2.0
3 
4 Copyright 2007-2011 EcoEquity and Stockholm Environment Institute
5 
6 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
7 file except in compliance with the License. You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software distributed under
12 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 KIND, either express or implied. See the License for the specific language governing
14 permissions and limitations under the License.
15 ****************************************************/
16 
21 #ifndef DATASTRUCT_H
22 #define DATASTRUCT_H
23 
24 /*
25  * This defines data structures for blocks of data arranged by record x year
26  * Each "record" might correspond, for example, to a country.
27  *
28  * The number of records must be the same for each data block, and so it
29  * is part of "app data". The number of years can vary by data block. However,
30  * there are "app"-wide minimum and maximum years. These are not used when
31  * reading data blocks, but might be used by functions that use these
32  * data structures.
33  *
34  */
35 
36 #define DBLOCK_OK 0
37 #define DBLOCK_FREADERROR 1
38 #define DBLOCK_FCLOSEERROR 2
39 #define DBLOCK_NOMEM 3
40 #define DBLOCK_NRECMISMATCH 4
41 #define DBLOCK_BADYEARS 5
42 
48 struct appdata {
49  int numrecs;
50  int minyear;
51  int maxyear;
52 };
53 
59 struct datablock {
60  int y_start;
61  int y_end;
62  double *data;
63 };
64 
70 typedef struct datablock* datablockp;
71 
78 
84 struct fixedfactor {
85  double *data;
86 };
87 
93 struct timeseries {
94  int y_start;
95  int y_end;
96  double *data;
97 };
98 
99 // Read in datablock from a formatted file--0 on success
100 int getdata(char *fname, struct datablock *dblock, struct appdata ad);
101 // A fixed factor has no time dimension
102 int getff(char *fname, struct fixedfactor *ff, char **name, int datacol, struct appdata ad);
103 // A timeseries has no records
104 int getts(char *fname, struct timeseries *ts, struct appdata ad);
105 // Get all values for a particular year from a datablock
106 void data2ff(struct datablock dblock, struct fixedfactor ff, int year, struct appdata ad);
107 // Put the values for a particular year into a datablock
108 void ff2data(struct fixedfactor ff, struct datablock dblock, int year, struct appdata ad);
109 // Note: must have sensible years defined to use this
110 int allocdata(struct datablock *dblock, struct appdata ad);
111 int allocff(struct fixedfactor *ff, struct appdata ad);
112 int allocts(struct timeseries *ts, struct appdata ad);
113 // Free the memory taken up by a datablock--0 on success
114 void cleardata(struct datablock dblock);
115 void clearff(struct fixedfactor ff);
116 void clearts(struct timeseries ts);
117 
118 // Output
119 int writedb2delim(struct datablock dblock, char *fname, char delim, struct appdata ad);
120 int writets2delim(struct timeseries ts, char * fname, char delim, struct appdata ad);
121 int writeff2delim(struct fixedfactor ff, char * fname, char delim, struct appdata ad);
122 
123 // Get the data value from the datablock for specified country (row) & year
124 // Return NAN if out of bounds
125 double getval(struct datablock db, int ctry, int year, struct appdata ad);
126 void setval(struct datablock db, double val, int row, int year, struct appdata ad);
127 
128 double gettsval(struct timeseries ts, int year, struct appdata ad);
129 void settsval(struct timeseries ts, double val, int year, struct appdata ad);
130 
131 double getffval(struct fixedfactor ff, int row, struct appdata ad);
132 void setffval(struct fixedfactor ff, double val, int row, struct appdata ad);
133 
134 #endif
For the application, basic dimensions: number of countries and bounding years.
Definition: datastruct.h:48
int writedb2delim(struct datablock dblock, char *fname, char delim, struct appdata ad)
Convenience function to dump a datablock to a file.
Definition: datastruct.c:337
int y_start
The start year.
Definition: datastruct.h:60
int allocdata(struct datablock *dblock, struct appdata ad)
Create a datablock in memory, using application-specific dimensions.
Definition: datastruct.c:223
void data2ff(struct datablock dblock, struct fixedfactor ff, int year, struct appdata ad)
Get all values for a particular year from a datablock.
Definition: datastruct.c:190
int maxyear
Final year for the dataset.
Definition: datastruct.h:51
void clearff(struct fixedfactor ff)
Free the memory taken up by a fixedfactor.
Definition: datastruct.c:314
double getval(struct datablock db, int ctry, int year, struct appdata ad)
Get the data value from the datablock for specified row & year.
Definition: datastruct.c:432
A one-dimensional array that contains information on countries that is the same for all years...
Definition: datastruct.h:84
int numrecs
Number of records (i.e., number of countries)
Definition: datastruct.h:49
int y_end
The end year.
Definition: datastruct.h:95
void setffval(struct fixedfactor ff, double val, int row, struct appdata ad)
Set a value for a specified row (country) for a fixedfactor.
Definition: datastruct.c:524
int allocts(struct timeseries *ts, struct appdata ad)
Create a timeseries in memory, of length = y_start - y_end + 1.
Definition: datastruct.c:278
double * data
The array, dimensioned (y_start - y_end + 1) * ad.numrecs when created.
Definition: datastruct.h:62
int y_end
The end year.
Definition: datastruct.h:61
int writeff2delim(struct fixedfactor ff, char *fname, char delim, struct appdata ad)
Convenience function to dump a fixedfactor to a file.
Definition: datastruct.c:403
void setval(struct datablock db, double val, int row, int year, struct appdata ad)
Set a specific value in a datablock, specified by row and year.
Definition: datastruct.c:454
void ff2data(struct fixedfactor ff, struct datablock dblock, int year, struct appdata ad)
Put the values for a particular year into a datablock.
Definition: datastruct.c:207
A one-dimensional array that contains global information that is the same for all countries...
Definition: datastruct.h:93
datablockp * datablockpp
Pointer to a datablock pointer: for building arrays of datablock pointers.
Definition: datastruct.h:77
int minyear
Earliest year for the dataset.
Definition: datastruct.h:50
int writets2delim(struct timeseries ts, char *fname, char delim, struct appdata ad)
Convenience function to dump a timeseries to a file.
Definition: datastruct.c:374
double * data
The array, length = ad.numrecs when created.
Definition: datastruct.h:85
int getff(char *fname, struct fixedfactor *ff, char **name, int datacol, struct appdata ad)
Read a fixedfactor from a formatted file.
Definition: datastruct.c:90
double * data
The data, length = y_start - y_end + 1 when created.
Definition: datastruct.h:96
void settsval(struct timeseries ts, double val, int year, struct appdata ad)
Set the value for a particular year in a timeseries.
Definition: datastruct.c:491
int getts(char *fname, struct timeseries *ts, struct appdata ad)
Read a timeseries from a formatted file.
Definition: datastruct.c:150
int allocff(struct fixedfactor *ff, struct appdata ad)
Create a fixedfactor in memory, of length = number of countries.
Definition: datastruct.c:254
int getdata(char *fname, struct datablock *dblock, struct appdata ad)
Read a datablock from a formatted file.
Definition: datastruct.c:40
double gettsval(struct timeseries ts, int year, struct appdata ad)
Get the value for a particular year from a timeseries.
Definition: datastruct.c:474
int y_start
The start year.
Definition: datastruct.h:94
struct datablock * datablockp
Pointer to a datablock.
Definition: datastruct.h:70
A two-dimensional array that contains information for country/year combinations.
Definition: datastruct.h:59
double getffval(struct fixedfactor ff, int row, struct appdata ad)
Get a value for a specified row (country) from a fixedfactor.
Definition: datastruct.c:507
void cleardata(struct datablock dblock)
Free the memory taken up by a datablock.
Definition: datastruct.c:305
void clearts(struct timeseries ts)
Free the memory taken up by a timeseries.
Definition: datastruct.c:323