TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpctac
matio.c
Go to the documentation of this file.
1
4
/*****************************************************************************/
5
#include "tpcclibConfig.h"
6
/*****************************************************************************/
7
#include "
tpcift.h
"
8
#include "
tpcisotope.h
"
9
#include "
tpccsv.h
"
10
/*****************************************************************************/
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <math.h>
14
#include <time.h>
15
#include <string.h>
16
/*****************************************************************************/
17
#include "
tpctac.h
"
18
/*****************************************************************************/
19
20
/*****************************************************************************/
25
int
tacReadMat
(
27
TAC
*tac,
29
CSV
*csv,
31
TPCSTATUS
*status
32
) {
33
int
verbose=0;
if
(status!=NULL) verbose=status->
verbose
;
34
if
(tac==NULL) {
35
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_FAIL
);
36
return
TPCERROR_FAIL
;
37
}
38
tacFree
(tac);
39
if
(csv==NULL || csv->
row_nr
<1 || csv->
col_nr
<1) {
40
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_NO_DATA
);
41
return
TPCERROR_NO_DATA
;
42
}
43
if
(verbose>0) printf(
"%s()\n"
, __func__);
44
45
int
ret, n;
46
47
/* Check that CSV is of regular size */
48
if
(!
csvIsRegular
(csv)) {
49
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_INVALID_FORMAT
);
50
return
TPCERROR_INVALID_FORMAT
;
51
}
52
/* Column and row numbers must be at least 2 */
53
if
(csv->
col_nr
<2 || csv->
row_nr
<2) {
54
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_INVALID_FORMAT
);
55
return
TPCERROR_INVALID_FORMAT
;
56
}
57
58
/* Allocate memory for TAC data */
59
ret=
tacAllocate
(tac, csv->
col_nr
-1, csv->
row_nr
-1);
60
statusSet
(status, __func__, __FILE__, __LINE__, ret);
61
if
(ret!=
TPCERROR_OK
)
return
ret;
62
tac->
tacNr
=csv->
row_nr
-1; tac->
sampleNr
=csv->
col_nr
-1;
63
tac->
isframe
=1;
64
65
/* First cell may contain study number */
66
char
*cptr=
csvCell
(csv, 0, 0);
67
if
(cptr!=NULL && cptr[0] &&
strnlen
(cptr, 20)<20)
68
tacSetHeaderStudynr
(&tac->
h
, cptr);
69
70
/* Get sample times from the first line */
71
for
(
int
j=0; j<tac->
sampleNr
; j++) {
72
cptr=
csvCell
(csv, 0, j+1);
if
(cptr==NULL) {ret++;
break
;}
73
n=strlen(cptr);
if
(n<3) {ret++;
break
;}
74
char
*cptr2=strchr(cptr+1,
'-'
);
if
(cptr2!=NULL) {*cptr2=(char)0; cptr2++;}
75
tac->
x1
[j]=
atofVerified
(cptr);
76
tac->
x2
[j]=
atofVerified
(cptr2);
77
if
(isnan(tac->
x1
[j]) || isnan(tac->
x2
[j])) {ret++;
break
;}
78
tac->
x
[j]=0.5*(tac->
x1
[j]+tac->
x2
[j]);
79
}
80
if
(ret) {
81
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_INVALID_FORMAT
);
82
return
TPCERROR_INVALID_FORMAT
;
83
}
84
85
86
/* Get region names from the first column */
87
ret=0;
88
for
(
int
i=0; i<tac->
tacNr
; i++) {
89
cptr=
csvCell
(csv, i+1, 0);
if
(cptr==NULL) {ret++;
break
;}
90
n=strlen(cptr);
if
(n>2 && cptr[n-1]==
'\''
) {cptr[n-1]=(char)0; n--;}
91
if
(n>1 && cptr[0]==
'\''
) {cptr++; n--;}
92
if
(n>0)
strlcpy
(tac->
c
[i].
name
, cptr,
MAX_TACNAME_LEN
);
93
}
94
if
(ret) {
95
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_INVALID_FORMAT
);
96
return
TPCERROR_INVALID_FORMAT
;
97
}
98
99
/* Get concentrations */
100
n=0; ret=0;
101
for
(
int
i=0; i<tac->
tacNr
&& ret==0; i++) {
102
for
(
int
j=0; j<tac->
sampleNr
; j++) {
103
cptr=
csvCell
(csv, i+1, j+1);
if
(cptr==NULL) {ret++;
break
;}
104
tac->
c
[i].
y
[j]=
atofVerified
(cptr);
if
(!isnan(tac->
c
[i].
y
[j])) n++;
105
}
106
}
107
if
(ret) {
108
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_INVALID_FORMAT
);
109
return
TPCERROR_INVALID_FORMAT
;
110
}
111
if
(n<1) {
112
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_NO_DATA
);
113
return
TPCERROR_NO_DATA
;
114
}
115
116
117
tac->
format
=
TAC_FORMAT_MAT
;
118
tac->
weighting
=
WEIGHTING_OFF
;
119
120
statusSet
(status, __func__, __FILE__, __LINE__,
TPCERROR_OK
);
121
return
(
TPCERROR_OK
);
122
}
123
/*****************************************************************************/
124
125
/*****************************************************************************/
csvIsRegular
int csvIsRegular(CSV *csv)
Definition
csv.c:292
csvCell
char * csvCell(CSV *csv, int row, int col)
Definition
csv.c:358
atofVerified
double atofVerified(const char *s)
Definition
decpoint.c:75
tacReadMat
int tacReadMat(TAC *tac, CSV *csv, TPCSTATUS *status)
Definition
matio.c:25
statusSet
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition
statusmsg.c:142
strnlen
size_t strnlen(const char *s, size_t n)
Definition
stringext.c:565
strlcpy
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition
stringext.c:630
CSV
Definition
tpccsv.h:36
CSV::row_nr
int row_nr
Definition
tpccsv.h:44
CSV::col_nr
int col_nr
Definition
tpccsv.h:46
TACC::name
char name[MAX_TACNAME_LEN+1]
Definition
tpctac.h:81
TACC::y
double * y
Definition
tpctac.h:75
TAC
Definition
tpctac.h:87
TAC::x
double * x
Definition
tpctac.h:97
TAC::format
tacformat format
Definition
tpctac.h:93
TAC::sampleNr
int sampleNr
Definition
tpctac.h:89
TAC::h
IFT h
Optional (but often useful) header information.
Definition
tpctac.h:141
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::x1
double * x1
Definition
tpctac.h:99
TAC::tacNr
int tacNr
Definition
tpctac.h:91
TPCSTATUS
Definition
tpcextensions.h:241
TPCSTATUS::verbose
int verbose
Verbose level, used by statusPrint() etc.
Definition
tpcextensions.h:242
tacFree
void tacFree(TAC *tac)
Definition
tac.c:106
tacAllocate
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition
tac.c:130
tacSetHeaderStudynr
int tacSetHeaderStudynr(IFT *h, const char *s)
Definition
tacift.c:79
tpccsv.h
Header file for library libtpccsv.
WEIGHTING_OFF
@ WEIGHTING_OFF
Not weighted or weights not available (weights for all included samples are 1.0).
Definition
tpcextensions.h:54
MAX_TACNAME_LEN
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero).
Definition
tpcextensions.h:35
TPCERROR_FAIL
@ TPCERROR_FAIL
General error.
Definition
tpcextensions.h:192
TPCERROR_INVALID_FORMAT
@ TPCERROR_INVALID_FORMAT
Invalid file format.
Definition
tpcextensions.h:201
TPCERROR_OK
@ TPCERROR_OK
No error.
Definition
tpcextensions.h:191
TPCERROR_NO_DATA
@ TPCERROR_NO_DATA
File contains no data.
Definition
tpcextensions.h:203
tpcift.h
Header file for library libtpcift.
tpcisotope.h
Header file for library libtpcisotope.
tpctac.h
Header file for library libtpctac.
TAC_FORMAT_MAT
@ TAC_FORMAT_MAT
Matlab matrix TAC format (reading supported).
Definition
tpctac.h:49
Generated on
for TPCCLIB by
1.17.0