Commit 47f4465e authored by John Douglas Veitch's avatar John Douglas Veitch 💬
Browse files

Added XML Serialisation of LALInferenceVariable first support

Original: 33cd6a0c0d4c28952fcce72f503ceee298ec4294
parent 7b003d07
......@@ -78,6 +78,7 @@ LALSUITE_ENABLE_LALFRAME
LALSUITE_ENABLE_LALMETAIO
LALSUITE_ENABLE_LALINSPIRAL
LALSUITE_ENABLE_LALPULSAR
LALSUITE_ENABLE_LALXML
# check for c compiler
m4_pattern_allow([AC_PROG_CC_C99])
......@@ -132,6 +133,9 @@ LALSUITE_CHECK_LIB([LALFrame],[1.0.2.1],[LALFrOpen],[lal/LALFrameL.h])
LALSUITE_CHECK_LIB([LALInspiral],[1.1.0.2],[LALInspiralParameterCalc],[lal/LALInspiral.h])
LALSUITE_CHECK_LIB([LALPulsar],[1.1.0.2],[XLALComputeFaFb],[lal/ComputeFstat.h])
# check for lalxml
LALSUITE_CHECK_OPT_LIB([LALXML],[1.0.0.2],[XLALXMLFilePrintElements],[lal/LALXML.h])
# add gcc specific flags
if test "$GCC" = yes; then
CFLAGS="$CFLAGS $lal_gcc_flags"
......
......@@ -299,9 +299,53 @@ void LALInferenceCopyVariables(LALInferenceVariables *origin, LALInferenceVariab
return;
}
/** Prints a variable item to a string (must be pre-allocated!) */
void LALInferencePrintVariableItem(char *out, LALInferenceVariableItem *ptr)
{
if(ptr==NULL) {
XLALPrintError("Null LALInferenceVariableItem *");
XLAL_ERROR_VOID("LALInferencePrintVariableItem",XLAL_EFAULT);
}
if(out==NULL) {
XLALPrintError("Null output string *");
XLAL_ERROR_VOID("LALInferencePrintVariableItem",XLAL_EFAULT);
}
switch (ptr->type) {
case LALINFERENCE_INT4_t:
sprintf(out, "%d", *(INT4 *) ptr->value);
break;
case LALINFERENCE_INT8_t:
sprintf(out, "%" LAL_INT8_FORMAT, *(INT8 *) ptr->value);
break;
case LALINFERENCE_UINT4_t:
sprintf(out, "%ud", *(UINT4 *) ptr->value);
break;
case LALINFERENCE_REAL4_t:
sprintf(out, "%.15lf", *(REAL4 *) ptr->value);
break;
case LALINFERENCE_REAL8_t:
sprintf(out, "%.15lf", *(REAL8 *) ptr->value);
break;
case LALINFERENCE_COMPLEX8_t:
sprintf(out, "%e + i*%e",
(REAL4) ((COMPLEX8 *) ptr->value)->re, (REAL4) ((COMPLEX8 *) ptr->value)->im);
break;
case LALINFERENCE_COMPLEX16_t:
sprintf(out, "%e + i*%e",
(REAL8) ((COMPLEX16 *) ptr->value)->re, (REAL8) ((COMPLEX16 *) ptr->value)->im);
break;
case LALINFERENCE_gslMatrix_t:
sprintf(out, "<can't print matrix>");
break;
default:
sprintf(out, "<can't print>");
}
return;
}
void LALInferencePrintVariables(LALInferenceVariables *var)
/* output contents of a 'LALInferenceVariables' structure */
/* (by now only prints names and types, but no values) */
/** output contents of a 'LALInferenceVariables' structure * /
/ * (by now only prints names and types, but no values) */
{
LALInferenceVariableItem *ptr = var->head;
fprintf(stdout, "LALInferenceVariables:\n");
......
......@@ -35,6 +35,7 @@
# include <stdlib.h>
#define VARNAME_MAX 128
#define VARVALSTRINGSIZE_MAX 128
# include <lal/LALStdlib.h>
# include <lal/LALConstants.h>
......@@ -152,6 +153,10 @@ char **LALInferenceGetHeaderLine(FILE *inp);
const char *LALInferenceTranslateInternalToExternalParamName(const char *inName);
INT4 LALInferenceFprintParameterNonFixedHeaders(FILE *out, LALInferenceVariables *params);
/** Prints a variable item to a string (must be pre-allocated!) */
void LALInferencePrintVariableItem(char *out, LALInferenceVariableItem *ptr);
/** Return a pointer to the variable asked for */
void *LALInferenceGetVariable(LALInferenceVariables * vars, const char * name);
/** Get number of dimensions in this variable */
......
/*
* Copyright (C) 2011 John Veitch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with with program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/**
* \file
* \ingroup XML
* \brief Implementation of the VOTable serializers XML LALInference API
*/
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <lal/XLALError.h>
#include <lal/LALXML.h>
#include <lal/LALXMLVOTableCommon.h>
#include <lal/LALXMLVOTableSerializers.h>
#include <lal/LALInferenceXML.h>
#define INT4STR_MAXLEN 15
#define REAL8STR_MAXLEN 25
#define NAMESTR_MAXLEN 256
/**
* \brief Serializes an array of \c LALInferenceVariables into a VOTable XML %node
*
* This function takes a \c LALInferenceVariables structure and serializes it into a VOTable
* \c RESOURCE %node identified by the given name. The returned \c xmlNode can then be
* embedded into an existing %node hierarchy or turned into a full VOTable document.
* A VOTable Table element is returned, with fixed variables as PARAMs and the varying ones as FIELDs.
*
* \param varsArray [in] Pointer to an array of \c LALInferenceVariables structures to be serialized
* \param N [in] Number of items in the array
*
* \return A pointer to a \c xmlNode that holds the VOTable fragment that represents
* the \c LALInferenceVariables array.
* In case of an error, a null-pointer is returned.\n
* \b Important: the caller is responsible to free the allocated memory (when the
* fragment isn't needed anymore) using \c xmlFreeNode. Alternatively, \c xmlFreeDoc
* can be used later on when the returned fragment has been embedded in a XML document.
*
* \sa XLALCreateVOTParamNode
* \sa XLALCreateVOTResourceNode
* \sa XLALCreateVOTDocumentFromTree
*
* \author John Veitch\n
*
*/
xmlNodePtr XLALInferenceVariablesArray2VOTTable(const LALInferenceVariables **varsArray, UINT4 N)
{
}
/**
* \brief Serializes a \c LALInferenceVariables structure into a VOTable XML %node
*
* This function takes a \c LALInferenceVariables structure and serializes it into a VOTable
* \c RESOURCE %node identified by the given name. The returned \c xmlNode can then be
* embedded into an existing %node hierarchy or turned into a full VOTable document.
*
* \param vars [in] Pointer to the \c LALInferenceVariables structure to be serialized
* \param name [in] Unique identifier of this particular \c LALInferenceVariables structure instance
*
* \return A pointer to a \c xmlNode that holds the VOTable fragment that represents
* the \c LALInferenceVariables structure.
* In case of an error, a null-pointer is returned.\n
* \b Important: the caller is responsible to free the allocated memory (when the
* fragment isn't needed anymore) using \c xmlFreeNode. Alternatively, \c xmlFreeDoc
* can be used later on when the returned fragment has been embedded in a XML document.
*
* \sa LALInferenceVariableItem2VOTParamNode
*
* \author John Veitch\n
*
*/
xmlNodePtr XLALInferenceVariables2VOTNode (const LALInferenceVariables *const vars,const char *name)
{
/* set up local variables */
const char *fn = __func__;
xmlNodePtr xmlChildNodeList = NULL;
LALInferenceVariableItem *marker=vars->head;
xmlNodePtr *xmlChildNodePtr=&xmlChildNodeList;
/* Walk through the LALInferenceVariables adding each one */
while(marker){
*xmlChildNodePtr = (LALInferenceVariableItem2VOTParamNode(marker));
if(!*xmlChildNodePtr) {
/* clean up */
xmlFreeNodeList(xmlChildNodeList);
XLALPrintError("Couldn't create PARAM node: %s.%s\n", name,marker->name);
XLAL_ERROR_NULL(fn, XLAL_EFAILED);
}
marker=marker->next;
xmlChildNodePtr = &((*xmlChildNodePtr)->next);
}
return(xmlChildNodeList);
}
/**
* \brief Serializes a \c LALInferenceVariableItem structure into a VOTable XML %node
*
* This function takes a \c LALInferenceVariableItem structure and serializes it into a VOTable
* \c RESOURCE %node identified by the given name. The returned \c xmlNode can then be
* embedded into an existing %node hierarchy or turned into a full VOTable document.
*
* \param varitem [in] Pointer to the \c LALInferenceVariables structure to be serialized
*
* \return A pointer to a \c xmlNode that holds the VOTable fragment that represents
* the \c LALInferenceVariableItem structure.
* In case of an error, a null-pointer is returned.\n
* \b Important: the caller is responsible to free the allocated memory (when the
* fragment isn't needed anymore) using \c xmlFreeNode. Alternatively, \c xmlFreeDoc
* can be used later on when the returned fragment has been embedded in a XML document.
*
* \sa XLALCreateVOTParamNode
*
* \author John Veitch\n
*
*/
xmlNodePtr LALInferenceVariableItem2VOTParamNode(LALInferenceVariableItem *varitem)
{
VOTABLE_DATATYPE vo_type;
CHAR *unitName={0};
CHAR valString[VARVALSTRINGSIZE_MAX]="";
/* Special case for matrix */
if(varitem->type==LALINFERENCE_gslMatrix_t)
return(XLALgsl_matrix2VOTNode((gsl_matrix *)varitem->value, varitem->name, unitName));
/* Check the type of the item */
vo_type=LALInferenceVariableType2VOT(varitem->type);
LALInferencePrintVariableItem(valString, varitem);
return(XLALCreateVOTParamNode(varitem->name,unitName,vo_type,NULL,valString));
}
/**
* \brief Convert a \c LALInferenceVariableType into a VOType
*/
VOTABLE_DATATYPE LALInferenceVariableType2VOT(const LALInferenceVariableType litype){
switch(litype){
case LALINFERENCE_INT4_t: return VOT_INT4;
case LALINFERENCE_INT8_t: return VOT_INT8;
case LALINFERENCE_UINT4_t: return VOT_INT8; /* Need a signed INT8 to store an unsigned UINT4 */
case LALINFERENCE_REAL4_t: return VOT_REAL4;
case LALINFERENCE_REAL8_t: return VOT_REAL8;
case LALINFERENCE_COMPLEX8_t: return VOT_COMPLEX8;
case LALINFERENCE_COMPLEX16_t: return VOT_COMPLEX16;
case LALINFERENCE_string_t: return VOT_CHAR;
default: {XLALPrintError("Unsupported LALInferenceVarableType"); return VOT_DATATYPE_LAST;}
}
}
\ No newline at end of file
/*
* Copyright (C) 2011 John Veitch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with with program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/**
* \file
* \ingroup XML
* \brief Header file declaring the public VOTable serializers XML LALInference API
*/
/* Double-include protection */
#ifndef _LALXMLVOTABLESERIALIZERLALINFERENCE_H
#define _LALXMLVOTABLESERIALIZERSLALINFERENCE_H
/* C++ protection */
#ifdef __cplusplus
extern "C" {
#endif
#include <libxml/tree.h>
#include <lal/LALXML.h>
#include <lal/LALXML.h>
#include <lal/LALXMLVOTableCommon.h>
#include <lal/LALXMLVOTableSerializers.h>
#include <lal/LALDatatypes.h>
#include <lal/LALInference.h>
xmlNodePtr XLALInferenceVariablesArray2VOTTable(const LALInferenceVariables **varsArray, UINT4 N);
xmlNodePtr XLALInferenceVariables2VOTNode(const LALInferenceVariables *const vars, const char *name);
INT4 XLALVOTDoc2LALInferenceVariablesByName(const xmlDocPtr xmlDocument, const char *name, LALInferenceVariables *bop);
xmlNodePtr LALInferenceVariableItem2VOTParamNode(LALInferenceVariableItem *varitem);
VOTABLE_DATATYPE LALInferenceVariableType2VOT(const LALInferenceVariableType litype);
/* C++ protection */
#ifdef __cplusplus
}
#endif
/* Double-include protection */
#endif
INCLUDES = -I$(top_builddir)/include
lalinferenceincludedir = $(includedir)/lal
lalinferenceinclude_HEADERS = \
HDR= \
LALInference.h \
LALInferenceConfig.h \
LALInferenceLikelihood.h \
......@@ -11,8 +12,10 @@ lalinferenceinclude_HEADERS = \
LALInferenceTemplate.h \
LALInferenceVCSInfo.h
lib_LTLIBRARIES = liblalinference.la
liblalinference_la_SOURCES = \
SRC= \
LALInference.c \
LALInferenceLikelihood.c \
LALInferenceNestedSampler.c \
......@@ -21,6 +24,15 @@ liblalinference_la_SOURCES = \
LALInferenceTemplate.c \
LALInferenceVCSInfo.c
if LALXML
XMLHDR = LALInferenceXML.h
XMLSRC = LALInferenceXML.c
endif
lalinferenceinclude_HEADERS = $(HDR) $(XMLHDR)
liblalinference_la_SOURCES = $(SRC) $(XMLSRC)
liblalinference_la_LDFLAGS = -version-info $(LIBVERSION)
CLEANFILES = \
......
......@@ -111,6 +111,7 @@ LALInferenceRunState *initialize(ProcessParamsTable *commandLine)
unsigned long int randomseed;
struct timeval tv;
FILE *devrandom;
int tmp;
irs = calloc(1, sizeof(LALInferenceRunState));
/* read data from files: */
......@@ -194,7 +195,7 @@ LALInferenceRunState *initialize(ProcessParamsTable *commandLine)
randomseed = tv.tv_sec + tv.tv_usec;
}
else {
fread(&randomseed, sizeof(randomseed), 1, devrandom);
tmp=fread(&randomseed, sizeof(randomseed), 1, devrandom);
fclose(devrandom);
}
}
......
#include <lal/LALInference.h>
#include <lal/LALInferenceXML.h>
#include <lal/LALXML.h>
#include <lal/LALXMLVOTableCommon.h>
#include <lal/LALXMLVOTableSerializers.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
int testLALInferenceVariables(void);
int testLALInferenceVariables(void){
LALInferenceVariables var;
char *xmlString = NULL;
xmlDocPtr xmlDocument = NULL;
var.dimension=0;
var.head=NULL;
REAL8 r8test=42.0;
xmlNodePtr xmlFragment;
LALInferenceAddVariable(&var, "real8 test", (void *)&r8test,
LALINFERENCE_REAL8_t, LALINFERENCE_PARAM_LINEAR);
printf("Initial LALInferenceVariables:\n");
LALInferencePrintVariables(&var);
printf( "--> Serializing into XML string ... ");
xmlFragment=XLALInferenceVariables2VOTNode(&var, "test");
/* convert VOTable tree into XML string */
if( (xmlString = XLALCreateVOTStringFromTree ( xmlFragment )) == NULL ) {
XLALPrintError ("%s: XLALCreateVOTStringFromTree() failed.\n", __func__);
return 1;
}
printf ("ok.\n");
/* display serialized structure */
printf( "----------------------------------------------------------------------\n");
printf( "%s", xmlString );
printf( "----------------------------------------------------------------------\n");
/* ---------- parse XML string back and validate ---------- */
/* convert VOTable string back into VOTable document */
printf ("--> Parsing XML string into xmlDoc ... ");
if ( (xmlDocument = XLALXMLString2Doc ( xmlString )) == NULL ) {
XLALPrintError( "%s: XLALXMLString2Doc() failed.\n", __func__);
return 1;
}
printf ("ok.\n");
xmlFreeDoc(xmlDocument);
xmlFreeNode ( xmlFragment );
XLALFree ( xmlString );
return 0;
}
int main(void)
{
return(testLALInferenceVariables());
}
\ No newline at end of file
......@@ -3,9 +3,16 @@ LDADD = $(LALSUPPORT_LIBS) $(top_builddir)/src/liblalinference.la
if LALSUPPORT
if LALINSPIRAL
if LALXML
XMLPRG = LALInferenceXMLTest
endif
check_PROGRAMS = \
InferenceTest
TESTS = $(check_PROGRAMS)
InferenceTest $(XMLPRG)
TESTS = $(check_PROGRAMS) $(XML_PROGRAMS)
endif
endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment