WAVEWATCH III  beta 0.0.1
scrip_iounitsmod.f90
Go to the documentation of this file.
1 !|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 
4 
5  !BOP
6  !
7  ! !MODULE: SCRIP_IOUnitsMod
8  !
9  ! !DESCRIPTION:
10  ! This module contains an I/O unit manager for tracking, assigning
11  ! and reserving I/O unit numbers.
12  !
13  ! There are three reserved I/O units set as parameters in this
14  ! module. The default units for standard input (stdin), standard
15  ! output (stdout) and standard error (stderr). These are currently
16  ! set as units 5,6,6, respectively as that is the most commonly
17  ! used among vendors. However, the user may change these if those
18  ! default units are conflicting with other models or if the
19  ! vendor is using different values.
20  !
21  ! The maximum number of I/O units per node is currently set by
22  ! the parameter SCRIP\_IOMaxUnits.
23  !
24  ! !REFDOC:
25  !
26  ! !REVISION HISTORY:
27  ! SVN:$Id: SCRIP_IOUnitsMod.F90 83 2008-02-22 17:26:54Z pwjones $
28 
29  ! !USES:
30 
31  use scrip_kindsmod
32 
33  implicit none
34  private
35  save
36 
37  ! !PUBLIC MEMBER FUNCTIONS:
38 
39  public :: scrip_iounitsget, &
44 
45  ! !PUBLIC DATA MEMBERS:
46 
47  integer (SCRIP_i4), parameter, public :: &
48  scrip_stdin = 5, &! reserved unit for standard input
49  scrip_stdout = 6, &! reserved unit for standard output
50  scrip_stderr = 6 ! reserved unit for standard error
51 
52  ! common formats for writing to stdout, stderr
53 
54  character (9), parameter, public :: &
55  scrip_delimformat = "(72('-'))"
56 
57  character (5), parameter, public :: &
58  scrip_blankformat = "(' ')"
59 
60  !EOP
61  !BOC
62  !-----------------------------------------------------------------------
63  !
64  ! private io unit manager variables
65  !
66  !-----------------------------------------------------------------------
67 
68  integer (SCRIP_i4), parameter :: &
69  scrip_iounitsminunits = 11, & ! do not use unit numbers below this
70  scrip_iounitsmaxunits = 99 ! maximum number of open units
71 
72  logical (SCRIP_Logical) :: &
73  scrip_iounitsinitialized = .false.
74 
75  logical (SCRIP_Logical), dimension(SCRIP_IOUnitsMaxUnits) :: &
76  scrip_iounitsinuse ! flag=.true. if unit currently open
77 
78  !EOC
79  !***********************************************************************
80 
81 contains
82 
83  !***********************************************************************
84  !BOP
85  ! !IROUTINE: SCRIP_IOUnitsGet
86  ! !INTERFACE:
87 
88  subroutine scrip_iounitsget(iunit)
89 
90  ! !DESCRIPTION:
91  ! This routine returns the next available i/o unit and marks it as
92  ! in use to prevent any later use.
93  ! Note that {\em all} processors must call this routine even if only
94  ! the master task is doing the i/o. This is necessary insure that
95  ! the units remain synchronized for other parallel I/O functions.
96  !
97  ! !REVISION HISTORY:
98  ! same as module
99 
100  ! !OUTPUT PARAMETERS:
101 
102  integer (SCRIP_i4), intent(out) :: &
103  iunit ! next free i/o unit
104 
105  !EOP
106  !BOC
107  !-----------------------------------------------------------------------
108  !
109  ! local variables
110  !
111  !-----------------------------------------------------------------------
112 
113  integer (SCRIP_i4) :: n ! dummy loop index
114 
115  logical (SCRIP_Logical) :: alreadyinuse
116 
117  !-----------------------------------------------------------------------
118  !
119  ! check to see if units initialized and initialize if necessary
120  !
121  !-----------------------------------------------------------------------
122 
123  if (.not. scrip_iounitsinitialized) then
124  scrip_iounitsinuse = .false.
125  scrip_iounitsinuse(scrip_stdin) = .true.
126  scrip_iounitsinuse(scrip_stdout) = .true.
127  scrip_iounitsinuse(scrip_stderr) = .true.
128 
129  scrip_iounitsinitialized = .true.
130  endif
131 
132  !-----------------------------------------------------------------------
133  !
134  ! find next free unit
135  !
136  !-----------------------------------------------------------------------
137 
138  srch_units: do n=scrip_iounitsminunits, scrip_iounitsmaxunits
139  if (.not. scrip_iounitsinuse(n)) then ! I found one, I found one
140 
141  !*** make sure not in use by library or calling routines
142  INQUIRE (unit=n,opened=alreadyinuse)
143 
144  if (.not. alreadyinuse) then
145  iunit = n ! return the free unit number
146  scrip_iounitsinuse(iunit) = .true. ! mark iunit as being in use
147  exit srch_units
148  else
149  !*** if inquire shows this unit in use, mark it as
150  !*** in use to prevent further queries
151  scrip_iounitsinuse(n) = .true.
152  endif
153  endif
154  end do srch_units
155 
156  if (iunit > scrip_iounitsmaxunits) &
157  stop 'SCRIP_IOUnitsGet: No free units'
158 
159  !-----------------------------------------------------------------------
160  !EOC
161 
162  end subroutine scrip_iounitsget
163 
164  !***********************************************************************
165  !BOP
166  ! !IROUTINE: SCRIP_IOUnitsRelease
167  ! !INTERFACE:
168 
169  subroutine scrip_iounitsrelease(iunit)
170 
171  ! !DESCRIPTION:
172  ! This routine releases an i/o unit (marks it as available).
173  ! Note that {\em all} processors must call this routine even if only
174  ! the master task is doing the i/o. This is necessary insure that
175  ! the units remain synchronized for other parallel I/O functions.
176  !
177  ! !REVISION HISTORY:
178  ! same as module
179 
180  ! !INPUT PARAMETER:
181 
182  integer (SCRIP_i4), intent(in) :: &
183  iunit ! i/o unit to be released
184 
185  !EOP
186  !BOC
187  !-----------------------------------------------------------------------
188  !
189  ! check for proper unit number
190  !
191  !-----------------------------------------------------------------------
192 
193  if (iunit < 1 .or. iunit > scrip_iounitsmaxunits) then
194  stop 'SCRIP_IOUnitsRelease: bad unit'
195  endif
196 
197  !-----------------------------------------------------------------------
198  !
199  ! mark the unit as not in use
200  !
201  !-----------------------------------------------------------------------
202 
203  scrip_iounitsinuse(iunit) = .false. ! that was easy...
204 
205  !-----------------------------------------------------------------------
206  !EOC
207 
208  end subroutine scrip_iounitsrelease
209 
210  !***********************************************************************
211  !BOP
212  ! !IROUTINE: SCRIP_IOUnitsReserve
213  ! !INTERFACE:
214 
215  subroutine scrip_iounitsreserve(iunit)
216 
217  ! !DESCRIPTION:
218  ! This routine marks an IO unit as in use to reserve its use
219  ! for purposes outside of SCRIP IO. This is necessary for
220  ! cases where you might be importing code developed elsewhere
221  ! that performs its own I/O and open/closes units.
222  ! Note that {\em all} processors must call this routine even if only
223  ! the master task is doing the i/o. This is necessary insure that
224  ! the units remains synchronized for other parallel I/O functions.
225  !
226  ! !REVISION HISTORY:
227  ! same as module
228 
229  ! !INPUT PARAMETER:
230 
231  integer (SCRIP_i4), intent(in) :: &
232  iunit ! i/o unit to be reserved
233 
234  !EOP
235  !BOC
236  !-----------------------------------------------------------------------
237  !
238  ! local variables
239  !
240  !-----------------------------------------------------------------------
241 
242  logical (SCRIP_Logical) :: alreadyinuse
243 
244  !-----------------------------------------------------------------------
245  !
246  ! check for proper unit number
247  !
248  !-----------------------------------------------------------------------
249 
250  if (iunit < scrip_iounitsminunits .or. &
251  iunit > scrip_iounitsmaxunits) then
252  stop 'SCRIP_IOUnitsReserve: invalid unit'
253  endif
254 
255  !-----------------------------------------------------------------------
256  !
257  ! check to see if SCRIP already using this unit
258  !
259  !-----------------------------------------------------------------------
260 
261  if (scrip_iounitsinuse(iunit)) then
262  stop 'SCRIP_IOUnitsReserve: unit already in use by SCRIP'
263  endif
264 
265  !-----------------------------------------------------------------------
266  !
267  ! check to see if others already using this unit
268  !
269  !-----------------------------------------------------------------------
270 
271  INQUIRE (unit=iunit, opened=alreadyinuse)
272  if (alreadyinuse) then
273  stop 'SCRIP_IOUnitsReserve: unit already in use by others'
274  endif
275 
276  !-----------------------------------------------------------------------
277  !
278  ! mark the unit as in use
279  !
280  !-----------------------------------------------------------------------
281 
282  scrip_iounitsinuse(iunit) = .true.
283 
284  !-----------------------------------------------------------------------
285  !EOC
286 
287  end subroutine scrip_iounitsreserve
288 
289  !***********************************************************************
290  !BOP
291  ! !IROUTINE: SCRIP_IOUnitsRedirect
292  ! !INTERFACE:
293 
294  subroutine scrip_iounitsredirect(iunit, filename)
295 
296  ! !DESCRIPTION:
297  ! This routine enables a user to redirect stdin, stdout, stderr to
298  ! a file instead of to the terminal. It is only permitted for these
299  ! special units. The SCRIP IO file operators should be used for
300  ! normal I/O.
301  ! Note that {\em all} processors must call this routine even if only
302  ! the master task is doing the i/o. This is necessary insure that
303  ! the units remains synchronized for other parallel I/O functions.
304  !
305  ! !REVISION HISTORY:
306  ! same as module
307 
308  ! !INPUT PARAMETER:
309 
310  integer (SCRIP_i4), intent(in) :: &
311  iunit ! i/o unit to be redirected to file
312 
313  character (*), intent(in) :: &
314  filename ! filename, including path, to which
315  ! i/o should be directed
316 
317  !EOP
318  !BOC
319  !-----------------------------------------------------------------------
320  !
321  ! check for proper unit number and open file
322  !
323  !-----------------------------------------------------------------------
324 
325  if (iunit == scrip_stdin) then ! open input file for stdin
326  open(unit=iunit, file=filename, status='old', form='formatted')
327 
328  else if (iunit == scrip_stdout) then ! open output file for stdout
329  open(unit=iunit, file=filename, status='unknown', form='formatted')
330 
331  else if (iunit == scrip_stderr .and. scrip_stderr /= scrip_stdout) then
332  ! open output file for stderr
333  open(unit=iunit, file=filename, status='unknown', form='formatted')
334 
335  else
336  stop 'SCRIP_IOUnitsRedirect: invalid unit'
337 
338  endif
339 
340  !-----------------------------------------------------------------------
341  !EOC
342 
343  end subroutine scrip_iounitsredirect
344 
345  !***********************************************************************
346  !BOP
347  ! !IROUTINE: SCRIP_IOUnitsFlush
348  ! !INTERFACE:
349 
350  subroutine scrip_iounitsflush(iunit)
351 
352  ! !DESCRIPTION:
353  ! This routine enables a user to flush the output from an IO unit
354  ! (typically stdout) to force output when the system is buffering
355  ! such output. Because this system function is system dependent,
356  ! we only support this wrapper and users are welcome to insert the
357  ! code relevant to their local machine.
358  !
359  ! !REVISION HISTORY:
360  ! same as module
361 
362  ! !INPUT PARAMETER:
363 
364  integer (SCRIP_i4), intent(in) :: &
365  iunit ! i/o unit to be flushed
366 
367  !EOP
368  !BOC
369  !-----------------------------------------------------------------------
370  !
371  ! insert your system code here
372  !
373  !-----------------------------------------------------------------------
374 
375  !-----------------------------------------------------------------------
376  !EOC
377 
378  end subroutine scrip_iounitsflush
379 
380  !***********************************************************************
381 
382 end module scrip_iounitsmod
383 
384 !|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
scrip_iounitsmod::scrip_stderr
integer(scrip_i4), parameter, public scrip_stderr
Definition: scrip_iounitsmod.f90:47
scrip_iounitsmod::scrip_stdin
integer(scrip_i4), parameter, public scrip_stdin
Definition: scrip_iounitsmod.f90:47
scrip_iounitsmod::scrip_iounitsrelease
subroutine, public scrip_iounitsrelease(iunit)
Definition: scrip_iounitsmod.f90:170
scrip_iounitsmod::scrip_delimformat
character(9), parameter, public scrip_delimformat
Definition: scrip_iounitsmod.f90:54
scrip_iounitsmod::scrip_iounitsflush
subroutine, public scrip_iounitsflush(iunit)
Definition: scrip_iounitsmod.f90:351
scrip_iounitsmod::scrip_iounitsreserve
subroutine, public scrip_iounitsreserve(iunit)
Definition: scrip_iounitsmod.f90:216
file
file(STRINGS ${CMAKE_BINARY_DIR}/switch switch_strings) separate_arguments(switches UNIX_COMMAND $
Definition: CMakeLists.txt:3
scrip_iounitsmod::scrip_iounitsget
subroutine, public scrip_iounitsget(iunit)
Definition: scrip_iounitsmod.f90:89
scrip_kindsmod
Definition: scrip_kindsmod.f90:3
scrip_iounitsmod::scrip_iounitsredirect
subroutine, public scrip_iounitsredirect(iunit, filename)
Definition: scrip_iounitsmod.f90:295
scrip_iounitsmod
Definition: scrip_iounitsmod.f90:3
scrip_iounitsmod::scrip_blankformat
character(5), parameter, public scrip_blankformat
Definition: scrip_iounitsmod.f90:57
scrip_iounitsmod::scrip_stdout
integer(scrip_i4), parameter, public scrip_stdout
Definition: scrip_iounitsmod.f90:47