Advertisement
shihab2196

speciesfile_parser_module_12-10-2023

Dec 10th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 28 15:24:40 2023
  4. last checked on 12/10/2023
  5.  
  6. @author: arup2
  7. """
  8. import pandas as pd
  9.  
  10. def get_species_count(speciesfile):
  11.     with open(speciesfile,'r') as sf:
  12.         species = {}
  13.         for line in sf:
  14.             if "Timestep" in line:
  15.                 headers       = line.strip().split()[1:] # not taking '#'
  16.                 species_name  = headers[3:] # skip Timestep, No_moles, No_Specs
  17.             else:
  18.                 values        = [int(x) for x in line.strip().split()]
  19.                 timestep      = values[0]
  20.                 species_count = values[3:]
  21.                 species[timestep]={}
  22.                 for key,value in zip(species_name,species_count):
  23.                     if key in species:
  24.                         species[timestep][key]+=value
  25.                     else:
  26.                         species[timestep][key] = value
  27.    
  28.     df = pd.DataFrame(species).fillna(0)
  29.     df.index.name = 'Timestep'
  30.     return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement