Home > Software design >  How to loop past metadata at the beginning of a data file?
How to loop past metadata at the beginning of a data file?

Time:03-23

I have got a data file, with metadata at the top and then 2 columns of data. What I am trying to do is loop past the metadata and then split the 2 columns into separate arrays, so I can plot them.

import numpy as np

filedata = r"/location of file"

with open(filedata, "r") as file:
    for line in enumerate(file):
        line=line.strip()
        if line == "END_OF_METADATA":
            break
        
        
data = np.genfromtxt(filedata, delimiter="\t", skip_header=True, unpack=True)

B_field=data[0,]
Frequency=data[1:,:]

My function "data" does not have dimensions for some reason, which means my for loop is not working as intended.

How do I get rid of the first few lines so the metadata at the top is omitted and its only the 2 columns of data so I can plot them?

Example of the data file in question -

START_OF_METADATA
<MetaDataAtStart>
mode=practice
noise=False
diff=False
multiple=False
DeltaH=1326.6306737319499
20GHz_peak=647260.7547863818
g=1.6025749789313735
20GHz_width=11660.90806201368
features=0
Hk=26468.153108123
Ms=73571.6334814928
alpha=0.012613061307703857
gamma=140932047084.41922
</MetaDataAtStart>
END_OF_METADATA
Magnetic Field (A/M)    20.00GHz
0.0 0.003524916631260696
1591.5494300525527  0.003542314194393793
3183.0988601051054  0.003559840873571519
4774.648290157658   0.003577497949573084
6366.197720210211   0.0035952867190977545
7957.747150262763   0.0036132084950028943
9549.296580315317   0.0036312646065461743
11140.846010367868  0.0036494563996320364
12732.395440420421  0.0036677852370624766
14323.944870472975  0.0036862524987922567
15915.494300525526  0.003704859582188614
17507.04373057808   0.0037236079022955866
19098.593160630633  0.003742498892103015
20690.142590683186  0.003761534002820334
22281.692020735736  0.0037807147041552676

CodePudding user response:

Your intention seems to be to loop through the file until you hit "END_OF_METADATA". That's the right idea. But you close the file after you've reached that line and then let np.genfromtext open it again.

Try:

with open("filedata.txt", "r") as file:
    for line in file:
        if line.strip() == "END_OF_METADATA":
            break
    data = np.genfromtxt(file, skip_header=1)

Output (print(data)):

array([[0.00000000e 00, 3.52491663e-03],
       [1.59154943e 03, 3.54231419e-03],
       [3.18309886e 03, 3.55984087e-03],
       [4.77464829e 03, 3.57749795e-03],
       [6.36619772e 03, 3.59528672e-03],
       [7.95774715e 03, 3.61320850e-03],
       [9.54929658e 03, 3.63126461e-03],
       [1.11408460e 04, 3.64945640e-03],
       [1.27323954e 04, 3.66778524e-03],
       [1.43239449e 04, 3.68625250e-03],
       [1.59154943e 04, 3.70485958e-03],
       [1.75070437e 04, 3.72360790e-03],
       [1.90985932e 04, 3.74249889e-03],
       [2.06901426e 04, 3.76153400e-03],
       [2.22816920e 04, 3.78071470e-03]])
  • Related