int main (int argc, char * const argv[]) { char *filename, *framedata, msg[MAXSTR]; int i, n, frameperbuf, status, outfile, opt, tmp; uint64_t nframe; float **data, ftmp, stdDev, mean; ssize_t nr; vdif_header header; int nbits = 2; int bandwidth = 64; int nchan = 1; int framesize = 8000; int iscomplex = 0; int year = -1; int month = -1; int day = -1; int dayno = -1; double mjd = -1; float tone = 6; // MHz float duration = 0; // Seconds struct option options[] = { {"bandwidth", 1, 0, 'b'}, {"day", 1, 0, 'd'}, {"dayno", 1, 0, 'D'}, {"month", 1, 0, 'm'}, {"mjd", 1, 0, 'M'}, {"year", 1, 0, 'y'}, {"time", 1, 0, 't'}, {"framesize", 1, 0, 'F'}, {"duration", 1, 0, 't'}, {"tone", 1, 0, 'T'}, {"nchan", 1, 0, 'n'}, {"nthread", 1, 0, 'N'}, {"complex", 0, 0, 'c'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; srand(time(NULL)); /* Read command line options */ while (1) { opt = getopt_long_only(argc, argv, "b:d:m:M:y:t:n:c:T:h", options, NULL); if (opt==EOF) break; #define CASEINT(ch,var) \ case ch: \ status = sscanf(optarg, "%d", &tmp); \ if (status!=1) \ fprintf(stderr, "Bad %s option %s\n", #var, optarg); \ else \ var = tmp; \ break #define CASEFLOAT(ch,var) \ case ch: \ status = sscanf(optarg, "%f", &ftmp); \ if (status!=1) \ fprintf(stderr, "Bad %s option %s\n", #var, optarg); \ else \ var = ftmp; \ break switch (opt) { CASEINT('b', bandwidth); CASEINT('d', day); CASEINT('D', dayno); CASEINT('m', month); CASEINT('M', mjd); CASEINT('y', year); CASEINT('n', nchan); CASEINT('F', framesize); CASEFLOAT('t', duration); CASEFLOAT('T', tone); case 'c': iscomplex = 1; break; case 'h': printf("Usage: generateVDIF [options]\n"); printf(" -bandwidth <BANWIDTH> Channel bandwidth in MHz (64)\n"); printf(" -n/-nchan <N> Number of channels in stream\n"); printf(" -day <DAY> Day of month of start time (now)\n"); printf(" -month <MONTH> Month of start time (now)\n"); printf(" -dayno <DAYNO> Day of year of start time (now)\n"); printf(" -year <YEAR> Year of start time (now)\n"); printf(" -time <HH:MM:SS> Year of start time (now)\n"); printf(" -mjd <MJD> MJD of start time\n"); return(1); break; case '?': default: break; } } if (argc==optind) { filename = strdup("Test.vdif"); } else { filename = strdup(argv[optind]); } int cfact = 1; if (iscomplex) cfact = 2; // Frame size and number of sampler/frame int completesample = nchan*nbits*cfact; uint64_t samplerate = bandwidth*1e6*2/cfact; framesize = (framesize/8)*8; // Round framesize multiple of 8 // need integral number of frames/sec uint64_t bytespersec = completesample*samplerate/8; // Need integral # frames/sec AND integral # completesamples/frame while (bytespersec % framesize != 0 && (framesize*8 % completesample !=0)) { framesize -=8; if (framesize <= 0) { printf("Could not select valid framesize. Aborting\n"); printf(" nbit=%d, nchan=%d, iscomplex=%d, bandwidth=%d, completesample=%d bits\n", nbits, nchan, iscomplex, bandwidth, completesample); exit(1); } } printf("Using framesize=%d\n", framesize); int samplesperframe = framesize*8/completesample; int framespersec = bytespersec/framesize; printf("DEBUG: %d %d\n", samplesperframe, framespersec); // Initialize memory data = malloc(nchan*sizeof(float*)); if (data==NULL) { perror("Memory allocation problem\n"); exit(1); } frameperbuf = BUFSIZE*1024*1024/nchan/framesize; printf("DEBUG: frameperbuf=%d\n", frameperbuf); if (duration==0) { // Just create BUFSIZE bytes nframe = frameperbuf; } else { nframe = duration*framespersec; nframe = (nframe/frameperbuf)*frameperbuf; } printf("DEBUG: Total frame = %lld\n", (long long)nframe); for (i=0;i<nchan;i++) { status = posix_memalign((void**)&data[i], 8, nframe*samplesperframe*sizeof(float)*cfact); if (status) { perror("Trying to allocate memory"); exit(EXIT_FAILURE); } } status = posix_memalign((void**)&framedata, 8, framesize); if (status) { perror("Trying to allocate memory"); exit(EXIT_FAILURE); } memset(framedata, 'Z', framesize); // THIS NEEDS TO MOVE TO MAIN LOOP // END OF MOVE LOOP double thismjd = currentmjd(); int thisday, thismonth, thisyear; double ut; mjd2cal(thismjd, &thisday, &thismonth, &thisyear, &ut); if (year==-1) year = thisyear; if (day==-1) day = thisday; if (month==-1) month = thismonth; if (dayno!=-1) dayno2cal(dayno, year, &day, &month); mjd = cal2mjd(day, month, year)+ut; status = createVDIFHeader(&header, framesize, 0, nbits, nchan, iscomplex, "Tt"); if (status!=VDIF_NOERROR) { printf("Error creating VDIF header. Aborting\n"); exit(1); } // Whack some texture to the extended bytes header.extended2 = 0xAAAAAAAA; header.extended3 = 0xBBBBBBBB; header.extended4 = 0xCCCCCCCC; setVDIFEpochMJD(&header, mjd); setVDIFFrameMJDSec(&header, (uint64_t)floor(mjd*60*60*24)); setVDIFFrameNumber(&header,0); outfile = open(filename, OPENWRITEOPTIONS, S_IRWXU|S_IRWXG|S_IRWXO); if (outfile==-1) { sprintf(msg, "Failed to open output (%s)", filename); perror(msg); exit(1); } printf("writing %s\n", filename); while (nframe>0) { printf("DEBUG: nframe=%llu\n", nframe); generateData(data, frameperbuf, samplesperframe, nchan, iscomplex, bandwidth, tone, &mean, &stdDev); for (i=0; i<frameperbuf; i++) { if (nbits==2) { if (nchan==1) { status = pack2bit1chan(data, i*samplesperframe, framedata, mean, stdDev, samplesperframe); if (status) exit(1); } else { status = pack2bitNchan(data, nchan, i*samplesperframe, framedata, mean, stdDev, samplesperframe); if (status) exit(1); } } else { printf("Unsupported number of bits\n"); exit(1); } nr = write(outfile, &header, VDIF_HEADER_BYTES); if (nr == -1) { sprintf(msg, "Writing to %s:", filename); perror(msg); exit(1); } else if (nr != VDIF_HEADER_BYTES) { printf("Error: Partial write to %s\n", filename); exit(1); } nr = write(outfile, framedata, framesize); if (nr == -1) { sprintf(msg, "Writing to %s:", filename); perror(msg); exit(1); } else if (nr != framesize) { printf("Error: Partial write to %s\n", filename); exit(1); } nextVDIFHeader(&header, framespersec); nframe--; } } close(outfile); return(0); }
int main(int argc, char **argv) { int c=0; int check = 0; int fix = 0; int verbose = 0; int test = 0; char *infile = NULL; FILE *input = stdin; FILE *output = stdout; int rate = 10000; if (argc > 1) { while ((c = getopt(argc, argv, "i:ctfr:v")) != -1) { switch(c) { case 'c': check = 1; break; case 'f': fix = 1; break; case 'i': infile = strdup(optarg); input = fopen(infile,"r"); break; case 'r': rate = atoi(optarg); break; case 't': test = 1; break; case 'v': verbose = 1; break; default: usage(); break; } } } else { usage(); } vdif_header vhdr; vdif_header last_vhdr; unsigned int frame = 0; int finished = 0; unsigned int rtn = 0; int got_first = 0; char *buffer = NULL; char *pattern = NULL; int second = 0; int last_second = 0; int last_frame=0; double MJD = 0.0; unsigned long frame_count = 0; int failed = 0; int diff_frame=0; int diff_seconds=0; if (check || fix || test) { while (!finished) { rtn = fread(&vhdr,32,1,input); if (feof(input) || rtn != 1) { if (verbose) fprintf(stderr,"chk_vdif finished:\n"); finished = 1; continue; } else { frame_count = frame_count + 1; } if (got_first == 0) { if (check && verbose) fprintf(stdout,"## Frame length: %d",vhdr.framelength8*8); buffer = malloc(vhdr.framelength8*8); assert(buffer); if (fix) { // initialise to a mean zero 2bit offset binary pattern pattern = malloc(vhdr.framelength8*8); assert(pattern); uint16_t mean_zero = 0xAA55; size_t count = 0; while (count < vhdr.framelength8*8) { memcpy(pattern+count,&mean_zero,2); count = count+2; } } got_first = 1; } else { frame = getVDIFFrameNumber(&vhdr); if (check && verbose) fprintf(stdout,"Frame: %d\n",frame); second = getVDIFFrameSecond(&vhdr); if (check && verbose) fprintf(stdout,"Second: %d\n", second) ; MJD = getVDIFDMJD(&vhdr,10000); if (check && verbose) fprintf(stdout,"MJD %f\n",MJD); if (frame_count > 2) { diff_frame = frame-last_frame; if (diff_frame > 1) { if (check) fprintf(stderr,"Skipped a frame between %d and %d\n",last_frame,frame); failed = 1; } diff_seconds = second-last_second; if (diff_seconds > 1) { if (check) fprintf(stderr,"Skipped a second between %d and %d\n",last_second,second); if (fix) { while (diff_seconds > 1) { nextVDIFHeader(&last_vhdr,rate); fwrite(&last_vhdr,VDIF_HEADER_BYTES,1,stdout); // repeat the last buffer fwrite(&buffer,(vhdr.framelength8*8-VDIF_HEADER_BYTES),1,output); last_second = getVDIFFrameSecond(&last_vhdr); diff_seconds = second-last_second; } } if (fix == 0) failed = 1; } } } if (failed == 1) { fprintf(stdout,"failed\n"); finished = 1; continue; } rtn = fread(buffer,(vhdr.framelength8*8-VDIF_HEADER_BYTES),1,input); if (feof(input) || rtn != 1) { if (verbose) fprintf(stderr,"chk_vdif finished:\n"); finished = 1; continue; } last_frame = frame; last_second = second; last_vhdr = vhdr; if (fix) { fwrite(&vhdr,VDIF_HEADER_BYTES,1,output); fwrite(buffer,(vhdr.framelength8*8-VDIF_HEADER_BYTES),1,output); } } } free(buffer); if (test && failed == 0) { fprintf(stdout,"passed\n"); } if (infile) { fclose(input); } }