Index: trunk/tools/module_scientific.f90
===================================================================
--- trunk/tools/module_scientific.f90	(revision 1650)
+++ trunk/tools/module_scientific.f90	(revision 1651)
@@ -37,4 +37,5 @@
   CONTAINS
 
+
   SUBROUTINE poly_overlap_tracks(dbg, dx, dy, dt, Nallpolys, allpolys, ctrpolys, areapolys, Nmaxpoly, &
     Nmaxtracks, tracks, finaltracks)
@@ -46,40 +47,32 @@
     INTEGER, INTENT(in)                                  :: dx, dy, dt, Nmaxpoly, Nmaxtracks
     INTEGER, DIMENSION(dt), INTENT(in)                   :: Nallpolys
-    INTEGER, DIMENSION(dt,dx,dy), INTENT(in)             :: allpolys
-    REAL(r_k), DIMENSION(dt,Nmaxpoly,2), INTENT(in)      :: ctrpolys
-    REAL(r_k), DIMENSION(dt,Nmaxpoly), INTENT(in)        :: areapolys
-    REAL(r_k), DIMENSION(dt,Nmaxtracks,Nmaxpoly,5),                                                   &
+    INTEGER, DIMENSION(dx,dy,dt), INTENT(in)             :: allpolys
+    REAL(r_k), DIMENSION(2,Nmaxpoly,dt), INTENT(in)      :: ctrpolys
+    REAL(r_k), DIMENSION(Nmaxpoly,dt), INTENT(in)        :: areapolys
+    REAL(r_k), DIMENSION(5,Nmaxpoly,Nmaxtracks,dt),                                                   &
       INTENT(out)                                        :: tracks
-    REAL(r_k), DIMENSION(dt,Nmaxtracks,4), INTENT(out)   :: finaltracks
+    REAL(r_k), DIMENSION(4,Nmaxtracks,dt), INTENT(out)   :: finaltracks
 
 ! Local
     INTEGER                                              :: i, j, ip, it, iip, itt
     INTEGER                                              :: ierr
-    INTEGER, DIMENSION(dt,Nmaxpoly)                      :: coincidencies, NOcoincidencies,           &
-      MULTIcoincidencies
+    INTEGER, DIMENSION(Nmaxpoly,dt)                      :: coincidencies, NOcoincidencies
     INTEGER, DIMENSION(:), ALLOCATABLE                   :: coins
     INTEGER, DIMENSION(:,:), ALLOCATABLE                 :: coinsNpts
-    INTEGER, DIMENSION(dt,Nmaxpoly)                      :: polycoincidencies
-    INTEGER, DIMENSION(dt,Nmaxpoly,Nmaxpoly)             :: coincidenciesNpts
+    INTEGER, DIMENSION(Nmaxpoly,dt)                      :: polycoincidencies
+    INTEGER, DIMENSION(Nmaxpoly,Nmaxpoly,dt)             :: coincidenciesNpts
     INTEGER                                              :: Nmeet, Nsearch, Nindep
     INTEGER, DIMENSION(dt)                               :: Nindeppolys
     CHARACTER(len=5)                                     :: NcoinS
-    INTEGER, DIMENSION(dt,Nmaxpoly,Nmaxpoly)             :: polysIndep
-    INTEGER, DIMENSION(dt,Nmaxpoly)                      :: NpolysIndep
-    INTEGER, DIMENSION(dt,Nmaxpoly)                      :: SpolysIndep
+    INTEGER, DIMENSION(Nmaxpoly,Nmaxpoly,dt)             :: polysIndep
+    INTEGER, DIMENSION(Nmaxpoly,dt)                      :: NpolysIndep
+    INTEGER, DIMENSION(Nmaxpoly,dt)                      :: SpolysIndep
     INTEGER                                              :: iindep, iiprev
-    INTEGER, DIMENSION(dt,Nmaxpoly,Nmaxpoly)             :: polysIndepchains
-    INTEGER                                              :: Nprev, polyprev, Nnotchained, Nprevchained
-    INTEGER                                              :: NNprev, Ntprev
-    LOGICAL                                              :: Indeppolychained, prevchained
-    INTEGER, DIMENSION(Nmaxpoly,Nmaxpoly)                :: polysIndepNotchained
-    INTEGER, DIMENSION(2)                                :: trackperiod
+    INTEGER                                              :: Nprev, NNprev, Ntprev
+    LOGICAL                                              :: Indeppolychained
     INTEGER                                              :: itrack, ictrack
     INTEGER                                              :: ixp, iyp, ttrack
-    INTEGER                                              :: Nnew
-    LOGICAL, DIMENSION(Nmaxpoly)                         :: chained
     INTEGER, DIMENSION(dt)                               :: Ntracks
     INTEGER                                              :: idtrack, maxtrack
-    INTEGER, DIMENSION(dt,Nmaxpoly)                      :: tracks_it
 
 !!!!!!! Variables
@@ -104,6 +97,4 @@
     ! Polygons without a coincidence
     NOcoincidencies = 0
-    ! Polygons with multiple coincidences
-    MULTIcoincidencies = 0
     ! Number of independent polygons by time step
     Nindeppolys = 0
@@ -120,7 +111,7 @@
     Nindeppolys(it) = Nmeet
     DO i=1, Nmeet
-      SpolysIndep(it,i) = i
-      NpolysIndep(it,1:Nmeet) = 1
-      polysIndep(it,i,1) = i
+      SpolysIndep(i,it) = i
+      NpolysIndep(1:Nmeet,it) = 1
+      polysIndep(1,i,it) = i
     END DO
 
@@ -140,9 +131,9 @@
       CALL ErrMsg(msg,fname,ierr)
 
-      CALL coincidence_all_polys(dbg, dx, dy, Nmeet, allpolys(it+1,:,:), ctrpolys(it+1,1:Nmeet,:),    &
-        Nsearch, allpolys(it,:,:), ctrpolys(it,1:Nsearch,:), areapolys(it,1:Nsearch), coins, coinsNpts)
-
-      polycoincidencies(it+1,1:Nmeet) = coins
-      coincidenciesNpts(it+1,1:Nmeet,1:Nsearch) = coinsNpts
+      CALL coincidence_all_polys(dbg, dx, dy, Nmeet, allpolys(:,:,it+1), ctrpolys(:,1:Nmeet,it+1),    &
+        Nsearch, allpolys(:,:,it), ctrpolys(:,1:Nsearch,it), areapolys(1:Nsearch,it), coins, coinsNpts)
+
+      polycoincidencies(1:Nmeet,it+1) = coins
+      coincidenciesNpts(1:Nmeet,1:Nsearch,it+1) = coinsNpts
 
       ! Counting the number of times that a polygon has a coincidency
@@ -158,8 +149,8 @@
         IF (coins(i) == -1) THEN
           Nindep = Nindep + 1
-          NOcoincidencies(it+1,i) = 1
-          SpolysIndep(it+1,Nindep) = -1
-          NpolysIndep(it+1,Nindep) = NpolysIndep(it+1,Nindep) + 1
-          polysIndep(it+1,Nindep,NpolysIndep(it+1,Nindep)) = i
+          NOcoincidencies(i,it+1) = 1
+          SpolysIndep(Nindep,it+1) = -1
+          NpolysIndep(Nindep,it+1) = NpolysIndep(Nindep,it+1) + 1
+          polysIndep(Nindep,NpolysIndep(Nindep,it+1),it+1) = i
         ELSE IF (coins(i) == -9) THEN
           WRITE(NcoinS,'(I5)')coins(i)
@@ -170,13 +161,13 @@
           DO ip=1, Nsearch
             IF (coins(i) == ip) THEN
