1 | program createRandomStart |
---|
2 | |
---|
3 | USE netcdf |
---|
4 | |
---|
5 | implicit none |
---|
6 | |
---|
7 | !======================================================================= |
---|
8 | ! Copy a start.nc file and change ucov, vcov, teta variables: |
---|
9 | ! teta -> teta +/- dTeta (K) |
---|
10 | ! ucov -> (0 +/- duNat)*cu (m/s) |
---|
11 | ! vcov -> (0 +/- dvNat)*cv (m/s) |
---|
12 | ! Perturbations are vertically the same. |
---|
13 | !======================================================================= |
---|
14 | |
---|
15 | !Input |
---|
16 | character (len=100) :: fileNetcdfInput ! input start.nc netcdf file |
---|
17 | !Output |
---|
18 | character (len=100) :: fileNetcdf ! output start.nc netcdf file |
---|
19 | !Usefull stuff |
---|
20 | integer :: nLat,nRlatv,nLong,nRlonu,nAlt,nTime ! dimensions in netcdf file (redundant point in longitude) |
---|
21 | real, dimension (:,:,:,:), allocatable :: uCov,vCov,uNat,vNat,uNat2,vNat2,teta |
---|
22 | real, dimension (:,:), allocatable :: cu,cv |
---|
23 | real :: duNat,dvNat,dTeta |
---|
24 | !Netcdf declarations |
---|
25 | integer :: idFile,idStuff,idUcov,idVcov,idCu,idCv,idTeta |
---|
26 | integer :: ierror |
---|
27 | !Temporary variables |
---|
28 | character (len=100) :: format,varTmp |
---|
29 | integer :: i,j,k,l,N=1,dimFilter |
---|
30 | logical :: isFile |
---|
31 | real :: alea |
---|
32 | character (len=200) :: command |
---|
33 | |
---|
34 | |
---|
35 | print*,'input start.nc?' |
---|
36 | read*, fileNetcdfInput |
---|
37 | inquire(file=fileNetcdfInput,exist=isFile) |
---|
38 | if (isFile .eqv. .false.) then |
---|
39 | stop 'input doesn t exist' |
---|
40 | end if |
---|
41 | |
---|
42 | print*,'output start.nc?' |
---|
43 | read*, fileNetcdf |
---|
44 | inquire(file=fileNetcdf,exist=isFile) |
---|
45 | if (fileNetcdfInput == fileNetcdf) then |
---|
46 | !name of the input netcdf file back-up |
---|
47 | varTmp=trim(fileNetcdfInput) // '.old' |
---|
48 | inquire(file=varTmp,exist=isFile) |
---|
49 | do while(isFile .eqv. .true.) |
---|
50 | varTmp=trim(varTmp) // '.old' |
---|
51 | print*,varTmp |
---|
52 | inquire(file=varTmp,exist=isFile) |
---|
53 | end do |
---|
54 | !input netcdf file back-up |
---|
55 | write(command,'(a6,a50,a1,a50)') 'cp -f ',trim(fileNetcdfInput),' ',trim(varTmp) |
---|
56 | call system(command) |
---|
57 | else |
---|
58 | inquire(file=fileNetcdf,exist=isFile) |
---|
59 | if (isFile .eqv. .true.) then |
---|
60 | print*,'output name existing. Want to overwrite? (y/n)' |
---|
61 | read*,varTmp |
---|
62 | if (varTmp == 'y') then |
---|
63 | write(command,'(a6,a50)') 'rm -f ',fileNetcdf |
---|
64 | print*,command |
---|
65 | call system(command) |
---|
66 | else |
---|
67 | stop 'existing output not deleted' |
---|
68 | end if |
---|
69 | end if |
---|
70 | end if |
---|
71 | |
---|
72 | print*,'du (m/s)?' |
---|
73 | read*, duNat |
---|
74 | |
---|
75 | print*,'dv (m/s)?' |
---|
76 | read*, dvNat |
---|
77 | |
---|
78 | print*,'dT (K)?' |
---|
79 | read*, dTeta |
---|
80 | |
---|
81 | !print*,'dim average filter? (0 --> no | 1,2,3... --> average)' |
---|
82 | !read*, dimFilter |
---|
83 | dimFilter=0 |
---|
84 | |
---|
85 | !Creation of the output netcdf file if different from input netcdf file |
---|
86 | if (fileNetcdfInput /= fileNetcdf) then |
---|
87 | write(command,'(a6,a50,a1,a50)') 'cp -f ',fileNetcdfInput,' ',fileNetcdf |
---|
88 | print*,command |
---|
89 | call system(command) |
---|
90 | end if |
---|
91 | |
---|
92 | !********** |
---|
93 | !Netcdf file reading |
---|
94 | PRINT*,'**********' |
---|
95 | PRINT*,'Netcdf file reading' |
---|
96 | !Open |
---|
97 | ierror=nf90_open(fileNetcdf,NF90_WRITE,idFile) |
---|
98 | if(ierror /= nf90_noerr) then |
---|
99 | print *, trim(nf90_strerror(ierror)) |
---|
100 | stop "Stopped open" |
---|
101 | end if |
---|
102 | !Read dimension latitude |
---|
103 | ierror=nf90_inq_dimid(idFile,'latitude',idStuff) |
---|
104 | if(ierror /= nf90_noerr) then |
---|
105 | print *, trim(nf90_strerror(ierror)) |
---|
106 | stop "Stopped" |
---|
107 | end if |
---|
108 | ierror=nf90_inquire_dimension(idFile,idStuff,varTmp,nLat) |
---|
109 | if(ierror /= nf90_noerr) then |
---|
110 | print *, trim(nf90_strerror(ierror)) |
---|
111 | stop "Stopped dim lat" |
---|
112 | end if |
---|
113 | !Read dimension latitude bis |
---|
114 | ierror=nf90_inq_dimid(idFile,'rlatv',idStuff) |
---|
115 | if(ierror /= nf90_noerr) then |
---|
116 | print *, trim(nf90_strerror(ierror)) |
---|
117 | stop "Stopped" |
---|
118 | end if |
---|
119 | ierror=nf90_inquire_dimension(idFile,idStuff,varTmp,nRlatv) |
---|
120 | if(ierror /= nf90_noerr) then |
---|
121 | print *, trim(nf90_strerror(ierror)) |
---|
122 | stop "Stopped dim rlatv" |
---|
123 | end if |
---|
124 | !Read dimension longitude |
---|
125 | ierror=nf90_inq_dimid(idFile,'longitude',idStuff) |
---|
126 | if(ierror /= nf90_noerr) then |
---|
127 | print *, trim(nf90_strerror(ierror)) |
---|
128 | stop "Stopped" |
---|
129 | end if |
---|
130 | ierror=nf90_inquire_dimension(idFile,idStuff,varTmp,nLong) |
---|
131 | if(ierror /= nf90_noerr) then |
---|
132 | print *, trim(nf90_strerror(ierror)) |
---|
133 | stop "Stopped dim long" |
---|
134 | end if |
---|
135 | !Read dimension longitude bis |
---|
136 | ierror=nf90_inq_dimid(idFile,'rlonu',idStuff) |
---|
137 | if(ierror /= nf90_noerr) then |
---|
138 | print *, trim(nf90_strerror(ierror)) |
---|
139 | stop "Stopped" |
---|
140 | end if |
---|
141 | ierror=nf90_inquire_dimension(idFile,idStuff,varTmp,nRlonu) |
---|
142 | if(ierror /= nf90_noerr) then |
---|
143 | print *, trim(nf90_strerror(ierror)) |
---|
144 | stop "Stopped dim rlonu" |
---|
145 | end if |
---|
146 | !Read dimension altitude |
---|
147 | ierror=nf90_inq_dimid(idFile,'altitude',idStuff) |
---|
148 | if(ierror /= nf90_noerr) then |
---|
149 | print *, trim(nf90_strerror(ierror)) |
---|
150 | stop "Stopped" |
---|
151 | end if |
---|
152 | ierror=nf90_inquire_dimension(idFile,idStuff,varTmp,nAlt) |
---|
153 | if(ierror /= nf90_noerr) then |
---|
154 | print *, trim(nf90_strerror(ierror)) |
---|
155 | stop "Stopped dim alt" |
---|
156 | end if |
---|
157 | !Read dimension time |
---|
158 | ierror=nf90_inq_dimid(idFile,'Time',idStuff) |
---|
159 | if(ierror /= nf90_noerr) then |
---|
160 | print *, trim(nf90_strerror(ierror)) |
---|
161 | stop "Stopped" |
---|
162 | end if |
---|
163 | ierror=nf90_inquire_dimension(idFile,idStuff,varTmp,nTime) |
---|
164 | if(ierror /= nf90_noerr) then |
---|
165 | print *, trim(nf90_strerror(ierror)) |
---|
166 | stop "Stopped dim time" |
---|
167 | end if |
---|
168 | !Read variables ucov and vcov |
---|
169 | allocate(uCov(nRlonu,nLat,nAlt,nTime)) |
---|
170 | allocate(vCov(nLong,nRlatv,nAlt,nTime)) |
---|
171 | ierror=nf90_inq_varid(idFile,"ucov",idUcov) |
---|
172 | if(ierror /= nf90_noerr) then |
---|
173 | print *, trim(nf90_strerror(ierror)) |
---|
174 | stop "Stopped" |
---|
175 | end if |
---|
176 | ierror=nf90_get_var(idFile,idUcov,uCov) |
---|
177 | if(ierror /= nf90_noerr) then |
---|
178 | print *, trim(nf90_strerror(ierror)) |
---|
179 | stop "Stopped get_var ucov" |
---|
180 | end if |
---|
181 | ierror=nf90_inq_varid(idFile,"vcov",idVcov) |
---|
182 | if(ierror /= nf90_noerr) then |
---|
183 | print *, trim(nf90_strerror(ierror)) |
---|
184 | stop "Stopped" |
---|
185 | end if |
---|
186 | ierror=nf90_get_var(idFile,idVcov,vCov) |
---|
187 | if(ierror /= nf90_noerr) then |
---|
188 | print *, trim(nf90_strerror(ierror)) |
---|
189 | stop "Stopped get_var vcov" |
---|
190 | end if |
---|
191 | !Read variables cu and cv |
---|
192 | allocate(cu(nRlonu,nLat)) |
---|
193 | allocate(cv(nLong,nRlatv)) |
---|
194 | ierror=nf90_inq_varid(idFile,"cu",idCu) |
---|
195 | if(ierror /= nf90_noerr) then |
---|
196 | print *, trim(nf90_strerror(ierror)) |
---|
197 | stop "Stopped" |
---|
198 | end if |
---|
199 | ierror=nf90_get_var(idFile,idCu,cu) |
---|
200 | if(ierror /= nf90_noerr) then |
---|
201 | print *, trim(nf90_strerror(ierror)) |
---|
202 | stop "Stopped get_var cu" |
---|
203 | end if |
---|
204 | ierror=nf90_inq_varid(idFile,"cv",idCv) |
---|
205 | if(ierror /= nf90_noerr) then |
---|
206 | print *, trim(nf90_strerror(ierror)) |
---|
207 | stop "Stopped" |
---|
208 | end if |
---|
209 | ierror=nf90_get_var(idFile,idCv,cv) |
---|
210 | if(ierror /= nf90_noerr) then |
---|
211 | print *, trim(nf90_strerror(ierror)) |
---|
212 | stop "Stopped get_var cv" |
---|
213 | end if |
---|
214 | !Read variables teta |
---|
215 | allocate(teta(nLong,nLat,nAlt,nTime)) |
---|
216 | ierror=nf90_inq_varid(idFile,"teta",idTeta) |
---|
217 | if(ierror /= nf90_noerr) then |
---|
218 | print *, trim(nf90_strerror(ierror)) |
---|
219 | stop "Stopped" |
---|
220 | end if |
---|
221 | ierror=nf90_get_var(idFile,idTeta,teta) |
---|
222 | if(ierror /= nf90_noerr) then |
---|
223 | print *, trim(nf90_strerror(ierror)) |
---|
224 | stop "Stopped get_var teta" |
---|
225 | end if |
---|
226 | |
---|
227 | !********** |
---|
228 | !uNat |
---|
229 | allocate(uNat(nRlonu,nLat,nAlt,nTime)) |
---|
230 | uNat(:,:,:,:)=0.0 |
---|
231 | do i=1,nLong |
---|
232 | do j=1,nLat |
---|
233 | do k=1,nAlt |
---|
234 | do l=1,nTime |
---|
235 | call random_number(alea) |
---|
236 | uNat(i,j,k,l) = -duNat+2*duNat*alea |
---|
237 | end do |
---|
238 | end do |
---|
239 | end do |
---|
240 | end do |
---|
241 | !-/+180 longitude points must be the same |
---|
242 | uNat(1,:,:,:)=uNat(nLong,:,:,:) |
---|
243 | !********** |
---|
244 | !vNat |
---|
245 | allocate(vNat(nLong,nRlatv,nAlt,nTime)) |
---|
246 | vNat(:,:,:,:)=0.0 |
---|
247 | do i=1,nLong |
---|
248 | do j=1,nRlatv |
---|
249 | do k=1,nAlt |
---|
250 | do l=1,nTime |
---|
251 | call random_number(alea) |
---|
252 | vNat(i,j,k,l) = -dvNat+2*dvNat*alea |
---|
253 | end do |
---|
254 | end do |
---|
255 | end do |
---|
256 | end do |
---|
257 | !-/+180 longitude points must be the same |
---|
258 | vNat(1,:,:,:)=vNat(nLong,:,:,:) |
---|
259 | !********** |
---|
260 | !mean (if dimFilter=0 mean mean_filter has no effect) |
---|
261 | allocate(uNat2(nRlonu,nLat,nAlt,nTime)) |
---|
262 | allocate(vNat2(nLong,nRlatv,nAlt,nTime)) |
---|
263 | uNat2(:,:,:,:)=uNat(:,:,:,:) |
---|
264 | vNat2(:,:,:,:)=vNat(:,:,:,:) |
---|
265 | do k=1,nAlt |
---|
266 | do l=1,nTime |
---|
267 | call mean_filter(uNat(:,:,k,l),nRlonu,nLat,dimFilter,uNat2(:,:,k,l)) |
---|
268 | call mean_filter(vNat(:,:,k,l),nLong,nRlatv,dimFilter,vNat2(:,:,k,l)) |
---|
269 | end do |
---|
270 | end do |
---|
271 | !********** |
---|
272 | !new ucov and vcov |
---|
273 | do k=1,nAlt |
---|
274 | do l=1,nTime |
---|
275 | uCov(:,:,k,l)=uNat2(:,:,k,l)*cu |
---|
276 | vCov(:,:,k,l)=vNat2(:,:,k,l)*cv |
---|
277 | end do |
---|
278 | end do |
---|
279 | !********** |
---|
280 | !teta |
---|
281 | do i=1,nLong |
---|
282 | do j=1,nRlatv |
---|
283 | do k=1,nAlt |
---|
284 | do l=1,nTime |
---|
285 | call random_number(alea) |
---|
286 | teta(i,j,k,l) = teta(i,j,k,l)-dTeta+2*dTeta*alea |
---|
287 | end do |
---|
288 | end do |
---|
289 | end do |
---|
290 | end do |
---|
291 | teta(1,:,:,:)=teta(nLong,:,:,:) |
---|
292 | !********** |
---|
293 | !Write variable ucov and vcov |
---|
294 | ierror=nf90_put_var(idFile,idUcov,uCov) !,(1,1,1,1),(nLong,nLat,nAlt,nTime), |
---|
295 | if(ierror /= nf90_noerr) then |
---|
296 | print *, trim(nf90_strerror(ierror)) |
---|
297 | stop "Stopped put ucov" |
---|
298 | end if |
---|
299 | ierror=nf90_put_var(idFile,idVcov,vCov) !,(1,1,1,1),(nLong,nLat,nAlt,nTime), |
---|
300 | if(ierror /= nf90_noerr) then |
---|
301 | print *, trim(nf90_strerror(ierror)) |
---|
302 | stop "Stopped put vcov" |
---|
303 | end if |
---|
304 | !********** |
---|
305 | !Write variable teta |
---|
306 | ierror=nf90_put_var(idFile,idTeta,teta) !,(1,1,1,1),(nLong,nLat,nAlt,nTime), |
---|
307 | if(ierror /= nf90_noerr) then |
---|
308 | print *, trim(nf90_strerror(ierror)) |
---|
309 | stop "Stopped put teta" |
---|
310 | end if |
---|
311 | !********** |
---|
312 | !Close |
---|
313 | ierror=nf90_close(idFile) |
---|
314 | if(ierror /= nf90_noerr) then |
---|
315 | print *, trim(nf90_strerror(ierror)) |
---|
316 | stop "Stopped close" |
---|
317 | end if |
---|
318 | |
---|
319 | contains |
---|
320 | |
---|
321 | subroutine interface_user(fileNetcdf,duNat,dvNat,dTeta) |
---|
322 | |
---|
323 | implicit none |
---|
324 | |
---|
325 | character (len=*), intent(out) :: fileNetcdf |
---|
326 | real, intent(out) :: duNat,dvNat,dTeta |
---|
327 | |
---|
328 | |
---|
329 | |
---|
330 | |
---|
331 | |
---|
332 | |
---|
333 | end subroutine interface_user |
---|
334 | |
---|
335 | subroutine mean_filter(matrix,m,n,dimFilter,matrixOut) |
---|
336 | |
---|
337 | implicit none |
---|
338 | |
---|
339 | integer, intent(in) :: m,n,dimFilter |
---|
340 | real, dimension(m,n), intent(in) :: matrix |
---|
341 | real, dimension(m,n), intent(out) :: matrixOut |
---|
342 | real, dimension(m+2*dimFilter,n+2*dimFilter) :: matrixTmp,matrixConv |
---|
343 | real, dimension(2*dimFilter+1,2*dimFilter+1) :: kernel |
---|
344 | integer :: mm,nn |
---|
345 | |
---|
346 | !matrix containing input matrix plus zero edges |
---|
347 | matrixTmp(:,:)=0.0 |
---|
348 | mm=m+2*dimFilter |
---|
349 | nn=n+2*dimFilter |
---|
350 | do i=dimFilter+1,mm-dimFilter |
---|
351 | do j=dimFilter+1,nn-dimFilter |
---|
352 | matrixTmp(i,j)=matrix(i-dimFilter,j-dimFilter) |
---|
353 | end do |
---|
354 | end do |
---|
355 | !print*,matrixTmp(5,:) |
---|
356 | |
---|
357 | !filter creation |
---|
358 | kernel(:,:) = 1.0/((2*dimFilter+1)*(2*dimFilter+1)) |
---|
359 | !print*,kernel |
---|
360 | |
---|
361 | !matrix convolution with the filter |
---|
362 | do i=dimFilter+1,mm-dimFilter |
---|
363 | do j=dimFilter+1,nn-dimFilter |
---|
364 | matrixConv(i,j)=sum(matrixTmp(i-dimFilter:i+dimFilter,j-dimFilter:j+dimFilter)*kernel) |
---|
365 | end do |
---|
366 | end do |
---|
367 | i=5 |
---|
368 | j=5 |
---|
369 | !print*,shape(matrixTmp(i-dimFilter:i+dimFilter,j-dimFilter:j+dimFilter)) |
---|
370 | !print*,matrixConv(5,:) |
---|
371 | |
---|
372 | !extraction of submatrix without zeros edges |
---|
373 | matrixOut(:,:)=0.0 |
---|
374 | do i=1,m |
---|
375 | do j=1,n |
---|
376 | matrixOut(i,j)=matrixConv(i+dimFilter,j+dimFilter) |
---|
377 | end do |
---|
378 | end do |
---|
379 | |
---|
380 | end subroutine mean_filter |
---|
381 | |
---|
382 | end program createRandomStart |
---|
383 | |
---|