source: lmdz_wrf/trunk/tools/interpolate.f90 @ 1625

Last change on this file since 1625 was 1608, checked in by lfita, 8 years ago

Adding new Fortran capabilities after Rominas' DistriCorrection?

File size: 10.7 KB
Line 
1PROGRAM interpolate
2! Program to interpolate values from a giving projection
3! To be included in a python
4! f2py -m ForInterpolate --f90exec=/usr/bin/gfortran-4.7 -c interpolate.F90 module_generic.F90
5
6  IMPLICIT NONE
7
8  CHARACTER(LEN=50)                                      :: main, ErrWarnMsg
9
10  main='interpolate'
11
12END PROGRAM interpolate
13
14SUBROUTINE CoarselonlatFind(dx, dy, ilon, ilat, nxlon, nxlat, fraclon, fraclat, lonv, latv, per,      &
15  Nperx, Npery, ilonlat, mindiffLl)
16! Function to search a given value from a coarser version of the data
17
18  USE module_definitions
19  USE module_generic
20
21  IMPLICIT NONE
22
23!  INTEGER, PARAMETER                                     :: r_k = KIND(1.d0)
24  INTEGER, INTENT(in)                                    :: dx, dy
25  REAL(r_k), DIMENSION(dx,dy), INTENT(in)                :: ilon, ilat
26  REAL(r_k), DIMENSION(Nperx,Npery), INTENT(in)          :: fraclon, fraclat
27  REAL(r_k), INTENT(in)                                  :: lonv, latv, per
28  REAL(r_k), DIMENSION(2), INTENT(in)                    :: nxlon, nxlat
29  INTEGER, INTENT(in)                                    :: Nperx, Npery
30  INTEGER, DIMENSION(2), INTENT(out)                     :: ilonlat
31  REAL(r_k), INTENT(out)                                 :: mindiffLl
32! Local
33  REAL(r_k), DIMENSION(Nperx,Npery)                      :: difffraclonlat
34  REAL(r_k)                                              :: mindifffracLl
35  INTEGER, DIMENSION(2)                                  :: ilonlatfrac
36  INTEGER                                                :: ixbeg, ixend, iybeg, iyend
37  INTEGER                                                :: fracx, fracy
38  REAL(r_k)                                              :: fraclonv, fraclatv
39  REAL(r_k), ALLOCATABLE, DIMENSION(:,:)                 :: difflonlat, lon, lat
40 
41! Variables
42! ilon, ilat: original 2D matrices with the longitudes and the latitudes
43! lonv, latv: longitude and latitude to find
44! nxlon, nxlat: minimum and maximum longitude and latitude of the target lon,lat
45! per: fraction of the whole domain (as percentage)
46! Nper[x/y]: period (as fraction over 1) of the fractions of the original grid to use to explore
47! fraclon, fraclat: longitude and latitude fractional matricies to perform the first guess
48
49  fname = 'CoarselonlatFind'
50
51  IF (lonv < nxlon(1) .OR. lonv > nxlon(2)) THEN
52    PRINT *, TRIM(ErrWarnMsg('err'))
53    PRINT *,'  ' // TRIM(fname) // ': longitude outside data range!!'
54    PRINT *,'    given value:', lonv,' outside (',nxlon(1),' ,',nxlon(2),' )'
55    STOP
56  END IF
57  IF (latv < nxlat(1) .OR. latv > nxlat(2)) THEN
58    PRINT *, TRIM(ErrWarnMsg('err'))
59    PRINT *,'  ' // TRIM(fname) // ': latitude outside data range!!'
60    PRINT *,'    given value:', latv,' outside (',nxlat(1),' ,',nxlat(2),' )'
61    STOP
62  END IF
63
64  fracx = int(dx*per)
65  fracy = int(dy*per)
66
67!  PRINT *,'fraclon _______'
68!  PRINT *,fraclon
69
70!  PRINT *,'fraclat _______'
71!  PRINT *,fraclat
72
73! Fraction point
74  difffraclonlat = SQRT((fraclon-lonv)**2. + (fraclat-latv)**2.)
75  mindifffracLl = MINVAL(difffraclonlat)
76  ilonlatfrac = index2DArrayR(difffraclonlat, Nperx, Npery, mindifffracLl)
77
78!  PRINT *, 'mindifffracLl:', mindifffracLl, ' ilonlatfrac:', ilonlatfrac
79!  PRINT *, 'frac lon, lat:', fraclon(ilonlatfrac(1),ilonlatfrac(2)), ' ,',                            &
80!    fraclat(ilonlatfrac(1),ilonlatfrac(2))
81!  PRINT *, 'values lon, lat:', lonv, latv
82     
83! Providing fraction range
84  fraclonv = fraclon(ilonlatfrac(1),ilonlatfrac(2))
85  fraclatv = fraclat(ilonlatfrac(1),ilonlatfrac(2))
86
87  IF (fraclonv >= lonv .AND. fraclatv >= latv) THEN
88    IF (ilonlatfrac(1) > 0) THEN
89      ixbeg = (ilonlatfrac(1)-1)*fracx
90      ixend = ilonlatfrac(1)*fracx+1
91    ELSE
92      ixbeg = 0
93      ixend = fracx+1
94    END IF
95    IF (ilonlatfrac(2) > 0) THEN
96      iybeg = (ilonlatfrac(2)-1)*fracy
97      iyend = ilonlatfrac(2)*fracy+1
98    ELSE
99      iybeg = 0
100      iyend = fracy+1
101    END IF
102  ELSE IF (fraclonv < lonv .AND. fraclatv >= latv) THEN
103    IF (ilonlatfrac(1) < Nperx) THEN
104      IF (ilonlatfrac(1) /= 0) THEN
105        ixbeg = (ilonlatfrac(1)-1)*fracx
106        ixend = ilonlatfrac(1)*fracx+1
107      ELSE
108        ixbeg = 0
109        ixend = fracx+1
110      END IF
111    ELSE
112      ixbeg = Nperx*fracx
113      ixend = dx+1
114    END IF
115    IF (ilonlatfrac(2) > 0) THEN
116      iybeg = (ilonlatfrac(2)-1)*fracy
117      iyend = ilonlatfrac(2)*fracy+1
118    ELSE
119      iybeg = 0
120      iyend = fracy+1
121    END IF   
122  ELSE IF (fraclonv < lonv .AND. fraclatv < latv) THEN
123    IF (ilonlatfrac(1) < Nperx) THEN
124      IF (ilonlatfrac(1) /= 0) THEN
125        ixbeg = (ilonlatfrac(1)-1)*fracx
126        ixend = ilonlatfrac(1)*fracx+1
127      ELSE
128        ixbeg = 0
129        ixend = fracx+1
130      END IF
131    ELSE
132      ixbeg = Nperx*fracx
133      ixend = dx+1
134    ENDIF
135    IF (ilonlatfrac(2) < Npery) THEN
136      IF (ilonlatfrac(2) /= 0) THEN
137        iybeg = (ilonlatfrac(2)-1)*fracy
138        iyend = ilonlatfrac(2)*fracy+1
139      ELSE
140        iybeg = 0
141        iyend = fracy+1
142      END IF
143    ELSE
144      iybeg = Npery*fracy
145      iyend = dy+1
146    END IF
147  ELSE IF (fraclonv >= lonv .AND. fraclatv < latv) THEN
148    IF (ilonlatfrac(1) > 0) THEN
149      ixbeg = (ilonlatfrac(1)-1)*fracx
150      ixend = ilonlatfrac(1)*fracx+1
151    ELSE
152      ixbeg = 0
153      ixend = fracx+1
154    END IF
155    IF (ilonlatfrac(2) < Npery) THEN
156      IF (ilonlatfrac(2) /= 0) THEN
157        iybeg = (ilonlatfrac(2)-1)*fracy
158        iyend = ilonlatfrac(2)*fracy+1
159      ELSE
160        iybeg = 0
161        iyend = fracy+1
162      END IF
163    ELSE
164      iybeg = Npery*fracy
165      iyend = dy+1
166    END IF
167  END IF
168
169  IF (ALLOCATED(lon)) DEALLOCATE(lon)
170  ALLOCATE(lon(ixend-ixbeg+1, iyend-iybeg+1))
171  IF (ALLOCATED(lat)) DEALLOCATE(lat)
172  ALLOCATE(lat(ixend-ixbeg+1, iyend-iybeg+1))
173  IF (ALLOCATED(difflonlat)) DEALLOCATE(difflonlat)
174  ALLOCATE(difflonlat(ixend-ixbeg+1, iyend-iybeg+1))
175
176  lon = ilon(ixbeg:ixend,iybeg:iyend)
177  lat = ilat(ixbeg:ixend,iybeg:iyend)
178
179!  print *,'lon _______'
180!  print *,lon
181!  print *,'lat _______'
182!  print *,lat
183
184! Find point
185  difflonlat = SQRT((lon-lonv)**2. + (lat-latv)**2.)
186  mindiffLl = MINVAL(difflonlat)
187  ilonlat = index2DArrayR(difflonlat, ixend-ixbeg+1, iyend-iybeg+1, mindiffLl)
188
189  ilonlat(1) = ilonlat(1) + ixbeg
190  ilonlat(2) = ilonlat(2) + iybeg
191
192!  PRINT *,'mindiffLl:', mindiffLl, ' ilatlon:', ilatlon
193!  PRINT *,'lon, lat:', lon(ilonlat(1),ilonlat(2)), ' ,', lat(ilonlat(1),ilonlat(2))
194
195  RETURN
196
197END SUBROUTINE CoarselonlatFind
198
199SUBROUTINE CoarseInterpolate(dimx, dimy, projlon, projlat, Ninpts, lonvs, latvs, percen, mindiff,     &
200  ivar, newvar, newvarin, newvarinpt, newvarindiff, ncid)
201! Subroutine which finds the closest grid point within a projection throughout a first guest
202!   approche from percentages of the whole domain
203
204  USE module_generic
205
206  IMPLICIT NONE
207
208!  INTEGER, PARAMETER                                     :: r_k = KIND(1.d0)
209  INTEGER, INTENT(in)                                    :: dimx, dimy
210  REAL(r_k), DIMENSION(dimx,dimy), INTENT(in)            :: projlon, projlat
211  INTEGER, INTENT(in)                                    :: Ninpts
212  REAL(r_k), DIMENSION(Ninpts), INTENT(in)               :: ivar, lonvs, latvs
213  REAL(r_k)                                              :: mindiff, percen
214  INTEGER, INTENT(in)                                    :: ncid
215  REAL(r_k), DIMENSION(dimx,dimy), INTENT(inout)         :: newvar
216  INTEGER, DIMENSION(dimx,dimy), INTENT(inout)           :: newvarin
217  INTEGER, DIMENSION(Ninpts), INTENT(inout)              :: newvarinpt
218  REAL(r_k), DIMENSION(Ninpts), INTENT(inout)            :: newvarindiff
219! Local
220  INTEGER                                                :: iv
221  INTEGER, DIMENSION(2)                                  :: ilonlat
222  REAL(r_k)                                              :: mindiffLl
223  INTEGER                                                :: Ninpts1
224  REAL(r_k), DIMENSION(2)                                :: extremelon, extremelat
225  REAL(r_k), ALLOCATABLE, DIMENSION(:,:)                 :: fractionlon, fractionlat
226  INTEGER                                                :: fracdx, fracdy
227
228!!!!!!! Variables
229! dimx, dimy: dimension length of the target interpolation
230! proj[lon/lat]: longitudes and latitudes of the target interpolation
231! Ninpts: number of points to interpolate
232! [lon/lat]vs: longitudes and latitudes of the points to interpolate
233! mindiff: minimal accepted distance to the target point
234! percen: size (as percentage of the total domain) of the first guess portions to provide the first gues
235! ivar: values to localize in the target projection
236! newvar: localisation of the [lon/lat]vs point in the target projection
237! newvarin: number of point from the input data
238! newvarinpt: integer value indicating if the value has been already located (0: no, 1: yes)
239! newvarindiff: distance of point from the input data to the closest target point
240! ncid: netCDF output file id
241
242  fname = 'CoarseInterpolate'
243  Ninpts1 = Ninpts/100
244
245  extremelon = (/ MINVAL(projlon), MAXVAL(projlon) /)
246  extremelat = (/ MINVAL(projlat), MAXVAL(projlat) /)
247
248  fracdx = INT(dimx*percen)
249  fracdy = INT(dimy*percen)
250
251  IF (ALLOCATED(fractionlon)) DEALLOCATE(fractionlon)
252  ALLOCATE(fractionlon(fracdx, fracdy))
253  IF (ALLOCATED(fractionlat)) DEALLOCATE(fractionlat)
254  ALLOCATE(fractionlat(fracdx, fracdy))
255
256  fractionlon = projlon(::fracdx,::fracdy)
257  fractionlat = projlat(::fracdx,::fracdy)
258
259  DO iv=1,Ninpts
260    IF (newvarinpt(iv) == 0) THEN
261      CALL CoarselonlatFind(dimx, dimy, projlon, projlat, extremelon, extremelat, fractionlon,        &
262        fractionlat, lonvs(iv), latvs(iv), percen, fracdx, fracdy, ilonlat, mindiffLl)
263
264      IF (mindiffLl <= mindiff) THEN
265!        percendone(iv,Ninpts,0.5,'done:')
266
267        IF (ilonlat(1) >= 0 .AND. ilonlat(1) >= 0) THEN
268          newvar(ilonlat(1),ilonlat(2)) = ivar(iv)
269          newvarin(ilonlat(1),ilonlat(2)) = iv
270          newvarinpt(iv) = 1
271          newvarindiff(iv) = mindiffLl
272          PRINT *,'Lluis iv:', newvarin(ilonlat(1),ilonlat(2)), ' localized:', newvarinpt(iv),        &
273            ' values:', newvar(ilonlat(1),ilonlat(2)), ' invalues:', ivar(iv), ' mindist:',           &
274            newvarindiff(iv), ' point:',ilonlat   
275        ELSE
276          PRINT *,TRIM(ErrWarnMsg('err'))
277          PRINT *,'  ' // TRIM(fname) // ': point iv:', iv, ' at', lonvs(iv), ' ,', latvs(iv),        &
278            ' not relocated !!'
279          PRINT *,'    mindiffl:', mindiffLl, ' ilon:', ilonlat(1), ' ilat:', ilonlat(2)
280          STOP
281        END IF
282
283!        IF (MOD(iv,Ninpts1) == 0) newnc.sync()
284      ELSE
285        PRINT *,TRIM(ErrWarnMsg('err'))
286        PRINT *,'  ' // TRIM(fname) // ': for point #', iv,' lon,lat in incomplet map:', lonvs(iv),   &
287          ' ,', latvs(iv), ' there is not a set of lon,lat in the completed map closer than: ',       &
288          mindiff, ' !!'
289        PRINT *,'    found minimum difference:', mindiffLl
290        STOP
291      END IF
292    END IF
293  END DO
294
295END SUBROUTINE CoarseInterpolate
Note: See TracBrowser for help on using the repository browser.