-              IF (coincidencies(it+1,ip) == 0) THEN
+              IF (coincidencies(ip,it+1) == 0) THEN
                 Nindep = Nindep + 1
-                SpolysIndep(it+1,Nindep) = ip
+                SpolysIndep(Nindep,it+1) = ip
               END IF
-              coincidencies(it+1,ip) = coincidencies(it+1,ip) + 1
+              coincidencies(ip,it+1) = coincidencies(ip,it+1) + 1
               DO iindep=1, Nindep
-                IF (SpolysIndep(it+1,iindep) == coins(i)) THEN
-                  NpolysIndep(it+1,iindep) = NpolysIndep(it+1,iindep) + 1
-                  polysIndep(it+1,iindep,NpolysIndep(it+1,iindep)) = i
+                IF (SpolysIndep(iindep,it+1) == coins(i)) THEN
+                  NpolysIndep(iindep,it+1) = NpolysIndep(iindep,it+1) + 1
+                  polysIndep(iindep,NpolysIndep(iindep,it+1),it+1) = i
                 END IF
               END DO
@@ -192,6 +183,6 @@
         PRINT *,'    indep_polygon prev_step_polygon Nassociated_polygons curr_ass_polygons _______'
         DO i=1, Nindeppolys(it+1)
-          PRINT *,i, SpolysIndep(it+1,i), NpolysIndep(it+1,i), ':',                                   &
-            polysIndep(it+1,i,1:NpolysIndep(it+1,i))
+          PRINT *,i, SpolysIndep(i,it+1), NpolysIndep(i,it+1), ':',                                   &
+            polysIndep(i,1:NpolysIndep(i,it+1),it+1)
         END DO
       END IF
@@ -204,6 +195,6 @@
         PRINT '(4x,3(A6,1x))','Nindep', 'PrevID', 'IDs'
         DO ip=1, Nindeppolys(it)
-          PRINT '(4x,I6,A1,I6,A1,100(I6))', ip, ',', SpolysIndep(it,ip), ':',                         &
-            polysIndep(it,ip,1:NpolysIndep(it,ip))
+          PRINT '(4x,I6,A1,I6,A1,100(I6))', ip, ',', SpolysIndep(ip,it), ':',                         &
+            polysIndep(ip,1:NpolysIndep(ip,it),it)
         END DO
       END DO
@@ -221,10 +212,9 @@
     DO ip=1, Nindeppolys(1)
       itrack = itrack + 1
-      tracks(1,itrack,1,1) = itrack*1.
-      tracks(1,itrack,1,2) = SpolysIndep(1,ip)
-      tracks(1,itrack,1,3) = ctrpolys(1,ip,1)
-      tracks(1,itrack,1,4) = ctrpolys(1,ip,2)
-      tracks(1,itrack,1,5) = 1
-      tracks_it(1,itrack) = itrack
+      tracks(1,1,itrack,1) = itrack*1.
+      tracks(2,1,itrack,1) = SpolysIndep(ip,1)
+      tracks(3,1,itrack,1) = ctrpolys(1,ip,1)
+      tracks(4,1,itrack,1) = ctrpolys(2,ip,1)
+      tracks(5,1,itrack,1) = 1
       Ntracks(1) = Ntracks(1) + 1
     END DO
@@ -235,6 +225,6 @@
       ! Indep polygons current time-step
       current_poly: DO i=1, Nindeppolys(it)
-        IF (dbg) PRINT *,'  curent poly:', i, 'Prev poly:', SpolysIndep(it,i), ' N ass. polygons:',   &
-          NpolysIndep(it,i), 'ass. poly:', polysIndep(it,i,1:NpolysIndep(it,i))
+        IF (dbg) PRINT *,'  curent poly:', i, 'Prev poly:', SpolysIndep(i,it), ' N ass. polygons:',   &
+          NpolysIndep(i,it), 'ass. poly:', polysIndep(i,1:NpolysIndep(i,it),it)
         Indeppolychained = .FALSE.
 
