text - Read, Slice and Re-structure data file block-by-block in Python -


a text file generated fortran program contains "blocks" data, need reformatted (python script).

each "block" of data in file corresponds "time:" specified in beginning of block. "blocks" have fixed size , structure.

i need extract data "head" , "moisture" columns corresponding different "depths" (0, -1, , -2) each "time:".

note: header @ beginning not part of repeating "blocks" of data.

sample input file:

 ******* program simulation  *******   initial header information simulation                                date:   1. 6.    time:  15: 3:39  units: l = cm   , t = min  , m = mmol    time:        0.0000   node      depth      head moisture       k                     [l]        [l]    [-]        [l/t]           1     0.0000     -37.743 0.0630   0.5090e-05      2    -1.0000     -36.123 0.0750   0.5090e-05      3    -2.0000     -33.002 0.0830   0.5090e-05   end   time:      360.0000   node      depth      head moisture       k                     [l]        [l]    [-]        [l/t]          1     0.0000 -0.1000e+07 0.0450   0.1941e-32      2    -1.0000    -253.971 0.0457   0.4376e-10      3    -2.0000     -64.510 0.0525   0.2264e-06   end   time:      720.0000   node      depth      head moisture       k                     [l]        [l]    [-]        [l/t]          1     0.0000 -0.1000e+07 0.0550   0.1941e-32      2    -1.0000    -282.591 0.0456   0.2613e-10     3    -2.0000     -71.829 0.0513   0.1229e-06   end 

desired output:

time        head(depth=0)   head(depth=-1)  head(depth=-2)  moisture(depth=0)   moisture(depth=-1)  moisture(depth=-2) 0.0000      -37.743         -36.123         -33.002         0.0630              0.0750              0.0830 360.0000    -0.1000e+07     -253.971        -64.510         0.0450              0.0457              0.0525 720.0000    -0.1000e+07     -282.591        -71.829         0.0550              0.0456              0.0513 

how read input file block-by-block each "time:" "end" keywords , reformat desired output?

edit: have made couple of changes actually runs.

from itertools import chain  def get_lines(f, n=1):     return [f.next() in xrange(n)]  class blockreader(object):     def __init__(self, f, n=1):         self.f = f         self.n = n     def __iter__(self):         return self     def next(self):         return [self.f.next() in xrange(self.n)]  fmt = "{:<12}" + "{:<16}"*6 + "\n" cols = [     "time",     "head(depth=0)",     "head(depth=-1)",     "head(depth=-2)",     "moisture(depth=0)",     "moisture(depth=-1)",     "moisture(depth=-2)" ]  def main():     open("simulation.txt") inf, open("result.txt","w") outf:         # throw away input header         get_lines(inf, 5)         # write output header         outf.write(fmt.format(*cols))          # read input file in ten-line chunks         block in blockreader(inf, 10):             # grab time value             time = float(block[1].split()[1])              # grab head , moisture columns             data = (line.split()[2:4] line in block[6:9])             values = (map(float,dat) dat in data)             h,m = zip(*values)              # write data output file             outf.write(fmt.format(*chain([time],h,m)))  if __name__=="__main__":     main() 

output is

time        head(depth=0)   head(depth=-1)  head(depth=-2)  moisture(depth=0)moisture(depth=-1)moisture(depth=-2) 0.0         -37.743         -36.123         -33.002         0.063           0.075           0.083            360.0       -1000000.0      -253.971        -64.51          0.045           0.0457          0.0525           720.0       -1000000.0      -282.591        -71.829         0.055           0.0456          0.0513           

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -