Skip reading N real numbers while reading data from a ascii file

Click For Summary
SUMMARY

The discussion focuses on upgrading legacy Fortran 77 code to Fortran 90 standards, specifically addressing the challenge of reading data from an ASCII file while skipping a specified number of real numbers. The user needs to skip 27 real numbers that span multiple records of varying lengths before resuming data reading. A proposed solution involves using a dummy variable to read and ignore the unwanted numbers, allowing for efficient memory allocation for multidimensional arrays.

PREREQUISITES
  • Understanding of Fortran 90 syntax and features
  • Familiarity with reading data from ASCII files in Fortran
  • Knowledge of dynamic array allocation in Fortran
  • Basic concepts of implied DO loops in Fortran
NEXT STEPS
  • Explore advanced Fortran 90 file I/O techniques
  • Learn about dynamic memory management in Fortran 90
  • Investigate parallel programming options in Fortran
  • Study optimization techniques for Fortran code performance
USEFUL FOR

Fortran developers, software engineers upgrading legacy code, and anyone involved in numerical computing or data processing in Fortran.

prashanth.hsn
Messages
2
Reaction score
0
Hi all,

I'm upgrading a legacy f77 code to f90 standards. I'm trying to make some of the global arrays as dynamic arrays so that at a later stage it would be easier for me to parallelize whole application.
Coming to my problem, I need to read some numbers from a text file so that I can allocate (just enough) memory to the multidimensional arrays. However, while reading the input text file I need to skip say 'n' real numbers and then resume reading. These real numbers span several records (=lines) in the input text file and the number of real numbers per record is not constant sad.
In summary, I know how many real numbers I've to skip, but I do not know in how many records these numbers span.

Ex:
Input.data
Code:
3 3 3
1.23445 0.2345235 0.345234 0.123425235 0.12343245
0.31415927 1.23445 0.2345235 0.345234 0.123425235 0.12343245 
0.31415927 1.23445 0.2345235 
0.31415927 1.23445 0.2345235 0.2345235 
0.123 0.3245 0.74565355 4.34623431 56.2352354
0.31415927 1.23445 0.2345235 0.2345235
3
33
3
33
12
6

I have to read first line and then skip 27 real numbers and then again start reading from line 7.
I currently have a code which works but it is not efficient,
My current code looks like this

Code:
Integer:: i, j, k, ii, jj, kk, num
Real, Allocatable, Dimension(:,:,:):: x 
!Start reading the data from file, UNIT=16
[B]read[/B](16,*) i, j, k
!I want to Skip reading data to dummy x(:,:,:) array
[B]read [/B](16,*) (((x(ii,jj,kk),ii=1,i),jj=1,j),kk=1,k)
! Resume reading
[B]read[/B](16,*) num
 
Technology news on Phys.org
I would declare a dummy variable (e.g. SKIP) and then do
READ ... I , J , K , (SKIP, IJK = 1 , I*J*K ) , ...

In other words, read 27 numbers into the same variable, and then ignore it. That will handle the fact that you don't know how many numbers are on each line of the input file.

Usually, the variables in an implied DO loop are array elements with subscripts, but they don't have to be.

This trick can be useful for output as well, if you want to write N copies of the same value for some reason.
 
dear AlephZero, thanks for your solution. I appreciate your help.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K