MRedies / NPY-for-Fortran

A FORTRAN module to write Numpy's *.npy and *.npz files
MIT License
40 stars 9 forks source link

Collaborate with Fortran Standard Library to support npy format I/O #5

Open milancurcic opened 2 years ago

milancurcic commented 2 years ago

Hi Matthias, thank you for making this useful package.

I'm one of the maintainers of Fortran stdlib. We discussed supporting some common array storage file formats in https://github.com/fortran-lang/stdlib/issues/486.

Are you interested in collaborating there on adding support for npy+npz file formats to stdlib?

certik commented 2 years ago

I second what Milan wrote. @MRedies your implementation is short and simple, I think it would be a perfect fit.

@milancurcic I think you might have tagged a wrong person.

milancurcic commented 2 years ago

@milancurcic I think you might have tagged a wrong person.

Indeed, an @ snuck in :).

MRedies commented 2 years ago

Hello,

I am glad you like it and I'd be very happy if it can be part of stdlib. What do you need and how do you want me to help?

Maybe the library could cover all datatypes and dimensions more systematically, because currently I added them kind of on personal needs.

MRedies commented 2 years ago

I am not sure how stdlib is built, but it think it would ne nice if the library can still be build without any other files. (Kind of like Boost where you can also build some modules seperately)

certik commented 2 years ago

Thank you @MRedies.

We can definitely keep it separate, as well as include it in stdlib. Given how simple it is, I think there is a way to probably have exactly the same source code, so that there is no duplication of effort.

The next step is to discuss the API. The process is described here: https://github.com/fortran-lang/stdlib/blob/master/WORKFLOW.md

We can probably do it at the issue https://github.com/fortran-lang/stdlib/issues/486, or open a new one.

TejasAvinashShetty commented 2 years ago

Is this going anywhere? @certik @MRedies @milancurcic

certik commented 2 years ago

I think I was waiting for @MRedies or @milancurcic to drive the effort :), but we got all busy.

TejasAvinashShetty commented 2 years ago

Thanks @certik for replying back so soon. I somehow have the feeling that the code could be compressed by using the

KIND 

PARAMETER to prevent the duplication of code.

TejasAvinashShetty commented 2 years ago

libnpy seems to be a library that provides simple routines for saving a C or Fortran array to a data file using NumPy's own binary format. Please see https://scipy-cookbook.readthedocs.io/items/InputOutput.html

Not my idea See first CAZT's comment on CAZT's stackoverflow answer

@certik @milancurcic

TejasAvinashShetty commented 2 years ago

The following is an excerpt from https://scipy-cookbook.readthedocs.io/items/InputOutput.html

program fex
    use fnpy
    use iso_c_binding
    implicit none

    integer  :: i
    real(C_DOUBLE) :: a(2,4) = reshape([(i, i=1,8)], [2,4])

    call save_double("fa.npy", shape(a), a)
end program fe

The program creates a file fa.npy that you can load into python in the usual way.

But the entries of the NumPy array now follow the Fortran (column-major) ordering.

>>> fa = np.load('fa.npy')
>>> print fa
[[ 1.  3.  5.  7.]
 [ 2.  4.  6.  8.]] 

You can build the executable fex with npy.mod and libnpy.a in the same directory as fex.f95, with the command.

gfortran -o fex fex.f95 libnpy.a