annotate file-data-provider.c @ 183:791ca26fee6a

Rewrite data writing to file Fmt() uses by default truncation to conver double arguments to their text representation. Rounding must be used. Rewrite using standard C functions to get rid of this problem (and probably make it more efficient). Extend to handle arbitrary number of channels and to report errors on opening the data files.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 21 Feb 2014 18:42:30 +0100
parents 4102fe614df2
children ec81395bf08d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 /* FXAnalise data provider reading data from KK data file on disc */
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
3 #include <userint.h>
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4 #include <ansi_c.h>
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 #include <utility.h>
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #include <formatio.h>
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
8 #include "data-provider.h"
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
9
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 //#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 #define LOGFILEPATH "C:\\temp"
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 int CVICALLBACK FileDataProvider (void *functionData)
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 {
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16 int mainThreadId;
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
17 char dataFileName[MAX_PATHNAME_LEN];
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
18 int fd;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
19 int read;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
20 int year, month, day, hour, min, sec, msec;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
21 struct tm t;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
22 char line[1024];
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
23 struct event event;
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
24 double now;
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 /* get main thread id to post messages to it */
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27 mainThreadId = CmtGetMainThreadID();
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
29 /* guess current data log file name */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
30 GetCurrentDateTime(&now);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
31 GetDateTimeElements(now, NULL, NULL, NULL, &month, &day, &year);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
32 snprintf(dataFileName, sizeof(dataFileName) - 1,
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
33 "%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year % 100, month, day);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
35 /* open file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
36 fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
37 if (fd < 0) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
38 /* notify error to the main thread */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
39 SendMessage(mainThreadId, "error opening data file '%s'", dataFileName);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
40 }
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
41
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
42 /* seek to file end */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
43 SetFilePtr(fd, 0, 2);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45 while (acquiring) {
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
46
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
47 /* read one line from data file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
48 read = ReadLine(fd, line, sizeof(line) - 1);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
49 if (read < 1) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
50 Delay(0.05);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
51 continue;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
52 }
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
53
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
54 /* parse data */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
55 read = Scan(line, "%d[w2]%d[w2]%d[w2] %d[w2]%d[w2]%d[w2].%d %f %f %f %f",
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
56 &year, &month, &day, &hour, &min, &sec, &msec,
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
57 &event.data[0], &event.data[1], &event.data[2], &event.data[3]);
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
58 if (read != 11) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
59 /* notify error to the main thread */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
60 SendMessage(mainThreadId, "error parsing data log line");
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
61 /* skip line */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
62 continue;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
63 }
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
64
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
65 t.tm_hour = hour;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
66 t.tm_min = min;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
67 t.tm_sec = sec;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
68 t.tm_mday = day;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
69 /* correct month count */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
70 t.tm_mon = month - 1;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
71 /* years since 1900 */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
72 t.tm_year = year + 2000 - 1900;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
73 /* deduce if daylight saving is in place from the date and time itself */
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
74 t.tm_isdst = -1;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
75
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
76 /* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
77 event.time.tv_sec = mktime(&t);
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
78 event.time.tv_usec = msec * 1000;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
79
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
80 /* convert from kHz to Hz */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
81 event.data[0] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
82 event.data[1] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
83 event.data[2] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
84 event.data[3] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
85
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
86 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
87 CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
89 /* handle switch to next data file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
90 if ((hour == 23) && (min == 59) && (sec == 59)) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
91
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
92 /* next data file name */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
93 snprintf(dataFileName, sizeof(dataFileName) - 1,
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
94 "%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year, month, day + 1);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
95
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
96 /* close data file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
97 CloseFile(fd);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
98
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
99 /* wait for new data file to appear */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 90
diff changeset
100 int retry = 20;
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
101 while (retry--) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
102 fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
103 if (fd != -1)
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
104 break;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
105 Delay(0.01);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
106 }
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
107 }
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
108 }
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
109 return 0;
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
110 }