Many people shall prefer avoiding to stage data in ASCII file on disk before performing the conversion in frames: typically they shall have a program running to produce the data, and they just want to add a few lines of code in order to save the results in frames.
This program shows how to do it: basically it goes through the following steps
FbeBuilder, and initialize with the desired settings, using New and Set functions FbeSampler as they are needed, connected to the particular FbeBuilder you are using, and initialize their settings. FbeFiler structure, initialize it and connect it to the FbeBuilder FbeFilerFree is mandatory to be sure that data are written on disk!) The program should be self explainatory, especially if you have also gone through the other examples.
/****************************************************************************** * * Copyright (C) 2001 by Andrea Vicere'. * */ #include <stdlib.h> #include <string.h> #include "fbe.h" /* This code shows how to use the Fbe library * * Its purpose is to create frames with two channels, * at different samplings. * The first channel contains a "ramp", and is saved in * a "adc" structure. * The second channel contains random numbers and is * saved in a "sim" structure */ /*********************** ramp generator *************************/ /* this structure is used to pass settings to a "ramp" generator */ struct ramp { double min; double max; double slope; double value; }; /* this is to create a "ramp" generator */ struct ramp * rampNew(double min, double max, double slope) { struct ramp * this; this = (struct ramp*) malloc(sizeof(struct ramp)); this->min = min; this->max = max; this->slope = slope; this->value = min; return this; } /* this is to run it */ int rampSim(struct ramp *this, double *value) { if(this->value >= this->max) this->value = this->min; *value = this->value; this->value += this->slope; return 0; } /* simple program to generate data from two user's functions */ int main(int argc, char * argv[]) { /* declarations */ int i, f; double tmp; int errorFlag = 0; int debug = 0; char buffer[128]; struct ramp * myRamp = NULL; struct FbeBase * fbeBuilder; double seconds_per_frame = 10.0; struct FbeBase * samplerRamp; double samplingRamp = 1024.0; double gainRamp = 2.0; int bitsRamp = 16; long nDataRamp; struct FbeBase * samplerRandom; double samplingRandom = 2048.0; double gainRandom = 1.0/((double) RAND_MAX); int bitsRandom = -32.0; long nDataRandom; struct FbeBase * oFile; int frames_per_file = 8; int total_frames = 32; /* initialize the ramp */ myRamp = (struct ramp*) rampNew(1.0, 16384.0,1.0); /* create and initialize the frame builder */ if((fbeBuilder = FbeBuilderNew(seconds_per_frame, NULL))==NULL) exit(1); sprintf(buffer, "debug=%d GTimeS=610000000 detector=test run=0 frame=47", debug); errorFlag |= FbeBuilderSet(fbeBuilder, buffer, NULL); if(errorFlag & FBE_FATAL) exit(errorFlag); /* create and initialize the sampler for the ramp: * it uses the function rampSim to get the data in */ if((samplerRamp = FbeSamplerNew(1.0/samplingRamp, fbeBuilder->frameH))==NULL) exit(1); sprintf(buffer, "debug=%d name=%s gain=%g bits=%d type=%s", debug, "ramp", gainRamp, bitsRamp, "adc"); errorFlag |= FbeSamplerSet(samplerRamp, buffer, (int (*)(void*,double*)) rampSim); /* create and initialize the sampler for the random numbers: * it does not use a function to get data */ if((samplerRandom = FbeSamplerNew(1.0/samplingRandom, fbeBuilder->frameH))==NULL) exit(1); sprintf(buffer, "debug=%d name=%s gain=%g bits=%d type=%s", debug, "random", gainRandom, bitsRandom, "sim"); errorFlag |= FbeSamplerSet(samplerRandom, buffer, NULL); if(errorFlag & FBE_FATAL) exit(errorFlag); /* create and initialize the file writer; we set some compression parameters */ if((oFile = FbeFilerNew(seconds_per_frame*frames_per_file,fbeBuilder->frameH))==NULL) exit(1); sprintf(buffer, "debug=%d name=dummy fileName=Test " "compress=3 gzip_level=1 frames_per_file=%d", debug, frames_per_file); errorFlag |= FbeFilerSet(oFile, buffer, NULL); if(errorFlag & FBE_FATAL) exit(errorFlag); /* any warnings till now ? Report and reset */ if(errorFlag) { fprintf(stderr, "%s: there were non fatal initialization errors: continuing\n", argv[0]); errorFlag = 0; } /* how many data of each channel in each frame? */ nDataRandom = (long) (seconds_per_frame * samplingRandom); nDataRamp = (long) (seconds_per_frame * samplingRamp); /* cycle reading and filling the frames */ for(f = 0; f < total_frames; f++) { /* start a new frame */ errorFlag |= FbeBuilderSim(fbeBuilder, NULL); if(errorFlag & FBE_FATAL) exit(errorFlag); /* fill the channels: ramp ... */ for(i = 0; i < nDataRamp; i++) errorFlag |= FbeSamplerSim(samplerRamp, myRamp); /* random ... */ for(i = 0; i < nDataRandom; i++) { tmp = (double) (rand() - RAND_MAX/2); errorFlag |= FbeSamplerSim(samplerRandom, &tmp); } /* close the frame and possibly write on the file */ errorFlag |= FbeFilerSim(oFile, NULL); if(errorFlag & FBE_FATAL) exit(errorFlag); } /* close the file, saving unwritten frames */ FbeFilerFree(oFile); exit(0); }
1.2.8.1 written by Dimitri van Heesch,
© 1997-2001