TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpctac
tac.c
Go to the documentation of this file.
1
4
/*****************************************************************************/
5
#include "tpcclibConfig.h"
6
/*****************************************************************************/
7
#include "
tpcift.h
"
8
#include "
tpcisotope.h
"
9
/*****************************************************************************/
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <math.h>
13
#include <time.h>
14
#include <string.h>
15
/*****************************************************************************/
16
#include "
tpctac.h
"
17
/*****************************************************************************/
18
19
/*****************************************************************************/
24
void
tacInit
(
26
TAC
*tac
27
) {
28
if
(tac==NULL)
return
;
29
tac->
sampleNr
=tac->
_sampleNr
=0;
30
tac->
tacNr
=tac->
_tacNr
=0;
31
tac->
format
=
TAC_FORMAT_UNKNOWN
;
32
tac->
isframe
=0;
33
tac->
x
=tac->
x1
=tac->
x2
=NULL;
34
tac->
cunit
=tac->
tunit
=
UNIT_UNKNOWN
;
35
tac->
w
=NULL;
36
tac->
weighting
=
WEIGHTING_UNKNOWN
;
37
tac->
c
=NULL;
38
tac->
_data
=NULL;
39
iftInit
(&tac->
h
);
40
}
41
/*****************************************************************************/
42
43
/*****************************************************************************/
48
void
taccInit
(
50
TACC
*tacc
51
) {
52
if
(tacc==NULL)
return
;
53
tacc->
type
=
TACTYPE_UNKNOWN
;
54
tacc->
size
=0.0;
55
tacc->
sunit
=
UNIT_UNKNOWN
;
56
tacc->
y
=NULL;
57
tacc->
sw
=tacc->
sw2
=0;
58
tacc->
name
[0]=
'\0'
;
59
}
60
/*****************************************************************************/
61
62
/*****************************************************************************/
68
void
taccFree
(
70
TACC
*tacc
71
) {
72
// TACC does not actually contain mallocated or callocated memory,
73
// therefore we can just clear the values
74
taccInit
(tacc);
75
}
76
/*****************************************************************************/
77
78
/*****************************************************************************/
84
void
tacFreeExceptHeader
(
86
TAC
*tac
87
) {
88
if
(tac==NULL)
return
;
89
free(tac->
_data
);
90
// Free allocated TACC contents; actually not necessary, but lets do it
91
// for consistency */
92
for
(
int
i=0; i<tac->
_tacNr
; i++)
taccFree
(&tac->
c
[i]);
93
// Free the TACC list
94
free(tac->
c
);
95
// then set everything to zero or NULL again
96
tacInit
(tac);
97
}
98
/*****************************************************************************/
99
100
/*****************************************************************************/
106
void
tacFree
(
108
TAC
*tac
109
) {
110
if
(tac==NULL)
return
;
111
free(tac->
_data
);
112
// Free allocated TACC contents; actually not necessary, but lets do it
113
// for consistency */
114
for
(
int
i=0; i<tac->
_tacNr
; i++)
taccFree
(&tac->
c
[i]);
115
// Free the TACC list
116
free(tac->
c
);
117
// Free the header data
118
iftFree
(&tac->
h
);
119
// then set everything to zero or NULL again
120
tacInit
(tac);
121
}
122
/*****************************************************************************/
123
124
/*****************************************************************************/
130
int
tacAllocate
(
133
TAC
*tac,
135
int
sampleNr,
137
int
tacNr
138
) {
139
if
(tac==NULL)
return
TPCERROR_FAIL
;
140
/* Delete any previous contents */
141
tacFree
(tac);
142
/* If no memory is requested, then just return */
143
if
(sampleNr<1 && tacNr<1)
return
TPCERROR_OK
;
144
if
(sampleNr<1 || tacNr<1)
return
TPCERROR_FAIL
;
145
146
/* Allocate memory for TACC data */
147
tac->
c
=(
TACC
*)malloc(tacNr*
sizeof
(
TACC
));
148
if
(tac->
c
==NULL)
return
TPCERROR_OUT_OF_MEMORY
;
149
for
(
int
i=0; i<tacNr; i++)
taccInit
(&tac->
c
[i]);
150
tac->
_tacNr
=tacNr;
151
152
/* Allocate memory for all double arrays */
153
int
n; n=sampleNr*(4+tacNr);
154
tac->
_data
=(
double
*)calloc(n,
sizeof
(
double
));
155
if
(tac->
_data
==NULL)
return
TPCERROR_OUT_OF_MEMORY
;
156
for
(
int
i=0; i<n; i++) tac->
_data
[i]=nan(
""
);
157
tac->
_sampleNr
=sampleNr;
158
159
/* Set pointers for curve data */
160
double
*d=tac->
_data
;
161
tac->
x
=d; d+=sampleNr;
162
tac->
x1
=d; d+=sampleNr;
163
tac->
x2
=d; d+=sampleNr;
164
tac->
w
=d; d+=sampleNr;
165
for
(
int
i=0; i<tacNr; i++) {tac->
c
[i].
y
=d; d+=sampleNr;}
166
167
return
TPCERROR_OK
;
168
}
169
/*****************************************************************************/
170
171
/*****************************************************************************/
178
int
tacAllocateMore
(
181
TAC
*tac,
184
int
tacNr
185
) {
186
if
(tac==NULL)
return
TPCERROR_FAIL
;
187
if
(tac->
_sampleNr
<1)
return
TPCERROR_FAIL
;
188
/* Check if there is enough space already */
189
int
newNr, addNr;
190
newNr=tac->
tacNr
+tacNr;
191
addNr=newNr-tac->
_tacNr
;
192
//printf("newNr := %d\naddNr := %d\n", newNr, addNr);
193
if
(addNr<=0)
return
TPCERROR_OK
;
194
195
/* Reallocate memory for TACC data */
196
TACC
*taccPtr;
197
taccPtr=realloc(tac->
c
,
sizeof
(
TACC
)*newNr);
198
if
(taccPtr==NULL)
return
TPCERROR_OUT_OF_MEMORY
;
199
tac->
c
=taccPtr;
200
/* Reallocate memory for double arrays */
201
double
*dPtr;
202
dPtr=realloc(tac->
_data
,
sizeof
(
double
)*(4+newNr)*tac->
_sampleNr
);
203
if
(dPtr==NULL)
return
TPCERROR_OUT_OF_MEMORY
;
204
tac->
_data
=dPtr;
205
/* If both ok, then update the nr of allocated TACs and initiate new TTACs */
206
for
(
int
i=tac->
_tacNr
; i<newNr; i++)
taccInit
(&tac->
c
[i]);
207
tac->
_tacNr
=newNr;
208
209
/* Reset pointers for curve data */
210
double
*d=tac->
_data
;
211
tac->
x
=d; d+=tac->
_sampleNr
;
212
tac->
x1
=d; d+=tac->
_sampleNr
;
213
tac->
x2
=d; d+=tac->
_sampleNr
;
214
tac->
w
=d; d+=tac->
_sampleNr
;
215
for
(
int
i=0; i<tac->
_tacNr
; i++) {tac->
c
[i].
y
=d; d+=tac->
_sampleNr
;}
216
217
/* Set new y values to NaN */
218
for
(
int
i=tac->
_tacNr
-addNr; i<tac->_tacNr; i++)
219
for
(
int
j=0; j<tac->
_sampleNr
; j++) tac->
c
[i].
y
[j]=nan(
""
);
220
221
return
TPCERROR_OK
;
222
}
223
/*****************************************************************************/
224
225
/*****************************************************************************/
233
int
tacCopyTacc
(
235
TACC
*d1,
237
TACC
*d2,
239
int
sampleNr
240
) {
241
int
ret;
242
243
/* Check that required data exists */
244
if
(d1==NULL || d2==NULL)
return
TPCERROR_FAIL
;
245
/* Copy TACC header */
246
ret=
tacCopyTacchdr
(d1, d2);
if
(ret!=
TPCERROR_OK
)
return
ret;
247
/* Copy TACC data */
248
ret=
tacCopyTaccdata
(d1, d2, sampleNr);
if
(ret!=
TPCERROR_OK
)
return
ret;
249
return
TPCERROR_OK
;
250
}
251
/*****************************************************************************/
252
253
/*****************************************************************************/
260
int
tacCopyTaccdata
(
262
TACC
*d1,
264
TACC
*d2,
266
int
sampleNr
267
) {
268
/* Check that required data exists */
269
if
(d1==NULL || d2==NULL)
return
TPCERROR_FAIL
;
270
if
(sampleNr<1)
return
TPCERROR_OK
;
271
/* Copy data array contents */
272
for
(
int
i=0; i<sampleNr; i++) d2->
y
[i]=d1->
y
[i];
273
return
TPCERROR_OK
;
274
}
275
/*****************************************************************************/
276
277
/*****************************************************************************/
282
int
tacCopyTacchdr
(
284
TACC
*d1,
286
TACC
*d2
287
) {
288
/* Check that required data exists */
289
if
(d1==NULL || d2==NULL)
return
TPCERROR_FAIL
;
290
/* Copy TACC header fields */
291
d2->
type
=d1->
type
;
292
d2->
size
=d1->
size
;
293
d2->
sunit
=d1->
sunit
;
294
d2->
sw
=d1->
sw
;
295
d2->
sw2
=d1->
sw2
;
296
strcpy(d2->
name
, d1->
name
);
297
return
TPCERROR_OK
;
298
}
299
/*****************************************************************************/
300
301
/*****************************************************************************/
310
int
tacCopyHdr
(
312
TAC
*tac1,
314
TAC
*tac2
315
) {
316
/* Check that required data exists */
317
if
(tac1==NULL || tac2==NULL)
return
TPCERROR_FAIL
;
318
/* Copy TAC header fields */
319
tac2->
format
=tac1->
format
;
320
tac2->
isframe
=tac1->
isframe
;
321
tac2->
cunit
=tac1->
cunit
;
322
tac2->
tunit
=tac1->
tunit
;
323
//tac2->weighting=tac1->weighting;
324
/* Copy IFT */
325
iftFree
(&tac2->
h
);
iftDuplicate
(&tac1->
h
, &tac2->
h
);
326
return
TPCERROR_OK
;
327
}
328
/*****************************************************************************/
329
330
/*****************************************************************************/
335
int
tacIsSize
(
337
TAC
*d
338
) {
339
if
(d==NULL || d->
tacNr
<1)
return
(0);
340
for
(
int
ri=0; ri<d->
tacNr
; ri++) {
341
if
(isnan(d->
c
[ri].
size
))
continue
;
342
if
(d->
c
[ri].
size
<=0.0)
continue
;
343
return
(1);
344
}
345
return
(0);
346
}
347
/*****************************************************************************/
348
349
/*****************************************************************************/
356
int
tacDuplicate
(
358
TAC
*tac1,
360
TAC
*tac2
361
) {
362
if
(tac1==NULL || tac2==NULL)
return
TPCERROR_FAIL
;
363
if
(tac1->
sampleNr
<1 || tac1->
tacNr
<1)
return
TPCERROR_FAIL
;
364
365
/* Empty the duplicate */
366
tacFree
(tac2);
367
368
int
ret;
369
370
/* Allocate memory for tac2 */
371
ret=
tacAllocate
(tac2, tac1->
sampleNr
, tac1->
tacNr
);
372
if
(ret!=
TPCERROR_OK
)
return
(ret);
373
tac2->
sampleNr
=tac1->
sampleNr
;
374
tac2->
tacNr
=tac1->
tacNr
;
375
376
/* Copy the contents */
377
ret=
tacCopyHdr
(tac1, tac2);
378
if
(ret!=
TPCERROR_OK
)
return
(ret);
379
for
(
int
i=0, ret=0; i<tac1->
tacNr
&& ret==0; i++)
380
ret=
tacCopyTacc
(&tac1->
c
[i], &tac2->
c
[i], tac1->
sampleNr
);
381
if
(ret!=0)
return
TPCERROR_FAIL
;
382
ret=
tacXCopy
(tac1, tac2, 0, tac1->
sampleNr
-1);
383
if
(ret!=0)
return
TPCERROR_FAIL
;
384
ret=
tacWCopy
(tac1, tac2, 0, tac1->
sampleNr
-1);
385
if
(ret!=0)
return
TPCERROR_FAIL
;
386
387
return
TPCERROR_OK
;
388
}
389
/*****************************************************************************/
390
391
/*****************************************************************************/
396
int
tacExtract
(
398
TAC
*d1,
401
TAC
*d2,
403
const
int
i
404
) {
405
if
(d1==NULL || d2==NULL)
return
TPCERROR_FAIL
;
406
if
(d1->
sampleNr
<1 || d1->
tacNr
<1)
return
TPCERROR_NO_DATA
;
407
if
(i<0 || i>=d1->
tacNr
)
return
TPCERROR_INVALID_VALUE
;
408
409
/* Remove any old contents in target structure */
410
tacFree
(d2);
411
412
/* Allocate space for the new TAC */
413
int
ret=
tacAllocate
(d2, d1->
sampleNr
, 1);
if
(ret!=
TPCERROR_OK
)
return
(ret);
414
d2->
sampleNr
=d1->
sampleNr
;
415
d2->
tacNr
=1;
416
417
/* Copy contents */
418
ret=
tacCopyHdr
(d1, d2);
if
(ret!=
TPCERROR_OK
) {
tacFree
(d2);
return
(ret);}
419
ret=
tacXCopy
(d1, d2, 0, d1->
sampleNr
-1);
if
(ret!=
TPCERROR_OK
) {
tacFree
(d2);
return
(ret);}
420
ret=
tacCopyTacc
(d1->
c
+i, d2->
c
, d1->
sampleNr
);
if
(ret!=
TPCERROR_OK
) {
tacFree
(d2);
return
(ret);}
421
ret=
tacWCopy
(d1, d2, 0, d1->
sampleNr
-1);
if
(ret!=
TPCERROR_OK
) {
tacFree
(d2);
return
(ret);}
422
423
return
(
TPCERROR_OK
);
424
}
425
/*****************************************************************************/
426
427
/*****************************************************************************/
435
int
tacAllocateMoreSamples
(
438
TAC
*tac,
441
int
addNr
442
) {
443
if
(addNr<1)
return
TPCERROR_OK
;
444
if
(tac==NULL)
return
TPCERROR_FAIL
;
445
if
(tac->
_sampleNr
<1 || tac->
_tacNr
<1)
return
TPCERROR_FAIL
;
446
447
/* Check if there is enough space already */
448
int
newNr;
449
newNr=tac->
sampleNr
+addNr;
450
addNr=newNr-tac->
_sampleNr
;
451
if
(addNr<=0)
return
TPCERROR_OK
;
452
453
/* Make a temporary copy of the original data */
454
int
ret;
455
TAC
temp;
tacInit
(&temp);
456
ret=
tacDuplicate
(tac, &temp);
if
(ret!=
TPCERROR_OK
)
return
(ret);
457
458
/* Delete and reallocate the original TAC */
459
tacFree
(tac);
460
ret=
tacAllocate
(tac, newNr, temp.
tacNr
);
461
if
(ret!=
TPCERROR_OK
) {
tacFree
(&temp);
return
(ret);}
462
tac->
sampleNr
=temp.
sampleNr
;
463
tac->
tacNr
=temp.
tacNr
;
464
465
/* Copy the contents */
466
ret=
tacCopyHdr
(&temp, tac);
467
if
(ret!=
TPCERROR_OK
) {
tacFree
(&temp);
return
(ret);}
468
for
(
int
i=0, ret=0; i<temp.
tacNr
&& ret==0; i++)
469
ret=
tacCopyTacc
(&temp.
c
[i], &tac->
c
[i], temp.
sampleNr
);
470
if
(ret!=0) {
tacFree
(&temp);
return
TPCERROR_FAIL
;}
471
ret=
tacXCopy
(&temp, tac, 0, temp.
sampleNr
-1);
472
if
(ret!=0) {
tacFree
(&temp);
return
TPCERROR_FAIL
;}
473
474
tacFree
(&temp);
475
return
TPCERROR_OK
;
476
}
477
/*****************************************************************************/
478
479
/*****************************************************************************/
iftFree
void iftFree(IFT *ift)
Definition
ift.c:37
iftInit
void iftInit(IFT *ift)
Definition
ift.c:21
iftDuplicate
int iftDuplicate(IFT *ift1, IFT *ift2)
Definition
ift.c:236
TACC
Definition
tpctac.h:66
TACC::type
tactype type
Definition
tpctac.h:69
TACC::sw
char sw
Definition
tpctac.h:77
TACC::name
char name[MAX_TACNAME_LEN+1]
Definition
tpctac.h:81
TACC::sunit
unit sunit
Definition
tpctac.h:73
TACC::y
double * y
Definition
tpctac.h:75
TACC::sw2
char sw2
Definition
tpctac.h:79
TACC::size
double size
Definition
tpctac.h:71
TAC
Definition
tpctac.h:87
TAC::x
double * x
Definition
tpctac.h:97
TAC::cunit
unit cunit
Definition
tpctac.h:105
TAC::format
tacformat format
Definition
tpctac.h:93
TAC::_sampleNr
int _sampleNr
Definition
tpctac.h:121
TAC::_data
double * _data
Definition
tpctac.h:126
TAC::sampleNr
int sampleNr
Definition
tpctac.h:89
TAC::h
IFT h
Optional (but often useful) header information.
Definition
tpctac.h:141
TAC::w
double * w
Definition
tpctac.h:111
TAC::isframe
int isframe
Definition
tpctac.h:95
TAC::c
TACC * c
Definition
tpctac.h:117
TAC::weighting
weights weighting
Definition
tpctac.h:115
TAC::x2
double * x2
Definition
tpctac.h:101
TAC::tunit
unit tunit
Definition
tpctac.h:109
TAC::_tacNr
int _tacNr
Definition
tpctac.h:119
TAC::x1
double * x1
Definition
tpctac.h:99
TAC::tacNr
int tacNr
Definition
tpctac.h:91
tacExtract
int tacExtract(TAC *d1, TAC *d2, const int i)
Extract the specified TAC from existing TAC structure into a new TAC.
Definition
tac.c:396
tacFreeExceptHeader
void tacFreeExceptHeader(TAC *tac)
Definition
tac.c:84
tacFree
void tacFree(TAC *tac)
Definition
tac.c:106
taccFree
void taccFree(TACC *tacc)
Definition
tac.c:68
tacDuplicate
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
Definition
tac.c:356
tacIsSize
int tacIsSize(TAC *d)
Definition
tac.c:335
tacAllocate
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition
tac.c:130
tacInit
void tacInit(TAC *tac)
Definition
tac.c:24
tacCopyTaccdata
int tacCopyTaccdata(TACC *d1, TACC *d2, int sampleNr)
Definition
tac.c:260
tacAllocateMoreSamples
int tacAllocateMoreSamples(TAC *tac, int addNr)
Allocate memory for more samples in TAC data.
Definition
tac.c:435
tacAllocateMore
int tacAllocateMore(TAC *tac, int tacNr)
Definition
tac.c:178
tacCopyTacc
int tacCopyTacc(TACC *d1, TACC *d2, int sampleNr)
Definition
tac.c:233
tacCopyTacchdr
int tacCopyTacchdr(TACC *d1, TACC *d2)
Definition
tac.c:282
tacCopyHdr
int tacCopyHdr(TAC *tac1, TAC *tac2)
Copy TAC header data from tac1 to tac2.
Definition
tac.c:310
taccInit
void taccInit(TACC *tacc)
Definition
tac.c:48
tacWCopy
int tacWCopy(TAC *tac1, TAC *tac2, int i1, int i2)
Definition
tacw.c:41
tacXCopy
int tacXCopy(TAC *tac1, TAC *tac2, int i1, int i2)
Definition
tacx.c:24
WEIGHTING_UNKNOWN
@ WEIGHTING_UNKNOWN
Not known; usually assumed that not weighted.
Definition
tpcextensions.h:53
UNIT_UNKNOWN
@ UNIT_UNKNOWN
Unknown unit.
Definition
tpcextensions.h:85
TPCERROR_INVALID_VALUE
@ TPCERROR_INVALID_VALUE
Invalid value.
Definition
tpcextensions.h:220
TPCERROR_FAIL
@ TPCERROR_FAIL
General error.
Definition
tpcextensions.h:192
TPCERROR_OUT_OF_MEMORY
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
Definition
tpcextensions.h:193
TPCERROR_OK
@ TPCERROR_OK
No error.
Definition
tpcextensions.h:191
TPCERROR_NO_DATA
@ TPCERROR_NO_DATA
File contains no data.
Definition
tpcextensions.h:203
TACTYPE_UNKNOWN
@ TACTYPE_UNKNOWN
Content type not known.
Definition
tpcextensions.h:67
tpcift.h
Header file for library libtpcift.
tpcisotope.h
Header file for library libtpcisotope.
tpctac.h
Header file for library libtpctac.
TAC_FORMAT_UNKNOWN
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition
tpctac.h:28
Generated on
for TPCCLIB by
1.17.0