subroutine run(im, lm, n_fields, output_filename)
integer, intent(in) :: im
integer, intent(in) :: lm
integer, intent(in) :: n_fields
character(*), intent(in) :: output_filename
type (Netcdf4_Fileformatter) :: formatter
type (FileMetadata) :: metadata
real, allocatable :: field(:,:,:)
integer :: jm
integer :: j0, j1
integer :: nj_local
integer :: rank, npes, ierror
integer :: j, n
character(:), allocatable :: field_name
character(3) :: field_idx_str
call mpi_comm_size(MPI_COMM_WORLD, npes, ierror)
_VERIFY(ierror)
call mpi_comm_rank(MPI_COMM_WORLD, rank, ierror)
_VERIFY(ierror)
jm = im*6 ! pseudo cubed sphere
call metadata%add_dimension('IM_WORLD', im)
call metadata%add_dimension('JM_WORLD', jm)
call metadata%add_dimension('LM', lm)
do n = 1, n_fields
write(field_idx_str,'(i3.3)') n
field_name = 'field_' // field_idx_str
call metadata%add_variable(field_name, Variable(pFIO_REAL32, dimensions='IM_WORLD,JM_WORLD,LM'))
end do
call formatter%create_par(output_filename, comm=MPI_COMM_WORLD)
call formatter%write(metadata)
j0 = 1 + rank*jm/npes
j1 = (rank+1)*jm/npes
nj_local = (j1 - j0) + 1
allocate(field(im, nj_local, lm))
do j = j0, j1
field(:,j-j0+1,:) = j
end do
do n = 1, n_fields
write(field_idx_str,'(i3.3)') n
field_name = 'field_' // field_idx_str
call formatter%put_var(field_name, field, start=[1,j0,1], count=[im,nj_local,lm])
end do
call formatter%close()
end subroutine run