Project

General

Profile

IFBeamCAPI

This document describes C API for IF Beam Data database

C API

Top level functions provide the beam data based on a bundle name or the combination of event name and variable name for the specified time or time interval. They are based on the Web Data Access C API (WDALIB) available from FIFE. All these functions return Dataset object, which is defined by WDALIB. WDALIB also includes functions to extract information from a Dataset. New version of the package provides the functions to extract information from a Dataset (see below) which aware about data structure. This is recommended way to extract the data.

/* 
 * Returns data for given bundle at given time
 */
Dataset getBundleForTime(const char *url, const char *bundle, const double t, int *error);

/*
 * Returns data for given bundle for given time interval
 */
Dataset getBundleForInterval(const char *url, const char *bundle, const double t0, const double t1, int *error);

/*
 * Returns value for given variable measured at given event at given time
 */
Dataset getEventVarForTime(const char *url, const char *event, const char *var, const double t, int *error);

/*
 * Returns value for given variable measured over given time interval
 */
Dataset getEventVarForInterval(const char *url, const char *event, const char *var, const double t0, const double t1, int *error);

typedef struct {
        long clock;             // Measurement epoch time
        char device[16];        // Device name
        char units[16];         // Units
        double value;           // Scalar device value && vector_size==0
        size_t vector_size;     // Number of elements in data array
        double vector[0];       // Vector device values && vector_size > 0
} *Measurement;

/*
 * Functions to work with the tuples in the dataset
 */
int getNmeasurements(Dataset dataset);              /* Returns number of measurements in the dataset     */

Measurement getMeasurement(Dataset dataset, int i); /* Returns NULL if out of range                     */

Measurement getFirstMeasurement(Dataset dataset);   /* Returns the first tuple in the dataset           */

Measurement getNextMeasurement(Dataset dataset);    /* Returns the next tuple if available              */

Complete sample code

#include <stdio.h>
#include <string.h>
#include <time.h>

#include "ifbeam_c.h" 

/*
 * Note: This code uses wda module. In order to compile it needs files wda.h and wda.o to be present.
 */

int main(void) 
{
    int i, j, k;
    int err;

    const char *url = "http://dbweb0.fnal.gov:8088/ifbeam/data";

    Dataset ds;
    Tuple tu;
    Measurement me;

    int len;
    int error;
    char ss[81920];
    double dd[4096];

    time_t t0 = time(NULL);
    ds = getBundleForInterval(url, "NuMI_Physics_1Hz", t0-320, t0-300, &error);     // Get the data for bundle

    if (error) {                                // Check for curl library errors
        fprintf(stderr, "error code=%d\n", error);    perror("error message");
    }
    if (getHTTPstatus(ds) != 200) {                        // Check for HTTP error
        fprintf(stderr, "HTTP code=%ld, message: '%s'\n", getHTTPstatus(ds), getHTTPmessage(ds));
    }

    fprintf(stderr, "\nntuples=%d\n\n", getNtuples(ds));            // Get the number of rows in the dataset

//==============================================    

    tu = getFirstTuple(ds);                                         // Get the very first row - contains the column names
    // If we get the names print them
    if (tu != NULL) {                                               // If everything is OK
        int nc = getNfields(tu);                                    // Get the number of columns in this row
        fprintf(stderr, "ncols=%d\n", nc);

        for (i = 0; i < nc; i++) {                                  // Loop to get column data as a string
            len = getStringValue(tu, i, ss, sizeof (ss), &err);     // Returns string length
            fprintf(stderr, "[%d]: l=%d, s='%s'\n", i, len, ss);    // Print the results
        }
        fprintf(stderr, "e=%s\n\n", strerror(err));                 // Was it OK?
    } 

//==============================================    

    tu = getTuple(ds, 1);                                           // Get the row with double array 

    if (tu != NULL) {                                               // If everything is OK
        int nc = getNfields(tu);                                    // Get the number of columns in this row
        fprintf(stderr, "ncols=%d\n", nc);

        // Get the first double
        fprintf(stderr, "[3]: v=%f\n", getDoubleValue(tu, 3, &err));// Now extract the double value from the 3-rd column 
        fprintf(stderr, "e=%s\n\n", strerror(err));                 // Was it OK?

        // Get double array
        len = getDoubleArray(tu, 3, dd, 4096, &err);                // Now extract the double array from the 3-rd column
        fprintf(stderr, "len=%d\n", len);                           // Print the length

        for (i = 0; i < len; i++) {                                 // Print the results
            fprintf(stderr, "%f, ", dd[i]);
        }
        fprintf(stderr, "\n");

        fprintf(stderr, "e=%s\n\n", strerror(err));                   // Was it OK?
    } else {
        fprintf(stderr, "No such tuple\n"); perror("...");
    }

//==============================================    

    me = getMeasurement(ds, 0);

    if (me != NULL) {
        fprintf(stderr, "clock=%ld\n", me->clock);
        fprintf(stderr, "device=%s\n", me->device);
        fprintf(stderr, "units=%s\n", me->units);
        fprintf(stderr, "vector_size=%d\n", me->vector_size);
        if (me->vector_size > 0) {
            len = me->vector_size;
            for (i = 0; i < len; i++) {                                 // Print the results
                fprintf(stderr, "%f, ", dd[i]);
            }
            fprintf(stderr, "\n\n");
        } else if (me->vector_size == 0) {
            fprintf(stderr, "value=%f\n\n", me->value);
        } else {
            perror("Something is wrong with the measurement");
        }
    } else {
        perror("No such measurement");
    }

    releaseDataset(ds);                                             // Release dataset to prevent memory leak!

//==============================================    

    ds = getEventVarForInterval(url, "e,8f", "E:HMGPR", t0-305, t0-300, &error);     // Get the data for the variable in event
    fprintf(stderr, "\nntuples=%d\n\n", getNtuples(ds));    // Get the number of rows in the dataset

    tu = getFirstTuple(ds);                                 // Get the very first row - contains the column names
    // If we get the names print them
    if (tu != NULL) {                                               // If everything is OK
        int nc = getNfields(tu);                                    // Get the number of columns in this row
        fprintf(stderr, "ncols=%d\n", nc);

        for (i = 0; i < nc; i++) {                                  // Loop to get column data as a string
            len = getStringValue(tu, i, ss, sizeof (ss), &err);     // Returns string length
            fprintf(stderr, "[%d]: l=%d, s='%s'\n", i, len, ss);    // Print the results
        }
        fprintf(stderr, "e=%s\n\n", strerror(err));                 // Was it OK?
    } 

//==============================================    

    tu = getNextTuple(ds);                                          // Get the next row

    if (tu != NULL) {                                               // If everything is OK
        int nc = getNfields(tu);                                    // Get the number of columns in this row
        fprintf(stderr, "ncols=%d\n", nc);

        for (i = 0; i < nc; i++) {                                  // Loop to get column data as a string
            len = getStringValue(tu, i, ss, sizeof (ss), &err);     // Returns string length
            fprintf(stderr, "[%d]: l=%d, s='%s'\n", i, len, ss);    // Print the results
        }
        // Get double
        fprintf(stderr, "[4]: v=%f\n", getDoubleValue(tu, 4, &err));// Now extract the double value from the 4-th column
        fprintf(stderr, "e=%s\n\n", strerror(err));                 // Was it OK?
    } else {
        fprintf(stderr, "No such tuple\n"); perror("...");
    }

    releaseDataset(ds);                                             // Release dataset to prevent memory leak!

    return 0;
}

Downloads

Download IFBeam Data C API

The IFBeam C API uses lower level WDALIB module v2.8 - you need to download this too !