cpld_gridgen  1.12.0
 All Data Structures Files Functions Variables Pages
scripgrid.F90
Go to the documentation of this file.
1 
7 
8 module scripgrid
9 
10  use gengrid_kinds, only: dbl_kind,int_kind,cm
11  use grdvars, only: ni,nj,nv
12  use grdvars, only: lonct,latct,lonct_vert,latct_vert
13  use grdvars, only: loncu,latcu,loncu_vert,latcu_vert
14  use grdvars, only: loncv,latcv,loncv_vert,latcv_vert
15  use grdvars, only: lonbu,latbu,lonbu_vert,latbu_vert
16  use charstrings, only: logmsg
17  use vartypedefs, only: maxvars, scripvars, scripvars_typedefine
18  use netcdf
19 
20  implicit none
21  private
22 
23  public write_scripgrid
24 
25 contains
33 
34  subroutine write_scripgrid(fname,cstagger, imask)
35 
36  character(len=*) , intent(in) :: fname
37  character(len=*) , intent(in) :: cstagger
38  integer(int_kind), optional, intent(in) :: imask(:,:)
39 
40  ! local variables
41  integer, parameter :: grid_rank = 2
42 
43  integer :: ii,n,id,rc, ncid, dim2(2),dim1(1)
44  integer :: idimid,jdimid,kdimid
45 
46  integer, dimension(grid_rank) :: gdims
47  integer(int_kind), dimension(ni*nj) :: cnmask !1-d mask
48  real(dbl_kind), dimension(ni*nj) :: cnlons, cnlats !1-d center lats,lons
49  real(dbl_kind), dimension(nv,ni*nj) :: crlons, crlats !2-d corner lats,lons
50 
51  real(dbl_kind), dimension(ni,nj) :: tmp
52 
53  character(len=2) :: vtype
54  character(len=CM) :: vname
55  character(len=CM) :: vunit
56 
57  !---------------------------------------------------------------------
58  !
59  !---------------------------------------------------------------------
60 
61  gdims(:) = (/ni,nj/)
62  if(trim(cstagger) .eq. 'Ct')then
63  cnlons = reshape(lonct, (/ni*nj/))
64  cnlats = reshape(latct, (/ni*nj/))
65  do n = 1,nv
66  tmp(:,:) = lonct_vert(:,:,n)
67  crlons(n,:) = reshape(tmp, (/ni*nj/))
68  tmp(:,:) = latct_vert(:,:,n)
69  crlats(n,:) = reshape(tmp, (/ni*nj/))
70  end do
71  end if
72 
73  if(trim(cstagger) .eq. 'Cu')then
74  cnlons = reshape(loncu, (/ni*nj/))
75  cnlats = reshape(latcu, (/ni*nj/))
76  do n = 1,nv
77  tmp(:,:) = loncu_vert(:,:,n)
78  crlons(n,:) = reshape(tmp, (/ni*nj/))
79  tmp(:,:) = latcu_vert(:,:,n)
80  crlats(n,:) = reshape(tmp, (/ni*nj/))
81  end do
82  end if
83 
84  if(trim(cstagger) .eq. 'Cv')then
85  cnlons = reshape(loncv, (/ni*nj/))
86  cnlats = reshape(latcv, (/ni*nj/))
87  do n = 1,nv
88  tmp(:,:) = loncv_vert(:,:,n)
89  crlons(n,:) = reshape(tmp, (/ni*nj/))
90  tmp(:,:) = latcv_vert(:,:,n)
91  crlats(n,:) = reshape(tmp, (/ni*nj/))
92  end do
93  end if
94 
95  if(trim(cstagger) .eq. 'Bu')then
96  cnlons = reshape(lonbu, (/ni*nj/))
97  cnlats = reshape(latbu, (/ni*nj/))
98  do n = 1,nv
99  tmp(:,:) = lonbu_vert(:,:,n)
100  crlons(n,:) = reshape(tmp, (/ni*nj/))
101  tmp(:,:) = latbu_vert(:,:,n)
102  crlats(n,:) = reshape(tmp, (/ni*nj/))
103  end do
104  end if
105 
106  if(present(imask))then
107  cnmask = reshape(imask, (/ni*nj/))
108  else
109  cnmask = 1
110  end if
111 
112  !---------------------------------------------------------------------
113  ! create the netcdf file
114  !---------------------------------------------------------------------
115 
116  ! define the output variables and file name
118  ! create the file
119  ! 64_bit offset reqd for 008 grid
120  ! produces b4b results for smaller grids
121  rc = nf90_create(trim(fname), nf90_64bit_offset, ncid)
122  logmsg = '==> writing SCRIP grid to '//trim(fname)
123  print '(a)',trim(logmsg)
124  if(rc .ne. 0)print '(a)', 'nf90_create = '//trim(nf90_strerror(rc))
125 
126  rc = nf90_def_dim(ncid, 'grid_size', ni*nj, idimid)
127  rc = nf90_def_dim(ncid, 'grid_corners', nv, jdimid)
128  rc = nf90_def_dim(ncid, 'grid_rank', grid_rank, kdimid)
129 
130  !grid_dims
131  dim1(:) = (/kdimid/)
132  rc = nf90_def_var(ncid, 'grid_dims', nf90_int, dim1, id)
133  ! mask
134  dim1(:) = (/idimid/)
135  rc = nf90_def_var(ncid, 'grid_imask', nf90_int, dim1, id)
136  rc = nf90_put_att(ncid, id, 'units', 'unitless')
137 
138  ! centers
139  do ii = 1,2
140  vname = trim(scripvars(ii)%var_name)
141  vunit = trim(scripvars(ii)%unit_name)
142  vtype = trim(scripvars(ii)%var_type)
143  dim1(:) = (/idimid/)
144  if(vtype .eq. 'r8')rc = nf90_def_var(ncid, vname, nf90_double, dim1, id)
145  if(vtype .eq. 'r4')rc = nf90_def_var(ncid, vname, nf90_float, dim1, id)
146  if(vtype .eq. 'i4')rc = nf90_def_var(ncid, vname, nf90_int, dim1, id)
147  rc = nf90_put_att(ncid, id, 'units', vunit)
148  enddo
149 
150  ! corners
151  do ii = 3,4
152  vname = trim(scripvars(ii)%var_name)
153  vunit = trim(scripvars(ii)%unit_name)
154  vtype = trim(scripvars(ii)%var_type)
155  dim2(:) = (/jdimid,idimid/)
156  if(vtype .eq. 'r8')rc = nf90_def_var(ncid, vname, nf90_double, dim2, id)
157  if(vtype .eq. 'r4')rc = nf90_def_var(ncid, vname, nf90_float, dim2, id)
158  if(vtype .eq. 'i4')rc = nf90_def_var(ncid, vname, nf90_int, dim2, id)
159  rc = nf90_put_att(ncid, id, 'units', vunit)
160  enddo
161  rc = nf90_enddef(ncid)
162 
163  rc = nf90_inq_varid(ncid, 'grid_dims', id)
164  rc = nf90_put_var(ncid, id, gdims)
165  rc = nf90_inq_varid(ncid, 'grid_imask', id)
166  rc = nf90_put_var(ncid, id, cnmask)
167 
168  rc = nf90_inq_varid(ncid, 'grid_center_lon', id)
169  rc = nf90_put_var(ncid, id, cnlons)
170  rc = nf90_inq_varid(ncid, 'grid_center_lat', id)
171  rc = nf90_put_var(ncid, id, cnlats)
172 
173  rc = nf90_inq_varid(ncid, 'grid_corner_lon', id)
174  rc = nf90_put_var(ncid, id, crlons)
175  rc = nf90_inq_varid(ncid, 'grid_corner_lat', id)
176  rc = nf90_put_var(ncid, id, crlats)
177 
178  rc = nf90_close(ncid)
179 
180  end subroutine write_scripgrid
181 end module scripgrid
subroutine, public write_scripgrid(fname, cstagger, imask)
Write a SCRIP grid file.
Definition: scripgrid.F90:34
subroutine scripvars_typedefine
Define the variables written to any SCRIP grid file.