Reading a two-dimensional array in namelist, f90

  • Thread starter Thread starter solarblast
  • Start date Start date
  • Tags Tags
    Array Reading
AI Thread Summary
The discussion focuses on reading two-dimensional arrays in Fortran 90 using namelists, with an example provided for creating and writing test data. Users express that it may be simpler to input arrays as a list of numbers rather than using specific array indexing syntax. There is a mention of the lack of comprehensive manuals for Gfortran under MinGW, leading to reliance on online resources for learning. Some participants question the necessity of using namelists, suggesting that traditional methods may suffice for many applications. Overall, the conversation highlights preferences and challenges in working with Fortran's data handling features.
solarblast
Messages
146
Reaction score
2
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?
 
Technology news on Phys.org
What language is this?
 
Whovian said:
What language is this?

If you don't know, you are not a http://en.wikipedia.org/wiki/Real_Programmer :smile:

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.

! write/read array data
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.
 

Similar threads

Replies
2
Views
3K
Replies
4
Views
2K
Replies
5
Views
4K
Replies
25
Views
2K
Replies
12
Views
3K
Replies
33
Views
5K
Replies
4
Views
11K
Back
Top