Skip to content

Instantly share code, notes, and snippets.

@simleb
Created October 6, 2010 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simleb/614175 to your computer and use it in GitHub Desktop.
Save simleb/614175 to your computer and use it in GitHub Desktop.
Create a CLF file out of a ASCII sheep data file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "CLF.h"
#define MAXLINE 256 /* Maximum length of a line */
int main(int argc, char * argv[])
{
int n, sheepid, id, *idframes, ntracks, nframes, mintime;
float time, *t, *x, *y, *z, *vx, *vy, *vz;
char *datapath, *clfpath;
char group, line[MAXLINE];
FILE *file;
CLF *clf;
if (argc != 9)
{
printf("usage: convert maxtracknumber maxframenumber mintime trackid sheepid group datapath clfpath\n");
return EXIT_SUCCESS;
}
ntracks = atol(argv[1]);
nframes = atol(argv[2]);
mintime = atol(argv[3]);
id = atol(argv[4]);
sheepid = atol(argv[5]);
group = argv[6][0];
datapath = malloc((strlen(argv[7])+1) * sizeof(char));
strcpy(datapath, argv[7]);
clfpath = malloc((strlen(argv[8])+1) * sizeof(char));
strcpy(clfpath, argv[8]);
if (!(file = fopen(datapath, "r")))
{
fprintf(stderr, "convert: Cannot open data file '%s'\n", datapath);
return EXIT_FAILURE;
}
/* Create a new or open an existing CLF file */
clf = CLFInit();
if (!(CLFOpen(clf, clfpath, "w") || CLFCreate(clf, clfpath, ntracks, nframes)))
{
fprintf(stderr, "convert: Cannot create or open CLF file '%s'\n", clfpath);
return EXIT_FAILURE;
}
if (CLFAddField(clf, "time", CLF_TRACK, "float"))
{
CLFAddField(clf, "id", CLF_FRAME, "int");
CLFAddField(clf, "group", CLF_FRAME, "char");
CLFAddField(clf, "x", CLF_NORMAL, "float");
CLFAddField(clf, "y", CLF_NORMAL, "float");
CLFAddField(clf, "z", CLF_NORMAL, "float");
CLFAddField(clf, "vx", CLF_NORMAL, "float");
CLFAddField(clf, "vy", CLF_NORMAL, "float");
CLFAddField(clf, "vz", CLF_NORMAL, "float");
}
/* Alloc memory */
idframes = (int*)calloc(nframes, sizeof(int));
t = (float*)calloc(nframes, sizeof(float));
x = (float*)calloc(nframes, sizeof(float));
y = (float*)calloc(nframes, sizeof(float));
z = (float*)calloc(nframes, sizeof(float));
vx = (float*)calloc(nframes, sizeof(float));
vy = (float*)calloc(nframes, sizeof(float));
vz = (float*)calloc(nframes, sizeof(float));
if (!idframes || !t || !x || !y || !z || !vx || !vy || !vz)
{
fprintf(stderr, "convert: out of memory!\n");
return EXIT_FAILURE;
}
if (sheepid == 12) /* Sheep 12 is special... */
{
/* Skip the 21 first lines */
for (n = 0; n < 21; n++)
fgets(line, MAXLINE, file);
/* For each line, store the data in memory */
for (n = 0; fgets(line, MAXLINE, file); n++)
{
time = atof(strtok(line, " "));
idframes[n] = (int)time - mintime;
t[n] = time;
y[n] = atof(strtok(NULL, " "));
x[n] = atof(strtok(NULL, " "));
atof(strtok(NULL, " "));
atof(strtok(NULL, " "));
z[n] = atof(strtok(NULL, " "));
atof(strtok(NULL, " "));
vy[n] = atof(strtok(NULL, " "));
atof(strtok(NULL, " "));
vx[n] = atof(strtok(NULL, " "));
atof(strtok(NULL, " "));
vz[n] = atof(strtok(NULL, " "));
}
}
else
{
/* Skip the 17 first lines */
for (n = 0; n < 17; n++)
fgets(line, MAXLINE, file);
/* For each line, store the data in memory */
for (n = 0; fgets(line, MAXLINE, file); n++)
{
time = atof(strtok(line, " "));
idframes[n] = (int)time - mintime;
t[n] = time;
x[n] = atof(strtok(NULL, " "));
y[n] = atof(strtok(NULL, " "));
z[n] = atof(strtok(NULL, " "));
vy[n] = atof(strtok(NULL, " "));
vz[n] = atof(strtok(NULL, " "));
vx[n] = atof(strtok(NULL, " "));
}
}
/* Close file */
fclose(file);
/* Write tracks */
CLFWriteFrame(clf, "id", 0, 1, &id, &sheepid);
CLFWriteFrame(clf, "group", 0, 1, &id, &group);
CLFWriteTrack(clf, "time", id, n, idframes, t);
CLFWriteTrack(clf, "x", id, n, idframes, x);
CLFWriteTrack(clf, "y", id, n, idframes, y);
CLFWriteTrack(clf, "z", id, n, idframes, z);
CLFWriteTrack(clf, "vx", id, n, idframes, vx);
CLFWriteTrack(clf, "vy", id, n, idframes, vy);
CLFWriteTrack(clf, "vz", id, n, idframes, vz);
/* Close CLF */
CLFClose(clf);
/* Free memory */
free(idframes);
free(t);
free(x);
free(y);
free(z);
free(vx);
free(vy);
free(vz);
free(datapath);
free(clfpath);
return EXIT_SUCCESS;
}
@simleb
Copy link
Author

simleb commented Nov 27, 2010

To build a CLF file using this little program, one typically uses:

./convert 10 91357 340159 0 gps_allday_03_03_2010/GroupA/SH1_1_UBXGPS.txt sheep_2010-03-03_GroupA.clf
./convert 10 91357 340159 1 gps_allday_03_03_2010/GroupA/SH1_2_UBXGPS.txt sheep_2010-03-03_GroupA.clf
./convert 10 91357 340159 2 gps_allday_03_03_2010/GroupA/SH1_3_UBXGPS.txt sheep_2010-03-03_GroupA.clf
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment