TPCCLIB
Loading...
Searching...
No Matches
idwc.c
Go to the documentation of this file.
1
5/******************************************************************************/
7#define MAX_IDWC_LINE_LEN 512
8/******************************************************************************/
9#include "libtpccurveio.h"
10#include <unistd.h>
11/******************************************************************************/
12
13/******************************************************************************/
20 DFT *dft,
22 char *filename
23) {
24 int ri, fi, n;
25 char tmp[1024], is_stdout=0;
26 FILE *fp;
27
28
29 /* Check that there is some data to write */
30 if(dft->voiNr<1 || dft->frameNr<1 || filename==NULL) {
31 strcpy(dfterrmsg, "no data"); return(1);}
32
33 /* Check if writing to stdout */
34 if(!strcasecmp(filename, "stdout")) is_stdout=1;
35
36 /* Check if file exists; backup, if necessary */
37 if(!is_stdout && access(filename, 0) != -1) {
38 strcpy(tmp, filename); strcat(tmp, BACKUP_EXTENSION);
39 if(access(tmp, 0) != -1) remove(tmp);
40 rename(filename, tmp);
41 }
42
43 /* Open output file */
44 if(is_stdout) fp=(FILE*)stdout;
45 else if((fp = fopen(filename, "w")) == NULL) {
46 strcpy(dfterrmsg, "cannot open file"); return(2);}
47
48 /* Write sample number */
49 n=fprintf(fp, "%d\n", dft->frameNr);
50 if(n<2) {
51 strcpy(dfterrmsg, "cannot write file");
52 fclose(fp); return(3);
53 }
54 /* write data lines */
55 for(ri=0; ri<dft->voiNr; ri++) {
56 for(fi=0; fi<dft->frameNr; fi++) {
57 n=fprintf(fp, "%6.1f %18.14f %18.14f %3d\n",
58 dft->x[fi], dft->voi[ri].y[fi], dft->w[fi], ri+1);
59 if(n<8) {
60 strcpy(dfterrmsg, "cannot write file");
61 fclose(fp); return(4);
62 }
63 }
64 }
65 /* close file */
66 fclose(fp);
67
68 return(0);
69}
70/******************************************************************************/
71
72/******************************************************************************/
79 char *filename,
81 DFT *dft
82) {
83 int ri, fi, n;
84 char tmp[MAX_IDWC_LINE_LEN], *lptr, *cptr;
85 FILE *fp;
86
87 /* Check the arguments */
88 if(filename==NULL || dft==NULL || strlen(filename)<1) {
89 strcpy(dfterrmsg, "program error"); return(1);
90 }
91
92 /* Open file */
93 fp=fopen(filename, "r");
94 if(fp==NULL) {
95 strcpy(dfterrmsg, "cannot open file"); return(2);
96 }
97
98 /* Read the line telling the sample number */
99 fi=0; do {
100 if(fgets(tmp, MAX_IDWC_LINE_LEN, fp)==NULL) {
101 strcpy(dfterrmsg, "wrong format"); fclose(fp); return(3);}
102 lptr=tmp;
103 /* Read first token, and check for empty lines as well */
104 cptr=strtok(lptr, "; \t\n\r"); if(cptr==NULL) continue;
105 /* Check for comment line */
106 if(cptr[0]=='#' || cptr[0]==';') continue;
107 /* Read the sample number */
108 fi=atoi(cptr); break;
109 } while(1);
110 if(fi<1) {strcpy(dfterrmsg, "wrong format"); fclose(fp); return(3);}
111
112 /* Read the line number */
113 n=0; while(fgets(tmp, MAX_IDWC_LINE_LEN, fp)!=NULL) {
114 lptr=tmp;
115 /* Read first token, and check for empty lines as well */
116 cptr=strtok(lptr, "; \t\n\r"); if(cptr==NULL) continue;
117 /* Check for comment line */
118 if(cptr[0]=='#' || cptr[0]==';') continue;
119 n++;
120 }
121 /* Calculate the number of TACs */
122 ri=n/fi; //printf("frameNr=%d voiNr=%d\n", fi, ri);
123 if(ri<1) {strcpy(dfterrmsg, "wrong format"); fclose(fp); return(4);}
124
125
126 /* Allocate memory for data */
127 if(dftSetmem(dft, fi, ri)) {
128 strcpy(dfterrmsg, "out of memory"); fclose(fp); return(11);}
129 dft->frameNr=fi; dft->voiNr=ri;
130
131 /* Read the data */
132 rewind(fp); n=ri=fi=0;
133 do {
134 if(fgets(tmp, MAX_IDWC_LINE_LEN, fp)==NULL) {
135 strcpy(dfterrmsg, "wrong format");
136 fclose(fp); dftEmpty(dft); return(3);
137 }
138 lptr=tmp;
139 /* Read first token, and check for empty lines as well */
140 cptr=strtok(lptr, "; \t\n\r"); if(cptr==NULL) continue;
141 /* Check for comment line */
142 if(cptr[0]=='#' || cptr[0]==';') continue;
143 n++; //printf("%d (fi=%d, ri=%d): %s\n", n, fi, ri, tmp);
144 /* This time forget the sample number */
145 if(n==1) {continue;}
146 /* read the sample time */
147 dft->x[fi]=atof(cptr);
148 /* read sample value */
149 cptr=strtok(NULL, "; \t\n\r"); if(cptr==NULL) continue;
150 dft->voi[ri].y[fi]=atof(cptr);
151 /* read sample weight */
152 cptr=strtok(NULL, "; \t\n\r"); if(cptr==NULL) continue;
153 dft->w[fi]+=atof(cptr);
154 /* read TAC number */
155 cptr=strtok(NULL, "; \t\n\r"); if(cptr==NULL) continue;
156 if(fi==0) sprintf(dft->voi[ri].voiname, "%-*.*s",
158 else if(strncasecmp(dft->voi[ri].voiname, cptr, strlen(cptr))!=0) {
159 strcpy(dfterrmsg, "wrong format");
160 //printf("'%s' '%s'\n", dft->voi[ri].voiname, cptr);
161 fclose(fp); dftEmpty(dft); return(4);
162 }
163 fi++;
164 if(fi==dft->frameNr) {fi=0; ri++;}
165 if(ri==dft->voiNr) break;
166 } while(1);
167 dft->voiNr=ri;
168 for(fi=0; fi<dft->frameNr; fi++) dft->w[fi]/=(double)dft->voiNr;
169
170 /* close file */
171 fclose(fp);
172
173 /* Set DFT "header" */
174 dft->_type=1;
175 dft->timetype=0; dftFrametimes(dft);
176 dft->timeunit=TUNIT_SEC; /* sec */
177 dft->isweight=1;
178
179 return(0);
180}
181/******************************************************************************/
182
183/******************************************************************************/
char dfterrmsg[64]
Definition dft.c:6
int dftSetmem(DFT *data, int frameNr, int voiNr)
Definition dft.c:57
void dftEmpty(DFT *data)
Definition dft.c:20
void dftFrametimes(DFT *data)
Definition dft.c:340
int idwcRead(char *filename, DFT *dft)
Definition idwc.c:77
int idwcWrite(DFT *dft, char *filename)
Definition idwc.c:18
#define MAX_IDWC_LINE_LEN
Definition idwc.c:7
Header file for libtpccurveio.
#define BACKUP_EXTENSION
#define MAX_REGIONSUBNAME_LEN
Definition libtpcmisc.h:158
int _type
int timetype
Voi * voi
int timeunit
double * w
int voiNr
int frameNr
int isweight
double * x
char voiname[MAX_REGIONSUBNAME_LEN+1]
double * y