29 character(len=*), 
intent(in)       :: input_file
 
   31 integer, 
intent(in)                :: localpet
 
   33 integer                            :: i, j, tile, cat, ncid, status, rc
 
   34 integer                            :: varid, water_category, max_cat
 
   35 integer                            :: isrctermprocessing
 
   36 integer                            :: category, num_categories
 
   38 integer(esmf_kind_i4), 
allocatable :: mask_mdl_one_tile(:,:)
 
   39 integer(esmf_kind_i4), 
pointer     :: unmapped_ptr(:)
 
   41 real(esmf_kind_r4), 
allocatable    :: data_src_global(:,:)
 
   42 real(esmf_kind_r4), 
allocatable    :: data_src_global2(:,:,:)
 
   43 real(esmf_kind_r4), 
allocatable    :: data_mdl_one_tile(:,:,:)
 
   44 real(esmf_kind_r4), 
allocatable    :: dom_cat_mdl_one_tile(:,:)
 
   45 real(esmf_kind_r4), 
allocatable    :: lat_mdl_one_tile(:,:)
 
   46 real(esmf_kind_r4), 
allocatable    :: sum_mdl_one_tile(:,:)
 
   47 real(esmf_kind_r4), 
allocatable    :: lon_mdl_one_tile(:,:)
 
   48 real(esmf_kind_r4), 
allocatable    :: land_frac_mdl_one_tile(:,:)
 
   49 real(esmf_kind_r4)                 :: max_frac
 
   51 type(esmf_regridmethod_flag)            :: method
 
   52 type(esmf_field)                        :: data_field_src
 
   53 type(esmf_field)                        :: data_field_mdl
 
   54 type(esmf_routehandle)                  :: regrid_data
 
   55 type(esmf_polemethod_flag)              :: pole
 
   57 if (localpet == 0) 
then 
   60   allocate(data_src_global(0,0))
 
   63 if (localpet == 0) 
then 
   64   print*,
'- OPEN SOURCE FILE ', trim(input_file)
 
   65   status = nf90_open(input_file, nf90_nowrite, ncid)
 
   66   call netcdf_err(status, 
"IN ROUTINE INTERP OPENING SOURCE FILE")
 
   67   status = nf90_inq_varid(ncid, 
field_names(1), varid)
 
   68   call netcdf_err(status, 
"IN ROUTINE INTERP READING FIELD ID")
 
   69   status = nf90_get_var(ncid, varid, data_src_global, start=(/1,1,1/), count=(/
i_src,
j_src,1/))
 
   70   call netcdf_err(status, 
"IN ROUTINE INTERP READING FIELD")
 
   71   print*,
'number of cats ',maxval(data_src_global)
 
   72   num_categories = nint(maxval(data_src_global))
 
   73   status = nf90_get_att(ncid, varid, 
'water_category', water_category)
 
   74   call netcdf_err(status, 
"IN ROUTINE INTERP READING water_category")
 
   75   print*,
'water cat ',water_category
 
   78 call mpi_bcast(num_categories,1,mpi_integer,0,mpi_comm_world,status)
 
   80 print*,
