Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema for EPR measurement #1

Closed
schumannj opened this issue May 28, 2024 · 2 comments · Fixed by #11
Closed

Schema for EPR measurement #1

schumannj opened this issue May 28, 2024 · 2 comments · Fixed by #11
Assignees

Comments

@schumannj
Copy link
Collaborator

Schema for EPR:
- class EPR(Measurement):
- method = ‘Electron spin resonance’
- add data to the results subsection
- metadata of the instrument could go into a settings section
- files (one contains metadata and the other on the measurement data):
- ReRH_Nia-C_H_EPR_exp_raw.DSC
- ReRH_Nia-C_H_EPR_exp_raw.DTA

@schumannj
Copy link
Collaborator Author

  • reader:

      ```python
      import struct
      import os
      import csv
      import json
      from itertools import zip_longest
      
      def parse_section(text, start_marker, end_marker):
          import re
          pattern = re.compile(rf"{re.escape(start_marker)}(.*?){re.escape(end_marker)}", re.DOTALL)
          match = pattern.search(text)
          return match.group(1).strip() if match else ""
      
      def refined_parse_key_value_pairs(section_text):
          data = {}
          # Remove asterisks and leading/trailing whitespace
          clean_lines = [line.strip().replace('*', '') for line in section_text.split('\n') if line.strip() and not line.startswith('*')]
          for line in clean_lines:
              if '=' in line:
                  key, value = line.split('=', 1)
                  data[key.strip()] = value.strip()
              elif '\t' in line:
                  key, value = line.split('\t', 1)
                  data[key.strip()] = value.strip()
          return data
      
      def generate_ydata_from_binary(inputfile):
          ydata = []
          with open(inputfile, 'rb') as file:
              indata = file.read()
          for i in range(0, len(indata), 8):
              pos = struct.unpack('>d', indata[i:i+8])
              ydata.append(pos[0])
          return ydata
      
      def generate_xdata_from_params(param_dict):
          xpoints = int(param_dict['XPTS'])
          xmin = float(param_dict['XMIN'])
          xwid = float(param_dict['XWID'])
          
          xmax = xmin + xwid
          xsampling = xwid / xpoints
          
          xdata = [xmin + (xsampling * i) for i in range(xpoints)]
          return xdata
      
      def read_dsc_file(file_path):
          with open(file_path, 'r') as file:
              dsc_contents = file.read()
          return dsc_contents
      
      def read_dta_file_full(file_path):
          with open(file_path, 'rb') as file:
              binary_data = file.read()
              
              data_dict = {
                  'header_raw': binary_data[:128],  # First 128 bytes as raw header
                  'data_points_raw': binary_data[128:],  # Remaining bytes as data points
              }
              
              try:
                  data_dict['header_ascii'] = binary_data[:128].decode('ascii').strip()
              except UnicodeDecodeError:
                  data_dict['header_ascii'] = None
              
              data_points_format = 'f' * ((len(binary_data) - 128) // 4)  # Assuming 4 bytes per float32
              try:
                  data_dict['data_points_float'] = struct.unpack_from(data_points_format, binary_data, 128)
              except struct.error:
                  data_dict['data_points_float'] = None
              
              return data_dict
      
      def parse_dta_dsc(dta_file, dsc_file):
          dta_data = read_dta_file_full(dta_file)
          dsc_contents = read_dsc_file(dsc_file)
      
          desc_section = parse_section(dsc_contents, '#DESC', '#SPL')
          spl_section = parse_section(dsc_contents, '#SPL', '#DSL')
          dsl_section = parse_section(dsc_contents, '#DSL', '#MHL')
          mhl_section = parse_section(dsc_contents, '#MHL', '*')
      
          desc_data = refined_parse_key_value_pairs(desc_section)
          spl_data = refined_parse_key_value_pairs(spl_section)
          dsl_data = refined_parse_key_value_pairs(dsl_section)
          mhl_data = refined_parse_key_value_pairs(mhl_section)
      
          dta_ydata = generate_ydata_from_binary(dta_file)
          dta_xdata = generate_xdata_from_params(desc_data)
      
          if len(dta_ydata) != len(dta_xdata):
              dta_ydata = dta_ydata[:len(dta_xdata)]
      
          merged_data = {
              'header': dta_data['header_ascii'],
              'data_points': dta_data['data_points_float'],
              'descriptor_info': desc_data,
              'standard_parameters': spl_data,
              'device_specific': dsl_data,
              'manipulation_history': mhl_data,
              'x_values': dta_xdata,
              'y_values': dta_ydata
          }
      
          return merged_data
      
      dta_file_path = '/mnt/data/ReRH_Nia-C_H_EPR_exp_raw.DTA'
      dsc_file_path = '/mnt/data/ReRH_Nia-C_H_EPR_exp_raw.DSC'
      
      merged_data = parse_dta_dsc(dta_file_path, dsc_file_path)
      
      # Save the merged data into a JSON file
      final_json_file_path = '/mnt/data/ReRH_Nia-C_H_EPR_exp_full_merged.json'
      with open(final_json_file_path, 'w') as json_file:
          json.dump(merged_data, json_file)
      
      final_json_file_path
      ```
    

@ahm531
Copy link
Collaborator

ahm531 commented May 29, 2024

In case additional parameters are needed (which my be missing from the file), the EPR is plotted in the paper in Figure 3D and is related to the sample ReRH_Nia-C_H lyophilizate.

@Pepe-Marquez Pepe-Marquez linked a pull request May 29, 2024 that will close this issue
@Pepe-Marquez Pepe-Marquez self-assigned this May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants