bpminterface/load_bpmconf.c

Go to the documentation of this file.
00001 
00006 #include <stdio.h>
00007 #include <bpm/bpm_messages.h>
00008 #include <bpm/bpm_interface.h>
00009 #include <bpm/bpm_version.h>
00010 #include <bpm/bpm_units.h>
00011 
00012 int load_bpmconf( const char* fname, bpmconf_t **conf, int *num_conf ){
00013 
00014   if ( ! fname ) {
00015     bpm_error( "Invalid filename in load_bpmconf(...)",
00016                __FILE__, __LINE__ );
00017     return BPM_FAILURE;
00018   }
00019 
00020   if ( ! conf ) {
00021     bpm_error( "Invalid configuration pointer in load_bpmconf(...)",
00022                __FILE__, __LINE__ );
00023     return BPM_FAILURE;
00024   }
00025 
00026   if ( ! num_conf ) {
00027     bpm_error( "Invalid number pointer in load_bpmconf(...)",
00028                __FILE__, __LINE__ );
00029     return BPM_FAILURE;
00030   }
00031 
00032   /*--------------------------------------------------------------------------
00033     First, open the file, load the header info and create the structures
00034     --------------------------------------------------------------------------
00035   */
00036   
00037   FILE *conf_file = fopen( fname, "r" );
00038   int ibpm;
00039 
00040   if ( ! conf_file ) {
00041     char buf[255];
00042 
00043     sprintf( buf, "Unable to open file \"%s\" in load_bpmconf(...)", fname );
00044     bpm_error( buf, __FILE__, __LINE__ );
00045 
00046     return BPM_FAILURE;
00047   }
00048 
00049   // Retrieve the header info
00050   int num_structs;
00051   double version;
00052   if ( get_header( conf_file, &version, &num_structs ) == BPM_FAILURE ) {
00053     char buf[255];
00054 
00055     sprintf( buf, "Unable to load/find header info in file \"%s\" ", fname );
00056     bpm_error( buf, __FILE__, __LINE__ );
00057 
00058     return BPM_FAILURE;
00059   }
00060     
00061   // Check version
00062   if ( version != BPM_VERSION_VAL ) {
00063     char buf[255];
00064     
00065     sprintf( buf, "Version of file \"%s\" does not match current libbpm version", fname );
00066     bpm_warning( buf, __FILE__, __LINE__ );
00067   }
00068   
00069   // Create the structures
00070   *num_conf = num_structs;
00071   *conf = (bpmconf_t*) calloc( *num_conf, sizeof(bpmconf_t) );
00072 
00073   // Reset to deafult values
00074   int ipar;
00075   for ( ibpm = 0; ibpm < *num_conf; ibpm++ ) {
00076 
00077     (*conf)[ibpm].cav_type = dipole;
00078     (*conf)[ibpm].cav_polarisation = vert;
00079     (*conf)[ibpm].cav_phasetype = locked;
00080     (*conf)[ibpm].cav_freq = 0;
00081     (*conf)[ibpm].cav_decaytime = 0;
00082     (*conf)[ibpm].cav_phase = 0;
00083     (*conf)[ibpm].cav_iqrotation = 0;
00084     (*conf)[ibpm].cav_chargesens = 0;
00085     (*conf)[ibpm].cav_possens = 0;
00086     (*conf)[ibpm].cav_tiltsens = 0;
00087 
00088     (*conf)[ibpm].rf_LOfreq = 0;
00089     (*conf)[ibpm].rf_filtertype = nofilter;
00090     (*conf)[ibpm].rf_nfiltpars = 0;
00091     (*conf)[ibpm].rf_filterpars = NULL;
00092     (*conf)[ibpm].rf_gain = 0;
00093 
00094     (*conf)[ibpm].digi_trigtimeoffset = 0.;
00095     (*conf)[ibpm].digi_freq = 100 * MHz;
00096     (*conf)[ibpm].digi_nbits = 14;
00097     (*conf)[ibpm].digi_nsamples = 256;
00098     (*conf)[ibpm].digi_ampnoise = 0.;
00099     (*conf)[ibpm].digi_voltageoffset = 8192;
00100     (*conf)[ibpm].digi_phasenoise = 0;
00101 
00102     for ( ipar = 0; ipar < 3; ipar++) {
00103       (*conf)[ibpm].geom_pos[ipar] = 0;
00104       (*conf)[ibpm].geom_tilt[ipar] = 0;
00105     }
00106 
00107     (*conf)[ibpm].ref_idx = 0;
00108     (*conf)[ibpm].diode_idx = 0;
00109   }
00110 
00111   /*--------------------------------------------------------------------------
00112     Next, load in each structure and fill them
00113     --------------------------------------------------------------------------
00114   */
00115   char **args, **arg_val;
00116   int num_args, iarg;
00117 
00118   for ( ibpm = 0; ibpm < *num_conf; ibpm++ ) {
00119       
00120       // Load in the structures
00121       load_struct( conf_file, &args, &arg_val, &num_args );
00122 
00123       // Fill the configurations from the arguments
00124       int bpm_temp_idx = -1;
00125 
00126       for ( iarg = 0; iarg < num_args; iarg++ ) {
00127           
00128         // Index of this structure
00129         if ( strcmp( "bpm_idx", args[iarg] ) == 0 ) {
00130           bpm_temp_idx = atoi( arg_val[iarg] );
00131           break;
00132         }
00133       }
00134 
00135       if ( bpm_temp_idx == -1 ) {
00136         bpm_error( "Index not specified BPM config structure in load_bpmconf(...)",
00137                    __FILE__, __LINE__ );
00138       }
00139       
00140       // Now for all the other parameters
00141       for ( iarg = 0; iarg < num_args; iarg++ ) {
00142 
00143         // Occasionally, there is a trailing whitespace added to the parameters. This gets rid of them.
00144         // If you can figure out why, do let me know!
00145         if ( ! isalnum( args[iarg][ strlen(args[iarg]) - 1 ] ) ) 
00146           args[iarg][ strlen(args[iarg]) - 1 ] = '\0';
00147 
00148         if ( ! isalnum( arg_val[iarg][ strlen(arg_val[iarg]) - 1 ] ) ) 
00149           {
00150             arg_val[iarg][ strlen(arg_val[iarg]) - 1 ] = '\0';
00151           }
00152 
00153         if ( strcmp( "bpm_name", args[iarg] ) == 0 ) {
00154           // ---------------------------------------------------------
00155           // BPM name
00156           if ( strlen( arg_val[iarg] ) > 20 ) {
00157             char buf[255];
00158             sprintf(buf, "BPM name \"%s\" too long. Truncating in load_bpmconf(...)", arg_val[iarg]);
00159             bpm_warning( buf, __FILE__, __LINE__);
00160             
00161             arg_val[iarg][20] = '\0';
00162           }
00163           
00164           strcpy((*conf)[bpm_temp_idx].name, arg_val[iarg]);
00165         } 
00166         else if ( strcmp( "cav_type", args[iarg] ) == 0 ) {
00167 
00168           // ---------------------------------------------------------
00169           // Cavity Type
00170 
00171           // What is the cavity type?
00172           if (strcmp( "diode", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_type = diode;
00173           else if (strcmp( "monopole", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_type = monopole;
00174           else if (strcmp( "dipole", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_type = dipole;
00175           else {
00176             char buf[255];
00177          
00178             sprintf( buf, "Unknown cavity type \"%s\". Defaulting to dipole in load_bpmconf(...)", 
00179                      arg_val[iarg] );
00180             bpm_warning( buf, __FILE__, __LINE__ );
00181             (*conf)[bpm_temp_idx].cav_type = dipole;
00182           }
00183         }
00184         else if ( strcmp( "cav_polarisation", args[iarg] ) == 0 ) {
00185 
00186           // ---------------------------------------------------------
00187           // Cavity Polarisation
00188 
00189           // What is the polarisation?
00190           if (strcmp( "horiz", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_polarisation = horiz;
00191           else if (strcmp( "vert", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_polarisation = vert;
00192           else {
00193             char buf[255];
00194          
00195             sprintf( buf, "Unknown cavity polarisation \"%s\". Defaulting to vert in load_bpmconf(...)", 
00196                      arg_val[iarg] );
00197             bpm_warning( buf, __FILE__, __LINE__ );
00198             (*conf)[bpm_temp_idx].cav_polarisation = vert;
00199           }
00200         }
00201         else if ( strcmp( "cav_phasetype", args[iarg] ) == 0 ) {
00202 
00203           // ---------------------------------------------------------
00204           // Cavity phase type
00205 
00206           // What is the phase type?
00207           if (strcmp( "randomised", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_phasetype = randomised;
00208           else if (strcmp( "locked", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_phasetype = locked;
00209           else {
00210             char buf[255];
00211          
00212             sprintf( buf, "Unknown cavity phase type \"%s\". Defaulting to locked in load_bpmconf(...)", 
00213                      arg_val[iarg] );
00214             bpm_warning( buf, __FILE__, __LINE__ );
00215             (*conf)[bpm_temp_idx].cav_phasetype = locked;
00216           }
00217         }
00218         else if ( strcmp( "cav_freq", args[iarg] ) == 0 ) {
00219 
00220           // ---------------------------------------------------------
00221           // Cavity frequency
00222           (*conf)[bpm_temp_idx].cav_freq = atof( arg_val[iarg] );
00223         }
00224         else if ( strcmp( "cav_decaytime", args[iarg] ) == 0 ) {
00225 
00226           // ---------------------------------------------------------
00227           // Cavity decay time
00228           (*conf)[bpm_temp_idx].cav_decaytime = atof( arg_val[iarg] );
00229         }
00230         else if ( strcmp( "cav_phase", args[iarg] ) == 0 ) {
00231 
00232           // ---------------------------------------------------------
00233           // Cavity Phase advance wrt to ref
00234           (*conf)[bpm_temp_idx].cav_phase = atof( arg_val[iarg] );
00235         }
00236         else if ( strcmp( "cav_iqrotation", args[iarg] ) == 0 ) {
00237 
00238           // ---------------------------------------------------------
00239           // Cavity IQ rotation
00240           (*conf)[bpm_temp_idx].cav_iqrotation = atof( arg_val[iarg] );
00241         }
00242         else if ( strcmp( "cav_chargesens", args[iarg] ) == 0 ) {
00243 
00244           // ---------------------------------------------------------
00245           // Cavity charge sensitivity
00246           (*conf)[bpm_temp_idx].cav_chargesens = atof( arg_val[iarg] );
00247         }
00248         else if ( strcmp( "cav_possens", args[iarg] ) == 0 ) {
00249 
00250           // ---------------------------------------------------------
00251           // Cavity position sensitivity
00252           (*conf)[bpm_temp_idx].cav_possens = atof( arg_val[iarg] );
00253         }
00254         else if ( strcmp( "cav_tiltsens", args[iarg] ) == 0 ) {
00255 
00256           // ---------------------------------------------------------
00257           // Cavity tilt sensitivity
00258           (*conf)[bpm_temp_idx].cav_tiltsens = atof( arg_val[iarg] );
00259         }
00260         else if ( strcmp( "rf_LOfreq", args[iarg] ) == 0 ) {
00261 
00262           // ---------------------------------------------------------
00263           // LO frequency of rf
00264           (*conf)[bpm_temp_idx].rf_LOfreq = atof( arg_val[iarg] );
00265         }       
00266         else if ( strcmp( "rf_filtertype", args[iarg] ) == 0 ) {
00267 
00268           // ---------------------------------------------------------
00269           // RF Filter type
00270           // What is the phase type?
00271           if (strcmp( "butterworth_low_pass", arg_val[iarg] ) == 0 ) {
00272             (*conf)[bpm_temp_idx].rf_filtertype = butterworth_low_pass;
00273           }
00274           else if (strcmp( "butterworth_band_pass", arg_val[iarg] ) == 0 ) {
00275             (*conf)[bpm_temp_idx].rf_filtertype = butterworth_band_pass;
00276           }
00277           else if (strcmp( "butterworth_high_pass", arg_val[iarg] ) == 0 ) {
00278             (*conf)[bpm_temp_idx].rf_filtertype = butterworth_high_pass;
00279           }
00280           else if (strcmp( "nofilter", arg_val[iarg] ) == 0 ) {
00281             (*conf)[bpm_temp_idx].rf_filtertype = nofilter;
00282           }
00283           else {
00284             char buf[255];
00285          
00286             sprintf( buf, "Unknown rf filter type \"%s\". Defaulting to nofilter in load_bpmconf(...)", 
00287                      arg_val[iarg] );
00288             bpm_warning( buf, __FILE__, __LINE__ );
00289             (*conf)[bpm_temp_idx].cav_phasetype = nofilter;
00290           }      
00291         }
00292         else if ( strcmp( "rf_nfiltpars", args[iarg] ) == 0 ) {
00293 
00294           // ---------------------------------------------------------
00295           // Number of RF filter parameters
00296           (*conf)[bpm_temp_idx].rf_nfiltpars = atoi( arg_val[iarg] );
00297           (*conf)[bpm_temp_idx].rf_filterpars = calloc( (*conf)[bpm_temp_idx].rf_nfiltpars,
00298                                                         sizeof(double) );
00299         }
00300         else if ( strcmp( "rf_filterpars", args[iarg] ) == 0 ) {
00301 
00302           // ---------------------------------------------------------
00303           // Filter parameters
00304           if ( ! (*conf)[bpm_temp_idx].rf_filterpars ) {
00305             bpm_warning( "Filter parameters given before specifying number of pars. Ignoring in load_bpmconf(...)", 
00306                          __FILE__, __LINE__ );
00307           }
00308 
00309           int ipar;
00310           char *tok = strtok( arg_val[iarg], " " );
00311           for ( ipar = 0; ipar < (*conf)[bpm_temp_idx].rf_nfiltpars; ipar++ ) {
00312 
00313             (*conf)[bpm_temp_idx].rf_filterpars[ipar] = atof( tok );
00314             tok = strtok( NULL, " " );
00315           }
00316         }
00317         else if ( strcmp( "rf_gain", args[iarg] ) == 0 ) {
00318 
00319           // ---------------------------------------------------------
00320           // Gain of the electronics
00321           (*conf)[bpm_temp_idx].rf_gain = atof( arg_val[iarg] );
00322         }
00323         else if ( strcmp( "digi_trigtimeoffset", args[iarg] ) == 0 ) {
00324 
00325           // ---------------------------------------------------------
00326           // Bunch arrival time offset
00327           (*conf)[bpm_temp_idx].digi_trigtimeoffset = atof( arg_val[iarg] );
00328         }
00329         else if ( strcmp( "digi_freq", args[iarg] ) == 0 ) {
00330 
00331           // ---------------------------------------------------------
00332           // Digitisation frequency
00333           (*conf)[bpm_temp_idx].digi_freq = atof( arg_val[iarg] );
00334         }
00335         else if ( strcmp( "digi_nbits", args[iarg] ) == 0 ) {
00336 
00337           // ---------------------------------------------------------
00338           // Number of bits
00339           (*conf)[bpm_temp_idx].digi_nbits = atoi( arg_val[iarg] );
00340         }
00341         else if ( strcmp( "digi_nsamples", args[iarg] ) == 0 ) {
00342 
00343           // ---------------------------------------------------------
00344           // Number of digi samples
00345           (*conf)[bpm_temp_idx].digi_nsamples = atoi( arg_val[iarg] );
00346         }
00347         else if ( strcmp( "digi_ampnoise", args[iarg] ) == 0 ) {
00348 
00349           // ---------------------------------------------------------
00350           // Amplitude noise
00351           (*conf)[bpm_temp_idx].digi_ampnoise = atof( arg_val[iarg] );
00352         }
00353         else if ( strcmp( "digi_voltageoffset", args[iarg] ) == 0 ) {
00354 
00355           // ---------------------------------------------------------
00356           // Pedestal position
00357           (*conf)[bpm_temp_idx].digi_voltageoffset = atoi( arg_val[iarg] );
00358         }
00359         else if ( strcmp( "digi_phasenoise", args[iarg] ) == 0 ) {
00360 
00361           // ---------------------------------------------------------
00362           // Phase nosie
00363           (*conf)[bpm_temp_idx].digi_phasenoise = atof( arg_val[iarg] );
00364         }       
00365         else if ( strcmp( "geom_pos", args[iarg] ) == 0 ) {
00366 
00367           // ---------------------------------------------------------
00368           // BPM Position
00369           int ipar;
00370           char *tok = strtok( arg_val[iarg], " " );
00371           for ( ipar = 0; ipar < 3; ipar++ ) {
00372 
00373             (*conf)[bpm_temp_idx].geom_pos[ipar] = atof( tok );
00374             tok = strtok( NULL, " " );
00375           }
00376         }
00377         else if ( strcmp( "geom_tilt", args[iarg] ) == 0 ) {
00378 
00379           // ---------------------------------------------------------
00380           // BPM Tilt
00381           int ipar;
00382           char *tok = strtok( arg_val[iarg], " " );
00383           for ( ipar = 0; ipar < 3; ipar++ ) {
00384 
00385             (*conf)[bpm_temp_idx].geom_tilt[ipar] = atof( tok );
00386             tok = strtok( NULL, " " );
00387           }
00388         }
00389         else if ( strcmp( "ref_idx", args[iarg] ) == 0 ) {
00390 
00391           // ---------------------------------------------------------
00392           // Index of reference BPM
00393           (*conf)[bpm_temp_idx].ref_idx = atoi( arg_val[iarg] );        
00394 
00395           if ( ((*conf)[bpm_temp_idx].cav_type != diode ) && ((*conf)[bpm_temp_idx].cav_type != monopole ) 
00396                && (( (*conf)[bpm_temp_idx].ref_idx >= *num_conf) || ( (*conf)[bpm_temp_idx].ref_idx < 0))) {
00397             char buf[255];
00398          
00399             sprintf( buf, "Reference index (%d) outside range of BPM structure in load_bpmconf(...)",
00400                      (*conf)[bpm_temp_idx].ref_idx );
00401             bpm_warning(buf, __FILE__, __LINE__ );
00402           }
00403         }
00404         else if ( strcmp( "diode_idx", args[iarg] ) == 0 ) {
00405 
00406           // ---------------------------------------------------------
00407           // Index of Diode
00408           (*conf)[bpm_temp_idx].diode_idx = atoi( arg_val[iarg] );
00409           
00410           if ( ((*conf)[bpm_temp_idx].cav_type != diode ) &&
00411               (( (*conf)[bpm_temp_idx].diode_idx >= *num_conf) || ( (*conf)[bpm_temp_idx].diode_idx < 0))) {
00412             char buf[255];
00413          
00414             sprintf( buf, "Diode index (%d) outside range of BPM structure in load_bpmconf(...)",
00415                      (*conf)[bpm_temp_idx].diode_idx );
00416             bpm_warning(buf, __FILE__, __LINE__ );
00417           }
00418         }
00419         else if ( strcmp( "bpm_idx", args[iarg] ) == 0 ) {
00420           // ---------------------------------------------------------
00421           // The bpm index - already dealt with
00422         }
00423         else {
00424           // ---------------------------------------------------------
00425           // Unknown parameter
00426           char buf[255];
00427          
00428           sprintf( buf, "Unknown parameter (\"%s\") in bpmconf structure", args[iarg]);
00429             bpm_warning(buf, __FILE__, __LINE__ );
00430           }
00431         
00432         // Free the memory - This causes a invalid pointer error. Don't know why as yet
00433         //free( arg[iarg] );
00434         //free( arg_val[iarg] );
00435       }
00436       
00437       free( args );
00438       free( arg_val );
00439   }
00440   
00441   return BPM_SUCCESS;
00442 }

Generated on Fri Nov 9 21:17:10 2007 for libbpm by  doxygen 1.5.1