Oyster Harvest Area Closure Analysis 0.1
Prediction and analysis of NSW oyster harvest area closures.
Loading...
Searching...
No Matches
timeseries.h File Reference
#include <curl/curl.h>
#include <cjson/cJSON.h>
#include <math.h>
#include <time.h>
#include <libpq-fe.h>
#include <log.h>
#include "IBM_EIS/authenticate.h"
#include "transform.h"
#include "http.h"
#include "utils.h"
Include dependency graph for timeseries.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  IBM_TimeseriesReq_TypeDef
 Request structure for IBM EMS timeseries data. More...
 
struct  IBM_TimeseriesDataset_TypeDef
 Request response data containing IBM EMS timeseries data. More...
 

Macros

#define IBM_URL_SIZE   250
 Max characters in formulated URL. More...
 
#define IBM_PRECIPITATION_ID   49097
 Define layer identifier (daily precip, max temp, min temp) More...
 
#define IBM_MIN_TEMPERATURE_ID   26019
 
#define IBM_MAX_TEMPERATURE_ID   26018
 
#define IBM_MAX_RESPONSE_LENGTH   2000
 Max number of values in response. More...
 

Functions

CURLcode IBM_GetTimeseries (IBM_AuthHandle_TypeDef *auth_handle, IBM_TimeseriesReq_TypeDef *request, IBM_TimeseriesDataset_TypeDef *dataset, uint8_t alt_flag)
 Get timeseries data from IBM EMS. More...
 
int8_t IBM_TimeseriesToCSV (IBM_TimeseriesReq_TypeDef *request, IBM_TimeseriesDataset_TypeDef *dataset)
 Write timeseries dataset to .csv file. More...
 
IBM_TimeseriesDataset_TypeDef IBM_TimeseriesFromCSV (const char *filename)
 Load IBM timeseries dataset from local .csv file. More...
 
void IBM_TimeseriesToDB (IBM_TimeseriesReq_TypeDef *req_info, IBM_TimeseriesDataset_TypeDef *dataset, T_LocationLookup_TypeDef *location, PGconn *psql_conn)
 Write timeseries dataset to PostgreSQL table. More...
 
void IBM_BuildTSDatabase (T_LocationsLookup_TypeDef *locations, const char *start_time, const char *end_time, PGconn *psql_conn)
 

Variables

static const char * IBM_REQUEST_URL = "https://pairs.res.ibm.com"
 IBM URL. More...
 
static const char * IBM_ALT_REQUEST_URL = "https://ibmpairs-mvp2-api.mybluemix.net"
 Alternative IBM URL. More...
 

Macro Definition Documentation

◆ IBM_MAX_RESPONSE_LENGTH

#define IBM_MAX_RESPONSE_LENGTH   2000

Max number of values in response.

◆ IBM_MAX_TEMPERATURE_ID

#define IBM_MAX_TEMPERATURE_ID   26018

◆ IBM_MIN_TEMPERATURE_ID

#define IBM_MIN_TEMPERATURE_ID   26019

◆ IBM_PRECIPITATION_ID

#define IBM_PRECIPITATION_ID   49097

Define layer identifier (daily precip, max temp, min temp)

◆ IBM_URL_SIZE

#define IBM_URL_SIZE   250

Max characters in formulated URL.

Function Documentation

◆ IBM_BuildTSDatabase()

void IBM_BuildTSDatabase ( T_LocationsLookup_TypeDef locations,
const char *  start_time,
const char *  end_time,
PGconn *  psql_conn 
)

◆ IBM_GetTimeseries()

CURLcode IBM_GetTimeseries ( IBM_AuthHandle_TypeDef auth_handle,
IBM_TimeseriesReq_TypeDef request,
IBM_TimeseriesDataset_TypeDef dataset,
uint8_t  alt_flag 
)

Get timeseries data from IBM EMS.

IBM EIS get timeseries data as a JSON response.

IBM EIS provides two endpoints to get data from:

The responses from these requests have different formats therefore they need to be treated seperatly. Luckly similar data is used for each request making the use of TimeseriesReq_TypeDef possible for both requests.

TimeseriesReq_TypeDef ts = {
.layer_id = 16700, // 16700 (alt_flag = 0) or 49097 (alt_flag = 1)
.latitude = -35.69701049568654,
.longitude = 150.1546566614602,
.start = 1654005600,
.end = 1654783200
};
TimeseriesDataset_TypeDef dataset;
IBM_GetTimeseries(auth_handle, &ts, &dataset, 0); // or 1 (for alt URL)
CURLcode IBM_GetTimeseries(IBM_AuthHandle_TypeDef *auth_handle, IBM_TimeseriesReq_TypeDef *request, IBM_TimeseriesDataset_TypeDef *dataset, uint8_t alt_flag)
Get timeseries data from IBM EMS.
Definition: timeseries.c:50
Note
Start and end times are represented as UNIX timestamps (in seconds).
Parameters
auth_handleIBM authentication handler
requestRequest struct with corresponding data.
datasetThe dataset to populate.
alt_flagA flag representing if the alt enpoint should be used.
Returns
Curl success code.

◆ IBM_TimeseriesFromCSV()

IBM_TimeseriesDataset_TypeDef IBM_TimeseriesFromCSV ( const char *  filename)

Load IBM timeseries dataset from local .csv file.

Load timeseries dataset from a cached .csv for .txt file.

Requires the file to be formatted as Unix;Date;Value

Parameters
filenameFilename of file to open, including path.
Returns
Dataset containing timeseries data

◆ IBM_TimeseriesToCSV()

int8_t IBM_TimeseriesToCSV ( IBM_TimeseriesReq_TypeDef request,
IBM_TimeseriesDataset_TypeDef dataset 
)

Write timeseries dataset to .csv file.

IBM timeseires dataset to csv.

Creates required directories and then builds a csv files with the downloaded dataset.

Parameters
requestTimeseries request information (layer ID is needed).
datasetDataset to write to file. Count is provided here too.
Returns
Error code. 0 = OK ... 1 = ERROR

◆ IBM_TimeseriesToDB()

void IBM_TimeseriesToDB ( IBM_TimeseriesReq_TypeDef req_info,
IBM_TimeseriesDataset_TypeDef dataset,
T_LocationLookup_TypeDef location,
PGconn *  psql_conn 
)

Write timeseries dataset to PostgreSQL table.

Insert an IBM dataset into a PostgreSQL table.

This function handles precipitation, min and max temperatures. Several other datasets are required to give context regarding the IBM datasets location and relevance.

Parameters
req_infoRequest information provided to IBM.
datasetDataset to insert into table.
locationLocation information from PostgreSQL lookup table.
psql_connPostgreSQL database connection handler.

Variable Documentation

◆ IBM_ALT_REQUEST_URL

const char* IBM_ALT_REQUEST_URL = "https://ibmpairs-mvp2-api.mybluemix.net"
static

Alternative IBM URL.

◆ IBM_REQUEST_URL

const char* IBM_REQUEST_URL = "https://pairs.res.ibm.com"
static

IBM URL.