subroutine add_variable(metadata, field, rc)
type(ESMF_Field), intent(in) :: field
type(FileMetaData), intent(inout) :: metadata
integer, intent(out), optional :: rc
type(Variable) :: v
integer :: status
character(len=:), allocatable :: dims
type(ESMF_TYPEKIND_FLAG) :: typekind
integer :: pfio_type
type(ESMF_Info) :: info
character(len=:), allocatable :: char
character(len=ESMF_MAXSTR) :: fname
type(MAPLGeom), pointer :: mapl_geom
type(StringVector) :: grid_variables
type(ESMF_Geom) :: esmfgeom
character(len=:), allocatable :: vert_dim_name, ungridded_names
call ESMF_FieldGet(field, geom=esmfgeom, _RC)
mapl_geom => get_mapl_geom(esmfgeom, _RC)
grid_variables = mapl_geom%get_gridded_dims()
dims = string_vec_to_comma_sep(grid_variables)
call ESMF_FieldGet(field, name=fname, typekind = typekind, _RC)
! add vertical dimension
vert_dim_name = get_vertical_dimension_name_from_field(field, _RC)
if(vert_dim_name /= EMPTY) dims = dims//","//vert_dim_name
! add any ungridded dimensions
ungridded_names = ungridded_dim_names(field, _RC)
if(ungridded_names /= EMPTY) dims = dims // ungridded_names
! add time dimension
dims = dims//",time"
pfio_type = esmf_to_pfio_type(typekind ,_RC)
v = Variable(type=pfio_type, dimensions=dims)
call ESMF_InfoGetFromHost(field, info, _RC)
call ESMF_InfoGetCharAlloc(info, 'MAPL/units', char, _RC)
call v%add_attribute('units',char)
call ESMF_InfoGetCharAlloc(info, 'MAPL/standard_name', char, _RC)
call v%add_attribute('long_name',char)
call metadata%add_variable(trim(fname), v, _RC)
_RETURN(_SUCCESS)
end subroutine add_variable