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 
00090     (*conf)[ibpm].digi_trigtimeoffset = 0.;
00091     (*conf)[ibpm].digi_freq = 100 * MHz;
00092     (*conf)[ibpm].digi_nbits = 14;
00093     (*conf)[ibpm].digi_nsamples = 256;
00094     (*conf)[ibpm].digi_ampnoise = 0.;
00095     (*conf)[ibpm].digi_voltageoffset = 8192;
00096     (*conf)[ibpm].digi_phasenoise = 0;
00097 
00098     for ( ipar = 0; ipar < 3; ipar++) {
00099       (*conf)[ibpm].geom_pos[ipar] = 0;
00100       (*conf)[ibpm].geom_tilt[ipar] = 0;
00101     }
00102 
00103     (*conf)[ibpm].ref_idx = 0;
00104     (*conf)[ibpm].diode_idx = 0;
00105   }
00106 
00107   /*--------------------------------------------------------------------------
00108     Next, load in each structure and fill them
00109     --------------------------------------------------------------------------
00110   */
00111   char **args, **arg_val;
00112   int num_args, iarg;
00113 
00114   for ( ibpm = 0; ibpm < *num_conf; ibpm++ ) {
00115       
00116       // Load in the structures
00117       load_struct( conf_file, &args, &arg_val, &num_args );
00118 
00119       // Fill the configurations from the arguments
00120       int bpm_temp_idx = -1;
00121 
00122       for ( iarg = 0; iarg < num_args; iarg++ ) {
00123           
00124         // Index of this structure
00125         if ( strcmp( "bpm_idx", args[iarg] ) == 0 ) {
00126           bpm_temp_idx = atoi( arg_val[iarg] );
00127           break;
00128         }
00129       }
00130 
00131       if ( bpm_temp_idx == -1 ) {
00132         bpm_error( "Index not specified BPM config structure in load_bpmconf(...)",
00133                    __FILE__, __LINE__ );
00134       }
00135       
00136       // Now for all the other parameters
00137       for ( iarg = 0; iarg < num_args; iarg++ ) {
00138 
00139         // Occasionally, there is a trailing whitespace added to the parameters. This gets rid of them.
00140         // If you can figure out why, do let me know!
00141         if ( ! isalnum( args[iarg][ strlen(args[iarg]) - 1 ] ) ) 
00142           args[iarg][ strlen(args[iarg]) - 1 ] = '\0';
00143 
00144         if ( ! isalnum( arg_val[iarg][ strlen(arg_val[iarg]) - 1 ] ) ) 
00145           {
00146             arg_val[iarg][ strlen(arg_val[iarg]) - 1 ] = '\0';
00147           }
00148 
00149         if ( strcmp( "bpm_name", args[iarg] ) == 0 ) {
00150           // ---------------------------------------------------------
00151           // BPM name
00152           if ( strlen( arg_val[iarg] ) > 20 ) {
00153             char buf[255];
00154             sprintf(buf, "BPM name \"%s\" too long. Truncating in load_bpmconf(...)", arg_val[iarg]);
00155             bpm_warning( buf, __FILE__, __LINE__);
00156             
00157             arg_val[iarg][20] = '\0';
00158           }
00159           
00160           strcpy((*conf)[bpm_temp_idx].name, arg_val[iarg]);
00161         } 
00162         else if ( strcmp( "cav_type", args[iarg] ) == 0 ) {
00163 
00164           // ---------------------------------------------------------
00165           // Cavity Type
00166 
00167           // What is the cavity type?
00168           if (strcmp( "diode", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_type = diode;
00169           else if (strcmp( "monopole", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_type = monopole;
00170           else if (strcmp( "dipole", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_type = dipole;
00171           else {
00172             char buf[255];
00173          
00174             sprintf( buf, "Unknown cavity type \"%s\". Defaulting to dipole in load_bpmconf(...)", 
00175                      arg_val[iarg] );
00176             bpm_warning( buf, __FILE__, __LINE__ );
00177             (*conf)[bpm_temp_idx].cav_type = dipole;
00178           }
00179         }
00180         else if ( strcmp( "cav_polarisation", args[iarg] ) == 0 ) {
00181 
00182           // ---------------------------------------------------------
00183           // Cavity Polarisation
00184 
00185           // What is the polarisation?
00186           if (strcmp( "horiz", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_polarisation = horiz;
00187           else if (strcmp( "vert", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_polarisation = vert;
00188           else {
00189             char buf[255];
00190          
00191             sprintf( buf, "Unknown cavity polarisation \"%s\". Defaulting to vert in load_bpmconf(...)", 
00192                      arg_val[iarg] );
00193             bpm_warning( buf, __FILE__, __LINE__ );
00194             (*conf)[bpm_temp_idx].cav_polarisation = vert;
00195           }
00196         }
00197         else if ( strcmp( "cav_phasetype", args[iarg] ) == 0 ) {
00198 
00199           // ---------------------------------------------------------
00200           // Cavity phase type
00201 
00202           // What is the phase type?
00203           if (strcmp( "randomised", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_phasetype = randomised;
00204           else if (strcmp( "locked", arg_val[iarg] ) == 0 ) (*conf)[bpm_temp_idx].cav_phasetype = locked;
00205           else {
00206             char buf[255];
00207          
00208             sprintf( buf, "Unknown cavity phase type \"%s\". Defaulting to locked in load_bpmconf(...)", 
00209                      arg_val[iarg] );
00210             bpm_warning( buf, __FILE__, __LINE__ );
00211             (*conf)[bpm_temp_idx].cav_phasetype = locked;
00212           }
00213         }
00214         else if ( strcmp( "cav_freq", args[iarg] ) == 0 ) {
00215 
00216           // ---------------------------------------------------------
00217           // Cavity frequency
00218           (*conf)[bpm_temp_idx].cav_freq = atof( arg_val[iarg] );
00219         }
00220         else if ( strcmp( "cav_decaytime", args[iarg] ) == 0 ) {
00221 
00222           // ---------------------------------------------------------
00223           // Cavity decay time
00224           (*conf)[bpm_temp_idx].cav_decaytime = atof( arg_val[iarg] );
00225         }
00226         else if ( strcmp( "cav_phase", args[iarg] ) == 0 ) {
00227 
00228           // ---------------------------------------------------------
00229           // Cavity Phase advance wrt to ref
00230           (*conf)[bpm_temp_idx].cav_phase = atof( arg_val[iarg] );
00231         }
00232         else if ( strcmp( "cav_iqrotation", args[iarg] ) == 0 ) {
00233 
00234           // ---------------------------------------------------------
00235           // Cavity IQ rotation
00236           (*conf)[bpm_temp_idx].cav_iqrotation = atof( arg_val[iarg] );
00237         }
00238         else if ( strcmp( "cav_chargesens", args[iarg] ) == 0 ) {
00239 
00240           // ---------------------------------------------------------
00241           // Cavity charge sensitivity
00242           (*conf)[bpm_temp_idx].cav_chargesens = atof( arg_val[iarg] );
00243         }
00244         else if ( strcmp( "cav_possens", args[iarg] ) == 0 ) {
00245 
00246           // ---------------------------------------------------------
00247           // Cavity position sensitivity
00248           (*conf)[bpm_temp_idx].cav_possens = atof( arg_val[iarg] );
00249         }
00250         else if ( strcmp( "cav_tiltsens", args[iarg] ) == 0 ) {
00251 
00252           // ---------------------------------------------------------
00253           // Cavity tilt sensitivity
00254           (*conf)[bpm_temp_idx].cav_tiltsens = atof( arg_val[iarg] );
00255         }
00256         else if ( strcmp( "rf_LOfreq", args[iarg] ) == 0 ) {
00257 
00258           // ---------------------------------------------------------
00259           // LO frequency of rf
00260           (*conf)[bpm_temp_idx].rf_LOfreq = atof( arg_val[iarg] );
00261         }       
00262         else if ( strcmp( "digi_trigtimeoffset", args[iarg] ) == 0 ) {
00263 
00264           // ---------------------------------------------------------
00265           // Bunch arrival time offset
00266           (*conf)[bpm_temp_idx].digi_trigtimeoffset = atof( arg_val[iarg] );
00267         }
00268         else if ( strcmp( "digi_freq", args[iarg] ) == 0 ) {
00269 
00270           // ---------------------------------------------------------
00271           // Digitisation frequency
00272           (*conf)[bpm_temp_idx].digi_freq = atof( arg_val[iarg] );
00273         }
00274         else if ( strcmp( "digi_nbits", args[iarg] ) == 0 ) {
00275 
00276           // ---------------------------------------------------------
00277           // Number of bits
00278           (*conf)[bpm_temp_idx].digi_nbits = atoi( arg_val[iarg] );
00279         }
00280         else if ( strcmp( "digi_nsamples", args[iarg] ) == 0 ) {
00281 
00282           // ---------------------------------------------------------
00283           // Number of digi samples
00284           (*conf)[bpm_temp_idx].digi_nsamples = atoi( arg_val[iarg] );
00285         }
00286         else if ( strcmp( "digi_ampnoise", args[iarg] ) == 0 ) {
00287 
00288           // ---------------------------------------------------------
00289           // Amplitude noise
00290           (*conf)[bpm_temp_idx].digi_ampnoise = atof( arg_val[iarg] );
00291         }
00292         else if ( strcmp( "digi_voltageoffset", args[iarg] ) == 0 ) {
00293 
00294           // ---------------------------------------------------------
00295           // Pedestal position
00296           (*conf)[bpm_temp_idx].digi_voltageoffset = atoi( arg_val[iarg] );
00297         }
00298         else if ( strcmp( "digi_phasenoise", args[iarg] ) == 0 ) {
00299 
00300           // ---------------------------------------------------------
00301           // Phase nosie
00302           (*conf)[bpm_temp_idx].digi_phasenoise = atof( arg_val[iarg] );
00303         }       
00304         else if ( strcmp( "geom_pos", args[iarg] ) == 0 ) {
00305 
00306           // ---------------------------------------------------------
00307           // BPM Position
00308           int ipar;
00309           char *tok = strtok( arg_val[iarg], " " );
00310           for ( ipar = 0; ipar < 3; ipar++ ) {
00311 
00312             (*conf)[bpm_temp_idx].geom_pos[ipar] = atof( tok );
00313             tok = strtok( NULL, " " );
00314           }
00315         }
00316         else if ( strcmp( "geom_tilt", args[iarg] ) == 0 ) {
00317 
00318           // ---------------------------------------------------------
00319           // BPM Tilt
00320           int ipar;
00321           char *tok = strtok( arg_val[iarg], " " );
00322           for ( ipar = 0; ipar < 3; ipar++ ) {
00323 
00324             (*conf)[bpm_temp_idx].geom_tilt[ipar] = atof( tok );
00325             tok = strtok( NULL, " " );
00326           }
00327         }
00328         else if ( strcmp( "ref_idx", args[iarg] ) == 0 ) {
00329 
00330           // ---------------------------------------------------------
00331           // Index of reference BPM
00332           (*conf)[bpm_temp_idx].ref_idx = atoi( arg_val[iarg] );        
00333 
00334           if ( ((*conf)[bpm_temp_idx].cav_type != diode ) && ((*conf)[bpm_temp_idx].cav_type != monopole ) 
00335                && (( (*conf)[bpm_temp_idx].ref_idx >= *num_conf) || ( (*conf)[bpm_temp_idx].ref_idx < 0))) {
00336             char buf[255];
00337          
00338             sprintf( buf, "Reference index (%d) outside range of BPM structure in load_bpmconf(...)",
00339                      (*conf)[bpm_temp_idx].ref_idx );
00340             bpm_warning(buf, __FILE__, __LINE__ );
00341           }
00342         }
00343         else if ( strcmp( "diode_idx", args[iarg] ) == 0 ) {
00344 
00345           // ---------------------------------------------------------
00346           // Index of Diode
00347           (*conf)[bpm_temp_idx].diode_idx = atoi( arg_val[iarg] );
00348           
00349           if ( ((*conf)[bpm_temp_idx].cav_type != diode ) &&
00350               (( (*conf)[bpm_temp_idx].diode_idx >= *num_conf) || ( (*conf)[bpm_temp_idx].diode_idx < 0))) {
00351             char buf[255];
00352          
00353             sprintf( buf, "Diode index (%d) outside range of BPM structure in load_bpmconf(...)",
00354                      (*conf)[bpm_temp_idx].diode_idx );
00355             bpm_warning(buf, __FILE__, __LINE__ );
00356           }
00357         }
00358         else if ( strcmp( "bpm_idx", args[iarg] ) == 0 ) {
00359           // ---------------------------------------------------------
00360           // The bpm index - already dealt with
00361         }
00362         else {
00363           // ---------------------------------------------------------
00364           // Unknown parameter
00365           char buf[255];
00366          
00367           sprintf( buf, "Unknown parameter (\"%s\") in bpmconf structure", args[iarg]);
00368             bpm_warning(buf, __FILE__, __LINE__ );
00369           }
00370         
00371         // Free the memory - This causes a invalid pointer error. Don't know why as yet
00372         //free( arg[iarg] );
00373         //free( arg_val[iarg] );
00374       }
00375       
00376       free( args );
00377       free( arg_val );
00378   }
00379   
00380   return BPM_SUCCESS;
00381 }

Generated on Thu Jan 10 10:18:04 2008 for libbpm by  doxygen 1.5.1