1 | !M. Indurain |
---|
2 | !March 2015 |
---|
3 | |
---|
4 | PROGRAM lmdz_harmonic |
---|
5 | |
---|
6 | !Computes wind field following some vectorial harmonics with spherepack routines. |
---|
7 | !Wind field follows lmdz convention: |
---|
8 | ! u(nlong+1,nlat) zonal eastward wind, v(nlong+1,nlat) latitudinal wind |
---|
9 | !Grid resolution is given in the command line: |
---|
10 | !./test_harmonic -lat n_latitudinal_bands -lon n_longitudinal_bands. |
---|
11 | !By default, n_latitudinal_bands = 48. n_longitudinal_bands = 64. |
---|
12 | |
---|
13 | use netcdf |
---|
14 | |
---|
15 | implicit none |
---|
16 | |
---|
17 | !List of harmonics we want |
---|
18 | character (len=100), dimension(5) :: harmonic=(/'2','10','22','6_3','15_20'/) |
---|
19 | !Physical grid |
---|
20 | integer :: nlat,nlatm1,nlong,nlongp1 |
---|
21 | double precision, dimension(:), allocatable :: latitude,longitude |
---|
22 | double precision, dimension(:,:), allocatable :: merid_wind,zonal_wind !spherepack convention |
---|
23 | double precision, dimension(:,:), allocatable :: merid_wind_lmdz,zonal_wind_lmdz !lmdz convention |
---|
24 | !Spectral grid |
---|
25 | integer :: mmax !spectra truncation mmax=min(nalt,nlong/2) or min(nalt,(nlong+1)/2) |
---|
26 | double precision, dimension (:,:), allocatable :: br,bi,cr,ci !spectra coefficients |
---|
27 | double precision, dimension (:,:), allocatable :: norm,norm_b,norm_c !norm of spectra coefficients |
---|
28 | double precision, dimension (:), allocatable :: spectra,spectra_b,spectra_c |
---|
29 | !Spherepack declarations |
---|
30 | integer :: ierror,lvhsec,ldwork,lwork,l1,l2,nt=1,ityp=0,idvw,jdvw,mdab,ndab |
---|
31 | double precision, dimension (:), allocatable :: wvhsec,work |
---|
32 | double precision, dimension (:), allocatable :: dwork |
---|
33 | !Netcdf stuff |
---|
34 | integer :: idfile,idmerid_wind,idzonal_wind |
---|
35 | integer :: idlat,idlong,idlat_var,idlong_var |
---|
36 | integer :: idmdab,idndab,idbr,idbi,idcr,idci |
---|
37 | integer :: idspectra,idspectra_b,idspectra_c |
---|
38 | character (len=100) :: file_netcdf |
---|
39 | !Other |
---|
40 | integer :: i,j,k |
---|
41 | double precision, parameter :: pi=2.*ASIN(1.) |
---|
42 | character (len=100) :: tmp_char |
---|
43 | !Input arguments |
---|
44 | character (len=100), dimension(:), allocatable :: arg |
---|
45 | integer :: narg |
---|
46 | |
---|
47 | !********** |
---|
48 | !Initialisation |
---|
49 | nlatm1=48 |
---|
50 | nlong=64 |
---|
51 | |
---|
52 | !********** |
---|
53 | !Input reading |
---|
54 | narg = command_argument_count() |
---|
55 | allocate(arg(narg)) |
---|
56 | do i = 1, narg |
---|
57 | call get_command_argument(i,arg(i)) |
---|
58 | end do |
---|
59 | i = 1 |
---|
60 | do while (i .le. narg) |
---|
61 | if (arg(i) == '-lat' .or. arg(i) == '-lon') then |
---|
62 | select case(arg(i)) |
---|
63 | case('-lat') |
---|
64 | read(arg(i+1),'(i10)' ) nlatm1 !number of latitudinal bands |
---|
65 | case('-lon') |
---|
66 | read(arg(i+1),'(i10)' ) nlong !number of longitudinal bands |
---|
67 | end select |
---|
68 | i = i + 2 |
---|
69 | elseif (arg(i) == '-h' .or. arg(i) == '--help') then |
---|
70 | print*,'Usage\n test_harmonic [option]\n [-h or --help]\t: brief help' |
---|
71 | print*,'[-lat int]\t: number of latitudinal bands (default: 48)' |
---|
72 | print*,'[-lon int]\t: number of longitudinal bands (default: 64)' |
---|
73 | stop 'End help' |
---|
74 | else |
---|
75 | print*,'No option ',trim(arg(i)) |
---|
76 | stop |
---|
77 | end if |
---|
78 | end do |
---|
79 | |
---|
80 | !********** |
---|
81 | !Physical grid |
---|
82 | nlat=nlatm1+1 |
---|
83 | nlongp1=nlong+1 |
---|
84 | print*,'harmonics computed on a ',nlatm1,' latitude bands x ',nlong,' longitude bands.' |
---|
85 | |
---|
86 | allocate(latitude(nlat)) |
---|
87 | allocate(longitude(nlongp1)) |
---|
88 | do j=1,nlat |
---|
89 | latitude(j)=90.-(j-1)*180./(nlat-1) |
---|
90 | end do |
---|
91 | do i=1,nlongp1 |
---|
92 | longitude(i)=-180.+(i-1)*2*180./nlong |
---|
93 | end do |
---|
94 | allocate(zonal_wind(nlat,nlong)) |
---|
95 | allocate(merid_wind(nlat,nlong)) |
---|
96 | allocate(zonal_wind_lmdz(nlongp1,nlat)) |
---|
97 | allocate(merid_wind_lmdz(nlongp1,nlat)) |
---|
98 | |
---|
99 | !##### |
---|
100 | !Spectra computation |
---|
101 | !##### |
---|
102 | !**********Maximum value for m |
---|
103 | if (mod(nlong,2) == 0) then |
---|
104 | mmax = min(nlat,nlong/2) |
---|
105 | else |
---|
106 | mmax = min(nlat,(nlong+1)/2) |
---|
107 | end if |
---|
108 | |
---|
109 | !**********Vhaeci function (initialisations for Vhaec function) |
---|
110 | if (mod(nlong,2) == 0) then |
---|
111 | l1 = min(nlat,nlong/2) |
---|
112 | else |
---|
113 | l1 = min(nlat,(nlong+1)/2) |
---|
114 | end if |
---|
115 | if (mod(nlat,2) == 0) then |
---|
116 | l2 = nlat/2 |
---|
117 | else |
---|
118 | l2 = (nlat+1)/2 |
---|
119 | end if |
---|
120 | lvhsec=4*nlat*l2+3*max(l1-2,0)*(nlat+nlat-l1-1)+nlong+15 |
---|
121 | allocate(wvhsec(lvhsec)) |
---|
122 | wvhsec(:) = 0. |
---|
123 | ldwork=2*(nlat+2) |
---|
124 | allocate(dwork(ldwork)) |
---|
125 | dwork(:) = 0. |
---|
126 | ierror=3 |
---|
127 | call vhseci(nlat,nlong,wvhsec,lvhsec,dwork,ldwork,ierror) |
---|
128 | |
---|
129 | !**********Vhseci function result |
---|
130 | select case (ierror) |
---|
131 | case(1) |
---|
132 | print*,'Vhseci: ERROR on nlat' |
---|
133 | case(2) |
---|
134 | print*,'Vhseci: ERROR on nlong' |
---|
135 | case(3) |
---|
136 | print*,'Vhseci: ERROR on lvhsec' |
---|
137 | case(4) |
---|
138 | print*,'Vhseci: ERROR on ldwork' |
---|
139 | end select |
---|
140 | |
---|
141 | !**********Loop over all harmonics |
---|
142 | do k=1,SIZE(harmonic) |
---|
143 | file_netcdf='harmonic_'//trim(integer2string(nlongp1-1))//'x'//& |
---|
144 | trim(integer2string(nlat-1))//'_lmdz_'//trim(harmonic(k))//'.nc' |
---|
145 | |
---|
146 | !**********Vhsec function |
---|
147 | idvw=nlat |
---|
148 | jdvw=nlong |
---|
149 | mdab=mmax |
---|
150 | ndab=nlat |
---|
151 | allocate(br(mdab,ndab)) |
---|
152 | allocate(bi(mdab,ndab)) |
---|
153 | allocate(cr(mdab,ndab)) |
---|
154 | allocate(ci(mdab,ndab)) |
---|
155 | br(:,:) = 0. |
---|
156 | bi(:,:) = 0. |
---|
157 | cr(:,:) = 0. |
---|
158 | ci(:,:) = 0. |
---|
159 | select case(harmonic(k)) |
---|
160 | case('2') |
---|
161 | br(3,3)=1. |
---|
162 | case('10') |
---|
163 | br(11,11)=1. |
---|
164 | case('22') |
---|
165 | br(23,23)=1. |
---|
166 | case('6_3') |
---|
167 | br(4,4)=1. |
---|
168 | br(7,7)=1. |
---|
169 | case('15_20') |
---|
170 | br(16,16)=1. |
---|
171 | br(21,21)=1. |
---|
172 | end select |
---|
173 | if (mod(nlong,2) == 0) then |
---|
174 | l1 = min(nlat,nlong/2) |
---|
175 | else |
---|
176 | l1 = min(nlat,(nlong+1)/2) |
---|
177 | end if |
---|
178 | if (mod(nlat,2) == 0) then |
---|
179 | l2 = nlat/2 |
---|
180 | else |
---|
181 | l2 = (nlat+1)/2 |
---|
182 | end if |
---|
183 | lvhsec=4*nlat*l2+3*max(l1-2,0)*(nlat+nlat-l1-1)+nlong+15 |
---|
184 | if (ityp .le. 2) then |
---|
185 | lwork=nlat*(2*nt*nlong+max(6*l2,nlong)) |
---|
186 | else |
---|
187 | lwork=l2*(2*nt*nlong+max(6*nlat,nlong)) |
---|
188 | end if |
---|
189 | allocate(work(lwork)) |
---|
190 | work(:) = 0. |
---|
191 | ierror=3 |
---|
192 | call vhsec(nlat,nlong,ityp,nt,merid_wind,zonal_wind,idvw,jdvw,br,bi,cr,ci,mdab,ndab,wvhsec,lvhsec,work,lwork,ierror) |
---|
193 | |
---|
194 | !**********Vhsec function result |
---|
195 | select case (ierror) |
---|
196 | case(1) |
---|
197 | print*,'Vhsec: ERROR on nlat' |
---|
198 | case(2) |
---|
199 | print*,'Vhsec: ERROR on nlong' |
---|
200 | case(3) |
---|
201 | print*,'Vhsec: ERROR on ityp' |
---|
202 | case(4) |
---|
203 | print*,'Vhsec: ERROR on nt' |
---|
204 | case(5) |
---|
205 | print*,'Vhsec: ERROR on idvw' |
---|
206 | case(6) |
---|
207 | print*,'Vhsec: ERROR on jdvw' |
---|
208 | case(7) |
---|
209 | print*,'Vhsec: ERROR on mdab' |
---|
210 | case(8) |
---|
211 | print*,'Vhsec: ERROR on ndab' |
---|
212 | case(9) |
---|
213 | print*,'Vhsec: ERROR on lvhsec' |
---|
214 | case(10) |
---|
215 | print*,'Vhsec: ERROR on lwork' |
---|
216 | end select |
---|
217 | |
---|
218 | !**********Energy spectra |
---|
219 | allocate(norm(mMax,nLat)) |
---|
220 | allocate(norm_b(mMax,nLat)) |
---|
221 | allocate(norm_c(mMax,nLat)) |
---|
222 | do i = 1,mMax |
---|
223 | do j = 1,nLat |
---|
224 | norm(i,j)=(br(i,j)*br(i,j)+bi(i,j)*bi(i,j)+cr(i,j)*cr(i,j)+ci(i,j)*ci(i,j)) |
---|
225 | norm_b(i,j)=(br(i,j)*br(i,j)+bi(i,j)*bi(i,j)) |
---|
226 | norm_c(i,j)=(cr(i,j)*cr(i,j)+ci(i,j)*ci(i,j)) |
---|
227 | end do |
---|
228 | end do |
---|
229 | allocate(spectra(nLat)) |
---|
230 | allocate(spectra_b(nLat)) |
---|
231 | allocate(spectra_c(nLat)) |
---|
232 | do j = 1,nLat |
---|
233 | spectra(j) = 0.5*norm(1,j) |
---|
234 | spectra_b(j) = 0.5*norm_b(1,j) |
---|
235 | spectra_c(j) = 0.5*norm_c(1,j) |
---|
236 | do i = 2,mMax |
---|
237 | spectra(j) = spectra(j) + norm(i,j) |
---|
238 | spectra_b(j) = spectra_b(j) + norm_b(i,j) |
---|
239 | spectra_c(j) = spectra_c(j) + norm_c(i,j) |
---|
240 | end do |
---|
241 | end do |
---|
242 | |
---|
243 | !**********From spherepack to lmdz convention |
---|
244 | zonal_wind_lmdz(1:nlong,:)=transpose(zonal_wind(:,:)) |
---|
245 | merid_wind_lmdz(1:nlong,:)=transpose(merid_wind(:,:)) |
---|
246 | zonal_wind_lmdz(nlongp1,:)=zonal_wind_lmdz(1,:) |
---|
247 | merid_wind_lmdz(nlongp1,:)=merid_wind_lmdz(1,:) |
---|
248 | |
---|
249 | !**********Write netcdf file |
---|
250 | call check(nf90_create(trim(file_netcdf),NF90_CLOBBER,idfile)) |
---|
251 | |
---|
252 | call check( nf90_def_dim(idfile,"mdab",mdab,idmdab)) |
---|
253 | call check( nf90_def_dim(idfile,"ndab",ndab,idndab)) |
---|
254 | call check(nf90_def_var(idfile,"br",NF90_DOUBLE,(/idmdab,idndab/),idbr)) |
---|
255 | call check(nf90_def_var(idfile,"bi",NF90_DOUBLE,(/idmdab,idndab/),idbi)) |
---|
256 | call check(nf90_def_var(idfile,"cr",NF90_DOUBLE,(/idmdab,idndab/),idcr)) |
---|
257 | call check(nf90_def_var(idfile,"ci",NF90_DOUBLE,(/idmdab,idndab/),idci)) |
---|
258 | call check(nf90_def_var(idfile,"spectra",NF90_DOUBLE,(/idndab/),idspectra)) |
---|
259 | call check(nf90_def_var(idfile,"spectra_div",NF90_DOUBLE,(/idndab/),idspectra_b)) |
---|
260 | call check(nf90_def_var(idfile,"spectra_vort",NF90_DOUBLE,(/idndab/),idspectra_c)) |
---|
261 | |
---|
262 | call check( nf90_def_dim(idfile,"latitude",nlat,idlat)) |
---|
263 | call check( nf90_def_dim(idfile,"longitude",nlongp1,idlong)) |
---|
264 | call check(nf90_def_var(idfile,"latitude",NF90_DOUBLE,(/idlat/),idlat_var)) |
---|
265 | call check(nf90_def_var(idfile,"longitude",NF90_DOUBLE,(/idlong/),idlong_var)) |
---|
266 | call check(nf90_def_var(idfile,"u",NF90_DOUBLE,(/idlong,idlat/),idzonal_wind)) |
---|
267 | call check(nf90_def_var(idfile,"v",NF90_DOUBLE,(/idlong,idlat/),idmerid_wind)) |
---|
268 | ! End define mode. This tells netCDF we are done defining metadata. |
---|
269 | call check(nf90_enddef(idfile)) |
---|
270 | |
---|
271 | ! Write the pretend data to the file. Although netCDF supports |
---|
272 | ! reading and writing subsets of data, in this case we write all the |
---|
273 | ! data in one operation. |
---|
274 | call check(nf90_put_var(idfile,idbr,br)) |
---|
275 | call check(nf90_put_var(idfile,idbi,bi)) |
---|
276 | call check(nf90_put_var(idfile,idcr,cr)) |
---|
277 | call check(nf90_put_var(idfile,idci,ci)) |
---|
278 | call check(nf90_put_var(idfile,idspectra,spectra)) |
---|
279 | call check(nf90_put_var(idfile,idspectra_b,spectra_b)) |
---|
280 | call check(nf90_put_var(idfile,idspectra_c,spectra_c)) |
---|
281 | |
---|
282 | call check(nf90_put_var(idfile,idlat_var,latitude)) |
---|
283 | call check(nf90_put_var(idfile,idlong_var,longitude)) |
---|
284 | call check(nf90_put_var(idfile,idzonal_wind,zonal_wind_lmdz)) |
---|
285 | call check(nf90_put_var(idfile,idmerid_wind,merid_wind_lmdz)) |
---|
286 | ! Close the file. This frees up any internal netCDF resources |
---|
287 | ! associated with the file, and flushes any buffers. |
---|
288 | call check(nf90_close(idfile)) |
---|
289 | |
---|
290 | deallocate(br) |
---|
291 | deallocate(bi) |
---|
292 | deallocate(cr) |
---|
293 | deallocate(ci) |
---|
294 | deallocate(work) |
---|
295 | deallocate(norm) |
---|
296 | deallocate(norm_b) |
---|
297 | deallocate(norm_c) |
---|
298 | deallocate(spectra) |
---|
299 | deallocate(spectra_b) |
---|
300 | deallocate(spectra_c) |
---|
301 | |
---|
302 | |
---|
303 | print*,trim(file_netcdf),' computed!' |
---|
304 | end do |
---|
305 | |
---|
306 | |
---|
307 | contains |
---|
308 | subroutine check(status) |
---|
309 | integer, intent (in) :: status |
---|
310 | |
---|
311 | if(status /= nf90_noerr) then |
---|
312 | print *, trim(nf90_strerror(status)) |
---|
313 | stop "Stopped" |
---|
314 | end if |
---|
315 | end subroutine check |
---|
316 | |
---|
317 | function integer2string(n) |
---|
318 | implicit none |
---|
319 | character(len=10) :: integer2string |
---|
320 | integer, intent(in) :: n |
---|
321 | integer :: nn |
---|
322 | |
---|
323 | nn=n |
---|
324 | if (n .lt. 0) nn=-n |
---|
325 | if (nn .ge. 0 .and. nn .lt. 10) write(integer2string,'(i1)') nn |
---|
326 | if (nn .ge. 10 .and. nn .lt. 100) write(integer2string,'(i2)') nn |
---|
327 | if (nn .ge. 100 .and. nn .lt. 1000) write(integer2string,'(i3)') nn |
---|
328 | if (nn .ge. 1000 .and. nn .lt. 10000) write(integer2string,'(i4)') nn |
---|
329 | if (nn .ge. 10000 .and. nn .lt. 100000) write(integer2string,'(i4)') nn |
---|
330 | if (nn .ge. 100000 .and. nn .lt. 1000000) write(integer2string,'(i4)') nn |
---|
331 | if (nn .ge. 1000000 .and. nn .lt. 10000000) write(integer2string,'(i4)') nn |
---|
332 | if (nn .ge. 10000000 .and. nn .lt. 100000000) write(integer2string,'(i4)') nn |
---|
333 | |
---|
334 | end function integer2string |
---|
335 | |
---|
336 | END PROGRAM lmdz_harmonic |
---|