gdrsclib
Functions
datastruct.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "datastruct.h"

Go to the source code of this file.

Functions

int getdata (char *fname, struct datablock *dblock, struct appdata ad)
 Read a datablock from a formatted file. More...
 
int getff (char *fname, struct fixedfactor *ff, char **name, int datacol, struct appdata ad)
 Read a fixedfactor from a formatted file. More...
 
int getts (char *fname, struct timeseries *ts, struct appdata ad)
 Read a timeseries from a formatted file. More...
 
void data2ff (struct datablock dblock, struct fixedfactor ff, int year, struct appdata ad)
 Get all values for a particular year from a datablock. More...
 
void ff2data (struct fixedfactor ff, struct datablock dblock, int year, struct appdata ad)
 Put the values for a particular year into a datablock. More...
 
int allocdata (struct datablock *dblock, struct appdata ad)
 Create a datablock in memory, using application-specific dimensions. More...
 
int allocff (struct fixedfactor *ff, struct appdata ad)
 Create a fixedfactor in memory, of length = number of countries. More...
 
int allocts (struct timeseries *ts, struct appdata ad)
 Create a timeseries in memory, of length = y_start - y_end + 1. More...
 
void cleardata (struct datablock dblock)
 Free the memory taken up by a datablock. More...
 
void clearff (struct fixedfactor ff)
 Free the memory taken up by a fixedfactor. More...
 
void clearts (struct timeseries ts)
 Free the memory taken up by a timeseries. More...
 
int writedb2delim (struct datablock dblock, char *fname, char delim, struct appdata ad)
 Convenience function to dump a datablock to a file. More...
 
int writets2delim (struct timeseries ts, char *fname, char delim, struct appdata ad)
 Convenience function to dump a timeseries to a file. More...
 
int writeff2delim (struct fixedfactor ff, char *fname, char delim, struct appdata ad)
 Convenience function to dump a fixedfactor to a file. More...
 
double getval (struct datablock db, int row, int year, struct appdata ad)
 Get the data value from the datablock for specified row & year. More...
 
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. More...
 
double gettsval (struct timeseries ts, int year, struct appdata ad)
 Get the value for a particular year from a timeseries. More...
 
void settsval (struct timeseries ts, double val, int year, struct appdata ad)
 Set the value for a particular year in a timeseries. More...
 
double getffval (struct fixedfactor ff, int row, struct appdata ad)
 Get a value for a specified row (country) from a fixedfactor. More...
 
void setffval (struct fixedfactor ff, double val, int row, struct appdata ad)
 Set a value for a specified row (country) for a fixedfactor. More...
 

Function Documentation

◆ allocdata()

int allocdata ( struct datablock dblock,
struct appdata  ad 
)

Create a datablock in memory, using application-specific dimensions.

Parameters
dblockDatablock to create
adappdata structure with dimensions
Returns
Success code Create a datablock of size numrecs * num years, filled with NaN

Definition at line 223 of file datastruct.c.

◆ allocff()

int allocff ( struct fixedfactor ff,
struct appdata  ad 
)

Create a fixedfactor in memory, of length = number of countries.

Parameters
ffFixed factor to allocate and fill
adappdata structure with dimensions
Returns
Success code Allocate space to ff and fill with NaN

Definition at line 254 of file datastruct.c.

◆ allocts()

int allocts ( struct timeseries ts,
struct appdata  ad 
)

Create a timeseries in memory, of length = y_start - y_end + 1.

Parameters
tsTime series to allocate and fill
adappdata structure with dimensions
Returns
Success code Allocate space to ts and fill with NaN

Definition at line 278 of file datastruct.c.

◆ cleardata()

void cleardata ( struct datablock  dblock)

Free the memory taken up by a datablock.

Parameters
dblockDatablock to free

Definition at line 305 of file datastruct.c.

◆ clearff()

void clearff ( struct fixedfactor  ff)

Free the memory taken up by a fixedfactor.

Parameters
ffFixed factor to free

Definition at line 314 of file datastruct.c.

◆ clearts()

void clearts ( struct timeseries  ts)

Free the memory taken up by a timeseries.

Parameters
tsTime series to free

Definition at line 323 of file datastruct.c.

◆ data2ff()

void data2ff ( struct datablock  dblock,
struct fixedfactor  ff,
int  year,
struct appdata  ad 
)

Get all values for a particular year from a datablock.

Parameters
dblockData block, 2-d data structure
ffFixed factor data structure
yearYear to pull data from dblock
adappdata structure with dimensions Fill in ff from one column of dblock

Definition at line 190 of file datastruct.c.

◆ ff2data()

void ff2data ( struct fixedfactor  ff,
struct datablock  dblock,
int  year,
struct appdata  ad 
)

