<jupyter_start><jupyter_text>Reading Archaic SSW `.genx` FilesTests for reading `.genx` files better so that they are easier to work with when we are calculating response functions.<jupyter_code>from scipy.io import readsav
from astropy.table import Table,QTable
import astropy.units as u
import numpy as np
import os
def genx_to_table(input_dir,channel_list,version):
    properties = [('wave','wavelength'),('wavemin','minimum_wavelength'),('wavestep','wavelength_interval'),
                  ('wavenumsteps','number_wavelength_intervals'),('effarea','effective_area'),('geoarea','geometric_area_ccd'),
                  ('platescale','plate_scale'),('elecperdn','electron_per_dn'),('elecperev','electron_per_ev'),
                  ('fp_filter','focal_plane_filter_efficiency'),('ent_filter','entire_filter_efficiency'),
                  ('primary','primary_mirror_reflectance'),('secondary','secondary_mirror_reflectance'),
                  ('ccd','quantum_efficiency_ccd'),('contam','ccd_contamination')
                 ]
    unitless = u.m/u.m
    units = [u.angstrom,u.angstrom,u.angstrom,unitless,u.cm**2,u.cm**2,1/u.cm,u.electron/u.count,u.electron/u.eV,
             unitless,unitless,unitless,unitless,unitless,unitless]
    units = {p[1] : u for p,u in zip(properties,units)}
    units['channel'] = u.angstrom
    
    instrument_files = [os.path.join(input_dir,'aia_V{0}_all_fullinst'.format(version)),
                        os.path.join(input_dir,'aia_V{0}_fuv_fullinst'.format(version))
                       ]
    field_name = 'A{0}_FULL'
    
    #read in values
    rows = []
    for instr_file in instrument_files:
        instrument_data = readsav(instr_file)['data']
        for channel in channel_list:
            if field_name.format(channel).upper() not in instrument_data.dtype.names:
                continue
            row = {'channel':channel}
            channel_data = instrument_data[field_name.format(channel)][0]
            for prop in properties:
                if prop[0].upper() not in channel_data.dtype.names:
                    print('Cannot find {0} for channel {1} in file {2}'.format(prop[0],channel,instr_file))
                    print('Setting {} to 1'.format(prop[1]))
                    row[prop[1]] = 1
                else:
                    row[prop[1]] = channel_data[prop[0]][0]
            rows.append(row)
    
    #assign units
    table = QTable(rows=rows,names=tuple(['channel']+[p[1] for p in properties]))
    for name in table.colnames:
        try:
            table[name].unit = units[name]
        except TypeError:
            pass
        
    return table<jupyter_output><empty_output><jupyter_text>Now, test out the function.<jupyter_code>channel_list = [94,335,171,193,211,131,304,1600,1700,4500]
genx_dir = '/Users/willbarnes/Documents/Projects/gsoc/aia_response/ssw_aia_response_data/'
version = 6
table = genx_to_table(genx_dir,channel_list,version)
table<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.132907133243607, 'fraction_numerical': 0.01816958277254374, 'mean_word_length': 3.1406685236768803, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '277740', 'license': 'MIT', 'n_tokens_mistral': 985, 'n_tokens_neox': 929, 'n_words': 152, 'path': 'notebooks/read_genx_files_better.ipynb', 'repo_name': 'wtbarnes/aia_response', 'size': '2972'}