Skip to content
Snippets Groups Projects
write_diachro.f90 82.4 KiB
Newer Older
  • Learn to ignore specific revisions
  •         if ( htype /= 'CART' ) call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                        ': group ' // Trim( hgroup ) //': gsplit=T not implemented for these dimensions and htype/=CART' )
    
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 2 ], kbutimepos =  3 )
    
    
            !Create the metadata of the field (has to be done only once)
            if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
            call IO_Field_write( tzfile, tzfield, Reshape( zdata2d, [ Size(zdata2d,1), Size(zdata2d,2), 1 ] ), &
                                 koffset= [ 0, 0, ( nbutshift - 1 ) * nbusubwrite ] )
          else
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 2 ] )
    
            call IO_Field_write( tzfile, tzfield, zdata2d )
          end if
    
        else if (       Any( tpfields(1)%ndimlist(1) == [ NMNHDIM_NI, NMNHDIM_NI_U, NMNHDIM_NI_V, NMNHDIM_BUDGET_CART_NI, &
                                                          NMNHDIM_BUDGET_CART_NI_U, NMNHDIM_BUDGET_CART_NI_V ] )          &
                  .and. Any( tpfields(1)%ndimlist(3) == [ NMNHDIM_LEVEL, NMNHDIM_LEVEL_W,                                 &
                                                          NMNHDIM_BUDGET_CART_LEVEL, NMNHDIM_BUDGET_CART_LEVEL_W ] )      ) then
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata2d( size(pvar,1), size(pvar,3) ) )
    
          zdata2d(:,:) = pvar(:, 1, :, 1, 1, 1)
    
    
          if ( gsplit ) then
            if ( htype /= 'CART' ) call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                        ': group ' // Trim( hgroup ) //': gsplit=T not implemented for these dimensions and htype/=CART' )
    
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 3 ], kbutimepos =  3 )
    
    
            !Create the metadata of the field (has to be done only once)
            if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
            call IO_Field_write( tzfile, tzfield, Reshape( zdata2d, [ Size(zdata2d,1), Size(zdata2d,2), 1 ] ), &
                                 koffset= [ 0, 0, ( nbutshift - 1 ) * nbusubwrite ] )
          else
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 3 ] )
    
        else if (       Any( tpfields(1)%ndimlist(2) == [ NMNHDIM_NJ, NMNHDIM_NJ_U, NMNHDIM_NJ_V, NMNHDIM_BUDGET_CART_NJ, &
                                                          NMNHDIM_BUDGET_CART_NJ_U, NMNHDIM_BUDGET_CART_NJ_V ] )          &
                  .and. Any( tpfields(1)%ndimlist(3) == [ NMNHDIM_LEVEL, NMNHDIM_LEVEL_W,                                 &
                                                          NMNHDIM_BUDGET_CART_LEVEL, NMNHDIM_BUDGET_CART_LEVEL_W ] )      ) then
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata2d( size(pvar,2), size(pvar,3) ) )
    
          zdata2d(:,:) = pvar(1, :, :, 1, 1, 1)
    
    
          if ( gsplit ) then
            if ( htype /= 'CART' ) call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                        ': group ' // Trim( hgroup ) //': gsplit=T not implemented for these dimensions and htype/=CART' )
    
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 2, 3 ], kbutimepos =  3 )
    
    
            !Create the metadata of the field (has to be done only once)
            if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
            call IO_Field_write( tzfile, tzfield, Reshape( zdata2d, [ Size(zdata2d,1), Size(zdata2d,2), 1 ] ), &
                                 koffset= [ 0, 0, ( nbutshift - 1 ) * nbusubwrite ] )
          else
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 2, 3 ] )
    
        else if (  Any( tpfields(1)%ndimlist(1) == [ NMNHDIM_BUDGET_CART_NI, NMNHDIM_BUDGET_CART_NI_U, NMNHDIM_BUDGET_CART_NI_V ] ) &
                  .and. tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_NGROUPS ) then
          Allocate( zdata1d(Size( pvar, 1 )) )
    
          do ji = 1, Size( pvar, 6 )
            zdata1d(:) = pvar(:, 1, 1, 1, 1, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 1 ], kbutimepos =  2 )
    
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield, Reshape( zdata1d, [ Size(zdata1d,1), 1 ] ), &
                                   koffset= [ 0, ( nbutshift - 1 ) * nbusubwrite ] )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 1 ] )
    
              call IO_Field_write( tzfile, tzfield, zdata1d )
            end if
          end do
    
          Deallocate( zdata1d )
    
        else if (  Any( tpfields(1)%ndimlist(2) == [ NMNHDIM_BUDGET_CART_NJ, NMNHDIM_BUDGET_CART_NJ_U, NMNHDIM_BUDGET_CART_NJ_V ] ) &
                  .and. tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_NGROUPS ) then
          Allocate( zdata1d(Size( pvar, 2 )) )
    
          do ji = 1, Size( pvar, 6 )
            zdata1d(:) = pvar(1, :, 1, 1, 1, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 2 ], kbutimepos =  2 )
    
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield, Reshape( zdata1d, [ Size(zdata1d,1), 1 ] ), &
                                   koffset= [ 0, ( nbutshift - 1 ) * nbusubwrite ] )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 2 ] )
    
              call IO_Field_write( tzfile, tzfield, zdata1d )
            end if
          end do
    
          Deallocate( zdata1d )
    
    
        else if (       tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_LES_LEVEL     &
           .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME &
                   .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata2d( size(pvar,3), size(pvar,4) ) )
    
          zdata2d(:,:) = pvar(1, 1, :, :, 1, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 3, 4 ] )
    
          CALL IO_Field_write( tzfile, tzfield, zdata2d )
    
          deallocate( zdata2d )
    
    
        else if ( Any( tpfields(1)%ndimlist(3) == [ NMNHDIM_BUDGET_CART_LEVEL, NMNHDIM_BUDGET_CART_LEVEL_W ] ) &
                  .and. tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_NGROUPS ) then
          tzfield%ndimlist(1)  = tpfields(1)%ndimlist(3)
          tzfield%ndimlist(2:) = NMNHDIM_UNUSED
    
          allocate( zdata1d( size(pvar,3) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, Size( pvar, 6 )
            zdata1d(:) = pvar(1, 1, :, 1, 1, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 3 ], kbutimepos =  2 )
    
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield, Reshape( zdata1d, [ Size(zdata1d,1), 1 ] ), &
                                   koffset= [ 0, ( nbutshift - 1 ) * nbusubwrite ] )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 3 ] )
    
              call IO_Field_write( tzfile, tzfield, zdata1d )
            end if
          end do
    
          deallocate( zdata1d )
    
    
        else if (  tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_TIME .and. tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_MASK_NBUMASK ) then
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
          Allocate( zdata2d(Size( pvar, 4 ), Size( pvar, 5 )) )
    
          zdata2d(:,:) = pvar(1, 1, 1, :, :, 1)
    
          if ( gsplit ) then
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 4, 5 ], kbutimepos =  1 )
    
    
            !Create the metadata of the field (has to be done only once)
            if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
            call IO_Field_write( tzfile, tzfield, zdata2d, koffset= [ ( nbutshift - 1 ) * nbusubwrite, 0 ] )
          else
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 4, 5 ] )
    
            call IO_Field_write( tzfile, tzfield, zdata2d )
          end if
    
          Deallocate( zdata2d )
    
    
        else if (  (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME &
    
                     .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
    
             .and. tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_LES_SV       ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata2d( size(pvar,4), size(pvar,5) ) )
    
          zdata2d(:,:) = pvar(1, 1, 1, :, :, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 4, 5 ] )
    
    
          CALL IO_Field_write( tzfile, tzfield, zdata2d )
    
          deallocate( zdata2d )
    
        else if (  tpfields(1)%ndimlist(4) == NMNHDIM_FLYER_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_FLYER_PROC ) then
          !Correspond to FLYER_DIACHRO
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          !Create local time dimension
          if ( isp == tzfile%nmaster_rank) then
            istatus = NF90_DEF_DIM( igrpid, 'time_flyer', Size( pvar, 4), idimid )
            if ( istatus /= NF90_NOERR ) &
              call IO_Err_handle_nc4( istatus, 'Write_diachro_nc4', 'NF90_DEF_DIM', Trim( tpfields(1)%cmnhname ) )
          end if
    
          allocate( zdata1d( size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata1d(:) = pvar(1, 1, 1, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata1d )
          end do
    
          deallocate( zdata1d )
        else if (  tpfields(1)%ndimlist(4) == NMNHDIM_STATION_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_STATION_PROC ) then
          !Correspond to STATION_DIACHRO_n
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata1d( size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata1d(:) = pvar(1, 1, 1, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata1d )
          end do
    
          deallocate( zdata1d )
        else if (  tpfields(1)%ndimlist(4) == NMNHDIM_SERIES_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_SERIES_PROC ) then
          !Correspond to WRITE_SERIES_n
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata1d( size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata1d(:) = pvar(1, 1, 1, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata1d )
          end do
    
          deallocate( zdata1d )
        else
          call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                          'case not yet implemented (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
    
        if (       Any( tpfields(1)%ndimlist(1) == [ NMNHDIM_NI, NMNHDIM_NI_U, NMNHDIM_NI_V, NMNHDIM_BUDGET_CART_NI, &
                                                     NMNHDIM_BUDGET_CART_NI_U, NMNHDIM_BUDGET_CART_NI_V ] )          &
             .and. Any( tpfields(1)%ndimlist(2) == [ NMNHDIM_NJ, NMNHDIM_NJ_U, NMNHDIM_NJ_V, NMNHDIM_BUDGET_CART_NJ, &
                                                     NMNHDIM_BUDGET_CART_NJ_U, NMNHDIM_BUDGET_CART_NJ_V ] )          &
             .and. Any( tpfields(1)%ndimlist(3) == [ NMNHDIM_LEVEL, NMNHDIM_LEVEL_W,                                 &
                                                     NMNHDIM_BUDGET_CART_LEVEL, NMNHDIM_BUDGET_CART_LEVEL_W ] )      ) then
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
          allocate( zdata3d( size(pvar,1), size(pvar,2), size(pvar,3) ) )
    
          zdata3d(:,:,:) = pvar(:, :, :, 1, 1, 1)
    
    
          if ( gsplit ) then
            if ( htype /= 'CART' ) call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                        ': group ' // Trim( hgroup ) //': gsplit=T not implemented for these dimensions and htype/=CART' )
    
            if ( htype == 'CART' .and. .not. oicp .and. .not. ojcp ) then
    
              call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 2, 3 ], kbutimepos = 4 )
    
    
              !Data is distributed between all the processes
              tzfield%cdir = 'XY'
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write_box( tzfile, tzfield, 'BUDGET',                                                     &
                                       Reshape( zdata3d, [ Size(zdata3d,1), Size(zdata3d,2), Size(zdata3d,3), 1 ] ) , &
                                       kil + jphext, kih + jphext, kjl + jphext, kjh + jphext,                        &
                                       koffset= [ 0, 0, 0, ( nbutshift - 1 ) * nbusubwrite ]                          )
            else
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
            end if
    
            if ( htype == 'CART' .and. .not. oicp .and. .not. ojcp ) then
    
              call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 2, 3 ] )
    
    
              !Data is distributed between all the processes
              tzfield%cdir = 'XY'
              call IO_Field_write_box( tzfile, tzfield, 'BUDGET', zdata3d, &
                                      kil + jphext, kih + jphext, kjl + jphext, kjh + jphext )
            else
    
              call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 2, 3 ]  )
    
    
    
        else if (       Any(tpfields(1)%ndimlist(1) == [ NMNHDIM_BUDGET_CART_NI, NMNHDIM_BUDGET_CART_NI_U, NMNHDIM_BUDGET_CART_NI_V ]) &
                  .and. Any(tpfields(1)%ndimlist(2) == [ NMNHDIM_BUDGET_CART_NJ, NMNHDIM_BUDGET_CART_NJ_U, NMNHDIM_BUDGET_CART_NJ_V ]) &
    
                  .and.     tpfields(1)%ndimlist(6) ==   NMNHDIM_BUDGET_NGROUPS                                                   ) then
    
          Allocate( zdata2d(Size( pvar, 1 ), Size( pvar, 2 )) )
    
          ! Loop on the processes
          do ji = 1, Size( pvar, 6 )
            zdata2d(:, :) = pvar(:, :, 1, 1, 1, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 2 ], kbutimepos = 3 )
    
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield,                                             &
                                   Reshape( zdata2d, [ Size(zdata2d,1), Size(zdata2d,2), 1 ] ), &
                                   koffset= [ 0, 0, ( nbutshift - 1 ) * nbusubwrite ]           )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 2 ] )
    
              call IO_Field_write( tzfile, tzfield, zdata2d )
            end if
          end do
    
          Deallocate( zdata2d )
    
    
        else if ( Any ( tpfields(1)%ndimlist(1) == [ NMNHDIM_BUDGET_CART_NI, NMNHDIM_BUDGET_CART_NI_U, NMNHDIM_BUDGET_CART_NI_V ] ) &
    
                  .and. tpfields(1)%ndimlist(3) ==   NMNHDIM_BUDGET_CART_LEVEL                                                      &
                  .and. tpfields(1)%ndimlist(6) ==   NMNHDIM_BUDGET_NGROUPS                                                      ) then
    
          Allocate( zdata2d(Size( pvar, 1 ), Size( pvar, 3 )) )
    
          ! Loop on the processes
          do ji = 1, Size( pvar, 6 )
            zdata2d(:, :) = pvar(:, 1, :, 1, 1, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 3 ], kbutimepos = 3 )
    
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield,                                             &
                                   Reshape( zdata2d, [ Size(zdata2d,1), Size(zdata2d,2), 1 ] ), &
                                   koffset= [ 0, 0, ( nbutshift - 1 ) * nbusubwrite ]           )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 3 ] )
    
              call IO_Field_write( tzfile, tzfield, zdata2d )
            end if
          end do
    
          Deallocate( zdata2d )
        else if ( Any ( tpfields(1)%ndimlist(2) == [ NMNHDIM_BUDGET_CART_NJ, NMNHDIM_BUDGET_CART_NJ_U, NMNHDIM_BUDGET_CART_NJ_V ] ) &
    
                  .and. tpfields(1)%ndimlist(3) ==   NMNHDIM_BUDGET_CART_LEVEL                                                      &
                  .and. tpfields(1)%ndimlist(6) ==   NMNHDIM_BUDGET_NGROUPS                                                      ) then
    
          Allocate( zdata2d(Size( pvar, 2 ), Size( pvar, 3 )) )
    
          ! Loop on the processes
          do ji = 1, Size( pvar, 6 )
            zdata2d(:, :) = pvar(1, :, :, 1, 1, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 2, 3 ], kbutimepos = 3 )
    
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield,                                             &
                                   Reshape( zdata2d, [ Size(zdata2d,1), Size(zdata2d,2), 1 ] ), &
                                   koffset= [ 0, 0, ( nbutshift - 1 ) * nbusubwrite ]           )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 2, 3 ] )
    
              call IO_Field_write( tzfile, tzfield, zdata2d )
            end if
          end do
    
          Deallocate( zdata2d )
    
        else if (         (      tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_MASK_LEVEL     &
                            .or. tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_MASK_LEVEL_W ) &
                   .and.         tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_TIME           &
                   .and.         tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_MASK_NBUMASK   ) then
    
          !Correspond to Store_one_budget_rho (MASK)
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata3d( size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          zdata3d(:,:,:) = pvar(1, 1, :, :, :, 1)
    
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 3, 4, 5 ], kbutimepos = 2 )
    
            !Create the metadata of the field (has to be done only once)
            if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
            call IO_Field_write( tzfile, tzfield, zdata3d(:,:,:), koffset= [ 0, ( nbutshift - 1 ) * nbusubwrite, 0 ] )
          else
    
            call Prepare_diachro_write( tpfields(1), tzfield, [ 3, 4, 5 ] )
    
        else if (              tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_LES_LEVEL      &
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
                  .and.        tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_LES_MASK       ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( nles_masks /= Size( pvar, 6 ) ) &
            call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4',                             &
                            'last dimension size of pvar is not equal to nles_masks (variable ' &
                            // Trim( tpfields(1)%cmnhname ) // ')' )
    
          allocate( zdata2d( size(pvar,3), size(pvar,4) ) )
    
          ! Loop on the masks (1 written variable per mask)
          do ji = 1, size(pvar,6)
            zdata2d(:,:) = pvar(1, 1, :, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata2d )
          end do
    
          deallocate( zdata2d )
        else if (       tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_LES_LEVEL     &
           .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME &
                   .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
           .and. tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_TERM      ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata2d( size(pvar,3), size(pvar,4) ) )
    
          ! Loop on the masks (1 written variable per mask)
          do ji = 1, size(pvar,6)
            zdata2d(:,:) = pvar(1, 1, :, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata2d )
          end do
    
          deallocate( zdata2d )
    
        else if (              tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_LES_LEVEL      &
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
                  .and.        tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_LES_SV         ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          !Correspond to Les_diachro_sv_new
          allocate( zdata3d( size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          zdata3d(:,:,:) = pvar(1, 1, :, :, :, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 3, 4, 5 ] )
    
    
          CALL IO_Field_write( tzfile, tzfield, zdata3d )
    
          deallocate( zdata3d )
    
        else if (              tpfields(1)%ndimlist(1) == NMNHDIM_SPECTRA_2PTS_NI       &
                  .and.        tpfields(1)%ndimlist(3) == NMNHDIM_SPECTRA_LEVEL         &
    
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata3d( size(pvar,1), size(pvar,3), size(pvar,4) ) )
    
          zdata3d(:,:,:) = pvar(:, 1, :, :, 1, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 3, 4 ] )
    
    
          CALL IO_Field_write( tzfile, tzfield, zdata3d )
    
          deallocate( zdata3d )
        else if (       tpfields(1)%ndimlist(2) == NMNHDIM_SPECTRA_2PTS_NJ                   &
                  .and. tpfields(1)%ndimlist(3) == NMNHDIM_SPECTRA_LEVEL                &
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata3d( size(pvar,2), size(pvar,3), size(pvar,4) ) )
    
          zdata3d(:,:,:) = pvar(1, :, :, :, 1, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 2, 3, 4 ] )
    
    
          CALL IO_Field_write( tzfile, tzfield, zdata3d )
    
          deallocate( zdata3d )
        else if (  tpfields(1)%ndimlist(3) == NMNHDIM_LEVEL      &
             .and. tpfields(1)%ndimlist(4) == NMNHDIM_FLYER_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_FLYER_PROC ) then
          !Correspond to FLYER_DIACHRO
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          !Create local time dimension
          if ( isp == tzfile%nmaster_rank) then
            istatus = NF90_DEF_DIM( igrpid, 'time_flyer', Size( pvar, 4), idimid )
            if ( istatus /= NF90_NOERR ) &
              call IO_Err_handle_nc4( istatus, 'Write_diachro_nc4', 'NF90_DEF_DIM', Trim( tpfields(1)%cmnhname ) )
          end if
    
          allocate( zdata2d( size(pvar,3), size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata2d(:, :) = pvar(1, 1, :, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata2d )
          end do
    
          deallocate( zdata2d )
        else if (  tpfields(1)%ndimlist(3) == NMNHDIM_LEVEL      &
             .and. tpfields(1)%ndimlist(4) == NMNHDIM_PROFILER_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_PROFILER_PROC ) then
          !Correspond to PROFILER_DIACHRO_n
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata2d( size(pvar,3), size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata2d(:, :) = pvar(1, 1, :, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata2d )
          end do
    
          deallocate( zdata2d )
    
        else if (  ( tpfields(1)%ndimlist(3) == NMNHDIM_SERIES_LEVEL .or. tpfields(1)%ndimlist(3) == NMNHDIM_SERIES_LEVEL_W ) &
    
             .and. tpfields(1)%ndimlist(4) == NMNHDIM_SERIES_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_SERIES_PROC ) then
          !Correspond to PROFILER_DIACHRO_n
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata2d( size(pvar,3), size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata2d(:, :) = pvar(1, 1, :, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata2d )
          end do
    
          deallocate( zdata2d )
        else if (  ( tpfields(1)%ndimlist(1) == NMNHDIM_NI .or. tpfields(1)%ndimlist(1) == NMNHDIM_NI_U )      &
             .and. tpfields(1)%ndimlist(4) == NMNHDIM_SERIES_TIME &
             .and. tpfields(1)%ndimlist(6) == NMNHDIM_SERIES_PROC ) then
          !Correspond to PROFILER_DIACHRO_n
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata2d( size(pvar,1), size(pvar,4) ) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, size(pvar,6)
            zdata2d(:, :) = pvar(:, 1, 1, :, 1, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 4 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata2d )
          end do
    
          deallocate( zdata2d )
    
    
        else if (       tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_TIME         &
                  .and. tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_MASK_NBUMASK &
                  .and. tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_NGROUPS      ) then
    
          Allocate( zdata2d(Size( pvar, 4 ), Size( pvar, 5 )) )
    
          ! Loop on the processes (1 written variable per process)
          do ji = 1, Size( pvar, 6 )
            zdata2d(:,:) = pvar(1, 1, 1, :, :, ji)
    
            if ( gsplit ) then
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 4, 5 ], kbutimepos = 1 )
    
              !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
              call IO_Field_write( tzfile, tzfield, zdata2d, koffset= [ ( nbutshift - 1 ) * nbusubwrite, 0 ] )
            else
              call IO_Field_write( tzfile, tzfield, zdata2d )
            end if
          end do
    
          Deallocate( zdata2d )
    
    
        else
          call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                          'case not yet implemented (variable '//trim(tpfields(1)%cmnhname)//')' )
        end if
    
      case (4)
    
    
        if (       Any( tpfields(1)%ndimlist(1) == [ NMNHDIM_BUDGET_CART_NI, NMNHDIM_BUDGET_CART_NI_U, NMNHDIM_BUDGET_CART_NI_V ] ) &
             .and. Any( tpfields(1)%ndimlist(2) == [ NMNHDIM_BUDGET_CART_NJ, NMNHDIM_BUDGET_CART_NJ_U, NMNHDIM_BUDGET_CART_NJ_V ] ) &
             .and. Any( tpfields(1)%ndimlist(3) == [ NMNHDIM_BUDGET_CART_LEVEL,NMNHDIM_BUDGET_CART_LEVEL_W ] )                      &
             .and.      tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_NGROUPS                                                        ) then
          !Correspond to Store_one_budget (CART)
    
          allocate( zdata3d( size(pvar,1), size(pvar,2), size(pvar,3) ) )
    
          ! Loop on the processes
          do ji = 1, size(pvar,6)
            zdata3d(:,:,:) = pvar(:, :, :, 1, 1, ji)
    
    
            if ( gsplit ) then
              if ( htype /= 'CART' ) call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                          ': group ' // Trim( hgroup ) //' gsplit=T not implemented for these dimensions and htype/=CART' )
    
              if ( htype == 'CART' .and. .not. oicp .and. .not. ojcp ) then
    
                call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 2, 3 ], kbutimepos = 4 )
    
    
                !Data is distributed between all the processes
                tzfield%cdir = 'XY'
    
    
                !Create the metadata of the field (has to be done only once)
                if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    
                call IO_Field_write_box( tzfile, tzfield, 'BUDGET',                                                     &
                                         Reshape( zdata3d, [ Size(zdata3d,1), Size(zdata3d,2), Size(zdata3d,3), 1 ] ) , &
                                         kil + jphext, kih + jphext, kjl + jphext, kjh + jphext,                        &
                                         koffset= [ 0, 0, 0, ( nbutshift - 1 ) * nbusubwrite ]                          )
              else
                call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                                ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
              end if
    
              if ( htype == 'CART' .and. .not. oicp .and. .not. ojcp ) then
    
                call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 2, 3 ] )
    
    
                !Data is distributed between all the processes
                tzfield%cdir = 'XY'
                call IO_Field_write_box( tzfile, tzfield, 'BUDGET', zdata3d, &
                                        kil + jphext, kih + jphext, kjl + jphext, kjh + jphext )
              else
    
                call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 2, 3 ] )
    
    
        elseif (  (        tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_MASK_LEVEL     &
    
                      .or. tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_MASK_LEVEL_W ) &
    
           .and.           tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_TIME           &
           .and.           tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_MASK_NBUMASK   &
           .and.           tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_NGROUPS        ) then
    
          !Correspond to Store_one_budget (MASK)
          allocate( zdata3d( size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          ! Loop on the processes
          do ji = 1, size(pvar,6)
            zdata3d(:,:,:) = pvar(1, 1, :, :, :, ji)
    
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4, 5 ], kbutimepos = 2 )
    
    
            !Create the metadata of the field (has to be done only once)
              if ( nbutshift == 1 ) call IO_Field_create( tzfile, tzfield )
    !           call IO_Field_partial_write( tzfile, tzfield, zdata3d(:,:,:), koffset= [ 0, ( nbutshift - 1 ) * nbusubwrite, 0 ] )
              call IO_Field_write( tzfile, tzfield, zdata3d(:,:,:), koffset= [ 0, ( nbutshift - 1 ) * nbusubwrite, 0 ] )
            else
    
              call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4, 5 ] )
    
        else if (              tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_LES_LEVEL      &
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
           .and.               tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_LES_SV         &
           .and.               tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_LES_MASK       ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( nles_masks /= Size( pvar, 6 ) ) &
            call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4',                             &
                            'last dimension size of pvar is not equal to nles_masks (variable ' &
                            // Trim( tpfields(1)%cmnhname ) // ')' )
    
          allocate( zdata3d( size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          ! Loop on the masks (1 written variable per mask)
          do ji = 1, size(pvar,6)
            zdata3d(:,:,:) = pvar(1, 1, :, :, :, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4, 5 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata3d )
          end do
    
          deallocate( zdata3d )
    
        else if (              tpfields(1)%ndimlist(3) == NMNHDIM_BUDGET_LES_LEVEL      &
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
           .and.               tpfields(1)%ndimlist(5) == NMNHDIM_BUDGET_LES_SV         &
           .and.               tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_TERM           ) then
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          allocate( zdata3d( size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          ! Loop on the masks (1 written variable per mask)
          do ji = 1, size(pvar,6)
            zdata3d(:,:,:) = pvar(1, 1, :, :, :, ji)
    
    
            call Prepare_diachro_write( tpfields(ji), tzfield, [ 3, 4, 5 ] )
    
            CALL IO_Field_write( tzfile, tzfield, zdata3d )
          end do
    
          deallocate( zdata3d )
    
        else if (             tpfields(1)%ndimlist(1) == NMNHDIM_SPECTRA_SPEC_NI       &
                 .and.        tpfields(1)%ndimlist(3) == NMNHDIM_SPECTRA_LEVEL         &
                 .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                         .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
                 .and.        tpfields(1)%ndimlist(5) == NMNHDIM_COMPLEX               ) then
    
          !Correspond to LES_DIACHRO_SPEC
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata4d( size(pvar,1), size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          zdata4d(:,:,:,:) = pvar(:, 1, :, :, :, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 1, 3, 4, 5 ] )
    
          CALL IO_Field_write( tzfile, tzfield, zdata4d )
    
          deallocate( zdata4d )
    
        else if (              tpfields(1)%ndimlist(2) == NMNHDIM_SPECTRA_SPEC_NJ       &
                  .and.        tpfields(1)%ndimlist(3) == NMNHDIM_SPECTRA_LEVEL         &
                  .and. (      tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_TIME       &
                          .or. tpfields(1)%ndimlist(4) == NMNHDIM_BUDGET_LES_AVG_TIME ) &
                  .and.        tpfields(1)%ndimlist(5) == NMNHDIM_COMPLEX               ) then
    
          !Correspond to LES_DIACHRO_SPEC
    
          if ( gsplit ) then
              call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                              ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
          end if
    
    
          if ( Size( tpfields ) /= 1 ) call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                                                       'wrong size of tpfields (variable '//trim(tpfields(1)%cmnhname)//')' )
    
    
          allocate( zdata4d( size(pvar,2), size(pvar,3), size(pvar,4), size(pvar,5) ) )
    
          zdata4d(:,:,:,:) = pvar(1, :, :, :, :, 1)
    
    
          call Prepare_diachro_write( tpfields(1), tzfield, [ 2, 3, 4, 5 ] )
    
          CALL IO_Field_write( tzfile, tzfield, zdata4d )
    
          deallocate( zdata4d )
        else
          call Print_msg( NVERB_FATAL, 'IO', 'Write_diachro_nc4', &
                          'case not yet implemented (variable '//trim(tpfields(1)%cmnhname)//')' )
        end if
    
    !   case (5)
    
    !   case (6)
    
      case default
    
        if ( gsplit ) then
            call Print_msg( NVERB_ERROR, 'IO', 'Write_diachro_nc4', Trim( tzfile%cname ) // &
                            ': group ' // Trim( hgroup ) //' gsplit=T not implemented' )
        end if
    
    
    !     if ( All( tpfields(1)%ndimlist(:) /= NMNHDIM_UNKNOWN ) ) then
    !       tzfield%ndimlist(1)  = tpfields(1)%ndimlist(1)
    !       tzfield%ndimlist(2)  = tpfields(1)%ndimlist(2)
    !       tzfield%ndimlist(3)  = tpfields(1)%ndimlist(3)
    !       tzfield%ndimlist(4)  = tpfields(1)%ndimlist(4)
    !       tzfield%ndimlist(5)  = tpfields(1)%ndimlist(5)
    !       tzfield%ndimlist(6:) = NMNHDIM_UNUSED
    !     end if
    
          call Prepare_diachro_write( tpfields(ji), tzfield, [ 1, 2, 3, 4, 5 ] )
    
          CALL IO_Field_write( tzfile, tzfield, pvar(:, :, :, :, :, ji ) )
        end do
    end select
    
    !Reset ndimlist (to prevent problems later)
    tzfield%ndimlist(:) = NMNHDIM_UNKNOWN
    
    TZFIELD%CMNHNAME   = 'dates'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CLONGNAME  = 'dates'
    TZFIELD%CUNITS     = 'seconds since YYYY-MM-DD HH:MM:SS.S'
    TZFIELD%CDIR       = '--'
    TZFIELD%CCOMMENT   = 'Dates at the middle of the budget timesteps'
    TZFIELD%NGRID      = 0
    TZFIELD%NTYPE      = TYPEDATE
    TZFIELD%NDIMS      = 1
    TZFIELD%LTIMEDEP   = .FALSE.
    
    if ( tpfields(1)%ndimlist(4) /= NMNHDIM_UNKNOWN .and. tpfields(1)%ndimlist(4) /= NMNHDIM_UNUSED) then
      tzfield%ndimlist(1) = tpfields(1)%ndimlist(4)
      tzfield%ndimlist(2:) = NMNHDIM_UNUSED
    end if
    
    CALL IO_Field_write( tzfile, tzfield, tpdates(:) )
    
    !Reset ndimlist
    tzfield%ndimlist(:) = NMNHDIM_UNKNOWN
    
    
    
    !Restore id of the file root group ('/' group)
    tzfile%nncid = isavencid
    
    
    end  subroutine Write_diachro_nc4
    
    
    
    subroutine Prepare_diachro_write( tpfieldin, tpfieldout, kdims, kbutimepos )
    use modd_field,          only: NMNHDIM_BUDGET_TIME, NMNHDIM_UNUSED, NMNHMAXDIMS, tfielddata, tfield_metadata_base
    
    
    class(tfield_metadata_base), intent(in)  :: tpfieldin
    type(tfielddata),            intent(out) :: tpfieldout
    integer, dimension(:),       intent(in)  :: kdims ! List of indices of dimensions to use
    integer,           optional, intent(in)  :: kbutimepos
    
    integer :: jdim
    integer :: idims
    
    print *,'PW: Prepare_diachro_write called for ',Trim( tpfieldin%cmnhname )
    
    idims = Size( kdims )
    
    if ( idims > NMNHMAXDIMS ) call Print_msg( NVERB_FATAL, 'IO', 'Prepare_diachro_write', &
                                               'kdims is too big for ' //Trim( tpfieldin%cmnhname ) )
    
    tpfieldout%cmnhname   = tpfieldin%cmnhname
    tpfieldout%cstdname   = tpfieldin%cstdname
    tpfieldout%clongname  = tpfieldin%clongname
    tpfieldout%cunits     = tpfieldin%cunits
    tpfieldout%cdir       = '--'
    tpfieldout%ccomment   = tpfieldin%ccomment
    tpfieldout%ngrid      = tpfieldin%ngrid
    tpfieldout%ntype      = tpfieldin%ntype
    tpfieldout%ltimedep   = .false.
    
    tpfieldout%ndims      = idims
    
    do jdim = 1, idims
      tpfieldout%ndimlist(jdim)  = tpfieldin%ndimlist(kdims(jdim))
    end do
    tpfieldout%ndimlist(idims + 1:) = NMNHDIM_UNUSED
    
    !Add budget time dimension if required
    if ( Present( kbutimepos ) ) then
      ! Note: if kbutimepos <= idims, the budget time dimension is assumed to be already present
      ! In that case, it is not necessary/useful to provide kbutimepos
      if ( kbutimepos > idims ) then
        if ( kbutimepos /= (idims + 1) ) call Print_msg( NVERB_FATAL, 'IO', 'Prepare_diachro_write', &
                                                       'unexpected value for kbutimepos for ' //Trim( tpfieldin%cmnhname ) )
        tpfieldout%ndims = tpfieldout%ndims + 1
        tpfieldout%ndimlist(kbutimepos) = NMNHDIM_BUDGET_TIME
      end if
    end if
    end subroutine Prepare_diachro_write