websiteWebsite
iris IRis
Analyse de critères acoustique des salles

 

Browse the code

Revision log Information on the revision
Revision: 20 (differences)
Author: Erwan JACQ <>
Log message: bugfixes
Change revision:
#! /usr/bin/python
# -*- coding: utf-8 -*-
 
"""
IRis
Copyright (C) 2010-2011   Erwan JACQ
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
 
import os
 
class result :
        def __init__(self) :
                self.name = u""
                self.source = u""
                self.rec = u""
                self.comments = u""
                self.volume = 0
                self.rays= 0
                self.T30 = None
                self.C80 = None
                self.D50 = None
                self.C50 = None
                self.LF = None
                self.G = None
                self.EDT = None
                self.STI_without_noise = None
                self.STI_with_noise = None
 
# { 31.5: 0.0, 63 : 0.0, 125 : 0.0, 250 : 0.0, 500 : 0.0, 1000 : 0.0, 2000: 0.0, 4000 : 0.0, 8000 : 0.0, 16000 : 0.0 }
 
 
def cattimporttxt(filetxt, fichier):
        print "Import Catt classic"
 
        #add a first result object (each rec will have its result object)
        results_liste = []
 
        source_name = u""
 
        #run across the lines of the text file
        l = 0
        while l < len(filetxt) :
                line = filetxt[l]
 
                #Source name
                if "Src id and loc     [m] : " in line:
                        sn = line[line.find(':')+1:line.rfind('"')].replace(' ', '')
                        if source_name == "" :
                                source_name += sn
                        else :
                                source_name += ", %s" % sn
 
                #Rec name
                if " Rec id and loc     [m] : " in line:
                        rec_name = line[line.find(':')+1:line.rfind('"')].replace(' ', '')
                        r = result()
                        r.rec = rec_name
                        r.source = source_name
                        results_liste.append(r)
 
 
                #Volume
                if " V[m" in line :
                        l += 2
                        line = filetxt[l]
                        volume = int(line.split(';')[0])
 
 
                #Criteria T30, G, D50, C80, LF
                if (" T-30  [s]" in line) or (" G    [dB]" in line) or ( " D-50  [%]" in line) or (" C-80 [dB]" in line) or (" LF    [%]" in line) :
                        criteria_title = line
                        l += 1
                        while len(filetxt[l]) > 2 :
                                line = filetxt[l]
                                line = line.split(";")
                                rec_name = line[0].replace(" ", "")
                                crit_results = [float(s.replace(",",".")) for s in line[1:7]]
                                for r in results_liste :
                                        if r.rec == rec_name :
                                                criteria = { 125 : crit_results[0], 250 : crit_results[1], 500 : crit_results[2], 1000 : crit_results[3], 2000: crit_results[4], 4000 : crit_results[5]}
                                                if " T-30  [s]" in criteria_title :
                                                        r.T30 = criteria
                                                elif " G    [dB]" in criteria_title :
                                                        r.G = criteria
                                                elif " D-50  [%]" in criteria_title :
                                                        r.D50 = criteria
                                                elif " C-80 [dB]" in criteria_title :
                                                        r.C80 = criteria
                                                elif " LF    [%]" in criteria_title :
                                                        r.LF = criteria
                                                break
                                l += 1
                        
 
                l += 1
 
        for r in results_liste :
                r.volume = volume
                r.name = u"%s - %s" %(r.source, r.rec)
        
        return results_liste
 
def tuctimporttxt(filetxt, fichier):
        print "Import TUCT"
 
        results_liste = []
        result_type = False
 
        #run across the lines of the text file
        l = 0
        while l < len(filetxt) :
                line = filetxt[l]
                
                if "CAG-file" in line : #Name of projectfile
                        debut = line[line.rfind("\\"):]
                        print debut, len(debut)
                        debut2 = line[line.rfind("/"):]
                        if len(debut2) < len(debut) :
                                debut = debut2
                        print debut, len(debut)
                        project_name = debut[1:debut.find(".CAG")]
                
                if "volume =" in line : #volume
                        volume = float(line[line.find("=")+1:line.find('(')-3].replace(",","."))
                        
                elif "Prim.rays" in line :
                        rays = int(line[line.find(':')+1:line.find('"',2)].replace(" ",""))
                        
                elif "Measures for sum of selected sources" in line : #sources
                        l += 1
                        line = filetxt[l]
                        source_name = line[line.find('"')+1:line.find('"',2)].replace(" ","")
 
 
                elif "A-w" in line : #result type
                        result_type = line[1:line.find('"',2)]
                        if result_type not in ["T-30", "C-80", "D-50", "LF", "G", "C-50", "EDT", "STI"] :
                                result_type = False
                
                elif 'IEC' in line and result_type == "STI" :
                        crit_results = line.split('\t')
                        #search for the result 
                        for r in results_liste :
                                if r.rec == crit_results[0] :
                                        rec_num = results_liste.index(r)
                                        break
                        
 
                                        
                elif '"noise off"       "(E)"' in line and result_type == "STI" :
                        r = results_liste[rec_num]
                        crit_results = line.split('\t')
                        for i in crit_results :
                                if "---" in i :
                                        crit_results[crit_results.index(i)] = "0.0"
                        criteria = { 125 : float(crit_results[2].replace(',','.')), 250 : float(crit_results[3].replace(',','.')), 500 : float(crit_results[4].replace(',','.')), 1000 : float(crit_results[5].replace(',','.')), 2000: float(crit_results[6].replace(',','.')), 4000 : float(crit_results[7].replace(',','.'))}
                        r.STI_without_noise = criteria
                        
                
                elif '"(E)"' in line and result_type != False :
                        crit_results = line.split('\t')
                        if crit_results[1] == '"(E)"' : #if not a sum of rec
                                #create the results entries
                                if result_type == "D-50" : #specify the first critera in the file
                                        r = result()
                                        r.rec = crit_results[0]
                                        results_liste.append(r)
                                        
                                elif result_type == "STI":
                                        r = results_liste[rec_num]
                                else:
                                        for r in results_liste :
                                                if r.rec == crit_results[0] :
                                                        break
                                                        
                                for i in crit_results :
                                        if "---" in i :
                                                crit_results[crit_results.index(i)] = "0.0"
                                        
                                criteria = { 125 : float(crit_results[2].replace(',','.')), 250 : float(crit_results[3].replace(',','.')), 500 : float(crit_results[4].replace(',','.')), 1000 : float(crit_results[5].replace(',','.')), 2000: float(crit_results[6].replace(',','.')), 4000 : float(crit_results[7].replace(',','.'))}
                                
                                if result_type ==  "T-30" :
                                        r.T30 = criteria
                                elif result_type ==  "G" :
                                        r.G = criteria
                                elif result_type ==  "D-50" :
                                        r.D50 = criteria
                                elif result_type ==  "C-50" :
                                        r.C50 = criteria
                                elif result_type ==  "EDT" :
                                        r.EDT = criteria
                                elif result_type ==  "C-80" :
                                        r.C80 = criteria
                                elif result_type == "STI" :
                                        if '"noise off"' in line :
                                                r.STI_without_noise = criteria
                                        elif '"noise on "' in line :
                                                r.STI_with_noise = criteria
                                        
                elif '"(h)"' in line and result_type != False :
                        crit_results = line.split('\t')
                        if crit_results[1] == '"(h)"' : #if not a sum of rec
                                for r in results_liste :
                                        if r.rec == crit_results[0] :
                                                break
                                
                                for i in crit_results :
                                        if "---" in i :
                                                crit_results[crit_results.index(i)] = "0.0"
                                        
                                criteria = { 125 : float(crit_results[2].replace(',','.')), 250 : float(crit_results[3].replace(',','.')), 500 : float(crit_results[4].replace(',','.')), 1000 : float(crit_results[5].replace(',','.')), 2000: float(crit_results[6].replace(',','.')), 4000 : float(crit_results[7].replace(',','.'))}
                                
                                if result_type ==  "LF" :
                                        r.LF = criteria
                                
                
                l +=1
 
        for r in results_liste :
                r.rays = rays
                r.volume = volume
                r.source = source_name
                if fichier == "clipboard" :
                        r.name = project_name
                else :
                        r.name = u"%s" % os.path.splitext(os.path.basename(fichier))[0]
                if fichier == "clipboard" :
                        r.comments = u"%s\nImport TUCT\nClipboard" % project_name
                else :
                        r.comments = u"%s\nImport TUCT" % os.path.basename(fichier)
 
        return results_liste
 
def auroraimporttxt(filetxt, fichier):
        def extract(line):
                #print line.split()[2:]
                a = [s for s in line.split()[2:]]
                criteria = { 125 : float(a[2]), 250 : float(a[3]), 500 : float(a[4]), 1000 : float(a[5]), 2000: float(a[6]), 4000 : float(a[7]) }
                return criteria
 
        print "Import Aurora"
 
        #Only one rec here
        results_liste = []
        r = result()
        if fichier == "clipboard" :
                r.name = u"Clipboard"
        else :
                r.name = u"%s" % os.path.basename(fichier)
        results_liste.append(r)
 
        #run across the lines of the text file
        l = 0
        while l < len(filetxt) :
                line = filetxt[l]
 
                if "C50" in line:
                        r.C50 = extract(line)
                
                if "D50" in line:
                        r.D50 = extract(line)
 
                if "T30" in line:
                        if not "r T30" in line:
                                r.T30 = extract(line)
                                print line.split()[3:]
 
                if "C80" in line:
                        r.C80 = extract(line)
 
                if "strenGth" in line:
                        r.G = extract(line)
                
                if "EDT" in line:
                        r.EDT = extract(line)
                        
                l +=1
 
        return results_liste
 
if __name__ == '__main__':
        #fichiertest = 'PARAM_A1x.TXT'
        fichiertest = 'S1-P2.txt'
        if os.path.exists(fichiertest):
                #cattimporttxt(fichiertest)
                auroraimporttxt(fichiertest)