Gundro
Gundro

Reputation: 129

Read a fortran binary file in a tar.gz archvie using python

I have a Fortran code that outputs its data in a unformatted file (binary file). I also have a python diagnostic code that reads that binary data in order to analyse it.

for ip in range(np.size(fileNames)):
    f = FortranFile(fileNames[ip], 'r')
    ints = f.read_record([('nx', 'i4'), ('nv', 'i4'), ('nl', 'i4'), ('nlb', 'i4')])
    record = f.read_record([('xx', 'f8', (nl)), ('vv', 'f8', (nv)), ('fav', 'f8', (nv)), ('f0', 'f8', (nv+2)), ('sing_f', 'f4', (nv+2,nl+2*nlb))])
    f.close()

Unfortunately I have a lot of data files, so to reduce the number of files I decided to archive the data in a tar.gz file.

For the moment I only found one way of reading my data from the archive. For this I extract the file I need from the archive, and then read it with my previous code, and once its done, deleting it.

tar = tarfile.open(tarName, "r:gz")
tarfileNames = tar.getnames()
for ip in range(np.size(tarfileNames)):
    tar.extractall(members=[x for x in tar.getmembers() if x.name in tarfileNames[ip]])
    f = FortranFile(tarfileNames[ip], 'r')
    ints = f.read_record([('nx', 'i4'), ('nv', 'i4'), ('nl', 'i4'), ('nlb', 'i4')])
    record = f.read_record([('xx', 'f8', (nl)), ('vv', 'f8', (nv)), ('fav', 'f8', (nv)), ('f0', 'f8', (nv+2)), ('sing_f', 'f4', (nv+2,nl+2*nlb))])
    f.close()
    os.remove(tarfileNames[ip])

Doing this only adds 2 seconds in total to my diagnostic (extracting and deleting each file), so not really bad. However i want to know if there is a way to do it without extracting the file.

I know you can read a data file inside a .zip file using python without extracting (I don't know if you can a binary file).

Is it possible to read a binary Fortran file inside a tar.gz with python?

Upvotes: 0

Views: 149

Answers (0)

Related Questions