TPCCLIB
Loading...
Searching...
No Matches
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/*****************************************************************************/
26 TAC *tac
27) {
28 if(tac==NULL) return;
29 tac->sampleNr=tac->_sampleNr=0;
30 tac->tacNr=tac->_tacNr=0;
32 tac->isframe=0;
33 tac->x=tac->x1=tac->x2=NULL;
34 tac->cunit=tac->tunit=UNIT_UNKNOWN;
35 tac->w=NULL;
37 tac->c=NULL;
38 tac->_data=NULL;
39 iftInit(&tac->h);
40}
41/*****************************************************************************/
42
43/*****************************************************************************/
50 TACC *tacc
51) {
52 if(tacc==NULL) return;
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
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/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int iftDuplicate(IFT *ift1, IFT *ift2)
Definition ift.c:236
Definition tpctac.h:66
tactype type
Definition tpctac.h:69
char sw
Definition tpctac.h:77
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
unit sunit
Definition tpctac.h:73
double * y
Definition tpctac.h:75
char sw2
Definition tpctac.h:79
double size
Definition tpctac.h:71
Definition tpctac.h:87
double * x
Definition tpctac.h:97
tacformat format
Definition tpctac.h:93
int _sampleNr
Definition tpctac.h:121
double * _data
Definition tpctac.h:126
int sampleNr
Definition tpctac.h:89
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
double * w
Definition tpctac.h:111
int cunit
Definition tpctac.h:105
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
weights weighting
Definition tpctac.h:115
int tunit
Definition tpctac.h:109
double * x2
Definition tpctac.h:101
int _tacNr
Definition tpctac.h:119
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
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
void tacFreeExceptHeader(TAC *tac)
Definition tac.c:84
void tacFree(TAC *tac)
Definition tac.c:106
void taccFree(TACC *tacc)
Definition tac.c:68
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
Definition tac.c:356
int tacIsSize(TAC *d)
Definition tac.c:335
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition tac.c:130
void tacInit(TAC *tac)
Definition tac.c:24
int tacCopyTaccdata(TACC *d1, TACC *d2, int sampleNr)
Definition tac.c:260
int tacAllocateMoreSamples(TAC *tac, int addNr)
Allocate memory for more samples in TAC data.
Definition tac.c:435
int tacAllocateMore(TAC *tac, int tacNr)
Definition tac.c:178
int tacCopyTacc(TACC *d1, TACC *d2, int sampleNr)
Definition tac.c:233
int tacCopyTacchdr(TACC *d1, TACC *d2)
Definition tac.c:282
int tacCopyHdr(TAC *tac1, TAC *tac2)
Copy TAC header data from tac1 to tac2.
Definition tac.c:310
void taccInit(TACC *tacc)
Definition tac.c:48
int tacWCopy(TAC *tac1, TAC *tac2, int i1, int i2)
Definition tacw.c:41
int tacXCopy(TAC *tac1, TAC *tac2, int i1, int i2)
Definition tacx.c:24
@ WEIGHTING_UNKNOWN
Not known; usually assumed that not weighted.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_INVALID_VALUE
Invalid value.
@ TPCERROR_FAIL
General error.
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
@ TACTYPE_UNKNOWN
Content type not known.
Header file for library libtpcift.
Header file for library libtpcisotope.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28