TPCCLIB
Loading...
Searching...
No Matches
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/*****************************************************************************/
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);
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);
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);
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);
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);
110 }
111 if(n<1) {
112 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
113 return TPCERROR_NO_DATA;
114 }
115
116
119
120 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
121 return(TPCERROR_OK);
122}
123/*****************************************************************************/
124
125/*****************************************************************************/
int csvIsRegular(CSV *csv)
Definition csv.c:292
char * csvCell(CSV *csv, int row, int col)
Definition csv.c:358
double atofVerified(const char *s)
Definition decpoint.c:75
int tacReadMat(TAC *tac, CSV *csv, TPCSTATUS *status)
Definition matio.c:25
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
Definition tpccsv.h:36
int row_nr
Definition tpccsv.h:44
int col_nr
Definition tpccsv.h:46
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
weights weighting
Definition tpctac.h:115
double * x2
Definition tpctac.h:101
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
void tacFree(TAC *tac)
Definition tac.c:106
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition tac.c:130
int tacSetHeaderStudynr(IFT *h, const char *s)
Definition tacift.c:79
Header file for library libtpccsv.
@ WEIGHTING_OFF
Not weighted or weights not available (weights for all included samples are 1.0).
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero)
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
Header file for library libtpcift.
Header file for library libtpcisotope.
Header file for library libtpctac.
@ TAC_FORMAT_MAT
Matlab matrix TAC format (reading supported)
Definition tpctac.h:49