Skip to content

Commit

Permalink
+Added open_ASCII_file
Browse files Browse the repository at this point in the history
  Added the new subroutine open_ASCII_file to open text files that can be read
or written to using simple Fortran read or write statements.  This was
previously handled via a common open_file call, but FMS2 is changing the
interface for this routine, and creating this new file de-conflicts most the
MOM6 calls to open_file from these altered interfaces.  The existing routine
open_file is unaltered and works exactly as before.  A number of existing calls
to open_file were also modified to use this new interface, in some cases where
only the root_PE was writing, this includes altering the code so that only the
root_PE actually opens the file.  All answers and output are bitwise identical.
  • Loading branch information
Hallberg-NOAA committed Feb 13, 2021
1 parent 3aad036 commit 27cd1c4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 74 deletions.
42 changes: 20 additions & 22 deletions config_src/ice_solo_driver/ice_shelf_driver.F90
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ program Shelf_main
use MOM_get_input, only : Get_MOM_Input, directories
use MOM_grid, only : ocean_grid_type, MOM_grid_init, MOM_grid_end
use MOM_hor_index, only : hor_index_type, hor_index_init
use MOM_io, only : MOM_io_init, file_exists, open_file, close_file
use MOM_io, only : MOM_io_init, file_exists, open_ASCII_file, close_file
use MOM_io, only : check_nml_error, io_infra_init, io_infra_end
use MOM_io, only : APPEND_FILE, ASCII_FILE, READONLY_FILE, SINGLE_FILE
use MOM_io, only : APPEND_FILE, READONLY_FILE, SINGLE_FILE
use MOM_open_boundary, only : ocean_OBC_type
use MOM_restart, only : save_restart
use MOM_string_functions,only : uppercase
Expand Down Expand Up @@ -176,7 +176,7 @@ program Shelf_main

if (file_exists('input.nml')) then
! Provide for namelist specification of the run length and calendar data.
call open_file(unit, 'input.nml', form=ASCII_FILE, action=READONLY_FILE)
call open_ASCII_file(unit, 'input.nml', action=READONLY_FILE)
read(unit, ice_solo_nml, iostat=io_status)
call close_file(unit)
ierr = check_nml_error(io_status,'ice_solo_nml')
Expand All @@ -187,15 +187,14 @@ program Shelf_main

