forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_functions.F90
165 lines (128 loc) · 5.76 KB
/
file_functions.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
!-----------------------------------------------------------------------
! $Id$
!===============================================================================
module file_functions
implicit none
public :: file_read_1d, file_read_2d
private ! Default Scope
contains
!===============================================================================
subroutine file_read_1d( file_unit, path_and_filename, &
num_datapts, entries_per_line, variable )
! Description:
! This subroutine reads in values from a data file with a number of
! rows and a declared number of columns (entries_per_line) of data.
! It reads in the data in the form of:
! 1 ==> (row 1, column 1); 2 ==> (row 1, column 2); etc.
!
! Example: a diagram of a data file with 18 total data points
! (DP1 to DP18), with 4 data points per row.
!
! i = 1 i = 2 i = 3 i = 4
! ---------------------------------------
! k = 1 | DP1 DP2 DP3 DP4
! |
! k = 2 | DP5 DP6 DP7 DP8
! |
! k = 3 | DP9 DP10 DP11 DP12
! |
! k = 4 | DP13 DP14 DP15 DP16
! |
! k = 5 | DP17 DP18
!
! See Michael Falk's comments below for more information.
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
use constants_clubb, only: fstderr ! Constant(s)
implicit none
integer, intent(in) :: &
file_unit, & ! Unit number of file being read.
num_datapts, & ! Total number of data points being read in.
entries_per_line ! Number of data points
! on one line of the file being read.
character(*), intent(in) :: &
path_and_filename ! Path to file and filename of file being read.
real( kind = core_rknd ), dimension(num_datapts), intent(out) :: &
variable ! Data values output into variable
integer :: k ! Data file row number.
integer :: i ! Data file column number.
integer :: ierr
! ---- Begin Code ----
! A ThreadLock is necessary here because FORTRAN can only have each file open on
! one file_unit at a time. For example, suppose we are running CLUBB in parallel
! with OpenMP using two threads. Suppose the first thread opens the file with file_unit = 0
! (file_unit is assigned a value based on thread number).
! Then suppose, that before thread 1 exits, thread 2 opens the same file with file_unit = 1.
! This would cause FORTRAN to crash.
!$omp critical
! Open data file.
open( unit=file_unit, file=path_and_filename, action='read', status='old', &
iostat=ierr )
if ( ierr /= 0 ) then
write(fstderr,*) "CLUBB encountered an error trying to open "//path_and_filename
stop "Error opening forcings file"
end if
! Michael Falk wrote this routine to read data files in a particular format for mpace_a.
! Each line has a specific number of values, until the last line in the file, which
! has the last few values and then ends. This reads the correct number of values on
! each line. 24 September 2007
! Loop over each full line of the input file.
do k = 1, (num_datapts/entries_per_line), 1
read(file_unit,*) ( variable( ((k-1)*entries_per_line) + i ), &
i=1,entries_per_line )
enddo
! Read any partial line remaining.
if ( mod(num_datapts,entries_per_line) /= 0 ) then
k = (num_datapts/entries_per_line)
read(file_unit,*) ( variable( (k*entries_per_line) + i ), &
i=1,(mod(num_datapts,entries_per_line)) )
endif
! Close data file.
close( file_unit )
!$omp end critical
return
end subroutine file_read_1d
!===============================================================================
subroutine file_read_2d( device, file_path, file_dimension1, &
file_dimension2, file_per_line, variable )
! Description:
! Michael Falk wrote this routine to read data files in a particular format for mpace_a.
! The 2d mpace_a files list the (file_dimension2) values on a given vertical level, then
! moves to the next level to list its values.
! Each line has a specific number of values, until the last line on a level, which
! is short-- it has the last few values and then a line break. The next line, beginning
! the next level, is full-sized again. 24 September 2007
!
! References:
! None
!-------------------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
integer, intent(in) :: &
device, &
file_dimension1, &
file_dimension2, &
file_per_line
character(*), intent(in) :: &
file_path
real( kind = core_rknd ), dimension(file_dimension1,file_dimension2), intent(out) :: &
variable
integer i, j, k
! ---- Begin Code ----
variable = -999._core_rknd ! Initialize to nonsense values
open(device,file=file_path,action='read')
do k=1,(file_dimension1) ! For each level in the data file,
do j=0,((file_dimension2/file_per_line)-1)
read(device,*) (variable(k,(j*file_per_line)+i), & ! read file_per_line values in,
i=1,file_per_line)
end do
read (device,*) (variable(k,(j*file_per_line)+i), & ! then read the partial line
i=1,(mod(file_dimension2,file_per_line)))
end do ! and then start over at the next level.
close(device)
return
end subroutine file_read_2d
!===============================================================================
end module file_functions