File indexing completed on 2025-01-05 04:11:58

0001 /***************************************************************************
0002  *                                  replay.c 
0003  *                             -------------------
0004  *                begin                : Aug 22 2006
0005  *                copyright            : (C) 2006 by Barth Netterfield
0006  *                email                :
0007  ****************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include <stdio.h>
0019 #include <stdlib.h>
0020 #include "getdata.h"
0021 #include "getdata_struct.h"
0022 #include <fcntl.h>
0023 #include <sys/stat.h>
0024 #include <sys/time.h>
0025 #include <time.h>
0026 
0027 
0028 #define OUTDIR "/data/replay"
0029 #define PREPLAY 1000
0030 #define BUFSIZE 1000000
0031 #define DT 0.2;
0032 
0033 char *dirfile;
0034 
0035 void usage() {
0036   fprintf(stderr,"usage: replay <dirfilename>\n");
0037   fprintf(stderr,"Replays a dirfile at a 5Hz frame rate, in %s\n", OUTDIR);
0038   fprintf(stderr,"This program is a hack and doesn't create %s\n", OUTDIR);
0039   fprintf(stderr,"so you need to do that by hand\n");
0040   exit(0);
0041 }
0042 
0043 void ProcessField(int fp, struct RawEntryType *R, int f0, int nf) {
0044   static char buffer[BUFSIZE];
0045   int error_code = 0;
0046 
0047   GetData(dirfile, R->field, f0, 0, nf, 0, R->type, buffer, &error_code);
0048 
0049   write(fp, buffer, nf*R->size*R->samples_per_frame); 
0050 }
0051 
0052 int main(int argc, char *argv[]) {
0053   int error_code = GD_E_OK;
0054   int N_frames;
0055   int i_raw, j_first;
0056   int *fp_out;
0057   char fieldfilename[200];
0058   char *first_field;
0059   char *field;
0060   struct FormatType *P;
0061   struct timeval tv;
0062   struct timezone zn;
0063   double last_t, new_t, dt;
0064   int nf;
0065   int f0;
0066   char copy_command[555];
0067   
0068   if (argc !=2) usage();
0069   if (argv[1][0]=='-') usage();
0070 
0071   dirfile = argv[1];
0072   P = GetFormat(argv[1], &error_code);
0073   if (error_code != GD_E_OK) {
0074     fprintf(stderr,"Error Reading format: %s in %s\n", GD_ERROR_CODES[error_code], argv[1]);
0075     exit(0);
0076   }
0077 
0078   sprintf(copy_command, "cp %s/format %s/format", argv[1], OUTDIR);
0079   system(copy_command);
0080 
0081   N_frames = GetNFrames(argv[1], &error_code, P->first_field.field);
0082   if (error_code != GD_E_OK) {
0083     fprintf(stderr,"Error: %s in %s (%s)\n", GD_ERROR_CODES[error_code], argv[1],P->first_field.field);
0084     exit(0);
0085   }
0086 
0087   fp_out = malloc(P->n_raw*sizeof(int));
0088 
0089   for (i_raw = 0; i_raw<P->n_raw; i_raw++) {
0090     sprintf(fieldfilename, "%s/%s", OUTDIR, P->rawEntries[i_raw].field);
0091     fp_out[i_raw] = open(fieldfilename, O_CREAT|O_WRONLY, 00644);
0092     if (fp_out[i_raw] < 0) {
0093       printf("could not create %s for writing\n", fieldfilename);
0094     } 
0095   }
0096 
0097 
0098   printf("%d frames available\n", N_frames);
0099 
0100   first_field = P->first_field.field;
0101   
0102   // preplay the fields
0103   for (i_raw = 0; i_raw < P->n_raw; i_raw++) {
0104     field = P->rawEntries[i_raw].field;
0105     if (strcmp(field,first_field)!=0) {
0106       ProcessField(fp_out[i_raw], &(P->rawEntries[i_raw]), 0, PREPLAY);
0107     } else {
0108       j_first = i_raw;
0109     }
0110   }
0111   ProcessField(fp_out[j_first], &(P->rawEntries[j_first]), 0, PREPLAY);
0112 
0113   printf("preplay done\n");
0114   f0 = PREPLAY;
0115 
0116   error_code = gettimeofday(&tv, &zn);
0117   last_t = (double)tv.tv_sec + (double)tv.tv_usec/1.0E6;
0118   
0119   usleep(1000);
0120   while (f0<N_frames-2) {
0121     gettimeofday(&tv, &zn);
0122     new_t = (double)tv.tv_sec + (double)tv.tv_usec/1.0E6;
0123     dt = new_t - last_t;
0124     //printf("last_t: %lg new_t: %lg dt: %lg\n", last_t, new_t, dt); 
0125     nf = dt/DT;
0126     if (nf>0) {
0127       for (i_raw = 0; i_raw < P->n_raw; i_raw++) {
0128         field = P->rawEntries[i_raw].field;
0129         if (strcmp(field,first_field)!=0) {
0130           ProcessField(fp_out[i_raw], &(P->rawEntries[i_raw]), f0, nf);
0131         } else {
0132           j_first = i_raw;
0133         }
0134       }
0135       ProcessField(fp_out[j_first], &(P->rawEntries[j_first]), f0, nf);
0136       f0 += nf;
0137       last_t+=nf*DT;
0138       printf("%d\n", f0);
0139     } else {
0140       usleep(1000);
0141     }
0142   }
0143 }
0144