[v4.2.x] `namcouple variable not used` error (NEMO-WRF-OASIS with nested domain)

I am trying to launch trunk NEMO with one nested domain in coupled mode (NEMO + WRF + OASIS).

The coupled model fails with the messages

(oasis_coupler_setup) ERROR: namcouple variable not used: 1_O_SSTSST
(oasis_coupler_setup) ERROR: namcouple variable not used: 1_O_OCurx1
etc

I checked debug files and it seems that NEMO does not call oasis_def_var() for nested domain, i.e. for variables 1_O_SSTSST 1_O_OCurx1 1_O_OCury1 1_O_OTaux1 1_O_OTauy1 1_O_QsrOce 1_O_QnsOce 1_OOEvaMPr 1_O_TauMod

I used the following command to compile NEMO

$ ./makenemo -m linux_ifort_cpl_agrif -r AGRIF_DEMO -n MY_OCE_cpl_agrif_no_xios -j 8 del_key "key_si3 key_top key_xios" add_key "key_netcdf4 key_agrif key_oasis3"

Also, I launched NEMO in uncoupled mode with one nested domain and it ran successfully.

What else options should I check?
Can you please give me hints/ suggestions?

the first thing to look at is your namcouple, namelist_cfg and 1_namelist_cfg files to make sure you properly defined all exchanges between OASIS and NEMO parent and child grids.

It seems there is a bug in NEMOGCM/src/OCE/SBC/cpl_oasis3.F90

#if defined key_agrif
      ! Warning: Agrif_Nb_Fine_Grids not yet defined at this stage for Agrif_Root -> must use Agrif_Root_Only()
      IF( Agrif_Root_Only() .OR. agrif_fixed() == Agrif_Nb_Fine_Grids() ) THEN
#endif
      CALL oasis_enddef(nerror)
      IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
#if defined key_agrif
      ENDIF
#endif

I checked that agrif_fixed() and Agrif_Nb_Fine_Grids() are both = 0 for a parent domain.
So NEMO calls oasis_enddef for a parent domain, which prevents initialisation of variables for a nested domain.

I replaced IF( Agrif_Root_Only() .OR. agrif_fixed() == Agrif_Nb_Fine_Grids() ) THEN by IF( .not. Agrif_Root() ) THEN

I agree with the diagnostic of the bug. It is an old staff that was not correctly fixed.
I created a related ticket on NEMO GitLab.

I propose a more general solution in 841e6954
Is it working for you?