New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
compute_sections.f90 in branches/2011/dev_r2802_MERCATOR10_diadct/NEMOGCM/TOOLS/SECTIONS_DIADCT/src – NEMO

source: branches/2011/dev_r2802_MERCATOR10_diadct/NEMOGCM/TOOLS/SECTIONS_DIADCT/src/compute_sections.f90 @ 2948

Last change on this file since 2948 was 2948, checked in by cbricaud, 13 years ago

corrections after review

  • Property svn:executable set to *
File size: 34.6 KB
Line 
1MODULE compute_sections
2   !!=====================================================================
3   !!                       ***  MODULE  diadct  ***
4   !! Ocean diagnostics: Compute the transport through a section
5   !!
6   !! History: 2011: Clement Bricaud, Mercator-Ocean
7   !!
8   !!===============================================================
9   !! * Modules used
10   USE declarations 
11   USE sections_tools
12 
13   IMPLICIT NONE
14   PRIVATE
15
16   !! * Routine accessibility
17   PUBLIC compsec
18   
19   !! * Module variables
20 
21CONTAINS
22
23  SUBROUTINE compsec(jsec,sec,lkdebug)         
24     !!---------------------------------------------------------------------
25     !!                     ***  ROUTINE compsec  ***
26     !!
27     !!  ** Purpose : Compute the series of mesh points that represent the section
28     !!               defined by its extremities.
29     !!
30     !!  ** Method  :
31     !!          I.   Find which cells of the mesh the section is crossing
32     !!          II.  Classification of the intersections mesh/section
33     !!                  -first  classification west to east         
34     !!                  -second classification south to north
35     !!          III. Find extremities of section in the mesh
36     !!          IV.  Find the series of mesh points that form the section
37     !!  ** Input: sec : the section to compute
38     !!
39     !!  ** Output:
40     !!---------------------------------------------------------------------
41     !! * Arguments
42     INTEGER,INTENT(IN)           :: jsec    ! number of the section
43     TYPE(SECTION), INTENT(INOUT) :: sec     ! information about the section
44     LOGICAL ,INTENT(IN)          :: lkdebug ! debug or not debug this section
45
46     !! * Local variables
47     INTEGER :: &
48        ji,jj            ,     & ! dummy loop argument
49        jseg             ,     & ! loop on segments that form the section   
50        nb_inmesh        ,     & ! number of intersections between the section and the mesh
51        nmesh                    ! number of cells in processor domain
52     INTEGER :: itest , jtest    ! dummy integer
53     REAL(wp),SAVE :: zdistmesh  ! Taller cell of the mesh in ocean in degrees
54     REAL(wp)      :: &
55        zdistEst   , zdistNorth , zdistWest , zdistSouth ,  &! temporary scalars
56        zdistEst2  , zdistNorth2, zdistWest2, zdistSouth2,  &! temporary scalars
57        zdistEst3  , zdistNorth3, zdistWest3, zdistSouth3,  &! temporary scalars
58        zdistFirst , zdistLast  , zdistref  ,               &! temporary scalars
59        zdistante  , zdistante2 , zdistnew  , zdistnew2  ,  &! temporary scalars
60        zdeltai    , zdeltaj                                 ! temporary scalars
61     LOGICAL :: & 
62        ll_overlap_sec_left = .FALSE. , ll_overlap_sec_right = .FALSE. ,&! temporary logical
63        ll_date_domain_left = .FALSE. , ll_date_domain_right = .FALSE. ,&! temporary logical
64        ll_overlap_sec      = .FALSE. , ll_date_domain       = .FALSE. ,&! temporary logical
65        ll_test             = .FALSE.                                    ! temporary logical
66     LOGICAL :: lest, lwest, lnorth, lsouth
67     LOGICAL :: l_oldmethod
68     CHARACTER(len=120) :: cltmp
69     TYPE(COORD_SECTION) :: &
70        coord_a   , coord_b  , coord_d ,  coord_t ,               &!temporary point (long/lat)
71        coordFirst, coordLast, coordTemp                           !        "
72     TYPE(COORD_SECTION), DIMENSION(nb_point_max) :: coordSec      !intersections mesh/section   
73     TYPE(POINT_SECTION) :: &
74        endingPoint, prevPoint , nextPoint,           &!temporary point (I/J)
75        SouthPoint , NorthPoint, EstPoint , WestPoint  !        "
76     !!---------------------------------------------------------------------
77     IF( jsec==1 )THEN
78          PRINT*,'                '
79          PRINT*,'COMPUTE SECTIONS'
80          PRINT*,'----------------'
81     ENDIF
82
83     !debug
84     CALL write_debug(jsec,'compute section:')
85     CALL write_debug(jsec,'================')
86
87
88     !==================!
89     !0. Initializations!
90     !==================!
91     
92     nmesh       = jpi*jpj          ! number of cells in processor domain
93     nb_inmesh   = 0                ! initialize number of intersections between section and the mesh
94     zdistEst  =0. ; zdistNorth=0. ; zdistWest=0. ; zdistSouth=0.   ! temporary scalars
95     zdistFirst=0. ; zdistLast =0.                                  ! temporary scalars
96     zdistante =0. ; zdistante2=0. ; zdistnew=0.  ; zdistnew2=0.    ! temporary scalars
97     zdeltai=0.    ; zdeltaj=0. 
98     coord_a     = COORD_SECTION( 0., 0. ) ; coord_b    = COORD_SECTION( 0., 0. )
99     coord_d     = COORD_SECTION( 0., 0. ) ; coord_t    = COORD_SECTION( 0., 0. ) 
100     coordFirst  = COORD_SECTION( 0., 0. ) ; coordLast  = COORD_SECTION( 0., 0. )
101     coordTemp   = COORD_SECTION( 0., 0. ) ; coordSec   = COORD_SECTION( 0., 0. )
102     endingPoint = POINT_SECTION( -1, -1 ) 
103     prevPoint   = POINT_SECTION( -1, -1 ) ; nextPoint   = POINT_SECTION( -1, -1 )
104     SouthPoint  = POINT_SECTION( -1, -1 ) ; NorthPoint  = POINT_SECTION( -1, -1 )
105     EstPoint    = POINT_SECTION( -1, -1 ) ; WestPoint   = POINT_SECTION( -1, -1 )
106
107     IF( jsec == 1 )THEN
108        !Find the taller cell of the mesh in ocean (used in compsec),
109        !unit= degrees
110        zdistmesh=0.
111        DO jj=2,jpj
112           DO ji=2,jpi
113              IF( zdistmesh .LT. &
114                 (e1t(ji,jj)/(cos(gphit(ji,jj))*110000))  ) THEN
115                 zdistmesh = e1t(ji,jj)/(cos(gphit(ji,jj))*110000)
116              ENDIF
117           ENDDO
118        ENDDO
119   
120     ENDIF
121
122     !debug
123     WRITE(cltmp,'(A10,f10.3)')'zdistmesh = ',zdistmesh
124     CALL write_debug(jsec,cltmp)
125
126     !===========================================================!   
127     !I. Find which cells of the mesh the section is  crossing  !
128     !===========================================================!
129
130     !loop on the mesh
131     DO jj=2,jpj
132        DO ji=2,jpi
133           !-----------------------------------------------------------
134           !For each cell of the mesh, we find the intersection between
135           !the section and the cell as described below:
136           !The cell is defined by 4 points A B C D
137           !and the section by S1 and S2
138           !
139           !Section\    ________
140           !        \ B|        |
141           !         \ |        |
142           !          \|one cell|
143           !      .....\        |
144           !           |\       |
145           !           | \      |
146           !          A|__\_____|D
147           !       ........\
148           !----------------\-------------------------------------------
149
150           !definition of points A,B,D (we don't need C)
151           coord_a = COORD_SECTION( glamf(ji-1,jj-1) ,gphif(ji-1,jj-1) )
152           coord_b = COORD_SECTION( glamf(ji-1,jj)   ,gphif(ji-1,jj)   )
153           coord_d = COORD_SECTION( glamf(ji,jj-1)   ,gphif(ji,jj-1)   )
154 
155           !size of the cell
156           zdeltai= glamf(ji-1,jj) - glamf(ji-1,jj-1) !zdeltai=[AB]
157           zdeltaj= gphif(ji,jj-1) - gphif(ji-1,jj-1) !zdeltaj=[AD]
158           
159           IF( ABS(zdeltai) .LE. 2.*zdistmesh .OR.  & 
160               ABS(zdeltaj) .LE. 2.*zdistmesh )THEN           
161 
162              !intersection section/[AB]=?
163              coordTemp     = intersec(sec,coord_a,coord_b)     !compute intersection
164 
165              IF( coordTemp%lon .NE. -9999 )THEN
166                 IF( nb_inmesh+1 .GT. nb_point_max )THEN
167                    PRINT*,"WARNING diadct: nb_point_max needs to be greater than ", nb_inmesh
168                 ELSE   
169                    nb_inmesh=nb_inmesh+1             
170                    coordSec(nb_inmesh) = coordTemp    !store the intersection's coordinates
171
172                    !We need to know if the section crosses the overlapping band.
173
174                    !Fist we look if there is an intersection mesh/section
175                    !just on the left of the overlapping band:
176                    IF( coordTemp%lon .GT. glamf(1,1)-5  .AND. coordTemp%lon .LT. glamf(1,1) ) & 
177                       & ll_overlap_sec_left  = .TRUE.
178                    !And we look if there is an intersection mesh/section
179                    !just on the right of the overlapping band:
180                    IF( coordTemp%lon .GT. glamf(jpi,1) .AND. coordTemp%lon .LT. glamf(1,1)+5 ) &
181                       & ll_overlap_sec_right = .TRUE.
182                 ENDIF
183              ENDIF
184             
185              !intersection section/[AD]=?
186              coordTemp=intersec(sec,coord_a,coord_d)       !compute intersection
187              coordTemp%lon = coordTemp%lon !* zmask(ji,jj)-9999.*(1-zmask(ji,jj))
188              coordTemp%lat = coordTemp%lat !* zmask(ji,jj)-9999.*(1-zmask(ji,jj))
189
190              IF( coordTemp%lon .NE. -9999 )THEN
191                 IF( nb_inmesh+1 .GT. nb_point_max )THEN
192                    PRINT*, "WARNING diadct: nb_point_max needs to be greater than ", nb_inmesh
193                 ELSE 
194                    nb_inmesh=nb_inmesh+1
195                    coordSec(nb_inmesh)=coordTemp
196                   
197                    !We need to know if the section crosses the overlapping band:
198                    !same test as above
199                    IF( coordTemp%lon .GE. glamf(1,1)-3  .AND. coordTemp%lon .LE. glamf(1,1) ) &
200                         & ll_overlap_sec_left  = .TRUE.
201                    IF( coordTemp%lon .GE. glamf(jpi,1) .AND. coordTemp%lon .LE. glamf(jpi,1)+3) & 
202                         & ll_overlap_sec_right = .TRUE. 
203                 ENDIF
204              ENDIF
205 
206           ENDIF
207         
208           !We need to know if the domain crosses the date line:
209           !Fist, we search a mesh point that is just one the left of date line:
210           IF( glamf(ji-1,jj-1) .GT.  175 .AND. glamf(ji-1,jj-1) .LT.  180 ) &
211              & ll_date_domain_left = .TRUE.
212           !And we search a mesh point that is just one the right of date line:
213           IF( glamf(ji-1,jj-1) .GT. -180 .AND. glamf(ji-1,jj-1) .LT. -175 ) & 
214              & ll_date_domain_right = .TRUE.
215 
216        ENDDO
217     ENDDO !End of the loop on the mesh
218
219 
220     !Crossing section/overlapping band (we need to know it for later):
221     !-----------------------------------------------------------------
222     !If there is one intersection mesh/section just on the left of
223     !the overlapping band (ll_overlap_sec_left  = .TRUE.)
224     !AND there is one just the right of the overlapping band
225     !(ll_overlap_sec_right  = .TRUE.),
226     !so the section crosses the overlapping band.
227     ll_overlap_sec = ll_overlap_sec_left .AND. ll_overlap_sec_right 
228
229     !Crossing of the domain and the date line (we need to know it for later):
230     !------------------------------------------------------------------------
231     !If there is one point of the domain that is just on the left of the date line
232     !(ll_date_domain_left = .TRUE.) AND one point that is just on the right of the
233     !date line (ll_date_domain_right = .TRUE. )
234     !So the domain crosses the date line:
235     ll_date_domain = ll_date_domain_left .AND. ll_date_domain_right
236
237     !=====================================================!
238     ! II. Classification of the intersections mesh/section!
239     !=====================================================!
240
241     !   -first classification  west to east         
242     !   -second classification south to north       
243     !CAUTION: routine qcksrt doesn't work in the same way if the section
244     !and the domain crosse the date line (sec%ll_date_line=T and ll_date_domain=T)
245
246     IF( sec%ll_date_line .AND. ll_date_domain )THEN
247
248        !we add 360° to negative longitudes to have a good classification
249        DO jseg=1,nb_inmesh
250           IF( coordSec(jseg)%lon .LT. 0 ) coordSec(jseg)%lon=coordSec(jseg)%lon+360.
251        ENDDO
252        IF( sec%coordSec(1)%lon .NE. sec%coordSec(2)%lon ) THEN
253           CALL qcksrt(coordSec(:)%lon,coordSec(:)%lat,nb_inmesh)
254        ELSE
255           CALL qcksrt(coordSec(:)%lat,coordSec(:)%lon,nb_inmesh)
256        ENDIF
257        DO jseg=1,nb_inmesh
258           IF( coordSec(jseg)%lon .GT. 180 ) coordSec(jseg)%lon=coordSec(jseg)%lon-360.
259        ENDDO
260
261     ELSE     
262
263        IF( sec%coordSec(1)%lon .NE. sec%coordSec(2)%lon )THEN
264           CALL qcksrt(coordSec(:)%lon,coordSec(:)%lat,nb_inmesh)
265        ELSE
266           CALL qcksrt(coordSec(:)%lat,coordSec(:)%lon,nb_inmesh)
267        ENDIF
268
269     ENDIF
270
271     !debug
272     WRITE(cltmp,'(A20,i3.3)')'number intersections = ',nb_inmesh ; CALL write_debug(jsec,cltmp)
273     CALL write_debug(jsec,'List of intersections between grid and section: ')
274     DO jseg=1,nb_inmesh
275        WRITE(cltmp,'(i4.4,1X,2(f8.3,1X) )')jseg,coordSec(jseg) ;  CALL write_debug(jsec,cltmp)
276     ENDDO
277 
278     !=====================================================!
279     ! III. Find extremities of section in the mesh        !
280     !=====================================================!
281     !we can find section's extremities in the mesh only if
282     !there is intersection between section and mesh (nb_inmesh .ne. 0)
283
284     IF( nb_inmesh .ne. 0 )THEN
285        coordFirst       = coordSec(1)
286        coordLast        = coordSec(nb_inmesh) 
287        sec%nb_point     = nb_inmesh
288        sec%listPoint(1) = POINT_SECTION(-1,-1)
289        zdistante        = 1000.
290        zdistante2       = 1000.
291
292        !First, we find the point of the mesh that is the closest
293        !to the first intersection section/mesh (=coordFirst=coordSec(1)):
294        !this point will be called sec%listPoint(1).
295        !Then, we find the point of the mesh that is the closest
296        !to the last intersection section/mesh (coordLast=coordSec(nb_inmesh))
297        !this point will be called endingPoint.
298
299        DO jj=1,jpj
300           DO ji=1,jpi 
301              coord_t=COORD_SECTION(glamf(ji,jj),gphif(ji,jj))
302              zdistFirst = distance2(coord_t,coordFirst)
303              zdistLast = distance2(coord_t,coordLast)
304              IF( zdistFirst .LT. zdistmesh .AND. zdistFirst .LT. zdistante )THEN
305                 sec%listPoint(1) = POINT_SECTION(ji,jj)
306                 zdistante=zdistFirst
307              ENDIF
308              IF( zdistLast .LT. zdistmesh .AND. zdistLast .LT. zdistante2 )THEN
309                 endingPoint = POINT_SECTION(ji,jj)
310                 zdistante2=zdistLast
311              ENDIF
312           ENDDO
313        ENDDO
314
315        IF( sec%listPoint(1)%I == endingPoint%I .AND. sec%listPoint(1)%J == endingPoint%J )THEN
316           sec%listPoint(1) = POINT_SECTION(-1,-1)
317           endingPoint      = POINT_SECTION(-1,-1)
318           coordFirst       = coordSec(1)
319           coordLast        = coordSec(2)
320           sec%nb_point     = 0
321        ENDIF
322
323     ELSE
324        !If there is no intersection section/mesh
325        sec%listPoint(1) = POINT_SECTION(-1,-1)
326        endingPoint      = POINT_SECTION(-1,-1)
327        coordFirst       = coordSec(1)
328        coordLast        = coordSec(2)
329        sec%nb_point     = 0
330     ENDIF 
331
332     !debug
333     CALL write_debug(jsec,"extremities of section in the grid : ")
334     ji=sec%listPoint(1)%I ; jj=sec%listPoint(1)%J
335     IF( sec%nb_point .ne. 0 )THEN
336        ji=sec%listPoint(1)%I ; jj=sec%listPoint(1)%J
337        WRITE(cltmp,'(A15,X,i4.4,X,i4.4,X,f8.3,X,f8.3)')'First point: ',sec%listPoint(1),glamf(ji,jj),gphif(ji,jj) 
338        CALL write_debug(jsec,cltmp)
339        ji=endingPoint%I ; jj=endingPoint%J
340        WRITE(cltmp,'(A15,X,i4.4,X,i4.4,X,f8.3,X,f8.3)')'Last  point: ',endingPoint,glamf(ji,jj),gphif(ji,jj)
341        CALL write_debug(jsec,cltmp)
342        !
343        coord_a=pointToCoordF(sec%listPoint(1)) ; coord_b=pointToCoordF(endingPoint)
344        ll_test = .FALSE.
345        IF ( ll_date_domain .AND. ABS( coord_a%lon - coord_b%lon ).GT. 180) ll_test= .TRUE.
346        zdistante=distance2(coord_a,coord_b ,ll_test )
347        WRITE(cltmp,'(A20,f10.3)' )'distance between IJ-extremities : ',zdistante
348        CALL write_debug(jsec,cltmp)
349        !
350        CALL write_debug(jsec,"Initial extremities : ") 
351        WRITE(cltmp,'( 2(f9.3),A3,2(f9.3) )')coordFirst,'---',coordLast
352        CALL write_debug(jsec,cltmp)
353        ll_test = .FALSE.
354        IF( ll_date_domain .AND. ABS(coordFirst%lon - coordLast%lon).GT. 180)ll_test= .TRUE.
355        zdistante=distance2(coordFirst,coordLast,ll_test)
356        WRITE(cltmp,'(A30,f10.3)')' distance between initial extremities : ',zdistante
357        CALL write_debug(jsec,cltmp)
358        CALL write_debug(jsec,"                  ")
359     ELSE
360        WRITE(cltmp,'(A50)' )"no intersection between section and mesh"
361     ENDIF
362
363     !==========================================================!
364     ! IV. Find the series of mesh points that form the section !
365     !==========================================================!
366     CALL write_debug(jsec,"Find the serie of mesh's points that form the section")
367
368     IF( sec%nb_point .ne. 0 )THEN
369
370        !The series of mesh points that form the section will 'link'
371        !sec%listPoint(1) to endingPoint: it will be stored in
372        !sec%listPoint(jseg)
373        !
374        !We take place on the first point (sec%listPoint(1))
375        ! a.  We find the 4 adjacent points (North, South, East, West)
376        ! b.  Compute distance between current point and endingPoint
377        ! c.  Compute distance between the 4 adjacent points and endingPoint
378        ! d.  Select the points which are closer to end-point than current point
379        ! e.1 If at least one point is selected, select the point which is closest to original section among selected points
380        ! e.2 If no point is selected, select the point which is the closest to end-point
381        ! f. save next point and direction of velocity.
382        ! g. Save nextPoint and go to nextPoint
383        !
384        !We get out of this loop if:
385        !    - we are on endingPoint
386        !    - the number of points (jseg) that link sec%listPoint(1) to endingPoint is
387        !      twice greater than number of section/mesh intersection (nb_inmesh):
388        !      it could be possible if thr algorithm can't link endingPoint (bug).
389
390        !initialize distnew value (with distance between section's extremities)
391        zdistnew  = distance2(coordFirst,coordLast,sec%ll_date_line) 
392        prevPoint  = POINT_SECTION(0,0)
393        jseg       = 1
394
395        DO WHILE ( (  sec%listPoint(jseg)%I .NE.  endingPoint%I    &
396                 .OR. sec%listPoint(jseg)%J .NE. endingPoint%J   ) &
397                 .AND. jseg .LT. 500 .AND. sec%listPoint(jseg)%I .GT. 0  )         
398   
399           ! a. find the 4 adjacent points (North, South, East, West)
400           !---------------------------------------------------------
401           SouthPoint = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J-1)
402           NorthPoint = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J+1)
403           WestPoint  = POINT_SECTION(sec%listPoint(jseg)%I-1,sec%listPoint(jseg)%J)
404           EstPoint   = POINT_SECTION(sec%listPoint(jseg)%I+1,sec%listPoint(jseg)%J)
405
406           !debug
407           CALL write_debug(jsec,"---------------")
408           WRITE(cltmp,100)'Current points: ',sec%listPoint(jseg), & 
409                         glamf(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J), &
410                         gphif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J)
411           CALL write_debug(jsec,cltmp)
412           CALL write_debug(jsec,"E/W/N/S points")
413           WRITE(cltmp,102)EstPoint,WestPoint,NorthPoint,SouthPoint
414           CALL write_debug(jsec,cltmp)
415           itest=MIN(MAX(EstPoint%I,0),jpi+1) ; jtest=MIN(MAX(EstPoint%J,0),jpj+1)
416           IF( itest .NE. 0 .AND. itest .NE. jpi+1 .AND. jtest .NE. 0 .AND. jtest .NE. jpj+1 )THEN
417              WRITE(cltmp,101)'Est',glamf(itest,jtest),gphif(itest,jtest)
418              CALL write_debug(jsec,cltmp)
419           ELSE
420              CALL write_debug(jsec,"Est point out of domain")
421           ENDIF
422           !
423           itest=MIN(MAX(WestPoint%I,0),jpi+1) ; jtest=MIN(MAX(WestPoint%J,0),jpj+1)
424           IF( itest .NE. 0 .AND. itest .NE. jpi+1 .AND. jtest .NE. 0 .AND. jtest .NE. jpj+1 )THEN
425              WRITE(cltmp,101)'West',glamf(itest,jtest),gphif(itest,jtest)
426              CALL write_debug(jsec,cltmp)
427           ELSE
428              CALL write_debug(jsec,"West point out of domain")
429           ENDIF
430           !
431           itest=MIN(MAX(NorthPoint%I,0),jpi+1) ; jtest=MIN(MAX(NorthPoint%J,0),jpj+1)
432           IF( itest .NE. 0 .AND. itest .NE. jpi+1 .AND. jtest .NE. 0 .AND. jtest .NE. jpj+1 )THEN
433              WRITE(cltmp,101)'North',glamf(itest,jtest),gphif(itest,jtest)
434              CALL write_debug(jsec,cltmp)
435           ELSE
436              CALL write_debug(jsec,"North point out of domain")
437           ENDIF
438           !
439           itest=MIN(MAX(SouthPoint%I,0),jpi+1) ; jtest=MIN(MAX(SouthPoint%J,0),jpj+1)
440           IF( itest .NE. 0 .AND. itest .NE. jpi+1 .AND. jtest .NE. 0 .AND. jtest .NE. jpj+1 )THEN
441              WRITE(cltmp,101)'South',glamf(itest,jtest),gphif(itest,jtest)
442              CALL write_debug(jsec,cltmp)
443           ELSE
444              CALL write_debug(jsec,"South point out of domain")
445           ENDIF
446           !
447           !
448100 FORMAT ( A15,2(i4.4," "),2(f8.3," ") )
449101 FORMAT ( A6,2(f7.3," "))
450102 FORMAT ( "E ",i4.4,' ',i4.4,"//W ",i4.4,' ',i4.4,"//N ",i4.4,' ',i4.4,"//S ",i4.4,' ',i4.4 )
451
452               
453           !Either we are at an end-point
454           !--------------------
455           IF(      SouthPoint%I==endingPoint%I .AND. SouthPoint%J==endingPoint%J )THEN
456               jseg = jseg+1 ; sec%listPoint(jseg) = SouthPoint
457           ELSE IF( NorthPoint%I==endingPoint%I .AND. NorthPoint%J==endingPoint%J )THEN
458               jseg = jseg+1 ; sec%listPoint(jseg) = NorthPoint
459           ELSE IF(  WestPoint%I==endingPoint%I .AND.  WestPoint%J==endingPoint%J )THEN
460               jseg = jseg+1 ; sec%listPoint(jseg) = WestPoint
461           ELSE IF(   EstPoint%I==endingPoint%I .AND.   EstPoint%J==endingPoint%J )THEN
462               jseg = jseg+1 ; sec%listPoint(jseg) = EstPoint
463
464           ELSE
465           !Else we are NOT on end-point
466           !------------------------
467
468              ! b. distance between current point and endingPoint
469              !--------------------------------------------------
470              zdistante = zdistnew
471
472              ! c. compute distance between the 4 adjacent points and endingPoint
473              !------------------------------------------------------------------
474              ! BE CAREFUL! When the domain crosses the date line (ll_date_domain):
475              !
476              ! When we will compute distances between W/E/S/N points and endingPoint,
477              ! we have to check if theses 4 lines crosses the date line
478              ! (test: ABS(coordTemp%lon - coordLast%lon).GT. 180)
479              !
480              ! If on of this 4 lines  crosses the date line, we have to add 360° its
481              ! extremities longitudes to compute the correct distance through the date line
482              ! and not around the Earth
483       
484              ! c.1 compute distWest: distance between west point and endingPoint
485              !----------------------
486              zdistWest2 = 99999999.9 ;  zdistWest3 = 99999999.9 
487              IF( sec%listPoint(jseg)%I .NE. 1 )THEN
488                 !When we are on the west side of the mesh (i=1),we can't go to the west.
489                 coordTemp = pointToCoordF(WestPoint) 
490                 ll_test = .FALSE.
491                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test = .TRUE.
492                 zdistWest2  = distance2(pointToCoordF(WestPoint) ,coordLast ,ll_test)
493              ENDIF
494
495              ! c.2 compute distEst: distance between east point and endingPoint
496              !---------------------
497              zdistEst2 = 99999999.9 ;  zdistEst3 = 99999999.9
498              IF( sec%listPoint(jseg)%I .EQ. jpi )THEN
499                 !We test if the current point is on the east side of the mesh
500                 ! The method is done such that we go toward east to link
501                 ! sec%listPoint(1) to endingPoint.
502                 ! So, if the section crosses the overlapping band (ll_overlap_sec=T),
503                 ! we won't have to stop if the current point is on the EAST side of the mesh:
504                 ! we have to follow the construction of the section on the
505                 ! WEST side of the mesh
506                 IF( ll_overlap_sec  )THEN
507                    !section crosses the overlapping band
508                    !So EastPoint is on the west side of the mesh
509                    EstPoint = POINT_SECTION(3,sec%listPoint(jseg)%J)
510                    zdistEst2= distance2(pointToCoordF(EstPoint)  ,coordLast ,.FALSE.)
511                 ENDIF
512              ELSE
513                 coordTemp = pointToCoordF(EstPoint) 
514                 ll_test = .FALSE.
515                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test= .TRUE.
516                 zdistEst2 = distance2(pointToCoordF(EstPoint)  ,coordLast ,ll_test )
517              ENDIF
518
519              ! c.3 compute distSouth: distance between south point and endingPoint
520              !-----------------------
521              zdistSouth2 = 99999999.9 ; zdistSouth3 = 99999999.9
522              IF( sec%listPoint(jseg)%J .NE. 1 )THEN
523                 !When we are on the south side of the mesh (j=1),we can't go to the south.
524                 coordTemp=pointToCoordF(SouthPoint)
525                 ll_test = .FALSE.
526                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test= .TRUE.
527                    zdistSouth2 = distance2(pointToCoordF(SouthPoint),coordlast ,ll_test )
528              ENDIF
529
530              ! c.4 compute distNorth: distance between north and endingPoint
531              !-----------------------
532              zdistNorth2 = 99999999.9 ; zdistNorth3 = 99999999.9 
533              IF( sec%listPoint(jseg)%J .NE. jpj )THEN
534                 !When we are on the north side of the mesh (j=jpj),we can't go to the south.
535                 coordTemp=pointToCoordF(NorthPoint)
536                 ll_test = .FALSE.
537                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test= .TRUE.
538                 zdistNorth2 = distance2(pointToCoordF(NorthPoint),coordlast ,ll_test )
539              ENDIF
540
541              ! d. select the points which are closer to end-point than current point
542              !----------------------------------------------------------------------
543              zdistref=distance2(pointToCoordF(sec%listPoint(jseg)),coordlast ,ll_test )
544              WRITE(cltmp,'( A56,f10.3 )' )'distance between actual point and last point: zdistref = ',zdistref
545              CALL write_debug(jsec,cltmp)
546              lest   = .FALSE. ; IF( zdistEst2   .LE. zdistref ) lest  = .TRUE.
547              lwest  = .FALSE. ; IF( zdistwest2  .LE. zdistref ) lwest = .TRUE.
548              lnorth = .FALSE. ; IF( zdistnorth2 .LE. zdistref ) lnorth= .TRUE.
549              lsouth = .FALSE. ; IF( zdistsouth2 .LE. zdistref ) lsouth= .TRUE.
550
551              !debug
552              IF( .NOT. lest   )CALL write_debug(jsec,'Est   point eliminated')
553              IF( .NOT. lwest  )CALL write_debug(jsec,'West  point eliminated')
554              IF( .NOT. lnorth )CALL write_debug(jsec,'North point eliminated')
555              IF( .NOT. lsouth )CALL write_debug(jsec,'South point eliminated')
556
557              l_oldmethod = .FALSE.
558
559              IF( ( COUNT((/lest/))+COUNT((/lwest/))+COUNT((/lnorth/))+COUNT((/lsouth/)) ) .NE. 0 )THEN
560
561                 ! e.1 If at least one point is selected, select the point
562                 !     which is the closest to original section among selected points
563                 !-------------------------------------------------------------------
564
565                 zdistWest3  = 9999999.9
566                 IF( lwest )zdistWest3  = &
567                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(WestPoint) ,pointToCoordF(endingPoint) ,lkdebug )
568                 zdistEst3   = 9999999.9
569                 IF( lest )zdistEst3   =  &
570                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(EstPoint)  ,pointToCoordF(endingPoint) ,lkdebug )
571                 zdistSouth3 = 9999999.9
572                 IF( lsouth )zdistSouth3 = &
573                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(SouthPoint),pointToCoordF(endingPoint) ,lkdebug )
574                 zdistNorth3 = 9999999.9
575                 IF( lnorth )zdistNorth3 = &
576                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(NorthPoint),pointToCoordF(endingPoint) ,lkdebug )
577
578                 zdistEst3   = zdistEst3   + (1-COUNT((/lest/))  )*9999999.9
579                 zdistWest3  = zdistWest3  + (1-COUNT((/lwest/)) )*9999999.9
580                 zdistNorth3 = zdistNorth3 + (1-COUNT((/lnorth/)))*9999999.9
581                 zdistSouth3 = zdistSouth3 + (1-COUNT((/lsouth/)))*9999999.9
582
583                 zdistnew = MIN(zdistEst3,zdistWest3,zdistnorth3,zdistSouth3)
584
585              ELSE 
586
587                 ! e.2 If no point is selected, select the point which is the closest to end-point
588                 !--------------------------------------------------------------------------------
589                 l_oldmethod = .TRUE.
590
591                 !debug
592                 WRITE(cltmp,'(A30,i3.3)' )'SEARCH NEW POINT WITH OLD METHOD: ',jsec
593                 CALL write_debug(jsec,cltmp)
594                   
595                 !be careful! we can't go backward.
596                 zdistNorth = zdistNorth2    ; zdistSouth = zdistSouth2
597                 zdistEst   = zdistEst2      ; zdistWest  = zdistWest2 
598
599                 IF(     prevPoint%I .EQ. NorthPoint%I .AND. prevPoint%J .EQ. NorthPoint%J) THEN
600                     zdistnew = MIN(zdistEst,zdistWest,zdistSouth)
601                 ELSE IF(prevPoint%I .EQ. SouthPoint%I .AND. prevPoint%J .EQ. SouthPoint%J) THEN
602                    zdistnew = MIN(zdistEst,zdistWest,zdistNorth)
603                 ELSE IF(prevPoint%I .EQ. WestPoint%I  .AND. prevPoint%J .EQ. WestPoint%J ) THEN
604                    zdistnew = MIN(zdistEst,zdistNorth,zdistSouth)
605                 ELSE IF(prevPoint%I .EQ. EstPoint%I   .AND. prevPoint%J .EQ. EstPoint%J  ) THEN
606                    zdistnew = MIN(zdistWest,zdistNorth,zdistSouth)
607                 ELSE
608                    zdistnew = MIN(zdistEst,zdistWest,zdistNorth,zdistSouth)
609                 ENDIF             
610
611              ENDIF
612
613              !debug
614              WRITE(cltmp,'(A11, f8.3)')'zdistref = ',zdistref
615              CALL write_debug(jsec,cltmp)
616              WRITE(cltmp, 103         )'distance2 :',zdistEst2,zdistWest2,zdistNorth2,zdistSouth2 
617              CALL write_debug(jsec,cltmp)
618              WRITE(cltmp, 103         )'distance3 :',zdistEst3,zdistWest3,zdistNorth3,zdistSouth3
619              CALL write_debug(jsec,cltmp)
620              WRITE(cltmp,'(A11, f8.3)')"zdistnew = ",zdistnew
621              CALL write_debug(jsec,cltmp)
622
623103 FORMAT (A12,"E",f12.3," W",f12.3," N",f12.3," S",f12.3)
624
625              !f. save next point and direction of velocity.
626              !---------------------------------------------
627              !nextPoint will be the one which is the closest to endingPoint.
628              !sec%direction will be direction between current point and nextPoint:
629              !It will be used to compute velocity through the segment [CurrentPoint,nextPoint}:
630              !                 
631              !A:Current Point    NorthPoint(I,J+1)   Nextpoint=NorthPoint(I,J+1) => sec%direction=3
632              !                   |                   Nextpoint=SouthPoint(I,J-1) => sec%direction=2   
633              !                   |                   Nextpoint=WestPoint(I-1,J)  => sec%direction=0   
634              !                   |==>V(I,J+1)        Nextpoint=EastPoint(I+1,J)  => sec%direction=1
635              !            U(I,J) |       U(I+1,J)
636              !            ^      |       ^
637              !   West_____|______A_______|_____EstPoint
638              !   Point           |(I,J)        (I+1,J) 
639              !   (I-1,J)         |               
640              !                   |==>V(I,J)
641              !                   |
642              !              SoutPoint(I,J-1)
643              IF( l_oldmethod )THEN
644                 IF( zdistnew == zdistWest )      THEN
645                      sec%direction(jseg)=0 ; nextPoint = WestPoint
646                 ELSE IF( zdistnew == zdistEst )  THEN
647                      sec%direction(jseg)=1 ; nextPoint = EstPoint
648                 ELSE IF( zdistnew == zdistSouth )THEN
649                      sec%direction(jseg)=2 ; nextPoint = SouthPoint
650                 ELSE IF( zdistnew == zdistNorth )THEN
651                      sec%direction(jseg)=3 ; nextPoint= NorthPoint
652                 ENDIF
653              ELSE
654                 IF( zdistnew == zdistWest3 )      THEN
655                      sec%direction(jseg)=0 ; nextPoint = WestPoint
656                 ELSE IF( zdistnew == zdistEst3 )  THEN
657                      sec%direction(jseg)=1 ; nextPoint = EstPoint
658                 ELSE IF( zdistnew == zdistSouth3 )THEN
659                      sec%direction(jseg)=2 ; nextPoint = SouthPoint
660                 ELSE IF( zdistnew == zdistNorth3 )THEN
661                      sec%direction(jseg)=3 ; nextPoint= NorthPoint
662                 ENDIF
663              ENDIF
664
665              WRITE(cltmp,'(A11, 2(i4.4,1X) )')'nextPoint = ', nextPoint
666              CALL write_debug(jsec,cltmp)
667       
668              !f. Save nextPoint and go to nextPoint
669              !-------------------------------------
670              prevPoint = sec%listPoint(jseg)
671              jseg = jseg+1                   !increment of number of segments that form the section
672              sec%listPoint(jseg) = nextPoint !Save next point
673   
674           ENDIF ! southP/northP/WestP/EstP == endingpoint ?
675
676        ENDDO                  !End of loop on jseg
677        sec%nb_point = jseg    !Save the number of segments that form the section
678
679
680     ELSE ! isec%nb_point == 0
681        DO jseg=1,nb_point_max 
682           sec%listPoint(:)=POINT_SECTION(0,0)
683        ENDDO
684        sec%direction(:)=0.
685        sec%nb_point = 0
686     ENDIF
687
688     !debug     
689     IF( sec%nb_point .ne. 0 )THEN
690        CALL write_debug(jsec,"-------------------------------------")
691        CALL write_debug(jsec,"list of points in the grid : ")
692        DO jseg=1,sec%nb_point 
693           ji=sec%listPoint(jseg)%I ; jj=sec%listPoint(jseg)%J
694           WRITE(cltmp, '(i4.4,X,i4.4,X,i4.4,X,f8.3,X,f8.3)' )jseg,ji,jj,glamf(ji,jj),gphif(ji,jj)
695           CALL write_debug(jsec,cltmp)
696        ENDDO
697   
698        !test if we are an end-point
699        IF( sec%listPoint(sec%nb_point)%I .NE. endingPoint%J .AND. sec%listPoint(sec%nb_point)%J .NE. endingPoint%J )THEN   
700           PRINT*,TRIM(sec%name)," NOT ARRIVED TO endingPoint FOR jsec =  ",jsec
701        ENDIF
702     ENDIF
703
704     !now compute new slopeSection with ij-coordinates of first and last point
705     sec%slopeSection = 0  ! default value
706     IF( sec%nb_point .ne. 0 )THEN
707        IF (  sec%listPoint(sec%nb_point)%I .NE.  sec%listPoint(1)%I ) THEN
708           sec%slopeSection = ( sec%listPoint(sec%nb_point)%J - sec%listPoint(1)%J ) /  &
709                              ( sec%listPoint(sec%nb_point)%I - sec%listPoint(1)%I )     
710        ELSE
711           sec%slopeSection = 10000._wp
712        ENDIF
713     ENDIF
714 
715  END SUBROUTINE compsec
716
717END MODULE compute_sections 
Note: See TracBrowser for help on using the repository browser.