unsafe protected eFileType DetermineFileType(string source_path_filename) { eFileType result = eFileType.eUnknown; mlogger.TraceEvent(LogLevels.Verbose, 33001, "Determining file type of {0}", source_path_filename); if (!File.Exists(source_path_filename)) // file is not there or no permissions { mlogger.TraceEvent(LogLevels.Warning, 33002, "Cannot access file {0}", source_path_filename); return(result); } FileInfo fi; try { fi = new System.IO.FileInfo(source_path_filename); } catch (Exception e) { mlogger.TraceException(e); return(result); } if (//(fi.Attributes & FileAttributes.Compressed) == FileAttributes.Compressed || fi.Extension.ToLower().Equals(".zip") | fi.Extension.ToLower().Equals(".zipx") | fi.Extension.ToLower().Equals(".7z")) { result = eFileType.eZip; mlogger.TraceEvent(LogLevels.Warning, 33039, "Compressed archive use is unavailable today {0}", source_path_filename); return(result); } FileStream stream; BinaryReader reader; byte[] buff; try { stream = fi.OpenRead(); reader = new BinaryReader(stream); buff = new byte[stream.Length]; } catch (Exception e) { mlogger.TraceException(e); return(result); } int thisread = 0; string str2, str2a, str2b; if (stream.Length < CALIBRATION_SAVE_RESTORE.Length) { mlogger.TraceEvent(LogLevels.Info, 33003, "Skipping this tiny file"); reader.Close(); return(result); } else if (stream.Length >= DETECTOR_SAVE_RESTORE.Length) { thisread = reader.Read(buff, 0, DETECTOR_SAVE_RESTORE.Length); // cannot throw due to length check under normal circumstances, so this is ok str2 = System.Text.ASCIIEncoding.ASCII.GetString(buff, 0, thisread); stream.Seek(0, SeekOrigin.Begin); thisread = reader.Read(buff, 0, INTEGRATED_REVIEW.Length); str2a = System.Text.ASCIIEncoding.ASCII.GetString(buff, 0, thisread); stream.Seek(0, SeekOrigin.Begin); thisread = reader.Read(buff, 0, OLD_REVIEW.Length); str2b = System.Text.ASCIIEncoding.ASCII.GetString(buff, 0, thisread); } else { mlogger.TraceEvent(LogLevels.Info, 33004, "Skipping this small file"); reader.Close(); return(result); } if (str2.Equals(DETECTOR_SAVE_RESTORE)) { result = eFileType.eInitialDataDetector; mlogger.TraceEvent(LogLevels.Info, 33009, "The file {0} is an initial data file with detector parameters", source_path_filename); } else if (str2a.Equals(INTEGRATED_REVIEW)) { result = eFileType.eNCC; mlogger.TraceEvent(LogLevels.Info, 33009, "The file {0} is a Radiation Review data file", source_path_filename); } else if (str2b.Equals(OLD_REVIEW)) { result = eFileType.eOldNCC; mlogger.TraceEvent(LogLevels.Info, 33009, "The file is an olde-style Radiation Review data file"); } else { mlogger.TraceEvent(LogLevels.Verbose, 33010, "The file is not an initial data file with detector parameters, or an NCC file"); stream.Seek(0, SeekOrigin.Begin); bool found = false; foreach (INCCFileExt fe in System.Enum.GetValues(typeof(INCCFileExt))) { if (fe < INCCFileExt.CALIB_PARAMETER_EXT && Extensions[(int)fe] == (fi.Extension.ToUpper())) { found = true; } } if (!found) { mlogger.TraceEvent(LogLevels.Verbose, 33011, "The file is not an initial data calibration or transfer file, suffix mismatch"); } else { bool calSuffix = false; calSuffix = Extensions[(int)INCCFileExt.CALIBRATION_EXT].Equals(fi.Extension.ToUpper()); if (calSuffix) { mlogger.TraceEvent(LogLevels.Verbose, 33012, "The file may be an initial data calibration or transfer file"); thisread = reader.Read(buff, 0, CALIBRATION_SAVE_RESTORE.Length); if (thisread > 0) { str2 = System.Text.ASCIIEncoding.ASCII.GetString(buff, 0, thisread); // emtpy string result should be ok here if (str2.Equals(CALIBRATION_SAVE_RESTORE)) { result = eFileType.eInitialDataCalibration; mlogger.TraceEvent(LogLevels.Info, 33013, "The file {0} is an initial data calibration file", source_path_filename); } } } else { mlogger.TraceEvent(LogLevels.Verbose, 33014, "The file may be a transfer file"); } if (result == eFileType.eUnknown) // check for transfer file now { stream.Seek(0, SeekOrigin.Begin); results_rec results = new results_rec(); double db_version = 5.0; int sz = Marshal.SizeOf(results); byte[] los_bytos = TransferUtils.TryReadBytes(reader, sz); if (los_bytos != null) fixed(byte *pData = los_bytos) { results = *(results_rec *)pData; } else { mlogger.TraceEvent(LogLevels.Warning, 33096, "Results not read", source_path_filename); reader.Close(); return(result); } if (results.db_version != db_version) { old_results_rec old_results = new old_results_rec(); sz = Marshal.SizeOf(old_results); stream.Seek(0, SeekOrigin.Begin); los_bytos = TransferUtils.TryReadBytes(reader, sz); if (los_bytos != null) { mlogger.TraceEvent(LogLevels.Verbose, 33015, "The file may be an older transfer file, from an earlier INCC version"); mlogger.TraceEvent(LogLevels.Warning, 33016, "Cannot use file {0}, not a version 5 result", source_path_filename); } else { mlogger.TraceEvent(LogLevels.Info, 33017, ("The file is not an older transfer file")); } reader.Close(); return(result); } else { mlogger.TraceEvent(LogLevels.Verbose, 33018, "The file may be a current INCC transfer file"); } string[] nums; stream.Seek(0, SeekOrigin.Begin); thisread = reader.Read(buff, 0, 9); // gotta be 9 here, I think the previous results read guarantees that str2 = System.Text.ASCIIEncoding.ASCII.GetString(buff, 0, thisread); nums = str2.Split(new char[] { '.' }, 3); // if the first 9 bytes are a date "11.08.05\0" followed by a null, then we likely have one // todo: make a better test, use the DateTimeFrom, it checks more bytes, and do a file suffix check for the meas type too if ((buff[8] == 0x0) && (nums.Length == 3)) { result = eFileType.eTransfer; mlogger.TraceEvent(LogLevels.Verbose, 33019, "The file is a likely a transfer file"); } else { mlogger.TraceEvent(LogLevels.Verbose, 33020, "The file is not a transfer file"); } } // transfer file content check } // transfer file or ini data cal content check } // transfer file or ini data cal suffix check try { reader.Close(); } catch (Exception e) { if (mlogger != null) { mlogger.TraceException(e); } } return(result); }
unsafe static void RunValuesToResults(run_rec_ext run, Cycle cycle, Multiplicity key, MultiplicityCountingRes mcr) { cycle.seq = run.run_number; cycle.TS = TimeSpan.FromSeconds(run.run_count_time); // is not always whole seconds hn 10-1. cycle.Totals = (ulong)run.run_singles; cycle.SinglesRate = run.run_singles / run.run_count_time; // table lookup on the strings, so test status is correct string s = TransferUtils.str(run.run_tests, INCC.MAX_RUN_TESTS_LENGTH); QCTestStatus qcts = QCTestStatusExtensions.FromString(s); cycle.SetQCStatus(key, qcts); // creates entry if not found mcr.Totals = cycle.Totals; mcr.TS = cycle.TS; mcr.DeadtimeCorrectedSinglesRate.v = run.run_singles_rate; mcr.DeadtimeCorrectedDoublesRate.v = run.run_doubles_rate; mcr.DeadtimeCorrectedTriplesRate.v = run.run_triples_rate; mcr.RASum = (ulong)run.run_reals_plus_acc; mcr.ASum = (ulong)run.run_acc; mcr.efficiency = run.run_multiplicity_efficiency; mcr.mass = run.run_mass; mcr.multiAlpha = run.run_multiplicity_alpha; mcr.multiplication = run.run_multiplicity_mult; cycle.HighVoltage = run.run_high_voltage; // assign the hits to a single channel (0) cycle.HitsPerChannel[0] = run.run_singles; mcr.RawSinglesRate.v = run.run_singles_rate; mcr.RawDoublesRate.v = run.run_doubles_rate; mcr.RawTriplesRate.v = run.run_triples_rate; mcr.Scaler1.v = run.run_scaler1; mcr.Scaler2.v = run.run_scaler2; mcr.Scaler1Rate.v = run.run_scaler1_rate; mcr.Scaler2Rate.v = run.run_scaler2_rate; long index = 0; for (ulong i = 0; i < INCC.SR_EX_MAX_MULT; i++) { if (run.run_mult_acc[i] > 0 || run.run_mult_reals_plus_acc[i] > 0) { index = (long)i; } } mcr.MaxBins = (ulong)index + 1; mcr.MinBins = (ulong)index + 1; mcr.NormedAMult = new ulong[mcr.MaxBins]; mcr.RAMult = new ulong[mcr.MaxBins]; mcr.UnAMult = new ulong[mcr.MaxBins]; // was not setting these to the right values hn 10-2 for (ulong i = 0; i < (ulong)mcr.MaxBins; i++) { mcr.RAMult[i] = (ulong)run.run_mult_reals_plus_acc[i]; mcr.NormedAMult[i] = (ulong)run.run_mult_acc[i]; } mcr.RASum = run.run_reals_plus_acc; mcr.ASum = run.run_acc; mcr.AB.Resize((int)mcr.MaxBins); }
//* <f> */ //*============================================================================ //* //* function name: sr_get_data() //* //* purpose: check for shift register stopped, and if so read the data and //* put it in the database run record. //* //* return value: SR_SUCCESS/SR_NOT_FINISHED/ZERO_COUNT_TIME/SR_TRY_AGAIN/FAIL //* //* special notes: if there is a failure, the shift register port is closed. //* //* revision history: //* //* date author revision //* ---- ------ -------- //* 11/30/93 Bill Harker created //* //*============================================================================*/ public unsafe int sr_get_data(ref run_rec_ext run_ptr) { sr_h.sr_rdout rdout = new sr_h.sr_rdout(); /* sr scaler data */ sr_h.sr_mult_rdout mult_rdout; /* sr multiplicity data */ sr_h.sr_mult_rdout2 mult_rdout2; /* sr multiplicity data */ int bincount = sr_h.SR_MAX_MULT; byte sr_status; int status, mult_status; ushort num_trys; ushort i, j; /* check for shift register stopped */ sr_status = 0; status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_STATUS, ref sr_status); if (status != sr_h.SR_SUCCESS) { status = sr_restart(); if (status == SUCCESS) { return(SR_TRY_AGAIN); } else { return(FAIL); } } if ((sr_status & sr_h.SR_S_FAULT) != 0) { status = sr_restart(); if (status == SUCCESS) { return(SR_TRY_AGAIN); } else { return(FAIL); } } if (((sr_status & sr_h.SR_S_TIMEOUT) == 0) && ((sr_status & sr_h.SR_S_STOPPED) == 0)) { return(SR_NOT_FINISHED); } log.TraceEvent(LogLevels.Info, 0x4f32E, "Getting data from the shift register"); /* get a set of scaler data from the shift register */ num_trys = 0; do { status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_RDOUT, ref rdout); num_trys++; } while ((status == sr_h.SR_TIMEOUT) && (num_trys <= SR_NUM_TRYS)); /* get a set of multiplicity data from the shift register */ if ((dsid.SRType == InstrType.MSR4A) || (dsid.SRType == InstrType.PSR) || AMSRFerSher) { mult_rdout = new sr_h.sr_mult_rdout(); /* sr multiplicity data */ num_trys = 0; do { Thread.Sleep(100); /* allow windows system to execute */ mult_status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_MULT_RDOUT, ref mult_rdout); num_trys++; } while ((mult_status == sr_h.SR_TIMEOUT) && (num_trys <= SR_NUM_TRYS)); } else if (JSR15orUNAPMasqueradingAsAMSR || dsid.SRType == InstrType.JSR15 || dsid.SRType == InstrType.UNAP) { bincount = sr_h.SR_MAX_MULT2; mult_rdout2 = new sr_h.sr_mult_rdout2(); /* 512 sr multiplicity data */ num_trys = 0; do { Thread.Sleep(100); /* allow windows system to execute */ mult_status = SRLib.Control(dsid.SerialPort, sr_h.SR_GET_MULT_RDOUT2, ref mult_rdout2); num_trys++; } while ((mult_status == sr_h.SR_TIMEOUT) && (num_trys <= SR_NUM_TRYS)); } else { mult_rdout = new sr_h.sr_mult_rdout(); mult_status = sr_h.SR_SUCCESS; mult_rdout.n = bincount; for (j = 0; j < bincount; j++) { mult_rdout.rpa[j] = 0; mult_rdout.a[j] = 0; } } if ((status != sr_h.SR_SUCCESS) || (mult_status != sr_h.SR_SUCCESS)) { status = sr_restart(); if (status == SUCCESS) { return(SR_TRY_AGAIN); } else { return(FAIL); } } else if (rdout.time <= 0) { return(ZERO_COUNT_TIME); } /* read high voltage */ if (dsid.SRType == InstrType.JSR12) { SRLib.Ioctl(dsid.SerialPort, sr_h.SR_JSR12_GET_HV, ref run_ptr.run_high_voltage); } else if ((dsid.SRType == InstrType.PSR) || (dsid.SRType == InstrType.AMSR) || (dsid.SRType == InstrType.UNAP)) { SRLib.Ioctl(dsid.SerialPort, sr_h.SR_PSR_GET_HV, ref run_ptr.run_high_voltage); } else if (dsid.SRType == InstrType.JSR15) // HHMR { SRLib.Ioctl(dsid.SerialPort, sr_h.SR_HHMR_GET_HV, ref run_ptr.run_high_voltage); } else if (dsid.SRType == InstrType.DGSR) { SRLib.Ioctl(dsid.SerialPort, sr_h.SR_DGSR_GET_HV, ref run_ptr.run_high_voltage); } else { run_ptr.run_high_voltage = 0.0; } /* put shift register data in run record */ /* use current date and time */ string dt = String.Empty; int x = NCCTransfer.INCC.Gen32.gen_date_time(NCCTransfer.INCC.Gen32.GEN_DTF_IAEA, ref dt); //gen_date_time (GEN_DTF_IAEA, &string_addr); //strcpy (run_date_time_string, string_addr); Byte[] dtba = Encoding.ASCII.GetBytes(dt); fixed(byte *rd = run_ptr.run_date, rt = run_ptr.run_time, tst = run_ptr.run_tests) { TransferUtils.Copy(dtba, 0, rd, 0, INCC.DATE_TIME_LENGTH); TransferUtils.Copy(dtba, 9, rt, 0, INCC.DATE_TIME_LENGTH); // ? index? TransferUtils.PassPack(tst); run_ptr.run_count_time = rdout.time; run_ptr.run_singles = rdout.totals; run_ptr.run_scaler1 = rdout.totals2; run_ptr.run_scaler2 = rdout.totals3; run_ptr.run_reals_plus_acc = rdout.rpa; if (dsid.SRType == InstrType.DGSR) // todo: gate_length2, but no-one will ever need it { run_ptr.run_acc = rdout.a * (sr_parms.gateLengthMS / sr_parms.gateLengthMS); // sr_parms.gate_length2); } else { run_ptr.run_acc = rdout.a; } fixed(double *RA = run_ptr.run_mult_reals_plus_acc, A = run_ptr.run_mult_acc) { for (i = 0; i < bincount; i++) { if (bincount == sr_h.SR_MAX_MULT) { RA[i] = mult_rdout.rpa[i]; A[i] = mult_rdout.a[i]; } else if (bincount == sr_h.SR_MAX_MULT2) { RA[i] = mult_rdout2.rpa[i]; A[i] = mult_rdout2.a[i]; } } } } return(sr_h.SR_SUCCESS); }