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

In summary, the speaker is upgrading a legacy code to meet new standards and is trying to make global arrays dynamic for easier parallelization. They are having trouble reading numbers from a text file and need to skip a certain number of real numbers before resuming reading. The current code works but is not efficient and the speaker is looking for a better solution. Another person suggests using a dummy variable and an implied DO loop to read and ignore the skipped numbers. This trick can also be useful for output.
  • #1
prashanth.hsn
2
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:
[COLOR="Blue"]Integer[/COLOR]:: i, j, k, ii, jj, kk, num
[COLOR="Blue"]Real, Allocatable, Dimension[/COLOR](:,:,:):: x 
[COLOR="DimGray"]!Start reading the data from file, UNIT=16[/COLOR]
[B]read[/B](16,*) i, j, k
[COLOR="DimGray"]!I want to Skip reading data to dummy x(:,:,:) array[/COLOR]
[B]read [/B](16,*) (((x(ii,jj,kk),ii=1,i),jj=1,j),kk=1,k)
[COLOR="DimGray"]! Resume reading[/COLOR]
[B]read[/B](16,*) num
 
Technology news on Phys.org
  • #2
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.
 
  • #3
dear AlephZero, thanks for your solution. I appreciate your help.
 

1. What is "skip reading N real numbers" in an ascii file?

"Skip reading N real numbers" refers to the process of ignoring a certain number of numerical values within an ascii file. This means that these numbers will not be included in the data being read and processed by the program.

2. Why would you want to skip reading certain numbers in an ascii file?

Skipping numbers in an ascii file can be useful when those particular values are not relevant to the analysis or calculations being performed. This can save time and memory by not including unnecessary data.

3. How do you specify the number of values to skip when reading an ascii file?

The number of values to skip can be specified in the code by using a variable or constant. This value is typically determined based on the file format or the specific data being read.

4. Can you skip reading a range of numbers in an ascii file?

Yes, it is possible to skip a range of numbers in an ascii file by using a loop or conditional statement in the code. This can be useful for skipping a specific set of data or a pattern of values.

5. Are there any potential drawbacks to skipping numbers in an ascii file?

One potential drawback is that if the skipped numbers contain important data, it may affect the accuracy of the analysis or calculations. It is important to carefully consider which numbers to skip and ensure they will not impact the results in a significant way.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
736
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
1
Views
579
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
Back
Top