Hi there,

I've got a little script which is not working nicely for me, hope you can help and find the problem.

I have two starting files:
traveltimes: contains the lines I need, it's a column file. The lines I need are separated by a line which starts with 11 whitespaces

header lines: contains three header lines

output_file: I want to get 29 files (STA%s). What's inside? Every file will contain the same header lines after which I want to append the group of lines contained in the traveltimes file (one different group of lines for every file). Every group of lines is made by 74307 rows (1 column)

So far this script creates 29 files with the same header lines but then it mixes up everything, I mean it writes something but it's not what I want.

Any idea????

def make_station_files(traveltimes, header_lines):
    """Gives the STAxx.tgrid files required by loc3d"""
    sta_counter = 1
   # traveltimes = ttarray_tms (traveltimes_data)
    with open (header_lines, 'r') as file_in:
        data = file_in.readlines()
        for i in range (29):
            with open ('STA%s' % (sta_counter), 'w') as output_files: 
                sta_counter += 1
                for i in data [0:3]:
                    values = i.strip()                   
                    output_files.write ("%s\n\t1\n" % (values))
                    with open (traveltimes, 'r') as times_file:
                        #collector = []
                        for line in times_file:
                                if line.startswith ("            "):
                                output_files.write ("%s" % (line))

I think the 'with open(...)' at line 13 should be indented at the same level as 'for i ...' from line 10. In the same way, you don't need the header file once it has been read, so line 7 could have the same indentation as line 5.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.