function create_output_grid(grid_name,lm,rc) result(new_grid)
type(ESMF_Grid) :: new_grid
character(len=*), intent(inout) :: grid_name
integer, intent(in) :: lm
integer, optional, intent(out) :: rc
type(ESMF_Config) :: cf
integer :: nn,im_world,jm_world,nx, ny
character(len=5) :: imsz,jmsz
character(len=2) :: pole,dateline
nn = len_trim(grid_name)
imsz = grid_name(3:index(grid_name,'x')-1)
jmsz = grid_name(index(grid_name,'x')+1:nn-3)
pole = grid_name(1:2)
dateline = grid_name(nn-1:nn)
read(IMSZ,*) im_world
read(JMSZ,*) jm_world
cf = MAPL_ConfigCreate(_RC)
call MAPL_ConfigSetAttribute(cf,value=lm, label=trim(grid_name)//".LM:",_RC)
if (dateline=='CF') then
call MAPL_MakeDecomposition(nx,ny,reduceFactor=6,_RC)
call MAPL_ConfigSetAttribute(cf,value=NX, label=trim(grid_name)//".NX:",_RC)
call MAPL_ConfigSetAttribute(cf,value="Cubed-Sphere", label=trim(grid_name)//".GRID_TYPE:",_RC)
call MAPL_ConfigSetAttribute(cf,value=6, label=trim(grid_name)//".NF:",_RC)
call MAPL_ConfigSetAttribute(cf,value=im_world,label=trim(grid_name)//".IM_WORLD:",_RC)
call MAPL_ConfigSetAttribute(cf,value=ny, label=trim(grid_name)//".NY:",_RC)
else if (dateline=='TM') then
_FAIL("Tripolar not yet implemented for outpout")
else
call MAPL_MakeDecomposition(nx,ny,_RC)
call MAPL_ConfigSetAttribute(cf,value=NX, label=trim(grid_name)//".NX:",_RC)
call MAPL_ConfigSetAttribute(cf,value="LatLon", label=trim(grid_name)//".GRID_TYPE:",_RC)
call MAPL_ConfigSetAttribute(cf,value=im_world,label=trim(grid_name)//".IM_WORLD:",_RC)
call MAPL_ConfigSetAttribute(cf,value=jm_world,label=trim(grid_name)//".JM_WORLD:",_RC)
call MAPL_ConfigSetAttribute(cf,value=ny, label=trim(grid_name)//".NY:",_RC)
call MAPL_ConfigSetAttribute(cf,value=pole, label=trim(grid_name)//".POLE:",_RC)
call MAPL_ConfigSetAttribute(cf,value=dateline, label=trim(grid_name)//".DATELINE:",_RC)
if (pole=='XY' .and. dateline=='XY') then
_FAIL("regional lat-lon output not supported")
end if
end if
new_grid = grid_manager%make_grid(cf,prefix=trim(grid_name)//".",_RC)
if (present(rc)) then
rc=_SUCCESS
end if
end function create_output_grid