Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I left-justify numerical output in fortran?

I am writing some simple output in fortran, but I want whitespace delimiters. If use the following statement, however:

format(A20,ES18.8,A12,ES18.8)

I get output like this:

p001t0000               3.49141273E+01obsgp_oden      1.00000000E+00

I would prefer this:

p001t0000           3.49141273E+01   obsgp_oden  1.00000000E+00

I tried using negative values for width (like in Python) but no dice. So, is there a way to left-justify the numbers?

Many thanks in advance!

like image 202
mishaF Avatar asked Dec 28 '10 21:12

mishaF


2 Answers

There's not a particularly beautiful way. However, using an internal WRITE statement to convert the number to a text string (formerly done with an ENCODE statement), and then manipulating the text may do what you need.

Quoting http://rsusu1.rnd.runnet.ru/develop/fortran/prof77/node168.html

An internal file WRITE is typically used to convert a numerical value to a character string by using a suitable format specification, for example:

  CHARACTER*8 CVAL 
  RVALUE = 98.6 
  WRITE(CVAL, '(SP, F7.2)') RVALUE

The WRITE statement will fill the character variable CVAL with the characters ' +98.60 ' (note that there is one blank at each end of the number, the first because the number is right-justified in the field of 7 characters, the second because the record is padded out to the declared length of 8 characters).

Once a number has been turned into a character-string it can be processed further in the various ways described in section 7. This makes it possible, for example, to write numbers left-justified in a field, ...

like image 79
Ubuntourist Avatar answered Sep 27 '22 17:09

Ubuntourist


This is easier with Fortran 95, but still not trivial. Write the number or other item to a string with a write statement (as in the first answer). Then use the Fortran 95 intrinsic "ADJUSTL" to left adjust the non-blank characters of the string.

like image 35
M. S. B. Avatar answered Sep 27 '22 17:09

M. S. B.