# jaz-2-irr.py
#
# Copyright Antoine Morin-Paulhus 2015
# @antoineMoPa
#
# run with: python3 jaz-2-irr.py
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import numpy as np
import sys,re

filenames = sys.argv[1:]

for filename in filenames:
    arr = np.genfromtxt(
        filename,
        skiprows=18,
        skip_footer=1,
        names='W,D,R,S'
    )
    
    # x values of interpolated values
    xs_interp = np.arange(188,1039,0.5)
    arr_interp = np.interp(
        xs_interp,
        arr['W'],
        arr['S'] - arr['D']
    )
    
    file_content = ""
    
    file_content += '" .jaz converted to .irr' + "\n"
    file_content += '" made with jaz-2-irr.py' + "\n"
    
    def get_lamp_irr_filename(filename):
        regex = re.compile(r".jaz|.JAZ")
        return regex.sub(".irr",filename)
    
    
    for i,line in enumerate(arr_interp):
        intensity = '{:.3E}'.format(line)
        file_content += " " + str(xs_interp[i]) + "  " + intensity + "\n"
    
    irr_file_name = get_lamp_irr_filename(filename)
    new_file = open(irr_file_name,"w")
    new_file.write(file_content)

