subroutine MAPL_create_bundle_from_metdata_id(bundle,metadata_id,file_name,only_vars,rc)
type(ESMF_FieldBundle), intent(inout) :: bundle
integer, intent(in) :: metadata_id
character(len=*), intent(in) :: file_name
character(len=*), optional, intent(in) :: only_vars
integer, optional, intent(out) :: rc
integer :: status
type(MAPLDataCollection), pointer :: collection => null()
type(fileMetaDataUtils), pointer :: metadata
type(ESMF_Grid) :: grid,file_grid
integer :: num_fields,dims,location
logical :: create_variable, has_vertical_level, var_has_levels
class (AbstractGridFactory), pointer :: factory
character(len=:), allocatable :: grid_vars,exclude_vars
type(StringVariableMap), pointer :: variables
type(Variable), pointer :: this_variable
type(StringVariableMapIterator) :: var_iter
character(len=:), pointer :: var_name,dim_name
character(len=:), allocatable :: lev_name
type(ESMF_Field) :: field
type (StringVector), pointer :: dimensions
type (StringVectorIterator) :: dim_iter
integer :: lev_size, grid_size(3)
character(len=:), allocatable :: units,long_name
collection => DataCollections%at(metadata_id)
metadata => collection%find(trim(file_name), _RC)
file_grid=collection%src_grid
lev_name = metadata%get_level_name(rc=status)
_VERIFY(status)
has_vertical_level = (metadata%get_level_name(rc=status)/='')
call ESMF_FieldBundleGet(bundle,grid=grid,FieldCount=num_fields,rc=status)
_VERIFY(status)
call MAPL_GridGet(grid,localCellCountPerDim=grid_size,rc=status)
_VERIFY(status)
_ASSERT(num_fields == 0,"Trying to fill non-empty bundle")
factory => get_factory(file_grid,rc=status)
_VERIFY(status)
grid_vars = factory%get_file_format_vars()
exclude_vars = grid_vars//",lev,time,lons,lats"
if (has_vertical_level) lev_size = metadata%get_dimension(trim(lev_name))
variables => metadata%get_variables()
var_iter = variables%begin()
do while (var_iter /= variables%end())
var_has_levels = .false.
var_name => var_iter%key()
this_variable => var_iter%value()
if (has_vertical_level) then
dimensions => this_variable%get_dimensions()
dim_iter = dimensions%begin()
do while (dim_iter /= dimensions%end())
dim_name => dim_iter%get()
if (trim(dim_name) == lev_name) var_has_levels=.true.
call dim_iter%next()
enddo
end if
if (index(','//trim(exclude_vars)//',',','//trim(var_name)//',') > 0) then
call var_iter%next()
cycle
end if
create_variable = .true.
if (present(only_vars)) then
if (index(','//trim(only_vars)//',',','//trim(var_name)//',') < 1) create_variable = .false.
end if
if (create_variable) then
if(var_has_levels) then
if (grid_size(3) == lev_size) then
location=MAPL_VLocationCenter
dims = MAPL_DimsHorzVert
field= ESMF_FieldCreate(grid,name=trim(var_name),typekind=ESMF_TYPEKIND_R4, &
ungriddedUbound=[grid_size(3)],ungriddedLBound=[1], rc=status)
block
real, pointer :: ptr3d(:,:,:)
call ESMF_FieldGEt(field,0,farrayPtr=ptr3d)
ptr3d =0.0
end block
else if (grid_size(3)+1 == lev_size) then
location=MAPL_VLocationEdge
dims = MAPL_DimsHorzVert
field= ESMF_FieldCreate(grid,name=trim(var_name),typekind=ESMF_TYPEKIND_R4, &
ungriddedUbound=[grid_size(3)],ungriddedLBound=[0], rc=status)
block
real, pointer :: ptr3d(:,:,:)
call ESMF_FieldGEt(field,0,farrayPtr=ptr3d)
ptr3d =0.0
end block
end if
else
location=MAPL_VLocationNone
dims = MAPL_DimsHorzOnly
field= ESMF_FieldCreate(grid,name=trim(var_name),typekind=ESMF_TYPEKIND_R4, &
rc=status)
block
real, pointer :: ptr2d(:,:)
call ESMF_FieldGEt(field,0,farrayPtr=ptr2d)
ptr2d =0.0
end block
end if
call ESMF_AttributeSet(field,name='DIMS',value=dims,rc=status)
_VERIFY(status)
call ESMF_AttributeSet(field,name='VLOCATION',value=location,rc=status)
_VERIFY(status)
units = metadata%get_var_attr_string(var_name,'units',_RC)
long_name = metadata%get_var_attr_string(var_name,'long_name',_RC)
call ESMF_AttributeSet(field,name='UNITS',value=units,rc=status)
_VERIFY(status)
call ESMF_AttributeSet(field,name='LONG_NAME',value=long_name,rc=status)
_VERIFY(status)
call MAPL_FieldBundleAdd(bundle,field,rc=status)
_VERIFY(status)
end if
call var_iter%next()
end do
_RETURN(_SUCCESS)
end subroutine MAPL_create_bundle_from_metdata_id