OMAPI  1.8
Open Movement Public API
convert.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2012, Newcastle University, UK.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23  * POSSIBILITY OF SUCH DAMAGE.
24  */
25 
41 /* Headers */
42 #ifdef _WIN32
43 #define _CRT_SECURE_NO_WARNINGS
44 #endif
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 
49 /* API header */
50 #include "omapi.h"
51 
52 
53 /* Conversion function */
54 int convert(const char *infile, const char *outfile, char tee)
55 {
56  FILE *ofp = NULL;
57  OmReaderHandle reader;
58  int numSamples;
59 
60  /* Open the binary file reader on the input file */
61  reader = OmReaderOpen(infile);
62  if (reader == NULL)
63  {
64  printf("ERROR: Problem opening file: %s\n", infile);
65  return -1;
66  }
67 
68  /* Open the output file (or stdout if none) */
69  if (outfile != NULL)
70  {
71  ofp = fopen(outfile, "wt");
72  }
73  if (ofp == NULL)
74  {
75  ofp = stdout;
76  }
77 
78  /* Iterate over all of the blocks of the file */
79  while ((numSamples = OmReaderNextBlock(reader)) >= 0)
80  {
81  short *buffer;
82  int i;
83 
84  buffer = OmReaderBuffer(reader);
85  for (i = 0; i < numSamples; i++)
86  {
87  OM_DATETIME dateTime;
88  unsigned short fractional;
89  short x, y, z;
90  static char timeString[25]; /* "YYYY-MM-DD hh:mm:ss.000"; */
91 
92  /* Get the date/time value for this sample, and the 1/65536th fractions of a second */
93  dateTime = OmReaderTimestamp(reader, i, &fractional);
94  sprintf(timeString, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
95  OM_DATETIME_YEAR(dateTime), OM_DATETIME_MONTH(dateTime), OM_DATETIME_DAY(dateTime),
96  OM_DATETIME_HOURS(dateTime), OM_DATETIME_MINUTES(dateTime), OM_DATETIME_SECONDS(dateTime),
97  (int)fractional * 1000 / 0xffff);
98 
99  /* Get the x/y/z/ values */
100  x = buffer[3 * i + 0];
101  y = buffer[3 * i + 1];
102  z = buffer[3 * i + 2];
103 
104  /* Output the data */
105  fprintf(ofp, "%s,%d,%d,%d\n", timeString, x, y, z);
106 
107  /* 'Tee' the data to stderr */
108  if (tee) { fprintf(stderr, "%s,%d,%d,%d\n", timeString, x, y, z); }
109  }
110  }
111 
112  /* Close the files */
113  OmReaderClose(reader);
114  if (ofp != NULL && ofp != stdout) { fclose(ofp); }
115  return 0;
116 }
117 
118 
119 /* Main function */
120 int convert_main(int argc, char *argv[])
121 {
122  printf("CONVERT: convert a specified binary data file to a CSV text file.\n");
123  printf("\n");
124  if (argc > 1)
125  {
126  const char *infile = argv[1];
127  const char *outfile = NULL;
128  char tee = 0;
129  if (argc > 2) { outfile = argv[2]; }
130  if (argc > 3 && !strcmp(argv[3], "-tee")) { tee = 1; }
131  return convert(infile, outfile, tee);
132  }
133  else
134  {
135  printf("Usage: convert <binary-input-file> [text-output-file [-tee]]\n");
136  printf("\n");
137  printf("Where: binary-input-file: the name of the binary file to convert.\n");
138  printf(" text-output-file: the name of the comma-separated-value text file to create, stdout if none.\n");
139  printf("\n");
140  printf("Example: convert data.cwa data.csv\n");
141  printf("\n");
142  }
143  return -1;
144 }
145 
omapi.h
Open Movement API.
OM_DATETIME
unsigned long OM_DATETIME
Definition: omapi.h:332
convert
int convert(const char *infile, const char *outfile, char tee)
Definition: convert.c:54
OM_DATETIME_MONTH
#define OM_DATETIME_MONTH(dateTime)
Extract the month (1-12) from a packed date/time value.
Definition: omapi.h:1082
OM_DATETIME_YEAR
#define OM_DATETIME_YEAR(dateTime)
Extract the year from a packed date/time value.
Definition: omapi.h:1081
OmReaderNextBlock
int OmReaderNextBlock(OmReaderHandle reader)
Reads the next block of data from the binary file.
OmReaderClose
void OmReaderClose(OmReaderHandle reader)
Closes the specified reader handle.
OM_DATETIME_DAY
#define OM_DATETIME_DAY(dateTime)
Extract the day (1-31) from a packed date/time value.
Definition: omapi.h:1083
OM_DATETIME_HOURS
#define OM_DATETIME_HOURS(dateTime)
Extract the hours (0-23) from a packed date/time value.
Definition: omapi.h:1084
convert_main
int convert_main(int argc, char *argv[])
Definition: convert.c:120
OM_DATETIME_SECONDS
#define OM_DATETIME_SECONDS(dateTime)
Extract the seconds (0-59) from a packed date/time value.
Definition: omapi.h:1086
OmReaderTimestamp
OM_DATETIME OmReaderTimestamp(OmReaderHandle reader, int index, unsigned short *fractional)
Determines the timestamp of the specified sample in the buffer read by OmReaderNextBlock().
printf
#define printf(...)
Definition: download.c:57
OmReaderOpen
OmReaderHandle OmReaderOpen(const char *binaryFilename)
Opens a binary data file for reading.
OM_DATETIME_MINUTES
#define OM_DATETIME_MINUTES(dateTime)
Extract the minutes (0-59) from a packed date/time value.
Definition: omapi.h:1085
OmReaderHandle
void * OmReaderHandle
Handle to a reader object.
Definition: omapi.h:1127
OmReaderBuffer
short * OmReaderBuffer(OmReaderHandle reader)
Obtains a pointer to the buffer of samples read, and unpacked, by OmReaderNextBlock().
tee
char tee
Definition: download.c:56
outfile
FILE * outfile
Definition: verify.c:154