# Reading a two-dimensional array in namelist, f90

## Main Question or Discussion Point

I'd like to create some test data to read 2-dim arrays in f90 with namelists. Suppose I have this in the main program:

integer, dimension(2,3) array_in, array_out
namelist /WW_ARRAY/ array_in, array_out
...
Open(unit=WW_ARRAY_infile, file="WW_ARRAY_in.dat", status = "OLD")
...

ww_array_in.dat might look like this:

&WW_ARRAY
array_in(1:1) = 1,
array_in(1:2) = 2,
array_in(2:1) = 3,
array_in(2:2) = 4

but maybe there's any easier way to write this?

Related Programming and Computer Science News on Phys.org
What language is this?

AlephZero
Homework Helper
What language is this?
If you don't know, you are not a http://en.wikipedia.org/wiki/Real_Programmer

I can't answer the OP's question, but I would expect you can input the array column by column just as a list of numbers, without all the "array_in(1:1)" stuff. It's "read the manual" time...

This does a better job of illustrating what happens by looking what a program can output for a 2-d array.

integer :: n=1
real :: delta = 0.12

real, dimension(1:5, 1:2) :: astro_data
namelist /astro_nml/ astro_data, a, b, c
real :: a, b
character*12 :: c
integer :: astro_file = 10

! generate astro_nml values
a=1.5
b=-3.5
c="just c"

do j = 1,5
do k = 1,2
astro_data(j,k) = j*1.0 + (k-1)*delta
end do
end do
write(*, ("(10x, a20)")) "ARRAY EXAMPLE"
Write(*, nml=astro_nml)

Open(unit=astro_file, file="z-astro_file.dat", status = "REPLACE")
Write(unit=astro_file, nml=astro_nml)
Close(unit=astro_file)
end

produces:
ARRAY EXAMPLE
&ASTRO_NML
ASTRO_DATA= 1.0000000 , 2.0000000 , 3.0000000 , 4.0000000 , 5.0000000 ,
1.1200000 , 2.1199999 , 3.1199999 , 4.1199999 , 5.1199999 ,
A= 1.5000000 ,
B= -3.5000000 ,
C="just c ",
/

As far as I know, there is no manual for Gfortran f90 under MinGW. I find myself writing in fortran several decades after I did it long ago out of necessity. I've picked up a few pdf files on it via Google, but they are somewhat incomplete, about a 100 pages or fewer. A few books I've found on Amazon about it cost \$100+. No thanks on that. Fortunately, my task is confined to taking apart a few data files created with formats, and convert them into namelists. Lucky me. :-)

Do you really NEED to convert them to namelists? I have never use namelists, I am doing fine.

By the way, there are quite a few on-line resources for Fortran; no need to buy a book.

Absolutely need them for my application. They are read as input to a computation program.

So, you do no have the source to such "computation program" and that program has already been compiled, then.

Otherwise, if you have the source to the program, nothing prevents you from simply reading values the typical way.

Anyway, I guess is a matter of preference; it's just that namelists never seem to come up to the point that I thought nobody used them.

Just like the local variables of third party library subroutines or functions, you do not need to know the name of the variables in them, you just need to pass values...this allows the programmer to change variable names to something more meaningful if so desired.

...with namelists, you need to keep variable names in program source and input files in sync.

my 2 cents

should you decide to do away from namelists, I am sure we can show you very efficient ways to read data.