"- CALL FieldCreate FOR SOURCE GRID DATA." 
   81 data_field_src = esmf_fieldcreate(
grid_src, &
 
   82                                  typekind=esmf_typekind_r4, &
 
   83                                  indexflag=esmf_index_global, &
 
   84                                  staggerloc=esmf_staggerloc_center, &
 
   85                                  ungriddedlbound=(/1/), &
 
   86                                  ungriddedubound=(/num_categories/), &
 
   89 if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
   92 print*,
"- CALL FieldCreate FOR model GRID veg DATA." 
   93 data_field_mdl = esmf_fieldcreate(
grid_mdl, &
 
   94                                  typekind=esmf_typekind_r4, &
 
   95                                  indexflag=esmf_index_global, &
 
   96                                  staggerloc=esmf_staggerloc_center, &
 
   97                                  ungriddedlbound=(/1/), &
 
   98                                  ungriddedubound=(/num_categories/), &
 
  101 if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  104 if (localpet == 0) 
then 
  105   allocate(data_src_global2(
i_src,
j_src,num_categories))
 
  106   allocate(data_mdl_one_tile(
i_mdl,
j_mdl,num_categories))
 
  114   allocate(data_src_global2(0,0,0))
 
  115   allocate(data_mdl_one_tile(0,0,0))
 
  116   allocate(dom_cat_mdl_one_tile(0,0))
 
  117   allocate(mask_mdl_one_tile(0,0))
 
  118   allocate(land_frac_mdl_one_tile(0,0))
 
  119   allocate(lat_mdl_one_tile(0,0))
 
  120   allocate(sum_mdl_one_tile(0,0))
 
  121   allocate(lon_mdl_one_tile(0,0))
 
  124 if (localpet == 0) 
then 
  125   data_src_global2 = 0.0
 
  128     category = nint(data_src_global(i,j))
 
  129     if (category < 1) cycle
 
  130     data_src_global2(i,j,category) = 1.0
 
  135 deallocate(data_src_global)
 
  137 print*,
"- CALL FieldScatter FOR SOURCE GRID DATA." 
  138 call esmf_fieldscatter(data_field_src, data_src_global2, rootpet=0, rc=rc)
 
  139 if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  142 deallocate(data_src_global2)
 
  144 isrctermprocessing = 1
 
  146 method = esmf_regridmethod_conserve
 
  147 pole = esmf_polemethod_none
 
  149 print*,
"- CALL FieldRegridStore." 
  150 nullify(unmapped_ptr)
 
  151 call esmf_fieldregridstore(data_field_src, &
 
  153                            srcmaskvalues=(/0/), &
 
  154                            dstmaskvalues=(/0/), &
 
  156                            unmappedaction=esmf_unmappedaction_ignore, &
 
  157                            normtype=esmf_normtype_fracarea, &
 
  158                            srctermprocessing=isrctermprocessing, &
 
  159                            routehandle=regrid_data, &
 
  160                            regridmethod=method, &
 
  161                            unmappeddstlist=unmapped_ptr,  &
 
  163 if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  166 print*,
"- CALL Field_Regrid." 
  167 call esmf_fieldregrid(data_field_src, &
 
  169                       routehandle=regrid_data, &
 
  170                       termorderflag=esmf_termorder_srcseq, &
 
  172 if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  175 print*,
"- CALL FieldRegridRelease." 
  176 call esmf_fieldregridrelease(routehandle=regrid_data, rc=rc)
 
  178 print*,
"- CALL FieldDestroy." 
  179 call esmf_fielddestroy(data_field_src, rc=rc)
 
  183   print*,
"- CALL FieldGather FOR MODEL LATITUDE." 
  184   call esmf_fieldgather(
latitude_field_mdl, lat_mdl_one_tile, rootpet=0, tile=tile, rc=rc)
 
  185   if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  188   print*,
"- CALL FieldGather FOR MODEL LONGITUDE." 
  190   if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  193   print*,
"- CALL FieldGather FOR MODEL GRID DATA." 
  194   call esmf_fieldgather(data_field_mdl, data_mdl_one_tile, rootpet=0, tile=tile, rc=rc)
 
  195   if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  198   print*,
"- CALL FieldGather FOR MODEL GRID LAND MASK." 
  199   call esmf_fieldgather(
mask_field_mdl, mask_mdl_one_tile, rootpet=0, tile=tile, rc=rc)
 
  200   if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  203   print*,
"- CALL FieldGather FOR MODEL GRID LAND FRACTION." 
  204   call esmf_fieldgather(
land_frac_field_mdl, land_frac_mdl_one_tile, rootpet=0, tile=tile, rc=rc)
 
  205   if(esmf_logfounderror(rctocheck=rc,msg=esmf_logerr_passthru,line=__line__,file=__file__)) &
 
  208   if (localpet == 0) 
then 
  209     print*,
'- CALL SEARCH FOR TILE ',tile
 
  213     sum_mdl_one_tile = sum(data_mdl_one_tile, dim=3)
 
  216       if (mask_mdl_one_tile(i,j) == 1 .and. sum_mdl_one_tile(i,j) == 0.0) 
then 
  217         data_mdl_one_tile(i,j,:) = -9999.9 
 
  222     print*,
'after regrid ',data_mdl_one_tile(
i_mdl/2,
j_mdl/2,:)
 
  227       if (mask_mdl_one_tile(i,j) == 0) 
then 
  228         data_mdl_one_tile(i,j,water_category) = 1.0
 
  240     if (maxval(land_frac_mdl_one_tile) > 0.0) 
then 
  241       print*,
'before rescale ',tile,land_frac_mdl_one_tile(42,82),data_mdl_one_tile(42,82,:)
 
  244         if (mask_mdl_one_tile(i,j) == 1) 
then 
  245           data_mdl_one_tile(i,j,:) = data_mdl_one_tile(i,j,:) * land_frac_mdl_one_tile(i,j)
 
  246           data_mdl_one_tile(i,j,water_category) = 1.0 - land_frac_mdl_one_tile(i,j)
 
  249           do cat = 1, num_categories
 
  250             if (cat == water_category) cycle
 
  251             if(data_mdl_one_tile(i,j,cat) > max_frac) 
then 
  252               max_frac = data_mdl_one_tile(i,j,cat)
 
  256           dom_cat_mdl_one_tile(i,j) = max_cat
 
  258           dom_cat_mdl_one_tile(i,j) = water_category
 
  263       dom_cat_mdl_one_tile = 0.0
 
  264       dom_cat_mdl_one_tile = maxloc(data_mdl_one_tile,dim=3)
 
  266     call output_driver (data_mdl_one_tile, dom_cat_mdl_one_tile, lat_mdl_one_tile, lon_mdl_one_tile, 
i_mdl, 
j_mdl, num_categories, tile)
 
  271 status=nf90_close(ncid)
 
  273 deallocate(data_mdl_one_tile, dom_cat_mdl_one_tile, mask_mdl_one_tile)
 
  274 deallocate(lat_mdl_one_tile, lon_mdl_one_tile, sum_mdl_one_tile, land_frac_mdl_one_tile)
 
  276 print*,
"- CALL FieldDestroy." 
  277 call esmf_fielddestroy(data_field_mdl, rc=rc)
 
  279 call mpi_barrier(mpi_comm_world, rc)