module write_field_vars_mod
   USE genmod
   IMPLICIT NONE

   INTEGER, SAVE :: ncprec

   TYPE ncvar
      INTEGER :: size
      INTEGER, POINTER :: nc_id(:)
      INTEGER :: displ
   END TYPE ncvar

   INTEGER, PARAMETER :: MaxWriteField = 1000
   INTEGER, DIMENSION(MaxWriteField), SAVE :: FieldId
   TYPE(ncvar), dimension(MaxWriteField), SAVE :: FieldVarId
   INTEGER, DIMENSION(MaxWriteField), SAVE :: FieldIndex
   CHARACTER(len=255), DIMENSION(MaxWriteField) ::  FieldName

   INTEGER, SAVE :: NbField = 0

contains

   function GetFieldIndex(name)
      integer          :: GetFieldindex
      character(len=*) :: name

      character(len=255) :: TrueName
      integer            :: i

      TrueName = TRIM(ADJUSTL(name))

      GetFieldIndex = -1
      do i = 1, NbField
         if (TrueName == FieldName(i)) then
            GetFieldIndex = i
            exit
         end if
      end do
   end function GetFieldIndex

   function int2str(int)
      integer, parameter :: MaxLen = 10
      integer, intent(in) :: int
      character(len=MaxLen) :: int2str
      logical :: flag
      integer :: i
      flag = .true.

      i = int

      int2str = ''
      do while (flag)
         int2str = CHAR(MOD(i, 10) + 48)//trim(int2str)
         i = i/10
         if (i == 0) flag = .false.
      end do
   end function int2str

end module write_field_vars_mod