! Read ocean_solo restart, which can override settings from the namelist.
if (file_exists(trim(dirs%restart_input_dir)//'ice_solo.res')) then
call open_file(unit,trim(dirs%restart_input_dir)//'ice_solo.res', &
form=ASCII_FILE,action=READONLY_FILE)
call open_ASCII_file(unit, trim(dirs%restart_input_dir)//'ice_solo.res', action=READONLY_FILE)
read(unit,*) calendar_type
read(unit,*) date_init
read(unit,*) date
call close_file(unit)
else
calendar = uppercase(calendar)
if (calendar(1:6) == 'JULIAN') then ; calendar_type = JULIAN
if (calendar(1:6) == 'JULIAN') then ; calendar_type = JULIAN
elseif (calendar(1:9) == 'GREGORIAN') then ; calendar_type = GREGORIAN
elseif (calendar(1:6) == 'NOLEAP') then ; calendar_type = NOLEAP
elseif (calendar(1:10)=='THIRTY_DAY') then ; calendar_type = THIRTY_DAY_MONTHS
Expand Down Expand Up @@ -341,15 +340,14 @@ program Shelf_main
call diag_mediator_close_registration(diag)

! Write out a time stamp file.
if (calendar_type /= NO_CALENDAR) then
call open_file(unit, 'time_stamp.out', form=ASCII_FILE, action=APPEND_FILE, &
threading=SINGLE_FILE)
if (is_root_pe() .and. (calendar_type /= NO_CALENDAR)) then
call open_ASCII_file(unit, 'time_stamp.out', action=APPEND_FILE)
call get_date(Time, date(1), date(2), date(3), date(4), date(5), date(6))
month = month_name(date(2))
if (is_root_pe()) write(unit,'(6i4,2x,a3)') date, month(1:3)
write(unit,'(6i4,2x,a3)') date, month(1:3)
call get_date(Time_end, date(1), date(2), date(3), date(4), date(5), date(6))
month = month_name(date(2))
if (is_root_pe()) write(unit,'(6i4,2x,a3)') date, month(1:3)
write(unit,'(6i4,2x,a3)') date, month(1:3)
call close_file(unit)
endif

Expand Down Expand Up @@ -428,19 +426,19 @@ program Shelf_main
dirs%restart_output_dir)

! Write ice shelf solo restart file.
call open_file(unit, trim(dirs%restart_output_dir)//'shelf.res', nohdrs=.true.)
if (is_root_pe())then
write(unit, '(i6,8x,a)') calendar_type, &
'(Calendar: no_calendar=0, thirty_day_months=1, julian=2, gregorian=3, noleap=4)'

call get_date(Start_time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Model start time: year, month, day, hour, minute, second'
call get_date(Time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Current model time: year, month, day, hour, minute, second'
call open_ASCII_file(unit, trim(dirs%restart_output_dir)//'shelf.res')
write(unit, '(i6,8x,a)') calendar_type, &
'(Calendar: no_calendar=0, thirty_day_months=1, julian=2, gregorian=3, noleap=4)'

call get_date(Start_time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Model start time: year, month, day, hour, minute, second'
call get_date(Time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Current model time: year, month, day, hour, minute, second'
call close_file(unit)
endif
call close_file(unit)
endif

if (is_root_pe()) then
Expand Down
44 changes: 21 additions & 23 deletions config_src/solo_driver/MOM_driver.F90
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ program MOM_main
use MOM_ice_shelf, only : shelf_calc_flux, add_shelf_forces, ice_shelf_save_restart
use MOM_ice_shelf, only : initialize_ice_shelf_fluxes, initialize_ice_shelf_forces
use MOM_interpolate, only : time_interp_external_init
use MOM_io, only : file_exists, open_file, close_file
use MOM_io, only : file_exists, open_ASCII_file, close_file
use MOM_io, only : check_nml_error, io_infra_init, io_infra_end
use MOM_io, only : APPEND_FILE, ASCII_FILE, READONLY_FILE, SINGLE_FILE
use MOM_io, only : APPEND_FILE, READONLY_FILE
use MOM_restart, only : MOM_restart_CS, save_restart
use MOM_string_functions,only : uppercase
use MOM_surface_forcing, only : set_forcing, forcing_save_restart
Expand Down Expand Up @@ -238,7 +238,7 @@ program MOM_main

if (file_exists('input.nml')) then
! Provide for namelist specification of the run length and calendar data.
call open_file(unit, 'input.nml', form=ASCII_FILE, action=READONLY_FILE)
call open_ASCII_file(unit, 'input.nml', action=READONLY_FILE)
read(unit, ocean_solo_nml, iostat=io_status)
call close_file(unit)
ierr = check_nml_error(io_status,'ocean_solo_nml')
Expand All @@ -252,15 +252,14 @@ program MOM_main

! Read ocean_solo restart, which can override settings from the namelist.
if (file_exists(trim(dirs%restart_input_dir)//'ocean_solo.res')) then
call open_file(unit,trim(dirs%restart_input_dir)//'ocean_solo.res', &
form=ASCII_FILE,action=READONLY_FILE)
call open_ASCII_file(unit, trim(dirs%restart_input_dir)//'ocean_solo.res', action=READONLY_FILE)
read(unit,*) calendar_type
read(unit,*) date_init
read(unit,*) date
call close_file(unit)
else
calendar = uppercase(calendar)
if (calendar(1:6) == 'JULIAN') then ; calendar_type = JULIAN
if (calendar(1:6) == 'JULIAN') then ; calendar_type = JULIAN
elseif (calendar(1:9) == 'GREGORIAN') then ; calendar_type = GREGORIAN
elseif (calendar(1:6) == 'NOLEAP') then ; calendar_type = NOLEAP
elseif (calendar(1:10)=='THIRTY_DAY') then ; calendar_type = THIRTY_DAY_MONTHS
Expand Down Expand Up @@ -432,15 +431,14 @@ program MOM_main
call diag_mediator_close_registration(diag)

! Write out a time stamp file.
if (calendar_type /= NO_CALENDAR) then
call open_file(unit, 'time_stamp.out', form=ASCII_FILE, action=APPEND_FILE, &
threading=SINGLE_FILE)
if (is_root_pe() .and. (calendar_type /= NO_CALENDAR)) then
call open_ASCII_file(unit, 'time_stamp.out', action=APPEND_FILE)
call get_date(Time, date(1), date(2), date(3), date(4), date(5), date(6))
month = month_name(date(2))
if (is_root_pe()) write(unit,'(6i4,2x,a3)') date, month(1:3)
write(unit,'(6i4,2x,a3)') date, month(1:3)
call get_date(Time_end, date(1), date(2), date(3), date(4), date(5), date(6))
month = month_name(date(2))
if (is_root_pe()) write(unit,'(6i4,2x,a3)') date, month(1:3)
write(unit,'(6i4,2x,a3)') date, month(1:3)
call close_file(unit)
endif

Expand Down Expand Up @@ -618,19 +616,19 @@ program MOM_main
if (use_ice_shelf) call ice_shelf_save_restart(ice_shelf_CSp, Time, &
dirs%restart_output_dir)
! Write ocean solo restart file.
call open_file(unit, trim(dirs%restart_output_dir)//'ocean_solo.res', nohdrs=.true.)
if (is_root_pe())then
write(unit, '(i6,8x,a)') calendar_type, &
'(Calendar: no_calendar=0, thirty_day_months=1, julian=2, gregorian=3, noleap=4)'

call get_date(Start_time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Model start time: year, month, day, hour, minute, second'
call get_date(Time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Current model time: year, month, day, hour, minute, second'
if (is_root_pe()) then
call open_ASCII_file(unit, trim(dirs%restart_output_dir)//'ocean_solo.res')
write(unit, '(i6,8x,a)') calendar_type, &
'(Calendar: no_calendar=0, thirty_day_months=1, julian=2, gregorian=3, noleap=4)'

call get_date(Start_time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Model start time: year, month, day, hour, minute, second'
call get_date(Time, yr, mon, day, hr, mins, sec)
write(unit, '(6i6,8x,a)') yr, mon, day, hr, mins, sec, &
'Current model time: year, month, day, hour, minute, second'
call close_file(unit)
endif
call close_file(unit)
endif

if (is_root_pe()) then
Expand Down
11 changes: 5 additions & 6 deletions src/diagnostics/MOM_PointAccel.F90
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ module MOM_PointAccel
use MOM_file_parser, only : get_param, log_param, log_version, param_file_type
use MOM_get_input, only : directories
use MOM_grid, only : ocean_grid_type
use MOM_io, only : open_file
use MOM_io, only : APPEND_FILE, ASCII_FILE, MULTIPLE, SINGLE_FILE
use MOM_io, only : open_ASCII_file, APPEND_FILE, MULTIPLE, SINGLE_FILE
use MOM_time_manager, only : time_type, get_time, get_date, set_date, operator(-)
use MOM_unit_scaling, only : unit_scale_type
use MOM_variables, only : ocean_internal_state, accel_diag_ptrs, cont_diag_ptrs
Expand Down Expand Up @@ -120,8 +119,8 @@ subroutine write_u_accel(I, j, um, hin, ADp, CDp, dt_in_T, G, GV, US, CS, vel_rp
! Open up the file for output if this is the first call.
if (CS%u_file < 0) then
if (len_trim(CS%u_trunc_file) < 1) return
call open_file(CS%u_file, trim(CS%u_trunc_file), action=APPEND_FILE, &
form=ASCII_FILE, threading=MULTIPLE, fileset=SINGLE_FILE)
call open_ASCII_file(CS%u_file, trim(CS%u_trunc_file), action=APPEND_FILE, &
threading=MULTIPLE, fileset=SINGLE_FILE)
if (CS%u_file < 0) then
call MOM_error(NOTE, 'Unable to open file '//trim(CS%u_trunc_file)//'.')
return
Expand Down Expand Up @@ -453,8 +452,8 @@ subroutine write_v_accel(i, J, vm, hin, ADp, CDp, dt_in_T, G, GV, US, CS, vel_rp
! Open up the file for output if this is the first call.
if (CS%v_file < 0) then
if (len_trim(CS%v_trunc_file) < 1) return
call open_file(CS%v_file, trim(CS%v_trunc_file), action=APPEND_FILE, &
form=ASCII_FILE, threading=MULTIPLE, fileset=SINGLE_FILE)
call open_ASCII_file(CS%v_file, trim(CS%v_trunc_file), action=APPEND_FILE, &
threading=MULTIPLE, fileset=SINGLE_FILE)
if (CS%v_file < 0) then
call MOM_error(NOTE, 'Unable to open file '//trim(CS%v_trunc_file)//'.')
return
Expand Down
10 changes: 4 additions & 6 deletions src/diagnostics/MOM_sum_output.F90
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module MOM_sum_output
use MOM_forcing_type, only : forcing
use MOM_grid, only : ocean_grid_type
use MOM_interface_heights, only : find_eta
use MOM_io, only : create_file, fieldtype, flush_file, open_file, reopen_file, stdout
use MOM_io, only : create_file, fieldtype, flush_file, open_ASCII_file, reopen_file, stdout
use MOM_io, only : file_exists, slasher, vardesc, var_desc, write_field, get_filename_appendix
use MOM_io, only : field_size, read_variable, read_attribute
use MOM_io, only : APPEND_FILE, ASCII_FILE, SINGLE_FILE, WRITEONLY_FILE
use MOM_io, only : APPEND_FILE, SINGLE_FILE, WRITEONLY_FILE
use MOM_open_boundary, only : ocean_OBC_type, OBC_segment_type
use MOM_open_boundary, only : OBC_DIRECTION_E, OBC_DIRECTION_W, OBC_DIRECTION_N, OBC_DIRECTION_S
use MOM_time_manager, only : time_type, get_time, get_date, set_time, operator(>)
Expand Down Expand Up @@ -583,11 +583,9 @@ subroutine write_energy(u, v, h, tv, day, n, G, GV, US, CS, tracer_CSp, OBC, dt_
! Reopen or create a text output file, with an explanatory header line.
if (is_root_pe()) then
if (day > CS%Start_time) then
call open_file(CS%fileenergy_ascii, trim(CS%energyfile), &
action=APPEND_FILE, form=ASCII_FILE, nohdrs=.true.)
call open_ASCII_file(CS%fileenergy_ascii, trim(CS%energyfile), action=APPEND_FILE)
else
call open_file(CS%fileenergy_ascii, trim(CS%energyfile), &
action=WRITEONLY_FILE, form=ASCII_FILE, nohdrs=.true.)
call open_ASCII_file(CS%fileenergy_ascii, trim(CS%energyfile), action=WRITEONLY_FILE)
if (abs(CS%timeunit - 86400.0) < 1.0) then
if (CS%use_temperature) then
write(CS%fileenergy_ascii,'(" Step,",7x,"Day, Truncs, &
Expand Down
10 changes: 5 additions & 5 deletions src/framework/MOM_io.F90
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ module MOM_io
use MOM_io_infra, only : MOM_read_data, MOM_read_vector, read_field_chksum
use MOM_io_infra, only : read_data=>MOM_read_data ! read_data will be removed soon.
use MOM_io_infra, only : file_exists, get_file_info, get_file_fields, get_field_atts
use MOM_io_infra, only : open_file, close_file, get_field_size, fieldtype, field_exists
use MOM_io_infra, only : flush_file, get_filename_suffix
use MOM_io_infra, only : get_file_times, axistype, get_axis_data
use MOM_io_infra, only : open_file, open_ASCII_file, close_file, flush_file
use MOM_io_infra, only : get_field_size, fieldtype, field_exists
use MOM_io_infra, only : get_file_times, axistype, get_axis_data, get_filename_suffix
use MOM_io_infra, only : write_field, write_metadata, write_version
use MOM_io_infra, only : MOM_namelist_file, check_namelist_error, io_infra_init, io_infra_end
use MOM_io_infra, only : APPEND_FILE, ASCII_FILE, MULTIPLE, NETCDF_FILE, OVERWRITE_FILE
Expand All @@ -40,8 +40,8 @@ module MOM_io
public :: get_var_sizes, verify_variable_units, num_timelevels, read_variable, read_attribute
public :: open_file_to_read, close_file_to_read
! The following are simple pass throughs of routines from MOM_io_infra or other modules.
public :: file_exists, open_file, close_file, flush_file, get_filename_appendix
public :: get_file_info, field_exists, get_file_fields, get_file_times
public :: file_exists, open_file, open_ASCII_file, close_file, flush_file
public :: get_file_info, field_exists, get_file_fields, get_file_times, get_filename_appendix
public :: fieldtype, field_size, get_field_atts
public :: axistype, get_axis_data
public :: MOM_read_data, MOM_read_vector, read_field_chksum
Expand Down
27 changes: 23 additions & 4 deletions src/framework/MOM_io_infra.F90
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module MOM_io_infra
implicit none ; private

! These interfaces are actually implemented or have explicit interfaces in this file.
public :: open_file, close_file, flush_file, file_exists, get_filename_suffix
public :: get_file_info, get_file_fields, get_file_times
public :: open_file, open_ASCII_file, close_file, flush_file, file_exists
public :: get_file_info, get_file_fields, get_file_times, get_filename_suffix
public :: MOM_read_data, MOM_read_vector, write_metadata, write_field
public :: field_exists, get_field_atts, get_field_size, get_axis_data, read_field_chksum
public :: io_infra_init, io_infra_end, MOM_namelist_file, check_namelist_error, write_version
Expand Down Expand Up @@ -129,14 +129,14 @@ end function FMS_file_exists
!> close_file closes a file (or fileset). If the file handle does not point to an open file,
!! close_file simply returns without doing anything.
subroutine close_file(unit)
integer, intent(out) :: unit !< The I/O unit for the file to be closed
integer, intent(inout) :: unit !< The I/O unit for the file to be closed

call mpp_close(unit)
end subroutine close_file

!> Ensure that the output stream associated with a unit is fully sent to dis.
subroutine flush_file(unit)
integer, intent(out) :: unit !< The I/O unit for the file to flush
integer, intent(in) :: unit !< The I/O unit for the file to flush

call mpp_flush(unit)
end subroutine flush_file
Expand Down Expand Up @@ -206,6 +206,25 @@ subroutine open_file(unit, file, action, form, threading, fileset, nohdrs, domai
endif
end subroutine open_file

!> open_file opens an ascii file for parallel or single-file I/O.
subroutine open_ASCII_file(unit, file, action, threading, fileset)
integer, intent(out) :: unit !< The I/O unit for the opened file
character(len=*), intent(in) :: file !< The name of the file being opened
integer, optional, intent(in) :: action !< A flag indicating whether the file can be read
!! or written to and how to handle existing files.
integer, optional, intent(in) :: threading !< A flag indicating whether one (SINGLE_FILE)
!! or multiple PEs (MULTIPLE) participate in I/O.
!! With the default, the root PE does I/O.
integer, optional, intent(in) :: fileset !< A flag indicating whether multiple PEs doing I/O due
!! to threading=MULTIPLE write to the same file (SINGLE_FILE)
!! or to one file per PE (MULTIPLE, the default).

call mpp_open(unit, file, action=action, form=ASCII_FILE, threading=threading, fileset=fileset, &
nohdrs=.true.)

end subroutine open_ASCII_file


!> Provide a string to append to filenames, to differentiate ensemble members, for example.
subroutine get_filename_suffix(suffix)
character(len=*), intent(out) :: suffix !< A string to append to filenames
Expand Down
14 changes: 6 additions & 8 deletions src/framework/MOM_write_cputime.F90
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module MOM_write_cputime

! This file is part of MOM6. See LICENSE.md for the license.

use MOM_coms, only : sum_across_PEs, num_pes
use MOM_coms, only : sum_across_PEs, num_pes
use MOM_error_handler, only : MOM_error, MOM_mesg, FATAL, is_root_pe
use MOM_io, only : open_file, close_file, APPEND_FILE, ASCII_FILE, WRITEONLY_FILE
use MOM_file_parser, only : get_param, log_param, log_version, param_file_type
use MOM_time_manager, only : time_type, get_time, operator(>)
use MOM_io, only : open_ASCII_file, close_file, APPEND_FILE, WRITEONLY_FILE
use MOM_file_parser, only : get_param, log_param, log_version, param_file_type
use MOM_time_manager, only : time_type, get_time, operator(>)

implicit none ; private

Expand Down Expand Up @@ -181,11 +181,9 @@ subroutine write_cputime(day, n, CS, nmax, call_end)
! Reopen or create a text output file.
if ((CS%previous_calls == 0) .and. (is_root_pe())) then
if (day > CS%Start_time) then
call open_file(CS%fileCPU_ascii, trim(CS%CPUfile), &
action=APPEND_FILE, form=ASCII_FILE, nohdrs=.true.)
call open_ASCII_file(CS%fileCPU_ascii, trim(CS%CPUfile), action=APPEND_FILE)
else
call open_file(CS%fileCPU_ascii, trim(CS%CPUfile), &
action=WRITEONLY_FILE, form=ASCII_FILE, nohdrs=.true.)
call open_ASCII_file(CS%fileCPU_ascii, trim(CS%CPUfile), action=WRITEONLY_FILE)
endif
endif

Expand Down

0 comments on commit 27cd1c4

Please sign in to comment.