@@ -242,14 +232,14 @@
         ! Looping overall
         it1_tracks: DO itt=1, Ntracks(it-1)
-          itrack = tracks(it-1,itt,1,1)
+          itrack = tracks(1,1,itt,it-1)
           ! Number polygons ID assigned
-          Ntprev = COUNT(tracks(it-1,itt,:,2) /= 0)
-          IF (dbg) PRINT *,itt,'  track:', itrack, 'assigned:', tracks(it-1,itt,1:Ntprev,2)
+          Ntprev = COUNT(tracks(2,:,itt,it-1) /= 0)
+          IF (dbg) PRINT *,itt,'  track:', itrack, 'assigned:', tracks(2,1:Ntprev,itt,it-1)
 
           ! Looking for coincidencies
           DO iip=1, Ntprev
-            IF (tracks(it-1,itt,iip,2) == SpolysIndep(it,i)) THEN
+            IF (tracks(2,iip,itt,it-1) == SpolysIndep(i,it)) THEN
               Indeppolychained = .TRUE.
-              IF (dbg) PRINT *,'    coincidence found by polygon:', tracks(it-1,itt,iip,2)
+              IF (dbg) PRINT *,'    coincidence found by polygon:', tracks(2,iip,itt,it-1)
               EXIT
             END IF
@@ -259,15 +249,14 @@
             ictrack = Ntracks(it)
             ! Assigning all the IDs to the next step of the track
-            DO iip=1, NpolysIndep(it,i)
-              iiprev = polysIndep(it,i,iip)
-              tracks(it,ictrack,iip,1) = itrack
-              tracks(it,ictrack,iip,2) = iiprev
-              ixp = ctrpolys(it,iiprev,1)
-              iyp = ctrpolys(it,iiprev,2)
-              tracks(it,ictrack,iip,3) = ixp
-              tracks(it,ictrack,iip,4) = iyp
-              tracks(it,ictrack,iip,5) = it
+            DO iip=1, NpolysIndep(i,it)
+              iiprev = polysIndep(i,iip,it)
+              tracks(1,iip,ictrack,it) = itrack
+              tracks(2,iip,ictrack,it) = iiprev
+              ixp = ctrpolys(1,iiprev,it)
+              iyp = ctrpolys(2,iiprev,it)
+              tracks(3,iip,ictrack,it) = ixp
+              tracks(4,iip,ictrack,it) = iyp
+              tracks(5,iip,ictrack,it) = it
             END DO
-            tracks_it(it,ictrack) = itrack
             EXIT
           END IF
@@ -279,19 +268,18 @@
           ictrack = Ntracks(it)
           ! ID of new track
-          maxtrack = INT(MAXVAL(tracks(:,:,:,1)*1.))
+          maxtrack = INT(MAXVAL(tracks(1,:,:,:)*1.))
           IF (dbg) PRINT *,'  New track!', maxtrack+1
 
           ! Assigning all the IDs to the next step of the track
-          DO j=1, NpolysIndep(it,i)
-            iiprev = polysIndep(it,i,j)
-            tracks(it,ictrack,j,1) = maxtrack+1
-            tracks(it,ictrack,j,2) = iiprev
-            ixp = ctrpolys(it,iiprev,1)
-            iyp = ctrpolys(it,iiprev,2)
-            tracks(it,ictrack,j,3) = ixp
-            tracks(it,ictrack,j,4) = iyp
-            tracks(it,ictrack,j,5) = it
+          DO j=1, NpolysIndep(i,it)
+            iiprev = polysIndep(i,j,it)
+            tracks(1,j,ictrack,it) = maxtrack+1
+            tracks(2,j,ictrack,it) = iiprev
+            ixp = ctrpolys(1,iiprev,it)
+            iyp = ctrpolys(2,iiprev,it)
+            tracks(3,j,ictrack,it) = ixp
+            tracks(4,j,ictrack,it) = iyp
+            tracks(5,j,ictrack,it) = it
           END DO
