Reading netCDF4 file in MY_TRC

This section describes the ways to read an user-defined nerCDF4 file during the time stepping of MY_TRC in NEMO 3.4.

Through NEMO’s existing file channel

By replacing the variable information in namelist/&namdta_dyn to your own, the tracer model can read your files during the simulation. An example of it can be found in here. (this section needs more explainations)

By creating a new namelist


The examples here and below are requiring a netCDF4 file in EXP00 which contains nav_lat, nav_lon, time_counter and var_name. The netCDF4 file should have the same longtitude/latitude dimensions as the file in your case. The time dimension should be “UNLIMITED” and var_name is the variable you want MY_TRC to read.

The arrangment of the dimensions (For Python users) should be (TIME, LAT, LON).

Create namelist_my_trc

In $your_case/EXP00/, create the file namelist_my_trc:

!      ! file name ! frequency (hours) ! variable ! time interp. !  clim  ! 'yearly'/ ! weights  ! rotation !
!      !           !  (if <0  months)  !   name   !   (logical)  !  (T/F) ! 'monthly' ! filename ! pairing  !
sn_var ='file_name',        -12        ,'var_name',    .false.   , .true. ,  'yearly' ,    ''    ,    ''
cn_dir = './'
  • frequency is the reading frequency of the variable in time dimension
  • clim is the flag of file batching ...
  • A section should be ended with /

Edit MY_TRC scripts

The structure and access information can be added in trcini_my_trc.F90 and trcnam_my_trc.F90. When the variable is read, it can be used in trcsms_my_trc.F90.

In trcini_my_trc.F90, assign the structure of your file by fld_fill(...).


PUBLIC   trc_ini_my_trc   ! called by trcini.F90 module

SUBROUTINE trc_ini_my_trc
  IF(trc_sms_my_trc_alloc() /= 0) THEN
     CALL ctl_stop('STOP', 'trc_ini_my_trc: unable to allocate MY_TRC arrays')
  ! Assign structure
  CALL fld_fill(sf_var, (/sn_var/), cn_dir, 'trc_ini_my_trc', 'docs', 'namelist_section')
END SUBROUTINE trc_ini_my_trc

In trcnam_my_trc.F90, read the variable through ctl_opn(...). The name of the namelist "namelist_my_trc" should be consistent with the one created in the section above.


PUBLIC   trc_nam_my_trc   ! called by trcnam.F90 module


SUBROUTINE trc_nam_my_trc
  INTEGER :: numnatl
  NAMELIST/namelist_section/ cn_dir, sn_var
  CALL ctl_opn(numnatl, 'namelist_my_trc', 'OLD', 'FORMATTED', 'SEQUENTIAL', 1, numout, .FALSE.)
  READ  (numnatl, namelist_section)
END SUBROUTINE trc_nam_my_trc

trcsms_my_trc.F90 call the two scripts above and allocates the array.


PUBLIC   trc_sms_my_trc       ! called by trcsms.F90 module
PUBLIC   trc_sms_my_trc_alloc ! called by trcini_my_trc.F90 module

CHARACTER(len=100), PUBLIC :: cn_dir = './'    ! Root directorY
TYPE(FLD_N) :: sn_var                          ! information about the file to be read
REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: var   ! Array receives the value from netCDF
TYPE(FLD), ALLOCATABLE, DIMENSION(:) :: sf_var ! structure variable (PUBLIC for TAM)


SUBROUTINE trc_sms_my_trc( kt )
  INTEGER, INTENT(in) :: kt ! ocean e-step index
  INTEGER :: i, j
  IF(nn_timing == 1) CALL timing_start('trc_sms_my_trc')
  CALL fld_read (kt, 1, sf_var)
  var(:, :) = sf_var(1)%fnow(:, :, 1)
  ! More code ...
END SUBROUTINE trc_sms_my_trc

INTEGER FUNCTION trc_sms_my_trc_alloc()
  INTEGER :: ierror
  ALLOCATE(var(jpi,jpj), STAT=trc_sms_my_trc_alloc)
  ALLOCATE(sf_var(1), STAT=ierror)
  IF(ierror > 0) THEN
     CALL ctl_stop('trc_sms_my_trc_alloc: unable to allocate');
  ALLOCATE(sf_var(1)%fnow(jpi, jpj, 1))
  IF(trc_sms_my_trc_alloc /= 0) THEN
     CALL ctl_warn('trc_sms_my_trc_alloc : failed to allocat')
END FUNCTION trc_sms_my_trc_alloc

For 4 dimension variables (time dimension has been subtrackted by keyword “frequency” in the namelist): var(:, :, :) = sf_var(1)%fnow(:, :, :).