43 int numrecs, row, col, ncol, retval;
46 if((fp = fopen(fname,
"r")) == NULL) {
51 fscanf(fp,
"%d%d%d", &dblock->
y_start, &dblock->
y_end, &numrecs);
63 for (row = 0; row < numrecs; row++) {
64 fscanf(fp,
"%s", iso3);
65 for (col = 0; col < ncol; col++) {
67 dblock->
data[row * ncol + col] = x;
92 char iso3[4], linebuff[8192], *namebuff;
93 int row, col, retval, ncols, numrecs;
96 if((fp = fopen(fname,
"r")) == NULL) {
101 fscanf(fp,
"%d%d", &ncols, &numrecs);
107 if ((retval =
allocff(ff, ad))) {
114 fgets(linebuff, 8192, fp);
115 namebuff = strtok(linebuff,
"\t");
116 for (col = 0; col < ncols; col++) {
117 namebuff = strtok(NULL,
"\t");
118 if (col == datacol) {
119 *name = strdup(namebuff);
124 for (row = 0; row < numrecs; row++) {
125 fscanf(fp,
"%s", iso3);
126 for (col = 0; col < ncols; col++) {
127 fscanf(fp,
"%f", &x);
128 if (col == datacol) {
152 int line, nlines, retval;
155 if((fp = fopen(fname,
"r")) == NULL) {
162 if ((retval =
allocts(ts, ad))) {
168 for (line = 0; line < nlines; line++) {
169 fscanf(fp,
"%f", &x);
193 for (i = 0; i < ad.
numrecs; i++) {
210 for (i = 0; i < ad.
numrecs; i++) {
224 int ncol, dblock_size, i;
233 dblock_size = ad.
numrecs * ncol;
234 dblock->
data = malloc(dblock_size *
sizeof(
double));
239 for (i = 0; i < dblock_size; i++) {
240 dblock->
data[i] = NAN;
263 for (i = 0; i < ad.
numrecs; i++) {
288 ts->
data = malloc(ncol *
sizeof(
double));
293 for (i = 0; i < ncol; i++) {
341 if((fp = fopen(fname,
"w")) == NULL) {
345 for (year = dblock.
y_start; year < dblock.
y_end; year++) {
346 fprintf(fp,
"%d%c", year, delim);
348 fprintf(fp,
"%d\n", year);
350 for (i = 0; i < ad.
numrecs; i++) {
351 for (year = dblock.
y_start; year < dblock.
y_end; year++) {
352 fprintf(fp,
"%f%c",
getval(dblock, i, year, ad), delim);
354 fprintf(fp,
"%f\n",
getval(dblock, i, year, ad));
378 if((fp = fopen(fname,
"w")) == NULL) {
383 fprintf(fp,
"%d%c%f\n", year, delim,
gettsval(ts, year, ad));
407 if((fp = fopen(fname,
"w")) == NULL) {
411 for (i = 0; i < ad.
numrecs; i++) {
412 fprintf(fp,
"%f\n", ff.
data[i]);
437 if (year < db.y_start || year > db.
y_end) {
459 if (year < db.y_start || year > db.
y_end) {
475 if (year < ts.y_start || year > ts.
y_end) {
492 if (year < ts.y_start || year > ts.
y_end) {
508 if ((row > ad.
numrecs - 1) || (row < 0)) {
525 if ((row > ad.
numrecs - 1) || (row < 0)) {
For the application, basic dimensions: number of countries and bounding years.
int y_start
The start year.
int getts(char *fname, struct timeseries *ts, struct appdata ad)
Read a timeseries from a formatted file.
void ff2data(struct fixedfactor ff, struct datablock dblock, int year, struct appdata ad)
Put the values for a particular year into a datablock.
int writets2delim(struct timeseries ts, char *fname, char delim, struct appdata ad)
Convenience function to dump a timeseries to a file.
A one-dimensional array that contains information on countries that is the same for all years...
int numrecs
Number of records (i.e., number of countries)
double * data
The array, dimensioned (y_start - y_end + 1) * ad.numrecs when created.
A one-dimensional array that contains global information that is the same for all countries...
void data2ff(struct datablock dblock, struct fixedfactor ff, int year, struct appdata ad)
Get all values for a particular year from a datablock.
double getffval(struct fixedfactor ff, int row, struct appdata ad)
Get a value for a specified row (country) from a fixedfactor.
void clearts(struct timeseries ts)
Free the memory taken up by a timeseries.
int getdata(char *fname, struct datablock *dblock, struct appdata ad)
Read a datablock from a formatted file.
double gettsval(struct timeseries ts, int year, struct appdata ad)
Get the value for a particular year from a timeseries.
int getff(char *fname, struct fixedfactor *ff, char **name, int datacol, struct appdata ad)
Read a fixedfactor from a formatted file.
void setffval(struct fixedfactor ff, double val, int row, struct appdata ad)
Set a value for a specified row (country) for a fixedfactor.
int allocdata(struct datablock *dblock, struct appdata ad)
Create a datablock in memory, using application-specific dimensions.
#define DBLOCK_NRECMISMATCH
#define DBLOCK_FREADERROR
void settsval(struct timeseries ts, double val, int year, struct appdata ad)
Set the value for a particular year in a timeseries.
void cleardata(struct datablock dblock)
Free the memory taken up by a datablock.
double getval(struct datablock db, int row, int year, struct appdata ad)
Get the data value from the datablock for specified row & year.
void clearff(struct fixedfactor ff)
Free the memory taken up by a fixedfactor.
int allocff(struct fixedfactor *ff, struct appdata ad)
Create a fixedfactor in memory, of length = number of countries.
double * data
The array, length = ad.numrecs when created.
int writedb2delim(struct datablock dblock, char *fname, char delim, struct appdata ad)
Convenience function to dump a datablock to a file.
int allocts(struct timeseries *ts, struct appdata ad)
Create a timeseries in memory, of length = y_start - y_end + 1.
double * data
The data, length = y_start - y_end + 1 when created.
#define DBLOCK_FCLOSEERROR
int y_start
The start year.
A two-dimensional array that contains information for country/year combinations.
int writeff2delim(struct fixedfactor ff, char *fname, char delim, struct appdata ad)
Convenience function to dump a fixedfactor to a file.
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.