Replicating formatting from Fortran code output

I am trying to replicate the formatting from a Fortran code output in Julia. Here is a MWE.

program formattest
    implicit none
    integer:: iyd
    real(4):: d(4),t

    iyd = 10000
    d = 9.999e-38
    t = 1.053e10

    open(77,file='outputtest.txt',status='replace')
    write(77,'(i7,4e13.4,f8.2)')  &
        iyd,d(1:4),t
    close(77)
end program

The output that I obtain is

10000   0.9999E-37   0.9999E-37   0.9999E-37   0.9999E-37  294.10

Is it possible to replicate the formatting from the “0.9999E-37” in Julia?

It looks small for real(4)… Did you already play with Printf Printf · The Julia Language ?

The 9.999E-38 is something special put in the original code so there is no way around it and I already tried to use Printf but I don’t see any way to print it as 0.9999E-37 instead of 9.999E-38

I don’t know of a Julia package that will do this directly. There is a Python package that you could use with PyCall

using PyCall

fortranformat = pyimport("fortranformat")
format = fortranformat.FortranRecordWriter("(i7,4e13.4,f8.2)")
iyd = 10000
d = 9.999e-38
t = 294.1
println(format.write((iyd, d, d, d, d, t)))

which outputs

  10000   0.9999E-37   0.9999E-37   0.9999E-37   0.9999E-37  294.10

You would need to install PyCall with a Python environment that has the fortranformat package installed. The details on how to do that depend on what OS you are using and whether you already have a Python environment installed.

1 Like

Why does it matter?

I want to be able to use diff in the command line for the Fortran output and the Julia output to compare them quickly instead of having to read in the Fortran output and compare both outputs in a Julia code

Sounds like you want functionality similar to numdiff or spiff.

2 Likes