FORTRAN read statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FORTRAN read statement
# 8  
Old 06-01-2013
Hi.
Quote:
Originally Posted by LMHmedchem
...
FORTRAN is a beast for io. All input has to be precisely formatted because you have to tell the FORTRAN code what to do with each individual character, including spaces, trailing zeros, etc. This doesn't look like f77, because the DO loop doesn't have a numbered CONTINUE statement. If you are using a more updated version of FORTRAN, it may be a little easier to use.

In my experience, the easiest thing to to with code like this is to create a c or cpp function to read the input file instead of reading it with the FORTRAN. In cpp, you can just create in input stream and say it's space delimited and dump your data into a vector. Then you can parse it out of the vector however you want. ...
Comments:

There are at least 2 methods to make input into Fortran easy. First is list-directed-input. That is signaled by the use of "*" as the format. That is apparently used in the OP fragment, and I show an example below.

Another method is by the use of NAMELIST. That s not used much these days, but is still present in Fortran-90 specifications. See Why do I get an error using a namelist in a fortran 90 program? for a demonstration.

I quite agree that a well-structured source is easier to understand (and therefore maintain) compared to mangled, messy code. That is why there are programs that do reformatting. One such is illustrated below.

Here is a script that demonstrates the points:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate list-directed-input, code reformatting.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C gfortran f77tidy

FILE=${1-one.f}

pl " Input Fortran source file $FILE:"
cat -A $FILE

pl " Input data file data1:"
cat data1

pl " Input data file data2:"
cat data2

pl " Results for dataset data1:"
gfortran $FILE
./a.out < data1

pl " Results for dataset data2:"
gfortran $FILE
./a.out < data2

pl " Results of a tidying program on Fortran source:"
f77tidy one.f
mv one.tid two.f
cat -A two.f

pl " Results of reformatted source compiled, execution:"
gfortran two.f
./a.out < data1

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
gfortran GNU Fortran (Debian 4.3.2-1.1) 4.3.2
f77tidy  Tidy 7.2  -  1999-10-15       

-----
 Input Fortran source file one.f:
      program main$
^I  character e*10$
      write(6,*) ' Hello, world from fortran.'$
$
^I  do i = 1, 1$
^I  read(5,*) a, b, c, d, e$
^I  write(6,*) a, b, c, d, e$
^I  end do$
      end$

-----
 Input data file data1:
12, 3.14159, 44.7e3, 1.7e9, "Good bye"

-----
 Input data file data2:
12 0003.14159000      44.7e3 1.7e9 "Good bye"

-----
 Results for dataset data1:
  Hello, world from fortran.
   12.000000       3.1415901       44700.000      1.70000000E+09 Good bye  

-----
 Results for dataset data2:
  Hello, world from fortran.
   12.000000       3.1415901       44700.000      1.70000000E+09 Good bye  

-----
 Results of a tidying program on Fortran source:
 Tidy 7.2  -  1999-10-15       
 Running
 No diagnostic messages were generated during this tidy run.
      PROGRAM main$
      CHARACTER e*10$
      WRITE (6,*) ' Hello, world from fortran.'$
  $
      DO i=1,1$
        READ (5,*) a,b,c,d,e$
        WRITE (6,*) a,b,c,d,e$
      END DO$
      END$

-----
 Results of reformatted source compiled, execution:
  Hello, world from fortran.
   12.000000       3.1415901       44700.000      1.70000000E+09 Good bye

The source can be a mixture of leading 6-space or TABs. The formatter does a lot more than that, making reserved words UPPER case, indenting, etc.

I will occasionally also use tools like:
Code:
       ftnchek (short for Fortran checker) is designed to detect certain
       errors in a Fortran program that a compiler usually does not.  ftnchek
       is not primarily intended to detect syntax errors.  Its purpose is to
       assist the user in finding semantic errors.

 excerpt from man ftnchek

see your repository or ftnchek, A static analyzer for Fortran 77

Note that the input files are essentially free-format. The first has whitespace and commas for separators that are ignored, the second only whitespace, and leading zeros are ignored.

It is true that if one has fixed-form data, then one needs to craft matching formats. However, as shown, one can use the flexibility of Fortran features to make one's computing life pleasant.

Compared to writing c++ io routines (possibly necessitating learning yet another language) and fitting it into the scheme of linking with Fortran object code, I have generally chosen list-directed-input, or have taken a few minutes to work with specific formats.

Best wishes ... cheers, drl
# 9  
Old 06-25-2013
Yes there are 6 leading spaces! Thanks very much! I will see if it works. Sorry for the late reply, it's been a hectic month for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list. while read line ;do hostname=${line//\"} a=`ssh user@$hostname uptime;echo $?` if ];then dt=`date` touch... (6 Replies)
Discussion started by: whegra
6 Replies

2. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

3. Shell Programming and Scripting

awk doesn't understand 'read' statement!!!

While working on awk programming, i found that it doesn't understand 'read' statement. Then what's the use of 'continue' and 'break' statement in awk. For ex: awk '{k=1; while (k<10) {print $0; k++}}' emp.lst Now, please say if I want to put the logic that after priting 1 line, it will ask for... (13 Replies)
Discussion started by: ravisingh
13 Replies

4. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

5. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

6. Shell Programming and Scripting

Read from user inside a while statement

Hi there I want to ask to a user something about each line of a file. This is my code: #cat test.txt first line second line third line #cat test.sh #!/bin/ksh read_write () { echo "Do you want to print the line (YES/NO)?\n" read TEST case ${TEST} in YES) return 0;; ... (3 Replies)
Discussion started by: tirkha
3 Replies

7. Shell Programming and Scripting

generating the variable list for WHILE READ statement at runtime

Hi, I am reading the contents of a file in variables as - cat ${var_file_name} | while read COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 The problem is ... my file can have any number of columns - 5, 10, 11 .... So i want a dynamic variable list as - cat ${var_file_name} |... (8 Replies)
Discussion started by: gopalss
8 Replies

8. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

9. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

10. Shell Programming and Scripting

how to read a file to an echo statement

I was searching for an option where i can echo some strings together with the contents of a file. Eg. I was to echo the below string to a file "alter application PMS_ add variable CurrYear Y2009;" >> ${ESS_MAXL_DIR}/PMS_SUB.txt The value 2009 is coming from a file called date.txt without the... (3 Replies)
Discussion started by: Celvin VK
3 Replies
Login or Register to Ask a Question