LAL  7.5.0.1-b72065a
H5FileIOFrequencySeries_source.c
Go to the documentation of this file.
1 #define CONCAT2x(a,b) a##b
2 #define CONCAT2(a,b) CONCAT2x(a,b)
3 
4 #define VTYPE CONCAT2(TYPE,Vector)
5 #define STYPE CONCAT2(TYPE,FrequencySeries)
6 
7 #define FILEWRITEFUNC CONCAT2(XLALH5FileWrite,STYPE)
8 #define FILEREADFUNC CONCAT2(XLALH5FileRead,STYPE)
9 
10 #define DSETALLOCFUNC CONCAT2(XLALH5DatasetAlloc,VTYPE)
11 #define DSETREADFUNC CONCAT2(XLALH5DatasetRead,VTYPE)
12 
13 int FILEWRITEFUNC(LALH5File *file, const char *name, STYPE *series)
14 {
15  char sampleUnits[LALUnitTextSize];
16  LALH5Dataset *dset;
17  if (!file || !name || !series)
19  if (!series->data)
21  dset = DSETALLOCFUNC(file, name, series->data);
22  if (!dset)
24  if (XLALH5AttributeAddString((LALH5Generic)dset, "name", series->name) < 0) {
25  XLALH5DatasetFree(dset);
26  XLAL_ERROR(XLAL_EFUNC, "Could not set name attribute");
27  }
28  if (XLALH5AttributeAddLIGOTimeGPS((LALH5Generic)dset, "epoch", &series->epoch) < 0) {
29  XLALH5DatasetFree(dset);
30  XLAL_ERROR(XLAL_EFUNC, "Could not set epoch attribute");
31  }
32  if (XLALH5DatasetAddREAL8Attribute(dset, "deltaF", series->deltaF) < 0) {
33  XLALH5DatasetFree(dset);
34  XLAL_ERROR(XLAL_EFUNC, "Could not set deltaF attribute");
35  }
36  if (XLALH5DatasetAddREAL8Attribute(dset, "f0", series->f0) < 0) {
37  XLALH5DatasetFree(dset);
38  XLAL_ERROR(XLAL_EFUNC, "Could not set f0 attribute");
39  }
40  if (XLALUnitAsString(sampleUnits, sizeof(sampleUnits), &series->sampleUnits) == NULL)
41  {
42  XLALH5DatasetFree(dset);
44  }
45  if (XLALH5AttributeAddString((LALH5Generic)dset, "sampleUnits", sampleUnits) < 0) {
46  XLALH5DatasetFree(dset);
47  XLAL_ERROR(XLAL_EFUNC, "Could not set sampleUnits attribute");
48  }
49  XLALH5DatasetFree(dset);
50  return 0;
51 }
52 
54 {
55  char sampleUnits[LALUnitTextSize];
56  STYPE *series;
57  LALH5Dataset *dset;
58  int n;
59 
60  if (!file || !name)
62 
63  dset = XLALH5DatasetRead(file, name);
64  if (!dset)
66 
67  series = XLALMalloc(sizeof(*series));
68  if (!series) {
69  XLALH5DatasetFree(dset);
71  }
72 
73  /* read metadata */
74 
75  n = XLALH5AttributeQueryStringValue(series->name, sizeof(series->name), (LALH5Generic)dset, "name");
76  if (n < 0) {
77  LALFree(series);
78  XLALH5DatasetFree(dset);
80  }
81  if ((size_t)n >= sizeof(series->name))
82  XLAL_PRINT_WARNING("Name of frequency series was truncated");
83 
84  n = XLALH5AttributeQueryStringValue(sampleUnits, sizeof(sampleUnits), (LALH5Generic)dset, "sampleUnits");
85  if (n < 0) {
86  LALFree(series);
87  XLALH5DatasetFree(dset);
89  }
90  /* note: treat failure to parse sample unit string as a warning */
91  if ((size_t)n >= sizeof(sampleUnits) || XLALParseUnitString(&series->sampleUnits, sampleUnits) == NULL) {
92  XLAL_PRINT_WARNING("Could not parse unit string `%s'", sampleUnits);
93  series->sampleUnits = lalDimensionlessUnit;
94  }
95 
96  if (XLALH5AttributeQueryLIGOTimeGPSValue(&series->epoch, (LALH5Generic)dset, "epoch") == NULL) {
97  LALFree(series);
98  XLALH5DatasetFree(dset);
100  }
101 
102  series->deltaF = XLALH5DatasetQueryREAL8AttributeValue(dset, "deltaF");
103  if (XLAL_IS_REAL8_FAIL_NAN(series->deltaF)) {
104  LALFree(series);
105  XLALH5DatasetFree(dset);
107  }
108 
109  series->f0 = XLALH5DatasetQueryREAL8AttributeValue(dset, "f0");
110  if (XLAL_IS_REAL8_FAIL_NAN(series->f0)) {
111  LALFree(series);
112  XLALH5DatasetFree(dset);
114  }
115 
116  series->data = DSETREADFUNC(dset);
117  XLALH5DatasetFree(dset);
118  if (!series->data) {
119  LALFree(series);
121  }
122 
123  return series;
124 }
125 
126 #undef CONCAT2x
127 #undef CONCAT2
128 
129 #undef VTYPE
130 #undef STYPE
131 
132 #undef FILEWRITEFUNC
133 #undef FILEREADFUNC
134 
135 #undef DSETALLOCFUNC
136 #undef DSETREADFUNC
LALH5Dataset * XLALH5DatasetRead(LALH5File *file, const char *name)
struct tagLALH5File LALH5File
Incomplete type for a HDF5 file.
Definition: H5FileIO.h:93
struct tagLALH5Dataset LALH5Dataset
Incomplete type for a HDF5 dataset.
Definition: H5FileIO.h:102
int XLALH5AttributeQueryStringValue(char *value, size_t size, const LALH5Generic object, const char *key)
int XLALH5AttributeAddLIGOTimeGPS(LALH5Generic object, const char *key, const LIGOTimeGPS *value)
void XLALH5DatasetFree(LALH5Dataset *dset)
int XLALH5AttributeAddString(LALH5Generic object, const char *key, const char *value)
LIGOTimeGPS * XLALH5AttributeQueryLIGOTimeGPSValue(LIGOTimeGPS *value, const LALH5Generic object, const char *key)
#define DSETREADFUNC
#define FILEWRITEFUNC
#define FILEREADFUNC
#define DSETALLOCFUNC
#define LALFree(p)
Definition: LALMalloc.h:96
const char *const name
type name
Definition: UserInput.c:193
int XLALH5DatasetAddREAL8Attribute(LALH5Dataset *dset, const char *key, REAL8 value)
Adds a scalar attribute to a LALH5Dataset.
REAL8 XLALH5DatasetQueryREAL8AttributeValue(LALH5Dataset *dset, const char *key)
Gets the value of a scalar attribute in a LALH5Dataset.
#define XLALMalloc(n)
Definition: LALMalloc.h:44
@ LALUnitTextSize
Definition: Units.h:174
LALUnit * XLALParseUnitString(LALUnit *output, const char *string)
Returns the pointer output upon return or a pointer to newly allocated memory if output was NULL; on ...
Definition: UnitDefs.c:354
const LALUnit lalDimensionlessUnit
dimensionless units
Definition: UnitDefs.c:156
char * XLALUnitAsString(char *string, UINT4 length, const LALUnit *input)
Returns the pointer to the input string, which is populated with the unit string if successful.
Definition: UnitDefs.c:276
#define XLAL_ERROR_NULL(...)
Macro to invoke a failure from a XLAL routine returning a pointer.
Definition: XLALError.h:713
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
Definition: XLALError.h:700
#define XLAL_PRINT_WARNING(...)
Macro that will print a warning message with a standard format.
Definition: XLALError.h:270
#define XLAL_IS_REAL8_FAIL_NAN(val)
Tests if val is a XLAL REAL8 failure NaN.
Definition: XLALError.h:393
@ XLAL_ENOMEM
Memory allocation error.
Definition: XLALError.h:407
@ XLAL_EFAULT
Invalid pointer.
Definition: XLALError.h:408
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EINVAL
Invalid argument.
Definition: XLALError.h:409
Incomplete type for a pointer to an HDF5 file or group or dataset.
Definition: H5FileIO.h:110