1 | SUBROUTINE grid_correspondance |
---|
2 | !-------------------------------------------------------------- |
---|
3 | ! Determine the grid correspondance between initial grid |
---|
4 | ! and final grid and calculates the weighting function based |
---|
5 | ! on the corresponding grid-cell area. |
---|
6 | ! |
---|
7 | ! It is assumed that the initial grid is defined as: |
---|
8 | ! |
---|
9 | ! ---------- lat_edge(j+1) |
---|
10 | ! | (i,j) | |
---|
11 | ! | + | |
---|
12 | ! | | |
---|
13 | ! ---------- lat_edge(j) |
---|
14 | ! | | |
---|
15 | ! lon_edge(i) lon_edge(i+1) |
---|
16 | ! |
---|
17 | !-------------------------------------------------------------- |
---|
18 | USE final_grid_lmdz |
---|
19 | USE grid |
---|
20 | USE correspondance |
---|
21 | IMPLICIT NONE |
---|
22 | |
---|
23 | !-------------------------------------------------------------- |
---|
24 | ! ... Local variables |
---|
25 | !-------------------------------------------------------------- |
---|
26 | INTEGER :: error |
---|
27 | INTEGER :: i, j |
---|
28 | INTEGER :: oldi, oldj |
---|
29 | INTEGER :: from_lon, to_lon |
---|
30 | INTEGER :: from_lat, to_lat |
---|
31 | INTEGER, SAVE :: countmax = 200000 |
---|
32 | |
---|
33 | REAL :: lonw, lone, lats, latn |
---|
34 | REAL :: d2r, pi |
---|
35 | REAL :: area |
---|
36 | |
---|
37 | pi = 4.*ATAN(1.) |
---|
38 | d2r = pi/180. |
---|
39 | |
---|
40 | PRINT *, 'Grid correspondance ...' |
---|
41 | |
---|
42 | ! ... Loop over countmax if needed |
---|
43 | outer : DO |
---|
44 | |
---|
45 | ! ... Free space if needed and allocate correspondance array |
---|
46 | IF ( ALLOCATED(corr) ) DEALLOCATE(corr) |
---|
47 | ALLOCATE(corr(countmax), STAT=error) |
---|
48 | IF (error /= 0) STOP 'Space requested not possible' |
---|
49 | |
---|
50 | count = 0 |
---|
51 | |
---|
52 | ! ... loop over new longitudes |
---|
53 | ! print*,'nlon_len',nlon_len |
---|
54 | ! print*,'lon_len',lon_len |
---|
55 | ! print*,'nlon_edge',nlon_edge |
---|
56 | ! print*,'lon_edge',lon_edge |
---|
57 | ! print*,'nlat_len',nlat_len |
---|
58 | ! print*,'lat_len',lat_len |
---|
59 | ! print*,'nlat_edge',nlat_edge |
---|
60 | ! print*,'lat_edge',lat_edge |
---|
61 | DO i = 1, nlon_len |
---|
62 | |
---|
63 | DO oldi = 1, lon_len |
---|
64 | from_lon = oldi |
---|
65 | IF (nlon_edge(i) <= lon_edge(oldi+1)) EXIT |
---|
66 | END DO |
---|
67 | DO oldi = from_lon, lon_len |
---|
68 | to_lon = oldi |
---|
69 | IF (nlon_edge(i+1) <= lon_edge(oldi+1)) EXIT |
---|
70 | END DO |
---|
71 | |
---|
72 | ! ... loop over new latitudes |
---|
73 | DO j = 1, nlat_len |
---|
74 | |
---|
75 | DO oldj = 1, lat_len |
---|
76 | from_lat = oldj |
---|
77 | IF (nlat_edge(j+1) > lat_edge(oldj) .AND. nlat_edge(j) < lat_edge(oldj+1) ) EXIT |
---|
78 | from_lat = 0 |
---|
79 | END DO |
---|
80 | IF ( from_lat == 0 ) CYCLE |
---|
81 | DO oldj = from_lat, lat_len |
---|
82 | IF (nlat_edge(j+1) > lat_edge(oldj) .AND. nlat_edge(j) < lat_edge(oldj+1) ) THEN |
---|
83 | to_lat = oldj |
---|
84 | CYCLE |
---|
85 | END IF |
---|
86 | EXIT |
---|
87 | END DO |
---|
88 | |
---|
89 | ! ... loop over old longitudes and latitudes |
---|
90 | DO oldi = from_lon, to_lon |
---|
91 | DO oldj = from_lat, to_lat |
---|
92 | |
---|
93 | lonw = MAX(nlon_edge(i),lon_edge(oldi)) |
---|
94 | lone = MIN(nlon_edge(i+1),lon_edge(oldi+1)) |
---|
95 | lats = MAX(nlat_edge(j),lat_edge(oldj)) |
---|
96 | latn = MIN(nlat_edge(j+1),lat_edge(oldj+1)) |
---|
97 | |
---|
98 | ! Validity checks |
---|
99 | IF ( (sin(latn*d2r)-sin(lats*d2r)) <0) THEN |
---|
100 | print *, 'WARNING: latn < lats ', latn, lats |
---|
101 | END IF |
---|
102 | IF ( lone < lonw ) THEN |
---|
103 | print *, 'WARNING: lone < lonw ', lone, lonw |
---|
104 | END IF |
---|
105 | |
---|
106 | count = count + 1 |
---|
107 | ! ... If needed increase countmax, reset, and restart |
---|
108 | IF (count > countmax) THEN |
---|
109 | PRINT *, 'count (=', count, ') larger than countmax (=', countmax, ')' |
---|
110 | PRINT *, 'Increase countmax to ', countmax*2, ' and restart' |
---|
111 | countmax = countmax * 2 |
---|
112 | CYCLE outer |
---|
113 | END IF |
---|
114 | |
---|
115 | area = (sin(latn*d2r)-sin(lats*d2r))*(lone-lonw)*d2r |
---|
116 | |
---|
117 | corr(count)%olon = oldi |
---|
118 | corr(count)%olat = oldj |
---|
119 | corr(count)%nlon = i |
---|
120 | corr(count)%nlat = j |
---|
121 | corr(count)%wght = area / area_out(i,j) |
---|
122 | |
---|
123 | END DO |
---|
124 | END DO |
---|
125 | |
---|
126 | END DO |
---|
127 | |
---|
128 | END DO |
---|
129 | |
---|
130 | EXIT outer |
---|
131 | END DO outer |
---|
132 | |
---|
133 | ! PRINT *, ' OK.' |
---|
134 | |
---|
135 | END SUBROUTINE grid_correspondance |
---|
136 | |
---|