ocnice_prep  1.13.0
All Data Structures Files Functions Variables Pages
init_mod.F90
Go to the documentation of this file.
1 
7 module init_mod
8 
9  implicit none
10 
11  public
12 
13  integer, parameter :: maxvars = 60
14  character(len=10) :: maskvar = 'h'
16  type :: vardefs
17  character(len= 20) :: var_name
18  character(len=120) :: long_name
19  character(len= 20) :: units
20  character(len= 20) :: var_remapmethod
21  integer :: var_dimen
22  character(len= 4) :: var_grid
23  character(len= 20) :: var_pair
24  character(len= 4) :: var_pair_grid
25  end type vardefs
26 
27  type(vardefs) :: outvars(maxvars)
29 
30  character(len=10) :: ftype
31  character(len=10) :: fsrc
32  character(len=10) :: fdst
33  character(len=120) :: wgtsdir
34  character(len=120) :: griddir
35  character(len=20) :: input_file
36 
37  integer :: nxt
38  integer :: nyt
39  integer :: nlevs
40 
41  integer :: nxr
42  integer :: nyr
43 
44  integer :: logunit
45  logical :: debug
46  logical :: do_ocnprep
47 
48 contains
56  subroutine readnml(fname,errmsg,rc)
57 
58  character(len=*), intent(in) :: fname
59  character(len=*), intent(out) :: errmsg
60  integer, intent(out) :: rc
61 
62  ! local variable
63  logical :: fexist
64  integer :: ierr, iounit
65  integer :: srcdims(2), dstdims(2)
66  !----------------------------------------------------------------------------
67 
68  namelist /ocniceprep_nml/ ftype, wgtsdir, griddir, srcdims, dstdims, debug
69 
70  srcdims = 0; dstdims = 0
71  errmsg='' ! for successful return
72  rc = 0 ! for successful retun
73 
74  inquire(file=trim(fname), exist=fexist)
75  if (.not. fexist) then
76  write (errmsg, '(a)') 'FATAL ERROR: input file '//trim(fname)//' does not exist.'
77  rc = 1
78  return
79  else
80  ! Open and read namelist file.
81  open (action='read', file=trim(fname), iostat=ierr, newunit=iounit)
82  read (nml=ocniceprep_nml, iostat=ierr, unit=iounit)
83  if (ierr /= 0) then
84  rc = 1
85  write (errmsg, '(a)') 'FATAL ERROR: invalid namelist format.'
86  return
87  end if
88  close (iounit)
89  end if
90 
91  ! append slash to wgtsdir and griddir
92  wgtsdir = trim(wgtsdir)//'/'
93  griddir = trim(griddir)//'/'
94 
95  ! check that model is either ocean or ice
96  if (trim(ftype) /= 'ocean' .and. trim(ftype) /= 'ice') then
97  rc = 1
98  write (errmsg, '(a)') 'FATAL ERROR: ftype must be ocean or ice'
99  return
100  end if
101 
102  ! set grid dimensions and names
103  nxt = srcdims(1); nyt = srcdims(2)
104  nxr = dstdims(1); nyr = dstdims(2)
105  fsrc = '' ; fdst = ''
106  if (nxt == 1440 .and. nyt == 1080) fsrc = 'mx025' ! 1/4deg tripole
107  if (len_trim(fsrc) == 0) then
108  rc = 1
109  write(errmsg,'(a)')'FATAL ERROR: source grid dimensions invalid'
110  return
111  end if
112 
113  if (nxr == 720 .and. nyr == 576) fdst = 'mx050' ! 1/2deg tripole
114  if (nxr == 360 .and. nyr == 320) fdst = 'mx100' ! 1deg tripole
115  if (nxr == 72 .and. nyr == 35) fdst = 'mx500' ! 5deg tripole
116  if (len_trim(fdst) == 0) then
117  rc = 1
118  write(errmsg,'(a)')'FATAL ERROR: destination grid dimensions invalid'
119  return
120  end if
121 
122  if (trim(fdst) == 'mx500') then
123  rc = 1
124  write(errmsg,'(a)')'FATAL ERROR: 5deg destination grid is not supported'
125  return
126  end if
127 
128  ! initialize the source file types
129  if (trim(ftype) == 'ocean') then
130  do_ocnprep = .true.
131  else
132  do_ocnprep = .false.
133  end if
134 
135  input_file = trim(ftype)//'.nc'
136  inquire (file=trim(input_file), exist=fexist)
137  if (.not. fexist) then
138  write (errmsg, '(a)') 'FATAL ERROR: input file '//trim(input_file)//' does not exist.'
139  rc=1
140  return
141  end if
142 
143  ! log file
144  open(newunit=logunit, file=trim(ftype)//'.prep.log',form='formatted')
145  if (debug) write(logunit, '(a)')'input file: '//trim(input_file)
146 
147  ! all checks pass, continue
148  write(errmsg,'(a)') 'Namelist successfully read, continue'
149  rc = 0
150 
151  end subroutine readnml
152 
161  subroutine readcsv(fname,errmsg,rc,nvalid)
163  character(len=*), intent(in) :: fname
164  character(len=*), intent(out) :: errmsg
165  integer, intent(out) :: rc
166  integer, intent(out) :: nvalid
167 
168  ! local variables
169  character(len=100) :: chead
170  character(len= 20) :: c1,c3,c4,c5,c6
171  integer :: i2, idx1,idx2
172  integer :: nn,n,ierr,iounit
173  !----------------------------------------------------------------------------
174 
175  errmsg='' ! for successful return
176  rc = 0 ! for successful retun
177 
178  open(newunit=iounit, file=trim(fname), status='old', iostat=ierr)
179  if (ierr /= 0) then
180  rc = 1
181  write (errmsg, '(a)') 'FATAL ERROR: input file '//trim(fname)//' does not exist.'
182  return
183  end if
184 
185  read(iounit,*)chead
186  nn=0
187  do n = 1,maxvars
188  read(iounit,*,iostat=ierr)c1,i2,c3,c4,c5,c6
189  if (ierr .ne. 0) exit
190  if (len_trim(c1) > 0) then
191  nn = nn+1
192  outvars(nn)%var_name = trim(c1)
193  outvars(nn)%var_dimen = i2
194  outvars(nn)%var_grid = trim(c3)
195  outvars(nn)%var_remapmethod = trim(c4)
196  outvars(nn)%var_pair = trim(c5)
197  outvars(nn)%var_pair_grid = trim(c6)
198  end if
199  end do
200  close(iounit)
201  nvalid = nn
202 
203  ! check for u,v pairs, these should be listed in csv file in ordered pairs
204  idx1 = 0; idx2 = 0
205  do n = 1,nvalid
206  if (len_trim(outvars(n)%var_pair) > 0 .and. idx1 .eq. 0) then
207  idx1 = n
208  idx2 = n+1
209  end if
210  end do
211 
212  if (trim(outvars(idx1)%var_pair) /= trim(outvars(idx2)%var_name)) then
213  rc = 1
214  write(errmsg,'(a)')'FATAL ERROR: vector pair for '//trim(outvars(idx1)%var_name) &
215  //' is not set correctly'
216  return
217  end if
218  if (trim(outvars(idx2)%var_pair) /= trim(outvars(idx1)%var_name)) then
219  rc = 1
220  write(errmsg,'(a)')'FATAL ERROR: vector pair for '//trim(outvars(idx2)%var_name) &
221  //' is not set correctly'
222  return
223  end if
224 
225  ! check for u velocities on u-staggers and v-velocities on v-staggers
226  if (outvars(idx1)%var_name(1:1) == 'u') then
227  if ((outvars(idx1)%var_grid(1:2) /= 'Cu') .and. outvars(idx1)%var_grid(1:2) /= 'Bu') then
228  rc = 1
229  write(errmsg,'(a)')'FATAL ERROR: u-vector has wrong grid '
230  return
231  end if
232  end if
233  if (outvars(idx2)%var_name(1:1) == 'v') then
234  if ((outvars(idx2)%var_grid(1:2) /= 'Cv') .and. outvars(idx2)%var_grid(1:2) /= 'Bu') then
235  rc = 1
236  write(errmsg,'(a)')'FATAL ERROR: v-vector has wrong grid '
237  return
238  end if
239  end if
240 
241  ! all checks pass, continue
242  write(errmsg,'(a)')'CSV successfully read, continue'
243  rc = 0
244 
245  end subroutine readcsv
246 end module init_mod