In astrophysics and in solar physics we have FITS format as a de-facto standard for 2D images. Of course there are libraries for all the mainstream languages & viewers.
In particular for C and Fortran there exist CFITSIO library (link). A while ago I’ve managed to get read/write access to FITS-files in my programs, but I admit that I don’t understand why it is working.
So, if I don’t get blind, there are no any interfaces defined and probably implicit interfaces were implied in my make (I’ve used default gfortran and ifort ~2019). Now when I’m trying to switch to fpm, I get an error about it, so had to specify implicit-external=true
So my questions are:
If anybody has experience with FITS?
If there exist a more modern approach/library?
Am I missing something in CFITSIO? They provide examples of compiling&linking and a simple program in a f77 style.
If not, is it a good idea (is it straightforward?) to write an interface and generics? For example to read data there is a set of functions like FTGPV[BIJKED] for different data types ( I - short int; J - int; etc)
I have no experience with this type of files and I have not checked the library as yet, but if the interfacing method is an old-style one (i.e. predating Fortran 2003 with ISO_C_BINDING etc.) then indeed the interfaces can be completely implicit and the burden is on you as a programmer to get everything right.
I am working myself on the interface to an extensive C library and I prefer to provide generic interfaces to routines that manipulate the various basic data types. It is a matter of taste, I guess, but I find it more enjoyable to be able to use such generic interfaces instead of having to wonder about the specific names.
As a research astronomer we used FITS files extensively and they are still in use widely, especially in radio astronomy and x-ray astronomy. As far as I know the CFITSIO library is still the only one that is C- and Fortran-callable, but as you note its interfaces were designed in the Fortran77 era. The library was written originally in Fortran77 at NASA Goddard Space Flight Institute by Bill Pence and his team. One problem was that there is, in Fortran77, no standard-conforming way of accessing a binary stream. It was in fact possible to access them direct-access files with a record length of 2880 bytes, and this works well, but specifying a record length in bytes was, before the IOLENGTH descriptor arrived, not easy to do in a standard-conforming way. Thus it was hard to write a library for FITS file access without using non-standard Fortran77.
Anyhow, many years ago a direction came from on high in NASA that they should no longer use Fortran77 so they re-wrote the whole thing in C with a Fortran77-callable interface on top. I tried to persuade them that Fortran90/95 (and later) standards were superior but GSFC never accepted that. So what exists now is a library written in C with a thin Fortran77 style interface.
The problem with providing a sensible modern interface layer is that a few procedures in CFITSIO have one or two arguments where the actual argument can be either a scalar or an array. I think there are work-arounds for this but it is messy. I constructed Fortran90 interfaces for some of the CFITSIO procedures that I needed for my own work, but decided that it would be too much effort to do this for all of them. It might even be easier to write a new library in modern Fortran for accessing FITS files, accessing them via access=“stream”. But as far as I know nobody has done that yet.
I agree with this with one caveat. You have to be extra careful if you need exact byte-for-byte compatability and reproducability with existing files written by CFITSIO.
If thats the case you should consider writting F2003 interop interfaces for just the routines you need. The issue with the CFITIO F77 interfaces is they appear to be generated using the old cfortran.h macro package. There can be subtle differences depending which operating system and C compiler you use to build the C library. I encountered this 20 plus years ago when I wrote the first version of the C-interop interfaces for netCDF that are now part of the netCDF Fortran package. Based on my experiences with netCDF and later my forSISL interfaces to the SISL NURBS library, Its not very complicated to write the interfaces but its a LOT of work (and I mean a LOT).