-          tracks_it(it,Ntracks(it)) = maxtrack+1
         END IF
 
@@ -300,6 +288,6 @@
       IF (dbg) PRINT *,'  At this time-step:', it, ' N trajectories:', Ntracks(it)
       DO i=1, Ntracks(it)
-        Nprev = COUNT(INT(tracks(it,i,:,2)) /= 0)
-        PRINT *,i,'tracks:', tracks(it,i,1:Nprev,2)
+        Nprev = COUNT(INT(tracks(2,:,i,it)) /= 0)
+        PRINT *,i,'tracks:', tracks(2,1:Nprev,i,it)
       END DO
 
@@ -310,16 +298,16 @@
 
     finaltracks = 0.
-    maxtrack = MAXVAL(tracks(:,:,:,1))
+    maxtrack = MAXVAL(tracks(1,:,:,:))
 
     DO it=1, dt
       DO itt=1, Ntracks(it)
-        itrack = INT(tracks(it,itt,1,1))
-        Nprev = COUNT(INT(tracks(it,itt,:,2)) /= 0)
+        itrack = INT(tracks(1,1,itt,it))
+        Nprev = COUNT(INT(tracks(2,:,itt,it)) /= 0)
         PRINT *,'it:', it,'itrack:', itrack, 'Nprev:', Nprev
-        finaltracks(it,itrack,1) = itrack*1.
-        finaltracks(it,itrack,2) = SUM(tracks(it,itt,:,3))/Nprev
-        finaltracks(it,itrack,3) = SUM(tracks(it,itt,:,4))/Nprev
-        finaltracks(it,itrack,4) = it*1.
-        PRINT *,'  finaltrack:', finaltracks(it,itrack,:)
+        finaltracks(1,itrack,it) = itrack*1.
+        finaltracks(2,itrack,it) = SUM(tracks(3,:,itt,it))/Nprev
+        finaltracks(3,itrack,it) = SUM(tracks(4,:,itt,it))/Nprev
+        finaltracks(4,itrack,it) = it*1.
+        PRINT *,'  finaltrack:', finaltracks(:,itrack,it)
       END DO
     END DO
@@ -339,6 +327,6 @@
     INTEGER, INTENT(in)                                  :: dx, dy, Nallpoly, Npoly
     INTEGER, DIMENSION(dx,dy), INTENT(in)                :: allpoly, polys
-    REAL(r_k), DIMENSION(Nallpoly,2), INTENT(in)         :: icpolys
-    REAL(r_k), DIMENSION(Npoly,2), INTENT(in)            :: cpolys
+    REAL(r_k), DIMENSION(2,Nallpoly), INTENT(in)         :: icpolys
+    REAL(r_k), DIMENSION(2,Npoly), INTENT(in)            :: cpolys
     REAL(r_k), DIMENSION(Npoly), INTENT(in)              :: apolys
     INTEGER, DIMENSION(Nallpoly), INTENT(out)            :: polycoins
@@ -383,5 +371,5 @@
         DO j=1, Npoly
           IF (coinNptss(ip,j) == maxcoin) THEN
-            dist = SQRT( (icpolys(ip,1)*1.-cpolys(j,1)*1.)**2 + (icpolys(ip,2)*1.-cpolys(j,2)*1.)**2 )
+            dist = SQRT( (icpolys(1,ip)*1.-cpolys(1,j)*1.)**2 + (icpolys(2,ip)*1.-cpolys(2,j)*1.)**2 )
             IF ( dist > maxcoindist) THEN
               maxcoindist = dist
@@ -481,5 +469,4 @@
 
 END SUBROUTINE coincidence_poly
-
 
   SUBROUTINE all_polygons_properties(dbg, dx, dy, Npoly, polys, lon, lat, values, xres, yres, projN,  &
