forked from xies/xies_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz2_parser.py
61 lines (43 loc) · 1.7 KB
/
z2_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 18 12:09:28 2022
@author: xies
"""
#%%
def parse_Z2(filename):
import pandas as pd
from glob import glob
from os import path
# Parse a Z2 file (histogram) into a dataframe
# Iterates through each line to find the [ demarcater and grab all numbers that comes after
# until the next [ mark. Will only extract Bindiam or Binheight
# @todo Need to modify to include fL measurement but that's easy
f = open(filename).readlines()
# start_parsing = False
parsing = False
df = pd.DataFrame()
for line in f:
#Check if we should start parsing
if line.startswith('['):
# If parsing already started, then need to save data and stop parsing
if parsing == True:
# save data into DF
df[diam_or_height] = data
# stop future parsing
parsing = False
diam_or_neight = 'None'
# If parsing has not started, then check which variable to parse into
if parsing == False and line.startswith('[#Bindiam]'):
parsing = True
diam_or_height = 'Diam'
data = []
elif parsing == False and line.startswith('[#Binheight]'):
parsing = True
diam_or_height = 'Count'
data = []
# elif --- here for fL if needed
# Make sure not to save the lines that start with '['
elif parsing:
data.append(float(line.rstrip()))
return df