#include #include #include //============================================================================== // // Title: Utilities.c // Purpose: A short description of the implementation. // // Created on: 5/7/2014 at 10:29:17 AM by Steve Marks. // Copyright: LBNL. All Rights Reserved. // //============================================================================== //============================================================================== // Include files #include "Utilities.h" #include "Hp3458.h" //============================================================================== // Constants #define LINE 80 #define NDVM 2 //============================================================================== // Types //============================================================================== // Static global variables //============================================================================== // Static functions //============================================================================== // Global variables //============================================================================== // Global functions /// HIFN What does your function do? /// HIPAR x/What inputs does your function expect? /// HIRET What does your function return? // ****************************************************************************** // // RdParamFile Reads scan parameter file. Order of paramaters is fixed, // but any number of comment lines, deliniated by '#', // may be used // int RdParamFile (char *fname) { int i; int nDVM = NDVM; char buffer[80]; FILE *fpi; // Set default parameter values operator_set = FALSE; Vx_offset = 0.0; Vy_offset = 0.0; Vx_increment = 0.0; Vy_increment = 0.0; if ((fpi = fopen (fname, "r")) == NULL) { FileSelectPopup ("W:\LCLS-II\data", "*.prm", "*.prm", "Find Parameter File", VAL_LOAD_BUTTON, 0, 0, 1, 0, fname); fpi = fopen (fname, "r"); } // Read DVM GPIB addresses for (i = 0; i < nDVM; i++) { fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%d", &GPIBDVM[i]); } // Read DVM Voltage scales for (i = 0; i < nDVM; i++) { fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &DCV[i]); } // Read integration time fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &int_tm); // Read triggering axis fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &trig_axis); // Read scan velocity fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &Sc_V); // Read x and y probe location for a scan fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &x_scan); fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &y_scan); // Read scan starting location fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &Sc_strt); // Read scan stop location fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &Sc_stp); // Read scan trigger spacing fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &trig_spc); num_pnts = (Sc_stp - Sc_strt)/trig_spc + 1; // Move scan stop point two trigger positions to make sure it gets the last trigger Sc_stp += 2*trig_spc; // Read x, y, z probe location for zero Gauss chamber #1 fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &HP_z[0]); // Read x, y, z probe location for zero Gauss chamber #2 fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &HP_z[1]); // Read Hall probe name or designator fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); for (i = 0; buffer[i] != '#'; i++) HP_ID[i] = buffer[i]; HP_ID[i] = '\0'; // Read Hall probe calibration file fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); for (i = 0; buffer[i] != '#'; i++) HP_cal_file[i] = buffer[i]; HP_cal_file[i-1] = '\0'; return 0; } // ************************************************************************************* // // RdCalFile Reads Hall probe calibration file. The following file format is assumed: // Header lines and misc comments have # as first character - not read // Vx_cal0 - calibration zero-ofset (Vx @ Bx = 0) // ncalx - number of nx, px polynomial pairs // nx px // .. // Vy_cal0 - calibration zero-ofset (Vy @ By = 0) // ncaly - number of ny, py polynomial pairs // ny py // .. void RdCalFile (char *cal_file) { int i; char buffer[80]; FILE *fpi; fpi = fopen (cal_file, "r"); fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &Vx_cal0); fgets (buffer, LINE, fpi); sscanf (buffer, "%d", &ncalx); HP_cal_nx = calloc (ncalx, sizeof(unsigned int)); HP_cal_px = calloc (ncalx, sizeof(double)); fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); for (i = 0; i < ncalx; i++) { sscanf (buffer, "%u%lf", &HP_cal_nx[i], &HP_cal_px[i]); fgets (buffer, LINE, fpi); } fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); sscanf (buffer, "%lf", &Vy_cal0); fgets (buffer, LINE, fpi); sscanf (buffer, "%d", &ncaly); HP_cal_ny = calloc (ncaly, sizeof(unsigned int)); HP_cal_py = calloc (ncaly, sizeof(double)); fgets (buffer, LINE, fpi); while (buffer[0] == '#') fgets (buffer, LINE, fpi); for (i = 0; i < ncaly; i++) { sscanf (buffer, "%u%lf", &HP_cal_ny[i], &HP_cal_py[i]); fgets (buffer, LINE, fpi); } fclose (fpi); return; } // ***************************************************************************** // // HP_Calibrate Generate B from polymial // double HP_Calibrate (double Vp, unsigned int cal_n[], double cal_p[], unsigned int np) { unsigned int i; double Bp; Bp = 0.0; for (i = 0; i < np; i++) Bp += cal_p[i]*pow(Vp, cal_n[i]); return Bp; } // ***************************************************************************** // // LogRecord Record a text line (from UI) to log. // int LogRecord (char *IDname, FILE *fpl) { char *buffer; buffer = DateStr (); fprintf (fpl, "%s\n", buffer); buffer = TimeStr (); fprintf (fpl, "%s\n", buffer); fprintf (fpl, "%s\n", und_ID); return 0; } // ***************************************************************************** // // WriteDataFile After completion of scan, write data to specified file. // int WriteDataFile (void) { int i; FILE *fpo; fpo = fopen (und_file, "w"); // Write header information fprintf (fpo, "LBNL Magnetic Measurements\n"); fprintf (fpo, "Date: %s\n", DateStr ()); fprintf (fpo, "Time: %s\n", TimeStr ()); fprintf (fpo, "\n"); fprintf (fpo, "Project: LCLS-II\n"); fprintf (fpo, "Undulator Type: %s\n", und_type); // \n contained in string fprintf (fpo, "Data Type:\n"); fprintf (fpo, "Undulator ID: %s\n", und_ID); // \n contained in string fprintf (fpo, "Data Set Number: %u\n", data_set); switch (tune_stage) { case 0 : fprintf (fpo, "Tuning Stage: Initial Data\n"); break; case 1 : fprintf (fpo, "Tuning Stage: Periodic Section\n"); break; case 2: fprintf (fpo, "Tuning Stage: End Section\n"); break; case 3: fprintf (fpo, "Tuning Stage: Verification\n"); break; default : fprintf (fpo, "Tuning Stage: Illegal Value\n"); break; } fprintf (fpo, "Run Number: %u\n", run_num); fprintf (fpo, "Measurement Type: Hall Probe Scan\n"); fprintf (fpo, "Operator: %s\n", operator); fprintf (fpo, "Comment: See Log File\n"); fprintf (fpo, "Hall Probe: %s\n", HP_ID); fprintf (fpo, "Calibration File: %s\n", HP_cal_file ); fprintf (fpo, "Data File: %s\n", und_file); fprintf (fpo, "Number of data lines: %d\n", num_pnts); fprintf (fpo, "\n"); fprintf (fpo, "Gap = \n"); fprintf (fpo, "\n"); fprintf (fpo, "Temperatures: \n"); fprintf (fpo, "\n"); fprintf (fpo, "\n"); fprintf (fpo, "Probe Position:\n"); fprintf (fpo, "x = %7.3f mm\n", x_scan); fprintf (fpo, "y = %7.3f mm\n", y_scan); fprintf (fpo, "\n"); fprintf (fpo, "Zero-Gauss Chamber Measurement at z = %f [mm]\n", HP_z[0]); fprintf (fpo, "Voltage Values:\n"); fprintf (fpo, "Vx = %f V\n", Vx_o1); fprintf (fpo, "Vy = %f V\n", Vy_o1); fprintf (fpo, "\n"); fprintf (fpo, "Zero-Gauss Chamber Measurement at z = %f [mm]\n", HP_z[1]); fprintf (fpo, "Voltage Values:\n"); fprintf (fpo, "Vx = %f V\n", Vx_o2); fprintf (fpo, "Vy = %f V\n", Vy_o2); fprintf (fpo, "\n"); fprintf (fpo, "Offset Correction: V = V - offset (offset - average of two Zero-Gauss readings\n"); fprintf (fpo, "Vx: offset = %f V\n", Vx_offset); fprintf (fpo, "Vy: offset = %f V\n", Vy_offset); // Two blank lines to align with SLAC file format fprintf (fpo, "\n"); fprintf (fpo, "\n"); // Write data fprintf (fpo, "Magnetic Field Measurements:\n"); fprintf (fpo, "\n"); fprintf (fpo, " z Vy Vx Vy_corr Vx_corr By Bx\n"); fprintf (fpo, " (m) (V) (V) (V) (V) (T) (T)\n"); fprintf (fpo, "-----------------------------------------------------------------------\n"); for (i = 0; i < num_pnts1; i++) fprintf (fpo, "%9.6lf %9.6lf %9.6lf %9.6lf %9.6lf %9.6lf %9.6lf\n", z[i], Vy[i], Vx[i], Vy_corr[i], Vx_corr[i], By[i], Bx[i]); fclose (fpo); return 0; } // ***************************************************************************** // // RefMeas Take reference Hall probe measurement // index = 0 or 1, indicating the first or second reference measurement int RefMeas (int index) { // Move to specified x, y, z location // Measure votages hp3458_get_voltage(hp3458_ID[0], &Vx_ref[index]); hp3458_get_voltage(hp3458_ID[1], &Vy_ref[index]); // Convert to B Bx_ref[index] = HP_Calibrate (Vx_ref[index], HP_cal_nx, HP_cal_px, ncalx); By_ref[index] = HP_Calibrate (Vy_ref[index], HP_cal_ny, HP_cal_py, ncaly); return 0; } double GetTemp (int channel) { return 0; } int MoveProbe (double x, double y, double z) { return 0; } // ***************************************************************************** // // LogZGauss Record ZGauss value in log. // int LogZGauss (FILE *fpl, double Vx0, double Vy0) { unsigned int i, n; double Vx_ave, Vy_ave, Vx_rms, Vy_rms; char *buffer1, *buffer2; char buffer[50], *strp; while (fgets(buffer, 50, fpl) != NULL); sscanf (buffer, "%u", &n); strp = strrchr (buffer, ':'); // Get pointer to the last : in the time stamp for (i = 0; i < 3; i++, strp++); // Advance pointer beyond time stamp sscanf (strp, "%lf %lf %lf %lf", &Vx_ave, &Vy_ave, &Vx_rms, &Vy_rms); // Calculate new ave and rms values Vx_ave = (n*Vx_ave + Vx0)/(n + 1); Vy_ave = (n*Vy_ave + Vy0)/(n + 1); Vx_rms = sqrt((n*Vx_rms*Vx_rms + (Vx0 - Vx_ave)*(Vx0 - Vx_ave))/(n + 1)); Vy_rms = sqrt((n*Vy_rms*Vy_rms + (Vy0 - Vy_ave)*(Vy0 - Vy_ave))/(n + 1)); buffer1 = DateStr (); buffer2 = TimeStr (); fprintf (fpl, "%u %s %s %12.7f %12.7f 12.7f %12.7f 12.7f %12.7f\n", buffer1, buffer2, Vx0, Vy0, Vx_ave, Vy_ave, Vy_rms, Vy_rms); return 0; } // ***************************************************************************** // // ReadZGauss Parses ZGauss log file and returns most recent values for Vx and Vy offsets // int ReadZGauss (FILE *fpl, double *Vx_ave, double *Vy_ave, double *Vx_rms, double *Vy_rms) { unsigned int i; char buffer[50]; char *strp; while (fgets(buffer, 50, fpl) != NULL); strp = strrchr (buffer, ':'); // Get pointer to the last : in the time stamp for (i = 0; i < 3; i++, strp++); // Advance pointer beyond time stamp sscanf (strp, "%lf %lf %lf %lf", Vx_ave, Vy_ave, Vx_rms, Vy_rms); return 0; }