how to read an outputted fortran binary NxNxN matrix into Python

2024/10/13 23:20:33

I wrote out a matrix in Fortran as follows:

real(kind=kind(0.0d0)), dimension(256,256,256) :: dense[...CALCULATION...]inquire(iolength=reclen)dense
open(unit=8,file=fname,&
form='unformatted',access='direct',recl=reclen)
write(unit=8,rec=1)dense(:,:,:) 
close(unit=8)

I want to read this back into Python. Everything I've seen is for 2D NxN arrays not 3D arrays. In Matlab I can read it as:

fid =    fopen(nfilename,'rb');
mesh_raw = fread(fid,ndim*ndim*ndim,'double');
fclose(fid);
mesh_reshape = reshape(mesh_raw,[ndim ndim ndim]);

I just need the equivalent in Python - presumably there is a similar load/reshape tool available. If there is a more friendly compact way to write it out for Python to understand, I am open to suggestions. It will presumably look something this: . I am just unfamiliar with the equivalent syntax for my case. A good reference would suffice. Thanks.

Answer

Using IRO-bot's link I modified/made this for my script (nothing but numpy magic):

def readslice(inputfilename,ndim):shape = (ndim,ndim,ndim)fd = open(fname, 'rb')data = np.fromfile(file=fd, dtype=np.double).reshape(shape)fd.close()return data

I did a mean,max,min & sum on the cube and it matches my fortran code. Thanks for your help.

https://en.xdnf.cn/q/69481.html

Related Q&A

python argparse subcommand with dependency and conflict

I want to use argparse to build a tool with subcommand. The possible syntax could be/tool.py download --from 1234 --interval 60 /tool.py download --build 1432 /tool.py clean --numbers 10So I want to us…

Running django project without django installation

I have developed a project with Django framework(python and mysql DB) in Linux OS(Ubuntu 12.04), I want to run this project in localhost in another machine with Linux(Ubuntu 12.04) without installing D…

redefine __and__ operator

Why I cant redefine the __and__ operator?class Cut(object):def __init__(self, cut):self.cut = cutdef __and__(self, other):return Cut("(" + self.cut + ") && (" + other.cut +…

How to find class of bound method during class construction in Python 3.1?

i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this…

Multi-Threaded NLP with Spacy pipe

Im trying to apply Spacy NLP (Natural Language Processing) pipline to a big text file like Wikipedia Dump. Here is my code based on Spacys documentation example:from spacy.en import Englishinput = open…

Django Tastypie throws a maximum recursion depth exceeded when full=True on reverse relation.

I get a maximum recursion depth exceeded if a run the code below: from tastypie import fields, utils from tastypie.resources import ModelResource from core.models import Project, Clientclass ClientReso…

Adding a colorbar to two subplots with equal aspect ratios

Im trying to add a colorbar to a plot consisting of two subplots with equal aspect ratios, i.e. with set_aspect(equal):The code used to create this plot can be found in this IPython notebook.The image …

Why is C++ much faster than python with boost?

My goal is to write a small library for spectral finite elements in Python and to that purpose I tried extending python with a C++ library using Boost, with the hope that it would make my code faster. …

pandas: How to get .to_string() method to align column headers with column values?

This has been stumping me for a while and I feel like there has to be a solution since printing a dataframe always aligns the columns headers with their respective values.example:df = pd.DataFrame({Fir…

Do I need to use `nogil` in Cython

I have some Cython code that Id like to run as quickly as possible. Do I need to release the GIL in order to do this? Lets suppose my code is similar to this: import numpy as np# trivial definition ju…