source: dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/regr_pr_av_m.F90 @ 3983

Last change on this file since 3983 was 3895, checked in by ymipsl, 9 years ago

Make LMDZ5 be compliant to generate initiale state and compute in openMP mode suing unstructured grid.

YM

File size: 4.8 KB
Line 
1! $Id$
2module regr_pr_av_m
3
4  ! Author: Lionel GUEZ
5
6  implicit none
7
8contains
9
10  subroutine regr_pr_av(ncid, name, julien, press_in_edg, paprs, v3)
11
12    ! "regr_pr_av" stands for "regrid pressure averaging".
13    ! In this procedure:
14    ! -- the root process reads 2D latitude-pressure fields from a
15    !    NetCDF file, at a given day.
16    ! -- the fields are packed to the LMDZ horizontal "physics"
17    !    grid and scattered to all threads of all processes;
18    ! -- in all the threads of all the processes, the fields are regridded in
19    !    pressure to the LMDZ vertical grid.
20    ! We assume that, in the input file, the fields have 3 dimensions:
21    ! latitude, pressure, julian day.
22    ! We assume that the input fields are already on the "rlatu"
23    ! latitudes, except that latitudes are in ascending order in the input
24    ! file.
25    ! We assume that all the inputs fields have the same coordinates.
26
27    ! The target vertical LMDZ grid is the grid of layer boundaries.
28    ! Regridding in pressure is done by averaging a step function of pressure.
29
30    ! All the fields are regridded as a single multi-dimensional array
31    ! so it saves CPU time to call this procedure once for several NetCDF
32    ! variables rather than several times, each time for a single
33    ! NetCDF variable.
34
35    use dimphy, only: klon, klev
36    use mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
37    use netcdf95, only: nf95_inq_varid, handle_err
38    use netcdf, only: nf90_get_var
39    use assert_m, only: assert
40    use assert_eq_m, only: assert_eq
41    use regr1_step_av_m, only: regr1_step_av
42    use mod_phys_lmdz_mpi_data, only: is_mpi_root
43
44    use mod_phys_lmdz_transfert_para, only: scatter2d
45    ! (pack to the LMDZ horizontal "physics" grid and scatter)
46
47    integer, intent(in):: ncid ! NetCDF ID of the file
48
49!ym   character(len=*), intent(in):: name(:) ! of the NetCDF variables
50!ym  for strange reason, ifort doens't detect correctly the array size (return 0)
51!ym   compilator bug , version dependent ?
52
53    character(len=*), intent(in):: name(:) ! of the NetCDF variables
54    integer, intent(in):: julien ! jour julien, 1 <= julien <= 360
55
56    real, intent(in):: press_in_edg(:)
57    ! edges of pressure intervals for input data, in Pa, in strictly
58    ! ascending order
59
60    real, intent(in):: paprs(:, :) ! (klon, klev + 1)
61    ! (pression pour chaque inter-couche, en Pa)
62
63    real, intent(out):: v3(:, :, :) ! (klon, klev, size(name))
64    ! regridded fields on the partial "physics" grid
65    ! "v3(i, k, l)" is at longitude "xlon(i)", latitude
66    ! "xlat(i)", in pressure interval "[paprs(i, k+1), paprs(i, k)]",
67    ! for NetCDF variable "name(l)".
68
69    ! Variables local to the procedure:
70
71    integer varid, ncerr ! for NetCDF
72
73    real  v1(nbp_lon, nbp_lat, size(press_in_edg) - 1, size(v3,3))
74    ! input fields at day "julien", on the global "dynamics" horizontal grid
75    ! First dimension is for longitude.
76    ! The values are the same for all longitudes.
77    ! "v1(:, j, k, l)" is at latitude "rlatu(j)", for
78    ! pressure interval "[press_in_edg(k), press_in_edg(k+1)]" and
79    ! NetCDF variable "name(l)".
80
81    real v2(klon, size(press_in_edg) - 1, size(v3,3))
82    ! fields scattered to the partial "physics" horizontal grid
83    ! "v2(i, k, l)" is at longitude "xlon(i)", latitude "xlat(i)",
84    ! for pressure interval "[press_in_edg(k), press_in_edg(k+1)]" and
85    ! NetCDF variable "name(l)".
86
87    integer i, n_var
88
89    !--------------------------------------------
90
91    call assert(size(v3, 1) == klon, size(v3, 2) == klev, "regr_pr_av v3 klon")
92    PRINT *,'size name',size(name)
93!ym    n_var = assert_eq(size(name), size(v3, 3), "regr_pr_av v3 n_var")
94!ym intel bugs compiler : ifort doesn't detect correcte size of nme (return 0)
95
96    n_var = size(v3, 3)
97
98    call assert(shape(paprs) == (/klon, klev+1/), "regr_pr_av paprs")
99
100    !$omp master
101    if (is_mpi_root) then
102       do i = 1, n_var
103          call nf95_inq_varid(ncid, trim(name(i)), varid)
104         
105          ! Get data at the right day from the input file:
106          ncerr = nf90_get_var(ncid, varid, v1(1, :, :, i), &
107               start=(/1, 1, julien/))
108          call handle_err("regr_pr_av nf90_get_var " // trim(name(i)), ncerr, &
109               ncid)
110       end do
111       
112       ! Latitudes are in ascending order in the input file while
113       ! "rlatu" is in descending order so we need to invert order:
114       v1(1, :, :, :) = v1(1, nbp_lat:1:-1, :, :)
115
116       ! Duplicate on all longitudes:
117       v1(2:, :, :, :) = spread(v1(1, :, :, :), dim=1, ncopies=nbp_lon-1)
118    end if
119    !$omp end master
120
121    call scatter2d(v1, v2)
122
123    ! Regrid in pressure at each horizontal position:
124    do i = 1, klon
125       v3(i, klev:1:-1, :) = regr1_step_av(v2(i, :, :), press_in_edg, &
126            paprs(i, klev+1:1:-1))
127       ! (invert order of indices because "paprs" is in descending order)
128    end do
129
130  end subroutine regr_pr_av
131
132end module regr_pr_av_m
Note: See TracBrowser for help on using the repository browser.