Skip to content

Commit

Permalink
update_mpas_states - copying variables from one file to another
Browse files Browse the repository at this point in the history
todo: wind, domain_id
see #753 (comment)
for various notes on update_mpas_states
  • Loading branch information
hkershaw-brown committed Dec 26, 2024
1 parent 40eda41 commit 78ba958
Showing 1 changed file with 29 additions and 51 deletions.
80 changes: 29 additions & 51 deletions models/mpas_atm/update_mpas_states.f90
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,68 @@ program update_mpas_states
! The update_mpas_states_nml namelist defines the input and output file
! name lists for all ensemble members.
! The input list should be matched with output_state_file_list in &filter_nml.
!
!
! variables that are not wind, copied from one file to the another
! variables that are wind, reconstructed
!
! author: Soyoung Ha 23 Aug 16
! Updated in 4 May 2017 for the Manhattan release
!----------------------------------------------------------------------

use types_mod, only : r8
use utilities_mod, only : initialize_utilities, finalize_utilities, &
find_namelist_in_file, check_namelist_read, &
logfileunit, get_next_filename, E_ERR, error_handler
logfileunit, get_next_filename, E_ERR, E_MSG, error_handler
use time_manager_mod, only : time_type, print_time, print_date, operator(-), &
get_time, get_date, operator(/=)
use direct_netcdf_mod,only : read_transpose, read_variables
use model_mod, only : static_init_model, &
get_model_size, &
get_analysis_time

use state_structure_mod, only : get_num_variables, get_variable_name, &
get_variable_size

use netcdf_utilities_mod, only : nc_open_file_readonly, &
nc_open_file_readwrite, &
nc_get_variable, nc_put_variable, &
nc_close_file

use netcdf


implicit none

! version controlled file description for error handling, do not edit
character(len=*), parameter :: source = 'models/mpas_atm/update_mpas_states.f90'
character(len=*), parameter :: revision = ''
character(len=*), parameter :: revdate = ''

!------------------------------------------------------------------
! The namelist variables
!------------------------------------------------------------------

character(len=256) :: update_input_file_list = 'filter_out.txt'
character(len=256) :: update_output_file_list = 'filter_in.txt'
logical :: print_data_ranges = .true.
integer :: dom_id = 1

namelist /update_mpas_states_nml/ update_input_file_list, update_output_file_list, dom_id, &
print_data_ranges
namelist /update_mpas_states_nml/ update_input_file_list, update_output_file_list

!----------------------------------------------------------------------
character (len=256) :: next_infile, next_outfile
character (len=512) :: string1
integer :: iunit, io, x_size, nvars
integer :: iunit, io
integer :: ncAnlID, ncBckID, istatus
integer :: filenum
real(r8), allocatable :: statevector(:)
integer :: filenum, i
real(r8), allocatable :: variable(:)
type(time_type) :: model_time
type(time_type) :: state_time
!----------------------------------------------------------------------

call initialize_utilities(progname=source)

! Read the namelist to get the input filename.

call find_namelist_in_file("input.nml", "update_mpas_states_nml", iunit)
read(iunit, nml = update_mpas_states_nml, iostat = io)
call check_namelist_read(iunit, io, "update_mpas_states_nml")

!----------------------------------------------------------------------
! Call model_mod:static_init_model() which reads the model namelists
! to set grid sizes, etc.
!----------------------------------------------------------------------

call static_init_model()

nvars = 1 !get_num_vars() !HK @todo
x_size = get_model_size()
allocate(statevector(x_size))

write(*,*)
write(*,*) 'update_mpas_states: Updating ',nvars,' variables'
write(*,*)

!----------------------------------------------------------------------
! Reads lists of input mpas (prior) and filter (analysis) files
! HK @todo this is bad to have a serial loop around files.
!----------------------------------------------------------------------
filenum = 1
fileloop: do ! until out of files
Expand All @@ -102,9 +88,6 @@ program update_mpas_states
ncAnlID = nc_open_file_readonly(next_infile, 'update_mpas_states - open readonly')
ncBckID = nc_open_file_readwrite(next_outfile, 'update_mpas_states - open readwrite')

!----------------------------------------------------------------------
! Read the model time
!----------------------------------------------------------------------
model_time = get_analysis_time(ncBckID, next_outfile)
state_time = get_analysis_time(ncAnlID, next_infile)
call print_time(state_time,'DART current time')
Expand All @@ -114,36 +97,31 @@ program update_mpas_states
call print_time(state_time,'DART current time',logfileunit)
call print_time(model_time,'mpas current time',logfileunit)
write(string1,*) trim(next_infile),' current time must equal model time'
call error_handler(E_ERR,'update_mpas_states',string1,source,revision,revdate)
call error_handler(E_ERR,'update_mpas_states',string1,source)
endif

!----------------------------------------------------------------------
! Read analysis state vector (assuming to be available at the model time)
!----------------------------------------------------------------------
call read_variables(ncAnlID, statevector, 1, nvars, dom_id) !HK @todo do not call read_variables directly
! copy variables that are not wind from analysis to background
varloop: do i = 1, get_num_variables(1)

allocate(variable(get_variable_size(1, i)))

!----------------------------------------------------------------------
! update the current model state vector
!----------------------------------------------------------------------
write(*,*) 'Overwriting states in ',trim(next_outfile)
!call statevector_to_analysis_file(statevector, ncBckID, next_outfile) !HK @todo
call error_handler(E_ERR, 'update_mpas_states', 'Not implemented yet', source)
call nc_get_variable(ncAnlID, get_variable_name(1,i), variable)
call nc_put_variable(ncBckID, get_variable_name(1,i), variable)

!----------------------------------------------------------------------
! Log what we think we're doing, and exit.
!----------------------------------------------------------------------
deallocate(variable)

enddo varloop

call error_handler(E_MSG, 'Overwriting states in ',trim(next_outfile), source)

call print_date( model_time,'update_mpas_states:model date')
call print_time( model_time,'update_mpas_states:model time')
call print_date( model_time,'update_mpas_states:model date',logfileunit)
call print_time( model_time,'update_mpas_states:model time',logfileunit)

! Because the files were open with the nc_open_file...() routines,
! it is not necessary to supply the filenames for error msg purposes.

call nc_close_file(ncAnlID,'update_mpas_states')
call nc_close_file(ncBckID,'update_mpas_states')

filenum = filenum + 1

end do fileloop
Expand Down

0 comments on commit 78ba958

Please sign in to comment.