Put the values for a particular year into a datablock.

Parameters
ffFixed factor (that is, values for all countries for one year) to insert
dblockDatablock for which values are inserted
yearYear where ff should be inserted into dblock
adappdata structure with dimensions Overwrite column for year=year of dblock

Definition at line 207 of file datastruct.c.

◆ getdata()

int getdata ( char *  fname,
struct datablock dblock,
struct appdata  ad 
)

Read a datablock from a formatted file.

This file provides functions for manipulating the array data structures used by the calculator.

Parameters
fnameName of the text file
dblockDatablock to read into
adappdata structure with dimensions
Returns
Success code
Note
Not currently used because now we read from an SQLite3 database

Definition at line 40 of file datastruct.c.

◆ getff()

int getff ( char *  fname,
struct fixedfactor ff,
char **  name,
int  datacol,
struct appdata  ad 
)

Read a fixedfactor from a formatted file.

Parameters
fnameName of the input file
ffFixed factor data structure
nameName of the data column to read in: reads from file
datacolIndex of the data column to read in
adappdata structure with dimensions
Returns
Success code
Note
Not currently used because now we read from an SQLite3 database

Definition at line 90 of file datastruct.c.

◆ getffval()

double getffval ( struct fixedfactor  ff,
int  row,
struct appdata  ad 
)

Get a value for a specified row (country) from a fixedfactor.

Parameters
ffFixed factor to read from
rowSpecified row (country index)
adappdata structure with dimensions
Returns
Value, or NaN if out of bounds

Definition at line 507 of file datastruct.c.

◆ getts()

int getts ( char *  fname,
struct timeseries ts,
struct appdata  ad 
)

Read a timeseries from a formatted file.

Parameters
fnameName of the input file
tsTime series data structure to fill in
adappdata structure with dimensions
Returns
Success code
Note
Not currently used because now we read from an SQLite3 database

Definition at line 150 of file datastruct.c.

◆ gettsval()

double gettsval ( struct timeseries  ts,
int  year,
struct appdata  ad 
)

Get the value for a particular year from a timeseries.

Parameters
tsTime series to read from
yearSpecified year
adappdata structure with dimensions
Returns
Value, or NaN if out of bounds

Definition at line 474 of file datastruct.c.

◆ getval()

double getval ( struct datablock  db,
int  row,
int  year,
struct appdata  ad 
)

Get the data value from the datablock for specified row & year.

Parameters
dbDatablock to get data from
rowSpecified row (country index)
yearSpecified year
adappdata structure with dimensions
Returns
Value, or NaN if out of bounds

Definition at line 432 of file datastruct.c.

◆ setffval()

void setffval ( struct fixedfactor  ff,
double  val,
int  row,
struct appdata  ad 
)

Set a value for a specified row (country) for a fixedfactor.

Parameters
ffFixed factor to write to
valValue to set
rowRow where value should be set
adappdata structure with dimensions Set value at row "row" in ff

Definition at line 524 of file datastruct.c.

◆ settsval()

void settsval ( struct timeseries  ts,
double  val,
int  year,
struct appdata  ad 
)

Set the value for a particular year in a timeseries.

Parameters
tsTime series to write to
valValue to set
yearYear for which value should be set
adappdata structure with dimensions Set value at year "year" in ts

Definition at line 491 of file datastruct.c.

◆ setval()

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.

Parameters
dbDatablock to write data to
valValue to write
rowSpecified row (country index)
yearSpecified year
adappdata structure with dimensions Set value at row, year in db

Definition at line 454 of file datastruct.c.

◆ writedb2delim()

int writedb2delim ( struct datablock  dblock,
char *  fname,
char  delim,
struct appdata  ad 
)

Convenience function to dump a datablock to a file.

Parameters
dblockDatablock to write
fnameFilename to write to
delimDelimiter to use
adappdata structure with dimensions
Returns
Success code Creates a file with contents of datablock

Definition at line 337 of file datastruct.c.

◆ writeff2delim()

int writeff2delim ( struct fixedfactor  ff,
char *  fname,
char  delim,
struct appdata  ad 
)

Convenience function to dump a fixedfactor to a file.

Parameters
ffFixed factor to write
fnameFilename to write to
delimDelimiter (currently ignored – just one column)
adappdata structure with dimensions
Returns
Success code Creates a file with contents of time series

Definition at line 403 of file datastruct.c.

◆ writets2delim()

int writets2delim ( struct timeseries  ts,
char *  fname,
char  delim,
struct appdata  ad 
)

Convenience function to dump a timeseries to a file.

Parameters
tsTime series to write
fnameFilename to write to
delimDelimiter to use
adappdata structure with dimensions
Returns
Success code Creates a file with contents of time series

Definition at line 374 of file datastruct.c.