TPCCLIB
Loading...
Searching...
No Matches
tac2xml.c File Reference

Save Excel-compatible XML file containing data from one or more TAC files. More...

Go to the source code of this file.

Functions

int tacWriteSheetIntoXML (TAC *tac, char *sheetname, FILE *fp, TPCSTATUS *status)

Detailed Description

Save Excel-compatible XML file containing data from one or more TAC files.

Author
Vesa Oikonen

Definition in file tac2xml.c.

Function Documentation

◆ tacWriteSheetIntoXML()

int tacWriteSheetIntoXML ( TAC * tac,
char * sheetname,
FILE * fp,
TPCSTATUS * status )

Write TAC data as a new sheet inside Excel compatible XML file.

Returns
enum tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
Parameters
tacPointer to TAC struct, contents of which are to be written.
sheetnamePointer to string containing name of sheet.
fpPointer to file opened for write. File must already contain XML header, and the footer must be written before it is closed.
statusPointer to status data; enter NULL if not needed.

Definition at line 57 of file tac2xml.c.

67 {
68 int verbose=0; if(status!=NULL) verbose=status->verbose;
69 if(verbose>0) printf("%s(%s)\n", __func__, sheetname);
70 if(fp==NULL) {
71 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_WRITE);
73 }
74 if(tac==NULL || tac->tacNr<1 || tac->sampleNr<1) {
75 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
76 return TPCERROR_NO_DATA;
77 }
78
79 char tunit[128], cunit[128];
80 strcpy(tunit, unitName(tac->tunit));
81 strcpy(cunit, unitName(tac->cunit));
82
83 /* Make sure that TAC names are available */
84 if(verbose>2) printf("constructing TAC names\n");
85 tacEnsureNames(tac);
86
87 /* Start worksheet and table */
88 {
89 int n=0;
90 n=fprintf(fp, " <ss:Worksheet ss:Name=\"%s\">\n", sheetname);
91 n+=fprintf(fp, " <ss:Table>\n");
92 if(n<10) {
93 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_WRITE);
95 }
96 }
97
98 /* Set column widths */
99 {
100 int n=tac->tacNr+1;
101 if(tac->isframe) n++;
102 if(tacIsWeighted(tac)) n++;
103 for(int i=0; i<n; i++) fprintf(fp, " <ss:Column ss:Width=\"80\"/>\n");
104 }
105
106 /* Write the title line */
107 fprintf(fp, " <ss:Row ss:StyleID=\"1\">\n");
108 if(tac->isframe==0) {
109 fprintf(fp, " <ss:Cell>\n");
110 fprintf(fp, " <ss:Data ss:Type=\"String\">time[%s]</ss:Data>\n", tunit);
111 fprintf(fp, " </ss:Cell>\n");
112 } else {
113 fprintf(fp, " <ss:Cell>\n");
114 fprintf(fp, " <ss:Data ss:Type=\"String\">start[%s]</ss:Data>\n", tunit);
115 fprintf(fp, " </ss:Cell>\n");
116 fprintf(fp, " <ss:Cell>\n");
117 fprintf(fp, " <ss:Data ss:Type=\"String\">end[%s]</ss:Data>\n", cunit);
118 fprintf(fp, " </ss:Cell>\n");
119 }
120 for(int ri=0; ri<tac->tacNr; ri++) {
121 /* write TAC names */
122 fprintf(fp, " <ss:Cell>\n");
123 if(ri==0 && tac->isframe==0)
124 fprintf(fp, " <ss:Data ss:Type=\"String\">%s[%s]</ss:Data>\n", tac->c[ri].name, cunit);
125 else
126 fprintf(fp, " <ss:Data ss:Type=\"String\">%s</ss:Data>\n", tac->c[ri].name);
127 fprintf(fp, " </ss:Cell>\n");
128 }
129 if(tacIsWeighted(tac)) {
130 fprintf(fp, " <ss:Cell>\n");
131 fprintf(fp, " <ss:Data ss:Type=\"String\">weight</ss:Data>\n");
132 fprintf(fp, " </ss:Cell>\n");
133 }
134 if(fprintf(fp, " </ss:Row>\n")<1) {
135 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_WRITE);
137 }
138
139 /* Write data */
140 if(verbose>2) printf("writing data table\n");
141 for(int fi=0; fi<tac->sampleNr; fi++) {
142
143 fprintf(fp, " <ss:Row>\n");
144
145 /* Note: missing values are written as empty cells, with string type, because with
146 number type Excel seems to read those as zeroes. */
147
148 /* Time(s) (x, or x1 and x2) */
149 double v; if(tac->isframe==0) v=tac->x[fi]; else v=tac->x1[fi];
150 fprintf(fp, " <ss:Cell>\n");
151 if(isnan(v)) fprintf(fp, " <ss:Data ss:Type=\"String\"></ss:Data>\n");
152 else fprintf(fp, " <ss:Data ss:Type=\"Number\">%g</ss:Data>\n", v);
153 fprintf(fp, " </ss:Cell>\n");
154
155 if(tac->isframe) {
156 v=tac->x2[fi];
157 fprintf(fp, " <ss:Cell>\n");
158 if(isnan(v)) fprintf(fp, " <ss:Data ss:Type=\"String\"></ss:Data>\n");
159 else fprintf(fp, " <ss:Data ss:Type=\"Number\">%g</ss:Data>\n", v);
160 fprintf(fp, " </ss:Cell>\n");
161 }
162
163 /* Concentrations (y values) */
164 for(int ri=0; ri<tac->tacNr; ri++) {
165 fprintf(fp, " <ss:Cell>\n");
166 if(isnan(tac->c[ri].y[fi])) fprintf(fp, " <ss:Data ss:Type=\"String\"></ss:Data>\n");
167 else fprintf(fp, " <ss:Data ss:Type=\"Number\">%g</ss:Data>\n", tac->c[ri].y[fi]);
168 fprintf(fp, " </ss:Cell>\n");
169 }
170
171 /* Weight */
172 if(tacIsWeighted(tac)) {
173 fprintf(fp, " <ss:Cell>\n");
174 if(isnan(tac->w[fi])) fprintf(fp, " <ss:Data ss:Type=\"String\"></ss:Data>\n");
175 else fprintf(fp, " <ss:Data ss:Type=\"Number\">%g</ss:Data>\n", tac->w[fi]);
176 fprintf(fp, " </ss:Cell>\n");
177 }
178
179 fprintf(fp, " </ss:Row>\n");
180 }
181
182 /* Write table and worksheet end part */
183 fprintf(fp, " </ss:Table>\n");
184 if(fprintf(fp, " </ss:Worksheet>\n")<1) {
185 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_CANNOT_WRITE);
187 }
188
189 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
190 return(TPCERROR_OK);
191}
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
double * y
Definition tpctac.h:75
double * x
Definition tpctac.h:97
unit cunit
Definition tpctac.h:105
int sampleNr
Definition tpctac.h:89
double * w
Definition tpctac.h:111
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
unit tunit
Definition tpctac.h:109
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
void tacEnsureNames(TAC *tac)
Definition tacname.c:50
int tacIsWeighted(TAC *tac)
Definition tacw.c:24
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_CANNOT_WRITE
Cannot write file.
char * unitName(int unit_code)
Definition units.c:143