1

I have an output of a Fortran program that sometimes writes in exponential notation without the E. I can't fix the program at this point and I have to deal with the output.

Basically I'm trying to read the ASCII output file with Numpy's loadtxt function, but it fails when it encounters the number -0.1923856859-100 which is supposed to be written -0.1923856859E-100.

Is there any way to either read that properly with Numpy, or to easily re-write that file converting every instance where that happens to a proper notation?

TomCho
  • 3,204
  • 6
  • 32
  • 83
  • 2
    This is not "wrong fortran output", it is standard Fortran output. And the "proper notation" is only proper for Python. – Vladimir F Героям слава Mar 16 '20 at 23:36
  • The minus (or plus) sign will always be immediately preceded by a digit. You can run sed ten times (e.g., cat file | sed s/9-/9E-/g). Or, you can write a python script with a regex that matches [0-9]- and [0-9]+, which then inserts [0-9]E- and [0-9]E+ – evets Mar 17 '20 at 00:28
  • 1
    @evets: "run sed 10 times" is silly. And `cat` is unnecessary. `sed -E 's/([0-9])([+-])/\1E\2/g' file`. – rici Mar 17 '20 at 02:01
  • @rici, yeah, I know running sed ten times is silly. Using python instead Fortran is also silly, to each his own. I did sate that TomCHo could use a suitable regex within python. I simply wasn't going to waste my time on trivialities. – evets Mar 17 '20 at 02:52
  • Another option is to specify enough exponent digits so that 'E' is not suppressed. This hasn't changed in 30 years, so F77 docs will do (or even those docs for bastard combinations of IBM360 fortran with F77). – tim18 Mar 17 '20 at 11:26
  • Could try https://stackoverflow.com/questions/34545060/reading-a-file-with-fortran-formatted-small-floats-using-numpy – cup Mar 18 '20 at 12:02

0 Answers0