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

Last change on this file since 2858 was 2858, checked in by cbricaud, 9 years ago

cleanning, minor modifications

  • Property svn:executable set to *
File size: 33.1 KB
Line 
1MODULE compute_sections
2   !!=====================================================================
3   !!                       ***  MODULE  diadct  ***
4   !! Ocean diagnostics: Compute the transport trough a sec.
5   !!
6   !!
7   !! History: 2011: cbricaud Mercator-Ocean
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 serie of mesh's points that represents the section
28     !!               defined by its extremities.
29     !!
30     !!  ** Method  :
31     !!          I.   Found 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 serie of mesh's 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 taht form the section   
50        nb_inmesh        ,     & ! number of intersection between section and the mesh
51        nmesh                    ! number of cells in processor domain
52     REAL(wp),SAVE :: zdistmesh      ! Taller cell of the mesh in ocean
53     REAL(wp)      :: &
54        zdistEst   , zdistNorth , zdistWest , zdistSouth ,  &! temporary scalars
55        zdistEst2  , zdistNorth2, zdistWest2, zdistSouth2,  &! temporary scalars
56        zdistEst3  , zdistNorth3, zdistWest3, zdistSouth3,  &! temporary scalars
57        zdistFirst , zdistLast  , zdistref  ,               &!         "
58        zdistante  , zdistante2 , zdistnew  , zdistnew2  ,  &!         "
59        zdeltai    , zdeltaj                                !         "
60     LOGICAL :: & 
61        ll_overlap_sec_left = .FALSE. , ll_overlap_sec_right = .FALSE. ,&! temporary logical
62        ll_date_domain_left = .FALSE. , ll_date_domain_right = .FALSE. ,&!       "
63        ll_overlap_sec      = .FALSE. , ll_date_domain       = .FALSE. ,&!       "
64        ll_test             = .FALSE.                                    !       "
65     LOGICAL :: lest, lwest, lnorth, lsouth
66     LOGICAL :: l_oldmethod
67     CHARACTER(len=120) :: cltmp
68     TYPE(COORD_SECTION) :: &
69        coord_a   , coord_b  , coord_d ,  coord_t ,               &!temporary point (long/lat)
70        coordFirst, coordLast, coordTemp                           !        "
71     TYPE(COORD_SECTION), DIMENSION(nb_point_max) :: coordSec      !intersections mesh/section   
72     TYPE(POINT_SECTION) :: &
73        endingPoint, prevPoint , nextPoint,           &!temporary point (I/J)
74        SouthPoint , NorthPoint, EstPoint , WestPoint  !        "
75     !!---------------------------------------------------------------------
76     IF( jsec==1 )THEN
77          PRINT*,'                '
78          PRINT*,'COMPUTE SECTIONS'
79          PRINT*,'----------------'
80     ENDIF
81
82     !debug
83     CALL write_debug(jsec,'compute section:')
84     CALL write_debug(jsec,'================')
85
86
87     !==================!
88     !0. Initializations!
89     !==================!
90     
91     nmesh       = jpi*jpj          ! number of cells in processor domain
92     nb_inmesh   = 0                !initialize number of intersection between section and the mesh
93     zdistEst  =0. ; zdistNorth=0. ; zdistWest=0. ; zdistSouth=0.   ! temporary scalars
94     zdistFirst=0. ; zdistLast =0.                                   !         "
95     zdistante =0. ; zdistante2=0. ; zdistnew=0.  ; zdistnew2=0.    !         "
96     zdeltai=0.    ; zdeltaj=0. 
97     coord_a     = COORD_SECTION( 0., 0. ) ; coord_b    = COORD_SECTION( 0., 0. )
98     coord_d     = COORD_SECTION( 0., 0. ) ; coord_t    = COORD_SECTION( 0., 0. ) 
99     coordFirst  = COORD_SECTION( 0., 0. ) ; coordLast  = COORD_SECTION( 0., 0. )
100     coordTemp   = COORD_SECTION( 0., 0. ) ; coordSec   = COORD_SECTION( 0., 0. )
101     endingPoint = POINT_SECTION( -1, -1 ) 
102     prevPoint   = POINT_SECTION( -1, -1 ) ; nextPoint   = POINT_SECTION( -1, -1 )
103     SouthPoint  = POINT_SECTION( -1, -1 ) ; NorthPoint  = POINT_SECTION( -1, -1 )
104     EstPoint    = POINT_SECTION( -1, -1 ) ; WestPoint   = POINT_SECTION( -1, -1 )
105
106     IF( jsec == 1 )THEN
107        !Found the taller cell of the mesh in ocean (used in compsec)
108        zdistmesh=0.
109        DO jj=2,jpj
110           DO ji=2,jpi
111              IF( zdistmesh .LT. &
112                 (e1t(ji,jj)/(cos(gphit(ji,jj))*110000))  ) THEN
113                 zdistmesh = e1t(ji,jj)/(cos(gphit(ji,jj))*110000)
114              ENDIF
115           ENDDO
116        ENDDO
117   
118     ENDIF
119
120     !debug
121     WRITE(cltmp,'(A10,f10.3)')'zdistmesh = ',zdistmesh
122     CALL write_debug(jsec,cltmp)
123
124     !===========================================================!   
125     !I. Found which cells of the mesh the section is  crossing  !
126     !===========================================================!
127
128     !loop on the mesh
129     DO jj=2,jpj
130        DO ji=2,jpi
131           !-----------------------------------------------------------
132           !For each cell of the mesh, we find the intersection between
133           !the section and the cell as described below:
134           !The cell is defined by 4 points A B C D
135           !and the section by S1 and S2
136           !
137           !Section\    ________
138           !        \ B|        |
139           !         \ |        |
140           !          \|one cell|
141           !      .....\        |
142           !           |\       |
143           !           | \      |
144           !          A|__\_____|D
145           !       ........\
146           !----------------\-------------------------------------------
147
148           !definition of points A,B,D (we don't need C)
149           coord_a = COORD_SECTION( glamf(ji-1,jj-1) ,gphif(ji-1,jj-1) )
150           coord_b = COORD_SECTION( glamf(ji-1,jj)   ,gphif(ji-1,jj)   )
151           coord_d = COORD_SECTION( glamf(ji,jj-1)   ,gphif(ji,jj-1)   )
152 
153           !size of the cell
154           zdeltai= glamf(ji-1,jj) - glamf(ji-1,jj-1) !zdeltai=[AB]
155           zdeltaj= gphif(ji,jj-1) - gphif(ji-1,jj-1) !zdeltaj=[AD]
156           
157           IF( ABS(zdeltai) .LE. 2.*zdistmesh .OR.  & 
158               ABS(zdeltaj) .LE. 2.*zdistmesh )THEN           
159 
160              !intersection section/[AB]=?
161              coordTemp     = intersec(sec,coord_a,coord_b)     !compute intersection
162              coordTemp%lon = coordTemp%lon !* zmask(ji,jj)-9999.*(1-zmask(ji,jj))
163              coordTemp%lat = coordTemp%lat !* zmask(ji,jj)-9999.*(1-zmask(ji,jj))
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 = distance(coord_t,coordFirst)
303              zdistLast = distance(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     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) 
336     CALL write_debug(jsec,cltmp)
337     ji=endingPoint%I ; jj=endingPoint%J
338     WRITE(cltmp,'(A15,X,i4.4,X,i4.4,X,f8.3,X,f8.3)')'Last  point: ',endingPoint,glamf(ji,jj),gphif(ji,jj)
339     CALL write_debug(jsec,cltmp)
340     !
341     coord_a=pointToCoordF(sec%listPoint(1)) ; coord_b=pointToCoordF(endingPoint)
342     ll_test = .FALSE.
343     IF( ll_date_domain .AND. ABS( coord_a%lon - coord_b%lon ).GT. 180) ll_test= .TRUE.
344     zdistante=distance2(coord_a,coord_b ,ll_test )
345     WRITE(cltmp,'(A20,f10.3)' )'distance between IJ-extremities : ',zdistante
346     CALL write_debug(jsec,cltmp)
347     !
348     CALL write_debug(jsec,"Initial extremities : ") 
349     WRITE(cltmp,'( 2(f9.3),A3,2(f9.3) )')coordFirst,'---',coordLast
350     CALL write_debug(jsec,cltmp)
351     ll_test = .FALSE.
352     IF( ll_date_domain .AND. ABS(coordFirst%lon - coordLast%lon).GT. 180)ll_test= .TRUE.
353     zdistante=distance2(coordFirst,coordLast,ll_test)
354     WRITE(cltmp,'(A30,f10.3)')' distance between initial extremities : ',zdistante
355     CALL write_debug(jsec,cltmp)
356     CALL write_debug(jsec,"                  ")
357
358     !==========================================================!
359     ! IV. Find the serie of mesh's points that form the section!
360     !==========================================================!
361     CALL write_debug(jsec,"Find the serie of mesh's points that form the section")
362
363     IF( nb_inmesh .NE. 0 )THEN
364
365        !The serie of mesh's points that form the section will 'link'
366        !sec%listPoint(1) to endingPoint: it will be stored in
367        !sec%listPoint(jseg)
368        !
369        !We take place on the fist point (sec%listPoint(1))
370        ! a.  We find the 4 adjacent points (North, South, East, West)
371        ! b.  Compute distance between current point and endingPoint
372        ! c.  Compute distance between the 4 adjacent points and endingPoint
373        ! d.  Select the points which are closer to end-point than current point
374        ! e.1 If at least one point is selected, select the point which is closest to original section among selected points
375        ! e.2 If no point is selected, select the point which is the closest to end-point
376        ! f. save next point and direction of velocity.
377        ! g. Save nextPoint and go to nextPoint
378        !
379        !We get out of this loop if:
380        !    - we are on endingPoint
381        !    - the number of points (jseg) that link sec%listPoint(1) to endingPoint is
382        !      twice greater than number of section/mesh intersection (nb_inmesh):
383        !      it could be possible if thr algorithm can't link endingPoint (bug).
384
385        !initialize distnew value (with distance between section's extremities)
386        zdistnew  = distance(coordFirst,coordLast,sec%ll_date_line) 
387        prevPoint  = POINT_SECTION(0,0)
388        jseg       = 1
389
390        DO WHILE ( (  sec%listPoint(jseg)%I .NE.  endingPoint%I    &
391                 .OR. sec%listPoint(jseg)%J .NE. endingPoint%J   ) &
392                 .AND. jseg .LT. 500 .AND. sec%listPoint(jseg)%I .GT. 0  )         
393   
394           ! a. find the 4 adjacent points (North, South, East, West)
395           !---------------------------------------------------------
396           SouthPoint = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J-1)
397           NorthPoint = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J+1)
398           WestPoint  = POINT_SECTION(sec%listPoint(jseg)%I-1,sec%listPoint(jseg)%J)
399           EstPoint   = POINT_SECTION(sec%listPoint(jseg)%I+1,sec%listPoint(jseg)%J)
400
401           !debug
402           CALL write_debug(jsec,"---------------")
403           WRITE(cltmp,100)'Current points: ',sec%listPoint(jseg), & 
404                         glamf(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J), &
405                         gphif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J)
406           CALL write_debug(jsec,cltmp)
407           CALL write_debug(jsec,"E/W/N/S points")
408           WRITE(cltmp,101)glamf(  EstPoint%I,EstPoint%J)  ,gphif(  EstPoint%I,  EstPoint%J), &
409                           glamf( WestPoint%I,WestPoint%J) ,gphif( WestPoint%I, WestPoint%J), &
410                           glamf(NorthPoint%I,NorthPoint%J),gphif(NorthPoint%I,NorthPoint%J) ,&
411                           glamf(SouthPoint%I,SouthPoint%J),gphif(SouthPoint%I,SouthPoint%J)
412           CALL write_debug(jsec,cltmp)
413           WRITE(cltmp,102)EstPoint,WestPoint,NorthPoint,SouthPoint
414           CALL write_debug(jsec,cltmp)
415
416100 FORMAT ( A15,2(i4.4," "),2(f7.3," ") )
417101 FORMAT ( "E ",2(f7.3," "),"W ",2(f7.3," "),"N ",2(f7.3," "),"S ",2(f7.3," "))
418102 FORMAT ( "E ",i4.4,' ',i4.4,"//W ",i4.4,' ',i4.4,"//N ",i4.4,' ',i4.4,"//S ",i4.4,' ',i4.4 )
419
420               
421           !we  are on end-point
422           !--------------------
423           IF(      SouthPoint%I==endingPoint%I .AND. SouthPoint%J==endingPoint%J )THEN
424               jseg = jseg+1 ; sec%listPoint(jseg) = SouthPoint
425           ELSE IF( NorthPoint%I==endingPoint%I .AND. NorthPoint%J==endingPoint%J )THEN
426               jseg = jseg+1 ; sec%listPoint(jseg) = NorthPoint
427           ELSE IF(  WestPoint%I==endingPoint%I .AND.  WestPoint%J==endingPoint%J )THEN
428               jseg = jseg+1 ; sec%listPoint(jseg) = WestPoint
429           ELSE IF(   EstPoint%I==endingPoint%I .AND.   EstPoint%J==endingPoint%J )THEN
430               jseg = jseg+1 ; sec%listPoint(jseg) = EstPoint
431
432           ELSE
433           !we  are NOT on end-point
434           !------------------------
435
436              ! b. distance between current point and endingPoint
437              !--------------------------------------------------
438              zdistante = zdistnew
439
440              ! c. compute distance between the 4 adjacent points and endingPoint
441              !------------------------------------------------------------------
442              ! BE CARREFUL! When the domain crosses the date line (ll_date_domain):
443              ! When we will compute distances between W/E/S/N points and endingPoint,
444              ! we have to check if theses 4 lines crosses the date line
445              ! (test: ABS(coordTemp%lon - coordLast%lon).GT. 180)
446              ! If it's true,we have to add 360° to coordLast%long to compute the
447              ! good distance through the date line and not through the complementary
448              ! in the mesh.
449       
450              ! c.1 compute distWest: distance between west point and endingPoint
451              !----------------------
452              zdistWest2 = 99999999.9 ;  zdistWest3 = 99999999.9 
453              IF( sec%listPoint(jseg)%I .NE. 1 )THEN
454                 !When we are on the west side of the mesh (i=1),we can't go to the west.
455                 coordTemp = pointToCoordF(WestPoint) 
456                 ll_test = .FALSE.
457                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test = .TRUE.
458                 zdistWest2  = distance2(pointToCoordF(WestPoint) ,coordLast ,ll_test)
459              ENDIF
460
461              ! c.2 compute distEst: distance between east point and endingPoint
462              !---------------------
463              zdistEst2 = 99999999.9 ;  zdistEst3 = 99999999.9
464              IF( sec%listPoint(jseg)%I .EQ. jpi )THEN
465                 !We test if the current point is on the east side of the mesh
466                 ! The method is done such as we go toward east to link
467                 ! sec%listPoint(1) to endingPoint.
468                 ! So, if the section crosses the overlapping band (ll_overlap_sec=T),
469                 ! we won't have to stop if the current point is on the EAST side of the mesh:
470                 ! we have to follow the construction of the section on the
471                 ! WEST side of the mesh
472                 IF( ll_overlap_sec  )THEN
473                    !section crosses the overlapping band
474                    !So EstPoint is on the west side of the mesh
475                    EstPoint = POINT_SECTION(3,sec%listPoint(jseg)%J)
476                    zdistEst2= distance2(pointToCoordF(EstPoint)  ,coordLast ,.FALSE.)
477                 ENDIF
478              ELSE
479                 coordTemp = pointToCoordF(EstPoint) 
480                 ll_test = .FALSE.
481                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test= .TRUE.
482                 zdistEst2 = distance2(pointToCoordF(EstPoint)  ,coordLast ,ll_test )
483              ENDIF
484
485              ! c.3 compute distSouth: distance between south point and endingPoint
486              !-----------------------
487              zdistSouth2 = 99999999.9 ; zdistSouth3 = 99999999.9
488              IF( sec%listPoint(jseg)%J .NE. 1 )THEN
489                 !When we are on the south side of the mesh (j=1),we can't go to the south.
490                 coordTemp=pointToCoordF(SouthPoint)
491                 ll_test = .FALSE.
492                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test= .TRUE.
493                    zdistSouth2 = distance2(pointToCoordF(SouthPoint),coordlast ,ll_test )
494              ENDIF
495
496              ! c.4 compute distNorth: distance between north and endingPoint
497              !-----------------------
498              zdistNorth2 = 99999999.9 ; zdistNorth3 = 99999999.9 
499              IF( sec%listPoint(jseg)%J .NE. jpj )THEN
500                 !When we are on the north side of the mesh (j=jpj),we can't go to the south.
501                 coordTemp=pointToCoordF(NorthPoint)
502                 ll_test = .FALSE.
503                 IF( ll_date_domain .AND. ABS(coordTemp%lon - coordLast%lon).GT. 180 )ll_test= .TRUE.
504                 zdistNorth2 = distance2(pointToCoordF(NorthPoint),coordlast ,ll_test )
505              ENDIF
506
507              ! d. select the points which are closer to end-point than current point
508              !----------------------------------------------------------------------
509              zdistref=distance2(pointToCoordF(sec%listPoint(jseg)),coordlast ,ll_test )
510              WRITE(cltmp,'( A56,f10.3 )' )'distance between actual point and last point: zdistref = ',zdistref
511              CALL write_debug(jsec,cltmp)
512              lest   = .FALSE. ; IF( zdistEst2   .LE. zdistref ) lest  = .TRUE.
513              lwest  = .FALSE. ; IF( zdistwest2  .LE. zdistref ) lwest = .TRUE.
514              lnorth = .FALSE. ; IF( zdistnorth2 .LE. zdistref ) lnorth= .TRUE.
515              lsouth = .FALSE. ; IF( zdistsouth2 .LE. zdistref ) lsouth= .TRUE.
516
517              !debug
518              IF( .NOT. lest   )CALL write_debug(jsec,'Est   point eliminated')
519              IF( .NOT. lwest  )CALL write_debug(jsec,'West  point eliminated')
520              IF( .NOT. lnorth )CALL write_debug(jsec,'North point eliminated')
521              IF( .NOT. lsouth )CALL write_debug(jsec,'South point eliminated')
522
523              l_oldmethod = .FALSE.
524
525              IF( ( COUNT((/lest/))+COUNT((/lwest/))+COUNT((/lnorth/))+COUNT((/lsouth/)) ) .NE. 0 )THEN
526
527                 ! e.1 If at least one point is selected, select the point
528                 !     which is the closest to original section among selected points
529                 !-------------------------------------------------------------------
530
531                 zdistWest3  = 9999999.9
532                 IF( lwest )zdistWest3  = &
533                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(WestPoint) ,pointToCoordF(endingPoint) ,lkdebug )
534                 zdistEst3   = 9999999.9
535                 IF( lest )zdistEst3   =  &
536                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(EstPoint)  ,pointToCoordF(endingPoint) ,lkdebug )
537                 zdistSouth3 = 9999999.9
538                 IF( lsouth )zdistSouth3 = &
539                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(SouthPoint),pointToCoordF(endingPoint) ,lkdebug )
540                 zdistNorth3 = 9999999.9
541                 IF( lnorth )zdistNorth3 = &
542                    distance3(pointToCoordF(sec%listPoint(1)),pointToCoordF(NorthPoint),pointToCoordF(endingPoint) ,lkdebug )
543
544                 zdistEst3   = zdistEst3   + (1-COUNT((/lest/))  )*9999999.9
545                 zdistWest3  = zdistWest3  + (1-COUNT((/lwest/)) )*9999999.9
546                 zdistNorth3 = zdistNorth3 + (1-COUNT((/lnorth/)))*9999999.9
547                 zdistSouth3 = zdistSouth3 + (1-COUNT((/lsouth/)))*9999999.9
548
549                 zdistnew = MIN(zdistEst3,zdistWest3,zdistnorth3,zdistSouth3)
550
551              ELSE 
552
553                 ! e.2 If no point is selected, select the point which is the closest to end-point
554                 !--------------------------------------------------------------------------------
555                 l_oldmethod = .TRUE.
556
557                 !debug
558                 WRITE(cltmp,'(A30,i3.3)' )'SEARCH NEW POINT WITH OLD METHOD: ',jsec
559                 CALL write_debug(jsec,cltmp)
560                   
561                 ! on passe à l'ancienne methode: le point parmies les 4 pts (NSWE)  qui se rapproche
562                 ! le + du dernier pt
563                 !-----------------------------
564                 !be carreful! we can't go backward.
565
566                 zdistNorth = zdistNorth2    ; zdistSouth = zdistSouth2
567                 zdistEst   = zdistEst2      ; zdistWest  = zdistWest2 
568
569                 IF(     prevPoint%I .EQ. NorthPoint%I .AND. prevPoint%J .EQ. NorthPoint%J) THEN
570                     zdistnew = MIN(zdistEst,zdistWest,zdistSouth)
571                 ELSE IF(prevPoint%I .EQ. SouthPoint%I .AND. prevPoint%J .EQ. SouthPoint%J) THEN
572                    zdistnew = MIN(zdistEst,zdistWest,zdistNorth)
573                 ELSE IF(prevPoint%I .EQ. WestPoint%I  .AND. prevPoint%J .EQ. WestPoint%J ) THEN
574                    zdistnew = MIN(zdistEst,zdistNorth,zdistSouth)
575                 ELSE IF(prevPoint%I .EQ. EstPoint%I   .AND. prevPoint%J .EQ. EstPoint%J  ) THEN
576                    zdistnew = MIN(zdistWest,zdistNorth,zdistSouth)
577                 ELSE
578                    zdistnew = MIN(zdistEst,zdistWest,zdistNorth,zdistSouth)
579                 ENDIF             
580
581              ENDIF
582
583              !debug
584              WRITE(cltmp,'(A11, f8.3)')'zdistref = ',zdistref
585              CALL write_debug(jsec,cltmp)
586              WRITE(cltmp, 103         )'distance2 :',zdistEst2,zdistWest2,zdistNorth2,zdistSouth2 
587              CALL write_debug(jsec,cltmp)
588              WRITE(cltmp, 103         )'distance3 :',zdistEst3,zdistWest3,zdistNorth3,zdistSouth3
589              CALL write_debug(jsec,cltmp)
590              WRITE(cltmp,'(A11, f8.3)')"zdistnew = ",zdistnew
591              CALL write_debug(jsec,cltmp)
592
593103 FORMAT (A12,"E",f12.3," W",f12.3," N",f12.3," S",f12.3)
594
595              !f. save next point and direction of velocity.
596              !---------------------------------------------
597              !nextPoint will be the one which is the closest to endingPoint.
598              !sec%direction will be direction between current point and nextPoint:
599              !It will be used to compute velocity through the segment [CurrentPoint,nextPoint}:
600              !                 
601              !A:Current Point    NorthPoint(I,J+1)   Nextpoint=NorthPoint(I,J+1) => sec%direction=3
602              !                   |                   Nextpoint=SouthPoint(I,J-1) => sec%direction=2   
603              !                   |                   Nextpoint=WestPoint(I-1,J)  => sec%direction=0   
604              !                   |==>V(I,J+1)        Nextpoint=EastPoint(I+1,J)  => sec%direction=1
605              !            U(I,J) |       U(I+1,J)
606              !            ^      |       ^
607              !   West_____|______A_______|_____EstPoint
608              !   Point           |(I,J)        (I+1,J) 
609              !   (I-1,J)         |               
610              !                   |==>V(I,J)
611              !                   |
612              !              SoutPoint(I,J-1)
613              IF( l_oldmethod )THEN
614                 IF( zdistnew == zdistWest )      THEN
615                      sec%direction(jseg)=0 ; nextPoint = WestPoint
616                 ELSE IF( zdistnew == zdistEst )  THEN
617                      sec%direction(jseg)=1 ; nextPoint = EstPoint
618                 ELSE IF( zdistnew == zdistSouth )THEN
619                      sec%direction(jseg)=2 ; nextPoint = SouthPoint
620                 ELSE IF( zdistnew == zdistNorth )THEN
621                      sec%direction(jseg)=3 ; nextPoint= NorthPoint
622                 ENDIF
623              ELSE
624                 IF( zdistnew == zdistWest3 )      THEN
625                      sec%direction(jseg)=0 ; nextPoint = WestPoint
626                 ELSE IF( zdistnew == zdistEst3 )  THEN
627                      sec%direction(jseg)=1 ; nextPoint = EstPoint
628                 ELSE IF( zdistnew == zdistSouth3 )THEN
629                      sec%direction(jseg)=2 ; nextPoint = SouthPoint
630                 ELSE IF( zdistnew == zdistNorth3 )THEN
631                      sec%direction(jseg)=3 ; nextPoint= NorthPoint
632                 ENDIF
633              ENDIF
634
635              WRITE(cltmp,'(A11, 2(i4.4,1X) )')'nextPoint = ', nextPoint
636              CALL write_debug(jsec,cltmp)
637       
638              !f. Save nextPoint and go to nextPoint
639              !-------------------------------------
640              prevPoint = sec%listPoint(jseg)
641              jseg = jseg+1                   !increment of number of segments that form the section
642              sec%listPoint(jseg) = nextPoint !Save next point
643   
644           ENDIF ! southP/northP/WestP/EstP == endingpoint ?
645
646        ENDDO                  !End of loop on jseg
647        sec%nb_point = jseg    !Save the number of segments that form the section
648
649
650     ELSE ! nb_inmesh == 0
651        DO jseg=1,nb_point_max 
652           sec%listPoint(:)=POINT_SECTION(0,0)
653        ENDDO
654        sec%direction(:)=0.
655        sec%nb_point = 0
656     ENDIF
657
658     !debug     
659     CALL write_debug(jsec,"-------------------------------------")
660     CALL write_debug(jsec,"list of points in the grid : ")
661     DO jseg=1,sec%nb_point 
662        ji=sec%listPoint(jseg)%I ; jj=sec%listPoint(jseg)%J
663        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)
664        CALL write_debug(jsec,cltmp)
665     ENDDO
666   
667     !test if we are one end-point
668     IF( sec%listPoint(sec%nb_point)%I .NE. endingPoint%J .AND. sec%listPoint(sec%nb_point)%J .NE. endingPoint%J )THEN   
669        PRINT*,TRIM(sec%name)," NOT ARRIVED TO endingPoint FOR jsec =  ",jsec
670     ENDIF
671
672     !now compute new slopeSection with ij-coordinates of first and last point
673     IF (  sec%listPoint(sec%nb_point)%I .NE.  sec%listPoint(1)%I ) THEN
674        sec%slopeSection = ( sec%listPoint(sec%nb_point)%J - sec%listPoint(1)%J ) /  &
675                           ( sec%listPoint(sec%nb_point)%I - sec%listPoint(1)%I )     
676     ELSE
677        sec%slopeSection = 10000._wp
678     ENDIF
679 
680  END SUBROUTINE compsec
681
682END MODULE compute_sections 
Note: See TracBrowser for help on using the repository browser.