source: lmdz_wrf/trunk/tools/trajectories_overlap.f90 @ 2343

Last change on this file since 2343 was 1664, checked in by lfita, 7 years ago

Making use of the new module `module_NCgeneric.f90'

File size: 9.1 KB
RevLine 
[1656]1PROGRAM trajectories_overlap
2! Porgram to compute trajectories from a series of consecutive maps of polygons
3! L. Fita, CIMA. September 2017
4!
5!   To be used with a netcdf file which must contain:
6!     - Series of 2D maps of polygons (numbered by integers)
7!     - Areal weighted centers of the polygons
8!     - Weighted areas of the polygons
9!
10
11  USE module_definitions
12  USE module_generic
[1664]13  USE module_NCgeneric
[1656]14  USE module_scientific
15  USE netcdf
16
17  IMPLICIT NONE
18
19! namelist
[1660]20  INTEGER                                                :: missing_value
[1656]21  REAL(r_k)                                              :: minarea
22  CHARACTER(len=50)                                      :: polyfilename
23  CHARACTER(len=50)                                      :: poly2Dn, polyNn, polywctrn, polywarean,   &
[1661]24    varnresx, varnresy, timen, projectn, waycomp, waymulti
[1656]25  LOGICAL                                                :: debug
26
27! Local
[1659]28  INTEGER                                                :: funit, ncunit, rcode
29  INTEGER                                                :: ierr, ios
[1656]30  INTEGER                                                :: Nvdims
31  INTEGER, DIMENSION(1)                                  :: dims1D
32  INTEGER, DIMENSION(2)                                  :: dims2D
33  INTEGER, DIMENSION(3)                                  :: dims3D
34  INTEGER, DIMENSION(:), ALLOCATABLE                     :: polyN
[1659]35  INTEGER, DIMENSION(:,:,:), ALLOCATABLE                 :: polys
[1656]36  REAL(r_k)                                              :: resx, resy
37  REAL(r_k), DIMENSION(:,:), ALLOCATABLE                 :: polyas
[1660]38  REAL(r_k), DIMENSION(:,:,:), ALLOCATABLE               :: polywctrs
[1656]39  LOGICAL                                                :: gotvar
40  CHARACTER(len=3)                                       :: num3S
41  INTEGER                                                :: dimx, dimy, dimt, Ncoord, Nmaxpoly,       &
42    Nmaxtrack
43
[1659]44  NAMELIST /files/ polyfilename, poly2Dn, polyNn, polywctrn, polywarean, varnresx, varnresy, timen
[1661]45  NAMELIST /config/ missing_value, minarea, projectn, waycomp, waymulti, debug
[1656]46
47!!!!!!!
48! polyfilename: name of the file with the polygon information
49! poly2Dn: name of the variable with the maps of 2D polygons
50! polyNn: name of the variable with the number of 2D polygons per time-step
51! polywctrn: name of the varibale with the coordinates of the weighted centers of all the polygons
52! polywarean: name of the variable with the weighted areas of the polygons
53! varnres[x/y]: name of the variable with the resolution along x an y axis (same units as polywarean)
54! lonn: name of the variable with the longitudes
55! latn: name of the variable with the latitudes
56! timen: name of the variable with the times
[1660]57! missing_value: value to determine the missing value
[1656]58! minarea: minimal area of the polygons to determine which polygons to use
59! projectn: name of the projection of the data in the file. Available ones:
60!   'ctsarea': Constant Area
61!   'lon/lat': for regular longitude-latitude
62!   'nadir-sat,[lonNADIR],[latNADIR]': for satellite data with the resolution given at nadir (lonNADIR, latNADIR)
[1660]63! waycomp: how to copmute when intermediate ASCII files are used
64!   'scratch': everything from the beginning
65!   'continue': skipt that parts which already have the ascii file written
[1661]66! waymulti: methodology to follow when multiple polygons are given for the same track
67!   'mean': get coordinates from the areal-weighted mean of the centers of the given polygons and their areas
68!   'largest': get the coorindates of the largest polygon
69!   'closest': get the coordinates of the closest polygon
[1656]70
71  fname = 'trajectories_overlap'
72
73  ! Reading namelist
[1659]74  funit = freeunit()
[1656]75  OPEN(funit, file='trajectories_overlap.namelist', status='old', form='formatted', iostat=ios)
76  msg = "Problems reading nameslit file: 'trajectories_overlap.namelist'"
77  CALL ErrMsg(msg, 'main', ios)
78
79  READ(funit, files)
80  READ(funit, config)
81  CLOSE(funit)
82
83  ! Opening polygon file
[1659]84  rcode = nf90_open(TRIM(polyfilename), NF90_NOWRITE, ncunit)
[1656]85  IF (rcode /= NF90_NOERR) CALL handle_err(rcode)
86
87  ! Getting variables
88  !!
89
90  ! polygons
[1659]91  gotvar = isin_ncunit(ncunit, poly2Dn)
[1656]92  IF (.NOT.gotvar) THEN
[1659]93    msg= "File '"//TRIM(polyfilename)//"' does not have 2D polygons variable '" // TRIM(poly2Dn) // "'"
[1656]94    CALL ErrMsg(msg, fname, -1)
95  END IF
[1659]96  Nvdims = get_varNdims_ncunit(ncunit, poly2Dn)
[1656]97  IF (Nvdims /= 3) THEN
98    WRITE(num3S,'(I3)')Nvdims
99    msg = "Variable with 2D polygons '" // TRIM(poly2Dn) // "' has: " // num3S //                     &
100      " it must have 3 dimensions !!"
101    CALL ErrMsg(msg, fname, -1)
102  END IF
103  dims3D = get_var3dims_ncunit(ncunit, poly2Dn)
104  IF (ALLOCATED(polys)) DEALLOCATE(polys)
105  ALLOCATE(polys(dims3D(1), dims3D(2), dims3D(3)), STAT=ierr)
106  msg = "Problems allocating 'polys'"
107  CALL ErrMsg(msg, fname, ierr)
[1659]108  CALL get_varI3D_ncunit(ncunit, dims3D(1), dims3D(2), dims3D(3), poly2Dn, polys)
109  IF (debug) PRINT *,"  Got variable 'polys':", UBOUND(polys)
[1656]110
[1660]111  ! Getting rid of the mising values
112  WHERE (polys == missing_value)
113    polys = 0
114  END WHERE
115
[1656]116  dimx = dims3D(1)
117  dimy = dims3D(2)
118  dimt = dims3D(3)
119
120  ! N polygons
[1659]121  gotvar = isin_ncunit(ncunit, polyNn)
[1656]122  IF (.NOT.gotvar) THEN
[1659]123    msg="File '"//TRIM(polyfilename)//"' does not have number of polygons variable '" // TRIM(polyNn) &
124      // "'"
[1656]125    CALL ErrMsg(msg, fname, -1)
126  END IF
[1659]127  Nvdims = get_varNdims_ncunit(ncunit, polyNn)
128  IF (Nvdims /= 1) THEN
[1656]129    WRITE(num3S,'(I3)')Nvdims
130    msg = "Variable with N polygons '" // TRIM(polyNn) // "' has: " // num3S //                       &
[1659]131      " it must have 1 dimensions !!"
[1656]132    CALL ErrMsg(msg, fname, -1)
133  END IF
[1659]134  dims1D = get_var1dims_ncunit(ncunit, polyNn)
[1656]135  IF (ALLOCATED(polyN)) DEALLOCATE(polyN)
[1659]136  ALLOCATE(polyN(dims1D(1)), STAT=ierr)
[1656]137  msg = "Problems allocating 'polyN'"
138  CALL ErrMsg(msg, fname, ierr)
[1659]139  CALL get_varI1D_ncunit(ncunit, dims1D(1), polyNn, polyN)
140  IF (debug) PRINT *,"  Got variable 'polyN':", UBOUND(polyN)
[1656]141
142  ! weighted-center of the polygons
[1659]143  gotvar = isin_ncunit(ncunit, polywctrn)
[1656]144  IF (.NOT.gotvar) THEN
[1659]145    msg = "File '" // TRIM(polyfilename) // "' does not have weighted-centerd polygon variable '" //  &
[1656]146      TRIM(polywctrn) // "'"
147    CALL ErrMsg(msg, fname, -1)
148  END IF
[1659]149  Nvdims = get_varNdims_ncunit(ncunit, polywctrn)
[1656]150  IF (Nvdims /= 3) THEN
151    WRITE(num3S,'(I3)')Nvdims
152    msg = "Variable with weighted-centerd polygons '" // TRIM(polywctrn) // "' has: " // num3S //     &
153      " it must have 3 dimensions !!"
154    CALL ErrMsg(msg, fname, -1)
155  END IF
156  dims3D = get_var3dims_ncunit(ncunit, polywctrn)
157  IF (ALLOCATED(polywctrs)) DEALLOCATE(polywctrs)
158  ALLOCATE(polywctrs(dims3D(1), dims3D(2), dims3D(3)), STAT=ierr)
159  msg = "Problems allocating 'polywctrs'"
160  CALL ErrMsg(msg, fname, ierr)
[1659]161  CALL get_varRK3D_ncunit(ncunit, dims3D(1), dims3D(2), dims3D(3), polywctrn, polywctrs)
162  IF (debug) PRINT *,"  Got variable 'polyectrs':", UBOUND(polywctrs)
[1656]163  Nmaxpoly = dims3D(2)
164  Ncoord = dims3D(3)
165
166  ! area of polygons
[1659]167  gotvar = isin_ncunit(ncunit, polywarean)
[1656]168  IF (.NOT.gotvar) THEN
[1659]169    msg = "File '" // TRIM(polyfilename) // "' does not have variable '" // TRIM(polywarean) // "'"
[1656]170    CALL ErrMsg(msg, fname, -1)
171  END IF
[1659]172  Nvdims = get_varNdims_ncunit(ncunit, polywarean)
173  IF (Nvdims /= 2) THEN
[1656]174    WRITE(num3S,'(I3)')Nvdims
175    msg = "Variable with area polygons '" // TRIM(polywarean) // "' has: " // num3S //                &
176      " it must have 2 dimensions !!"
177    CALL ErrMsg(msg, fname, -1)
178  END IF
179  dims2D = get_var2dims_ncunit(ncunit, polywarean)
180  IF (ALLOCATED(polyas)) DEALLOCATE(polyas)
181  ALLOCATE(polyas(dims2D(1), dims2D(2)), STAT=ierr)
182  msg = "Problems allocating 'polyas'"
183  CALL ErrMsg(msg, fname, ierr)
[1659]184  CALL get_varRK2D_ncunit(ncunit, dims2D(1), dims2D(2), polywarean, polyas)
185  IF (debug) PRINT *,"  Got variable 'polyas':", UBOUND(polyas)
[1656]186
187  ! Resolution along x/y axis
[1659]188  gotvar = isin_ncunit(ncunit, varnresx)
[1656]189  IF (.NOT.gotvar) THEN
[1659]190    msg = "File '" // TRIM(polyfilename) // "' does not have variable '" // TRIM(varnresx) // "'"
[1656]191    CALL ErrMsg(msg, fname, -1)
192  END IF
[1659]193  CALL get_varRK0D_ncunit(ncunit, varnresx, resx)
194  IF (debug) PRINT *,"  Got variable 'resx':", resx
195
196  gotvar = isin_ncunit(ncunit, varnresy)
[1656]197  IF (.NOT.gotvar) THEN
[1659]198    msg = "File '" // TRIM(polyfilename) // "' does not have variable '" // TRIM(varnresy) // "'"
[1656]199    CALL ErrMsg(msg, fname, -1)
200  END IF
[1659]201  CALL get_varRK0D_ncunit(ncunit, varnresy, resy)
202  IF (debug) PRINT *,"  Got variable 'resy':", resy
[1656]203
[1659]204  rcode = nf90_close(ncunit)
[1656]205  IF (rcode /= NF90_NOERR) CALL handle_err(rcode)
206
207  ! Preparing outcome (trajectories)
208  Nmaxtrack = Nmaxpoly*dimt/100
209
210  IF (debug) THEN
211    PRINT *,'Computing trajectories _______'
212    PRINT *,'  2D space:', dimx, ' x', dimy
213    PRINT *,'  number time-steps:', dimt
214    PRINT *,'  maximum number of polygons:', Nmaxpoly
215    PRINT *,'  maximum number of trajectories:', Nmaxtrack
216  END IF
217 
218  ! Computing trajectories
[1660]219  CALL poly_overlap_tracks_area_ascii(debug, waycomp, dimx, dimy, dimt, minarea, polyN, polys,        &
[1661]220    polywctrs, polyas, Nmaxpoly, Nmaxtrack, waymulti)
[1656]221
222  ! Writting trajectory files
223
224  DEALLOCATE(polys)
[1659]225  DEALLOCATE(polyN)
[1656]226  DEALLOCATE(polywctrs)
227  DEALLOCATE(polyas)
228
229END PROGRAM trajectories_overlap
230
Note: See TracBrowser for help on using the repository browser.