Created
July 18, 2019 14:25
-
-
Save jasonmhite/57195ba0648fbeb2a377c9f5feb78b36 to your computer and use it in GitHub Desktop.
A quick and dirty prototype for writing SPE files in becquerel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Based off the implementation that is already in becquerel.parsers.SpeFile, | |
| # but adapted to read *some* of the info from a Spectrum instance. | |
| # I'm a little fuzzy on the details of the SPE format (e.g. what is the difference | |
| # between $ENER_CAL and $MCA_CAL) but this produces files that load without | |
| # issue in Sandia's InterSpec tool. | |
| # This is a rough implementation as a prototype, it needs a lot of work. | |
| def write_spe( | |
| spec, | |
| spectrum_id="", | |
| sample_description="", | |
| roi=None, # list of channel pairs | |
| ener_fit=None, # tuple of coeffs | |
| mca_cal=None, # tuple of coeffs | |
| shape_cal=None, # tuple of coeffs | |
| metadata=None, # dict | |
| ): | |
| s = '' | |
| s += '$SPEC_ID:\n' | |
| s += spectrum_id + '\n' | |
| s += '$SPEC_REM:\n' | |
| s += sample_description + '\n' | |
| if spec.start_time is not None: | |
| s += '$DATE_MEA:\n' | |
| s += '{:%m/%d/%Y %H:%M:%S}\n'.format(spec.start_time) | |
| s += '$MEAS_TIM:\n' | |
| livetime = 0.0 if spec.livetime is None else spec.livetime | |
| realtime = 0.0 if spec.realtime is None else spec.realtime | |
| s += '{:.0f} {:.0f}\n'.format(livetime, realtime) | |
| s += '$DATA:\n' | |
| s += '{:.0f} {:d}\n'.format(spec.channels[0], len(spec.channels)) | |
| for j in range(len(spec.channels)): | |
| s += ' {:.0f}\n'.format(spec.counts_vals[j]) | |
| if roi is not None: | |
| s += '$ROI:\n' | |
| s += '{}\n'.format(len(roi)) | |
| for (left, right) in roi: | |
| s += "{} {}\n".format(left, right) | |
| if ener_fit is not None: | |
| s += '$ENER_FIT:\n' | |
| s += '{:f} {:f}\n'.format(ener_fit[0], ener_fit[1]) | |
| if mca_cal is not None: | |
| s += '$MCA_CAL:\n' | |
| n_coeff = len(mca_cal) | |
| s += '{:d}\n'.format(n_coeff) | |
| # s += '{:E}'.format(cal_coeff[0]) | |
| for j in range(0, n_coeff): | |
| s += ' {:E}'.format(mca_cal[j]) | |
| s += '\n' | |
| if shape_cal is not None: | |
| s += '$SHAPE_CAL:\n' | |
| n_coeff = len(shape_cal) | |
| s += '{:d}\n'.format(n_coeff) | |
| s += '{:E}'.format(shape_cal[0]) | |
| for j in range(1, n_coeff): | |
| s += ' {:E}'.format(shape_cal[j]) | |
| s += '\n' | |
| if metadata is not None: | |
| for key, values in metadata.items(): | |
| s += '$' + key + ':\n' | |
| for val in values: | |
| s += str(val) + '\n' | |
| return s[:-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment