Fortran 95 Reading in Multi-Dimensional Arrays

In summary, a program has been provided that reads a file with temperature and dew point data for different days and stores it in an array. The program can be modified to find the high, low, and average temperature and dew point for each day.
  • #1
Sam032789
11
0
Hi. I have a file like this:

Code:
1990     01       01       0000     27       16      
1990     01       01       0100     25       15      
1990     01       01       0200     24       16      
1990     01       01       0300     24       16      
1990     01       01       0400     22       15      
1990     01       01       0500     21       13      
1990     01       01       0600     20       13      
1990     01       01       0700     17       12      
1990     01       01       0800     16       12      
1990     01       01       0900     13       9       
1990     01       01       1000     12       8       
1990     01       01       1100     12       8       
1990     01       01       1200     12       8       
1990     01       01       1300     11       7       
1990     01       01       1400     9        5       
1990     01       01       1500     14       9       
1990     01       01       1600     21       16      
1990     01       01       1700     27       17      
1990     01       01       1800     30       17      
1990     01       01       1900     34       17      
1990     01       01       2000     37       17      
1990     01       01       2100     39       18      
1990     01       01       2200     38       18      
1990     01       01       2300     37       18      
1990     01       02       0000     34       19      
1990     01       02       0100     32       19      
1990     01       02       0200     33       20      
1990     01       02       0300     32       19      
1990     01       02       0400     32       19      
1990     01       02       0500     30       18      
1990     01       02       0600     25       18      
1990     01       02       0700     26       18      
1990     01       02       0800     30       20      
1990     01       02       0900     29       20      
1990     01       02       1000     30       21      
1990     01       02       1100     31       22      
1990     01       02       1200     27       20      
1990     01       02       1300     25       19      
1990     01       02       1400     27       21      
1990     01       02       1500     32       22      
1990     01       02       1600     38       24      
1990     01       02       1700     42       25      
1990     01       02       1800     45       26      
1990     01       02       1900     49       27      
1990     01       02       2000     50       28      
1990     01       02       2100     51       28      
1990     01       02       2200     51       28      
1990     01       02       2300     48       28  
2011     06       01       0054     79       45      
2011     06       01       0154     76       47      
2011     06       01       0254     65       53      
2011     06       01       0354     66       53      
2011     06       01       0454     57       52      
2011     06       01       0554     59       54      
2011     06       01       0600     59       54      
2011     06       01       0654     58       52      
2011     06       01       0754     56       52      
2011     06       01       0854     52       50      
2011     06       01       0954     52       51      
2011     06       01       1054     52       50      
2011     06       01       1154     57       51      
2011     06       01       1200     57       51      
2011     06       01       1254     62       53      
2011     06       01       1354     64       53      
2011     06       01       1454     70       54      
2011     06       01       1554     72       54      
2011     06       01       1654     74       55      
2011     06       01       1754     78       53      
2011     06       01       1800     78       53      
2011     06       01       1854     82       57      
2011     06       01       1954     85       59      
2011     06       01       2054     84       65      
2011     06       01       2154     82       64      
2011     06       01       2254     80       66     
2011     06       01       2354     76       61

Where the columns are year, month, day, hour, temperature, and dew point, respectively. I need to read each column in as an array so I can find the high, low, and average temperature and dewpoint of each day, and the times at which they occur. Please help me.
 
Technology news on Phys.org
  • #2
Nevermind, I figured it out:

Code:
PROGRAM FileArray
implicit none

integer :: i, j, lines
integer, dimension(6,75) :: a

!a(1,*) = year
!a(2,*) = month
!a(3,*) = day
!a(4,*) = hour
!a(5,*) = temp
!a(6,*) = dew point

100 FORMAT(I4, 5X, I2, 7X, I2, 7X, I4, 5X, I2, 7X, I2)

open(55,file='testfile.txt',status='OLD')

read(55,100) ((a(i,j),i=1,6),j=1,75)

write(*,100) a

close(55)

END PROGRAM FileArray
 
  • #3
Note that the following also works. (Fortran can work out its own implied do loops.)

read(55,*) a
 
  • #4
Sam032789 said:
Code:
1990     01       01       0000     27       16      
1990     01       01       0100     25       15      
1990     01       01       0200     24       16      
1990     01       01       0300     24       16      
1990     01       01       0400     22       15      
1990     01       01       0500     21       13      
1990     01       01       0600     20       13      
1990     01       01       0700     17       12      
1990     01       01       0800     16       12      
1990     01       01       0900     13       9       
1990     01       01       1000     12       8       
1990     01       01       1100     12       8       
1990     01       01       1200     12       8       
1990     01       01       1300     11       7       
1990     01       01       1400     9        5       
1990     01       01       1500     14       9       
1990     01       01       1600     21       16      
1990     01       01       1700     27       17      
1990     01       01       1800     30       17      
1990     01       01       1900     34       17      
1990     01       01       2000     37       17      
1990     01       01       2100     39       18      
1990     01       01       2200     38       18      
1990     01       01       2300     37       18      
1990     01       02       0000     34       19      
1990     01       02       0100     32       19      
1990     01       02       0200     33       20      
1990     01       02       0300     32       19      
1990     01       02       0400     32       19      
1990     01       02       0500     30       18      
1990     01       02       0600     25       18      
1990     01       02       0700     26       18      
1990     01       02       0800     30       20      
1990     01       02       0900     29       20      
1990     01       02       1000     30       21      
1990     01       02       1100     31       22      
1990     01       02       1200     27       20      
1990     01       02       1300     25       19      
1990     01       02       1400     27       21      
1990     01       02       1500     32       22      
1990     01       02       1600     38       24      
1990     01       02       1700     42       25      
1990     01       02       1800     45       26      
1990     01       02       1900     49       27      
1990     01       02       2000     50       28      
1990     01       02       2100     51       28      
1990     01       02       2200     51       28      
1990     01       02       2300     48       28  
2011     06       01       0054     79       45      
2011     06       01       0154     76       47      
2011     06       01       0254     65       53      
2011     06       01       0354     66       53      
2011     06       01       0454     57       52      
2011     06       01       0554     59       54      
2011     06       01       0600     59       54      
2011     06       01       0654     58       52      
2011     06       01       0754     56       52      
2011     06       01       0854     52       50      
2011     06       01       0954     52       51      
2011     06       01       1054     52       50      
2011     06       01       1154     57       51      
2011     06       01       1200     57       51      
2011     06       01       1254     62       53      
2011     06       01       1354     64       53      
2011     06       01       1454     70       54      
2011     06       01       1554     72       54      
2011     06       01       1654     74       55      
2011     06       01       1754     78       53      
2011     06       01       1800     78       53      
2011     06       01       1854     82       57      
2011     06       01       1954     85       59      
2011     06       01       2054     84       65      
2011     06       01       2154     82       64      
2011     06       01       2254     80       66     
2011     06       01       2354     76       61

Where the columns are year, month, day, hour, temperature, and dew point, respectively. I need to read each column in as an array so I can find the high, low, and average temperature and dewpoint of each day, and the times at which they occur.

From the code you supplied, you apparently want to read each row in the file. In an array, the rows go across, and the columns go up and down.
 
  • #5
I thought I had it, but apparently I cannot figure out how to obtain the high and low temperature values for each day. Anyone have any suggestions?
 

Related to Fortran 95 Reading in Multi-Dimensional Arrays

1. What is Fortran 95?

Fortran 95 is a programming language commonly used in scientific and engineering applications. It is an updated version of the original Fortran language, with improvements in syntax and functionality.

2. How do you read in multi-dimensional arrays in Fortran 95?

To read in multi-dimensional arrays in Fortran 95, you can use the READ statement along with the appropriate format specifier for the array. The READ statement allows you to specify the number of elements to be read in and the starting index for the array.

3. Can Fortran 95 read in arrays of any dimension?

Yes, Fortran 95 can read in arrays of any dimension. The READ statement allows you to specify the number of dimensions for the array and the starting index for each dimension. This makes it flexible for reading in arrays of different sizes and shapes.

4. How is the data stored in multi-dimensional arrays in Fortran 95?

In Fortran 95, multi-dimensional arrays are stored in column-major order, meaning the elements of each column are stored contiguously in memory. This is different from languages like C, which use row-major order. It is important to keep this in mind when accessing and manipulating multi-dimensional arrays in Fortran 95.

5. Are there any built-in functions for reading in multi-dimensional arrays in Fortran 95?

Yes, Fortran 95 has built-in functions, such as READ and READ*, that allow you to read in multi-dimensional arrays. These functions have parameters that allow you to specify the dimensions and starting indices for the array, making it easier to read in data from external sources.

Similar threads

Replies
58
Views
3K
Replies
2
Views
1K
Replies
31
Views
2K
Replies
2
Views
1K
  • Astronomy and Astrophysics
Replies
2
Views
1K
  • Science and Math Textbooks
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top