In [1]:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import numpy as np
import json
import csv
from collections import OrderedDict as odict
import argparse

# parser = argparse.ArgumentParser(description='Input TECPLOT file to to be converted to CSV.')
# parser.add_argument('-f','--file',  dest='input_file', required=True )
# args = parser.parse_args()
# input_file = args.input_file

input_file_prefix = "J-Chen-CommonHybrid-SA-1.SectionalCuts_"
input_file_levels = ["L1", "L2", "L3", "L4"] 
input_file_suffix = " copy.dat"

for input_file_level in input_file_levels:
    input_file = input_file_prefix + input_file_level + input_file_suffix
    with open(input_file) as f:
        print "file = " + input_file
        data = f.read()
        pieces_by_tag = re.split('TITLE     = |VARIABLES = |ZONE T=', data, flags=re.IGNORECASE)
        # Cleaning title
        title = re.sub('[\r\n\"]', '', pieces_by_tag[1])
        print "title = " + title
        # Cleaning variables and listing
        variables = re.sub('(,)(?=(?:[^"]|"[^"]*")*$)', ';', re.sub('\s+', '', pieces_by_tag[2]))
        print variables
        variables = re.split('DATASETAUXDATA', variables)[0]    
        variables = re.sub('""', ';', variables)
        variables = re.sub('"', '', variables)
        variables = re.split(';', variables) 
        n = len(variables)
        print str(n) + " variables = "
        #print variables 
        # Processing zones
        zones = pieces_by_tag[3:]
        print str(len(zones)) + " zones"
        for zone in zones:
            # Split lines
            lines = zone.splitlines()
            # Get header line of zone entry
            header = lines.pop(0)
            # Remove outside quotes 
            header = re.sub('"', '', header)
            header = re.sub('WING SECTION 10', 'S10', header)
            header = re.sub('WING SECTION 11', 'S11', header)
            header = re.sub('WING SECTION 12', 'S12', header)
            header = re.sub('WING SECTION 13', 'S13', header)
            header = re.sub('WING SECTION 14', 'S14', header)
            header = re.sub('WING SECTION 15', 'S15', header)
            header = re.sub('WING SECTION 16', 'S16', header)
            header = re.sub('WING SECTION  1', 'S01', header)
            header = re.sub('WING SECTION  2', 'S02', header)
            header = re.sub('WING SECTION  3', 'S03', header)
            header = re.sub('WING SECTION  4', 'S04', header)
            header = re.sub('WING SECTION  5', 'S05', header)
            header = re.sub('WING SECTION  6', 'S06', header)
            header = re.sub('WING SECTION  7', 'S07', header)
            header = re.sub('WING SECTION  8', 'S08', header)
            header = re.sub('WING SECTION  9', 'S09', header)      
            filename = header + '_' + input_file_level + '.csv'

            # Reconstruct data
            lines.pop(0)
            lines.pop(0)
            lines.pop(0)
            lines.pop(0)
            m = len(lines)
            print "there are " + str(m) + " lines of data" 
            data = ''.join(lines)
            data = re.split('\s+', data)
        
            # remove leading space
            data.pop(0)
            print "there are " + str(len(data)) + " elements of data" 
        
            data = map(float, data)
            data = np.fromiter(data, np.float)
            data = np.reshape(data, (-1, n))
        
            print "filename = " + filename

            writer = csv.writer(open(filename, 'w'), delimiter=',')
            writer.writerow(variables)
            for datum in data:
                writer.writerow(datum.tolist())


file = J-Chen-CommonHybrid-SA-1.SectionalCuts_L1 copy.dat
title = Chen-CommonHybrid-SA-1
9 variables = 
['X', 'Y', 'Z', 'CP', 'CF', 'CFX', 'CFY', 'CFZ', 'X/C']
16 zones
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S01_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S02_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S03_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S04_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S05_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S06_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S07_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S08_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S09_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S10_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S11_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S12_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S13_L1.csv
there are 256 lines of data
there are 2304 elements of data
filename = Chen-CommonHybrid-SA-1 - S14_L1.csv
there are 260 lines of data
there are 2340 elements of data
filename = Chen-CommonHybrid-SA-1 - S15_L1.csv
there are 260 lines of data
there are 2340 elements of data
filename = Chen-CommonHybrid-SA-1 - S16_L1.csv
file = J-Chen-CommonHybrid-SA-1.SectionalCuts_L2 copy.dat
title = Chen-CommonHybrid-SA-1
9 variables = 
['X', 'Y', 'Z', 'CP', 'CF', 'CFX', 'CFY', 'CFZ', 'X/C']
16 zones
there are 386 lines of data
there are 3474 elements of data
filename = Chen-CommonHybrid-SA-1 - S01_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S02_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S03_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S04_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S05_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S06_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S07_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S08_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S09_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S10_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S11_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S12_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S13_L2.csv
there are 384 lines of data
there are 3456 elements of data
filename = Chen-CommonHybrid-SA-1 - S14_L2.csv
there are 388 lines of data
there are 3492 elements of data
filename = Chen-CommonHybrid-SA-1 - S15_L2.csv
there are 388 lines of data
there are 3492 elements of data
filename = Chen-CommonHybrid-SA-1 - S16_L2.csv
file = J-Chen-CommonHybrid-SA-1.SectionalCuts_L3 copy.dat
title = Chen-CommonHybrid-SA-1
9 variables = 
['X', 'Y', 'Z', 'CP', 'CF', 'CFX', 'CFY', 'CFZ', 'X/C']
16 zones
there are 518 lines of data
there are 4662 elements of data
filename = Chen-CommonHybrid-SA-1 - S01_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S02_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S03_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S04_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S05_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S06_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S07_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S08_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S09_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S10_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S11_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S12_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S13_L3.csv
there are 512 lines of data
there are 4608 elements of data
filename = Chen-CommonHybrid-SA-1 - S14_L3.csv
there are 516 lines of data
there are 4644 elements of data
filename = Chen-CommonHybrid-SA-1 - S15_L3.csv
there are 516 lines of data
there are 4644 elements of data
filename = Chen-CommonHybrid-SA-1 - S16_L3.csv
file = J-Chen-CommonHybrid-SA-1.SectionalCuts_L4 copy.dat
title = Chen-CommonHybrid-SA-1
9 variables = 
['X', 'Y', 'Z', 'CP', 'CF', 'CFX', 'CFY', 'CFZ', 'X/C']
16 zones
there are 770 lines of data
there are 6930 elements of data
filename = Chen-CommonHybrid-SA-1 - S01_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S02_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S03_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S04_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S05_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S06_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S07_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S08_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S09_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S10_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S11_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S12_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S13_L4.csv
there are 768 lines of data
there are 6912 elements of data
filename = Chen-CommonHybrid-SA-1 - S14_L4.csv
there are 772 lines of data
there are 6948 elements of data
filename = Chen-CommonHybrid-SA-1 - S15_L4.csv
there are 772 lines of data
there are 6948 elements of data
filename = Chen-CommonHybrid-SA-1 - S16_L4.csv