cpld_gridgen  1.13.0
 All Data Structures Files Functions Variables Pages
mapped_mask.F90
Go to the documentation of this file.
1 
7 
8 module mapped_mask
9 
10  use gengrid_kinds, only : dbl_kind,int_kind,cl,cm,cs
11  use grdvars, only : ni,nj,npx
12  use charstrings, only : dirout,res,atmres,logmsg
13  use netcdf
14 
15  implicit none
16 
17 contains
18 
25 
26  subroutine make_frac_land(src, wgt)
27 
28  character(len=*), intent(in) :: src, wgt
29 
30  ! local variables
31  integer, parameter :: ntile = 6
32  integer(int_kind) :: n_a, n_b, n_s
33 
34  integer(int_kind), allocatable, dimension(:) :: col, row
35  real(dbl_kind), allocatable, dimension(:) :: s
36  real(dbl_kind), allocatable, dimension(:) :: lat1d, lon1d
37 
38  integer(int_kind), allocatable, dimension(:) :: src_field
39  real(dbl_kind), allocatable, dimension(:) :: dst_field
40 
41  real(dbl_kind), allocatable, dimension(:,:) :: dst2d
42  real(dbl_kind), allocatable, dimension(:,:) :: lat2d,lon2d
43 
44  character(len=CS) :: ctile
45  character(len=CL) :: fdst
46  integer :: i,ii,jj,id,rc,ncid, dim2(2)
47  integer :: istr,iend
48  integer :: idimid,jdimid
49 
50  character(len=CM) :: vname
51  !---------------------------------------------------------------------
52  ! retrieve the weights
53  !---------------------------------------------------------------------
54 
55  rc = nf90_open(trim(wgt), nf90_nowrite, ncid)
56  rc = nf90_inq_dimid(ncid, 'n_s', id)
57  rc = nf90_inquire_dimension(ncid, id, len=n_s)
58  rc = nf90_inq_dimid(ncid, 'n_a', id)
59  rc = nf90_inquire_dimension(ncid, id, len=n_a)
60  rc = nf90_inq_dimid(ncid, 'n_b', id)
61  rc = nf90_inquire_dimension(ncid, id, len=n_b)
62 
63  allocate(col(1:n_s))
64  allocate(row(1:n_s))
65  allocate( s(1:n_s))
66 
67  allocate(lat1d(1:n_b))
68  allocate(lon1d(1:n_b))
69 
70  rc = nf90_inq_varid(ncid, 'col', id)
71  rc = nf90_get_var(ncid, id, col)
72  rc = nf90_inq_varid(ncid, 'row', id)
73  rc = nf90_get_var(ncid, id, row)
74  rc = nf90_inq_varid(ncid, 'S', id)
75  rc = nf90_get_var(ncid, id, s)
76 
77  ! 1d-tiled lat,lon
78  rc = nf90_inq_varid(ncid, 'yc_b', id)
79  rc = nf90_get_var(ncid, id, lat1d)
80  rc = nf90_inq_varid(ncid, 'xc_b', id)
81  rc = nf90_get_var(ncid, id, lon1d)
82  rc = nf90_close(ncid)
83 
84  !---------------------------------------------------------------------
85  ! retrieve 1-d land mask from the SCRIP file and map it
86  !---------------------------------------------------------------------
87 
88  allocate(src_field(1:n_a))
89  allocate(dst_field(1:n_b))
90 
91  rc = nf90_open(trim(src), nf90_nowrite, ncid)
92 
93  !1-d ocean mask (integer)
94  rc = nf90_inq_varid(ncid, 'grid_imask', id)
95  rc = nf90_get_var(ncid, id, src_field)
96  rc = nf90_close(ncid)
97 
98  dst_field = 0.0
99  do i = 1,n_s
100  ii = row(i); jj = col(i)
101  dst_field(ii) = dst_field(ii) + s(i)*real(src_field(jj),dbl_kind)
102  enddo
103 
104  !---------------------------------------------------------------------
105  !
106  !---------------------------------------------------------------------
107 
108  allocate(dst2d(npx,npx))
109  allocate(lon2d(npx,npx)); allocate(lat2d(npx,npx))
110 
111  do i = 0,ntile-1
112  istr = i*npx*npx+1
113  iend = istr+npx*npx-1
114  !print *,i,istr,iend
115 
116  write(ctile,'(a5,i1)')'.tile',i+1
117  fdst = trim(dirout)//'/'//trim(atmres)//'.mx'//trim(res)//trim(ctile)//'.nc'
118  logmsg = 'creating mapped ocean mask file '//trim(fdst)
119  print '(a)',trim(logmsg)
120 
121  dst2d(:,:) = reshape(dst_field(istr:iend), (/npx,npx/))
122  lat2d(:,:) = reshape( lat1d(istr:iend), (/npx,npx/))
123  lon2d(:,:) = reshape( lon1d(istr:iend), (/npx,npx/))
124 
125  rc = nf90_create(trim(fdst), nf90_64bit_offset, ncid)
126  rc = nf90_def_dim(ncid, 'grid_xt', npx, idimid)
127  rc = nf90_def_dim(ncid, 'grid_yt', npx, jdimid)
128 
129  dim2(:) = (/idimid, jdimid/)
130  vname = 'grid_xt'
131  rc = nf90_def_var(ncid, vname, nf90_double, dim2, id)
132  vname = 'grid_yt'
133  rc = nf90_def_var(ncid, vname, nf90_double, dim2, id)
134  vname = 'land_frac'
135  rc = nf90_def_var(ncid, vname, nf90_double, dim2, id)
136  rc = nf90_enddef(ncid)
137 
138  rc = nf90_inq_varid(ncid, 'grid_xt', id)
139  rc = nf90_put_var(ncid, id, lon2d)
140  rc = nf90_inq_varid(ncid, 'grid_yt', id)
141  rc = nf90_put_var(ncid, id, lat2d)
142  rc = nf90_inq_varid(ncid, 'land_frac', id)
143  rc = nf90_put_var(ncid, id, dst2d)
144  rc = nf90_close(ncid)
145  end do
146 
147  !---------------------------------------------------------------------
148  ! clean up
149  !---------------------------------------------------------------------
150 
151  deallocate(col, row, s, lat1d, lon1d, src_field, dst_field)
152  deallocate(dst2d,lon2d,lat2d)
153 
154  end subroutine make_frac_land
155 end module mapped_mask
subroutine make_frac_land(src, wgt)
Use ESMF weights to map the ocean land mask to the FV3 tiles and write the mapped mask to 6 tile file...
Definition: mapped_mask.F90:26