Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading numpy arrays into Fortran quickly

I am currently working on a Fortran program, which requires a large data file as input. This data file is created using Python, and I am currently saving it in a human readable format using the np.savetxt() function.

However, the size of this file is very large (at least 1.5GB of disk space) and so reading in the file takes a long time. I think it might be easier to save the array of data in a binary format using np.save (or maybe pickle it?), however I have no idea how I would read this file into my Fortran program - is there a simple way to do this?

I realise that an alternative solution to this would be to entirely cut Python out of the picture and create the data array in Fortran, however as I am close to a complete beginner in Fortran I am trying to minimise the amount of things I need it for.

like image 826
Jack Avatar asked Dec 03 '25 21:12

Jack


1 Answers

It depends on your data structures, but if it is just one or a few arrays you don't need any external libraries (I am not impressed with all the hassle of NetCDF).

import numpy as np
a = np.zeros([10,10], order="F")
a.tofile("a.bin")

and

  use iso_fortran_env
  real(real64) :: a(10,10)

  open(newunit=iu,file="a.bin",access="stream",status="old",action="read")
  read(iu) a
  close(iu)
end

and that's all.

like image 157
Vladimir F Героям слава Avatar answered Dec 06 '25 09:12

Vladimir F Героям слава



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!