source: LMDZ6/branches/IPSLCM6.0.15/libf/phylmd/regr_pr_time_av_m.F90 @ 3828

Last change on this file since 3828 was 3314, checked in by Laurent Fairhead, 6 years ago

Solving reproductibility problem?

File size: 34.0 KB
Line 
1! $Id$
2MODULE regr_pr_time_av_m
3
4  USE write_field_phy
5  USE mod_phys_lmdz_transfert_para, ONLY: bcast
6  USE mod_phys_lmdz_para, ONLY: mpi_rank, omp_rank
7  USE print_control_mod,  ONLY: prt_level
8  IMPLICIT NONE
9
10!-------------------------------------------------------------------------------
11! Purpose: Regrid pressure with an averaging method. Operations done:
12!  * on the root process: read a NetCDF 3D or 4D field at a given day.
13!  * pack the fields to the LMDZ "horizontal "physics" grid and scatter
14!    to all threads of all processes;
15!  * in all the threads of all the processes, regrid the fields in pressure
16!    to the LMDZ vertical grid.
17!  * the forcing fields are stretched if the following arguments are present:
18!     - "lat_in":  input file latitudes.
19!     - "Ptrp_ou": target grid (LMDZ) tropopause pressure.
20!   so that the tropopause is shifted to the position of the one of LMDZ.
21!  Note that the ozone quantity conservation is not ensured.
22!-------------------------------------------------------------------------------
23! Initial routine: regr_pr_av_m module (L. Guez).
24! Present version: David Cugnet ; corresponding additions:
25!    - time interpolation
26!    - 3D compliant
27!    - vertical stretching of the field to allow tropopause matching between
28!    input field and current lmdz field.
29!-------------------------------------------------------------------------------
30! Remarks:
31!  * 3D fields are zonal means, with dimensions (latitude, pressure, julian day)
32!  * 4D fields have the dimensions:  (longitude, latitude, pressure, julian day)
33!  * All the fields are already on the horizontal grid (rlatu) or (rlatu,rlonv),
34!    except that the latitudes are in ascending order in the input file.
35!  * We assume that all the input fields have the same coordinates.
36!  * The target vertical LMDZ grid is the grid of layer centers.
37!  * Regridding in pressure can be:
38!    - Ploc=='I': pressures provided at Interfaces    => conservative 2nd order
39!         OK for ozone coefficients regridding in Cariolle routines.
40!    - Ploc=='C': pressures provides at cells Centers => classical linear       
41!         OK for ozone vertical regridding, especially when torpopause
42!      adjustment is activated, to avoid "strairs shape effect" on profiles.
43!  * All the fields are regridded as a single multi-dimensional array, so it
44!    saves CPU time to call this procedure once for several NetCDF variables
45!    rather than several times, each time for a single NetCDF variable.
46!  * The input file pressure at tropopause can be (in decreasing priority):
47!    1) read from the file if "tropopause_air_pressure" field is available.
48!    2) computed using "tro3" and "tro3_at_tropopause' (if available).
49!    3) computed using "tro3" and a fixed threshold otherwise, constant or
50!    determined using an empirical three parameters law:
51!         o3t(ppbV)=co1+co2*SIN(PI*(month-2)/6)*TANH(lat_deg/co3)
52!       => co1 and co2 are in ppbV, and co3 in degrees.
53!       => co3 allow a smooth transition between north and south hemispheres.
54!  * If available, the field "ps" (input file pressure at surface) is used.
55!    If not, the current LMDZ ground pressure is taken instead.
56!  * Fields with suffix "m"/"p" are at the closest records earlier/later than
57!  the mid-julian day "julien", on the global "dynamics" horizontal grid.
58!  * Fields(i,j,k,l) are at longitude-latitude-name "rlonv(i)-rlatu(j)-nam(l)",
59!    pressure level/interval (Ploc=="C"/"I") "pcen_in(k)"/"pcen_in(k:k+1)]".
60!  * In the 2D file case, the values are the same for all longitudes.
61!  * The tropopause correction works like this: the input fields (file) are
62!  interpolated on output (LMDZ) pressure field, which is streched using a power
63!  law in a limited zone made of 2 layers:
64!    1) between lower bound (lower than lowest tropopause) and LMDZ tropopause
65!    2) between LMDZ tropopause and upper bound (higher thzn highest tropopause)
66!  The stretching function has the following form:
67!        Sigma_str = Sigma^(1+alpha*phi(Sigma)), where:
68!   * alpha=LOG(SigT_in/SigT_ou)/LOG(SigT_ou)
69!     This value shifts the file tropopause to the height of the one of LMDZ.
70!   * phi is quasi-linear (sections of 1/log function) in the adjacent intervals:
71!       - from 0 to 1 in [Sig_top,SigT_ou]
72!       - from 1 to 0 in [SigT_ou,Sig_bot]
73!  This quasi-triangular localization function ponderates alpha-law from one near
74!  the tropopause to zero each side apart.
75!
76! * The following fields are on the global "dynamics" grid, as read from files:
77  REAL,    SAVE, ALLOCATABLE :: v1 (:,:,:,:)       !--- Current  time ozone fields
78! v1: Field read/interpol at time "julien" on the global "dynamics" horiz. grid.
79  REAL,    SAVE, ALLOCATABLE :: v1m(:,:,:,:)       !--- Previous time ozone fields
80  REAL,    SAVE, ALLOCATABLE :: v1p(:,:,:,:)       !--- Next     time ozone fields
81  REAL,    SAVE, ALLOCATABLE :: pgm(:,:), pgp(:,:) !--- Ground     pressure
82  REAL,    SAVE, ALLOCATABLE :: ptm(:,:), ptp(:,:) !--- Tropopause pressure
83  REAL,    SAVE, ALLOCATABLE :: otm(:,:), otp(:,:) !--- Tropopause o3 mix. ratio
84  INTEGER, SAVE :: ntim_in                         !--- Records nb in input file
85  INTEGER, SAVE :: itrp0                           !--- idx above chem tropop.
86  INTEGER, SAVE :: irec                            !--- Current time index
87!      * for daily   input files: current julian day number
88!      * for monthly input files: julien is in [time_in(irec),time_in(irec+1)]
89  LOGICAL, SAVE :: linterp                         !--- Interpolation in time
90  LOGICAL, SAVE :: lPrSfile                        !--- Surface pressure flag
91  LOGICAL, SAVE :: lPrTfile                        !--- Tropopause pressure flag
92  LOGICAL, SAVE :: lO3Tfile                        !--- Tropopause ozone flag
93  LOGICAL, SAVE :: lfirst=.TRUE.                   !--- First call flag
94!$OMP THREADPRIVATE(lfirst)
95  REAL,    PARAMETER :: pTropUp=9.E+3 !--- Value  <  tropopause pressure (Pa)
96  REAL,    PARAMETER :: gamm   =0.4   !--- Max. stretched layer sigma ratio
97  REAL,    PARAMETER :: rho    =1.4   !--- Max tropopauses sigma ratio
98  REAL,    PARAMETER :: o3t0   =1.E-7 !--- Nominal O3 vmr at tropopause
99  LOGICAL, PARAMETER :: lO3Tpara=.FALSE. !--- Parametrized O3 vmr at tropopause
100  LOGICAL, PARAMETER :: ldebug=.FALSE.!--- Force writefield_phy multiple outputs
101  REAL, PARAMETER :: ChemPTrMin=9.E+3 !--- Thresholds for minimum and maximum
102  REAL, PARAMETER :: ChemPTrMax=4.E+4 !    chemical  tropopause pressure (Pa).
103  REAL, PARAMETER :: DynPTrMin =8.E+3 !--- Thresholds for minimum and maximum
104  REAL, PARAMETER :: DynPTrMax =4.E+4 !    dynamical tropopause pressure (Pa).
105
106CONTAINS
107
108!-------------------------------------------------------------------------------
109!
110SUBROUTINE regr_pr_time_av(fID, nam, julien, Ploc, Pre_in, Pre_ou, v3, Pgnd_ou,&
111                                             time_in, lon_in, lat_in, Ptrp_ou)
112!
113!-------------------------------------------------------------------------------
114  USE dimphy,         ONLY: klon
115  USE netcdf95,       ONLY: NF95_INQ_VARID, NF95_INQUIRE_VARIABLE, handle_err, &
116                            NF95_INQ_DIMID, NF95_INQUIRE_DIMENSION
117  USE netcdf,         ONLY: NF90_INQ_VARID, NF90_GET_VAR, NF90_NOERR
118  USE assert_m,       ONLY: assert
119  USE assert_eq_m,    ONLY: assert_eq
120  USE comvert_mod,    ONLY: scaleheight
121  USE interpolation,  ONLY: locate
122  USE regr_conserv_m, ONLY: regr_conserv
123  USE regr_lint_m,    ONLY: regr_lint
124  USE slopes_m,       ONLY: slopes
125  USE mod_phys_lmdz_mpi_data,       ONLY: is_mpi_root
126  USE mod_grid_phy_lmdz, ONLY: nlon=>nbp_lon, nlat=>nbp_lat, nlev_ou=>nbp_lev
127  USE mod_phys_lmdz_transfert_para, ONLY: scatter2d, scatter
128  USE phys_cal_mod,                 ONLY: calend, year_len, days_elapsed, jH_cur
129!-------------------------------------------------------------------------------
130! Arguments:
131  INTEGER,           INTENT(IN) :: fID        !--- NetCDF file ID
132  CHARACTER(LEN=13), INTENT(IN) :: nam(:)     !--- NetCDF variables names
133  REAL,              INTENT(IN) :: julien     !--- Days since Jan 1st
134  CHARACTER(LEN=1),  INTENT(IN) :: Ploc       !--- Pressures locations
135  !--- File/LMDZ (resp. decreasing & increasing order) pressure, Pa
136  !    At cells centers or interfaces depending on "Ploc" keyword (C/I)
137  REAL,    INTENT(IN)  :: Pre_in(:)           !--- in:  file      (nlev_in[+1])
138  REAL,    INTENT(IN)  :: Pre_ou(:,:)         !--- out: LMDZ (klon,nlev_ou[+1])
139  REAL,    INTENT(OUT) :: v3(:,:,:)           !--- Regr. fld (klon,nlev_ou,n_var)
140  REAL,    INTENT(IN), OPTIONAL :: Pgnd_ou(:) !--- LMDZ ground pressure   (klon)
141  REAL,    INTENT(IN), OPTIONAL :: time_in(:) !--- Records times, in days
142                                              !    since Jan 1 of current year
143  REAL,    INTENT(IN), OPTIONAL :: lon_in(:)  !--- File longitudes vector (klon)
144  REAL,    INTENT(IN), OPTIONAL :: lat_in(:)  !--- File latitudes  vector (klon)
145  REAL,    INTENT(IN), OPTIONAL :: Ptrp_ou(:) !--- LMDZ tropopause pres   (klon)
146!-------------------------------------------------------------------------------
147! Local variables:
148  include "clesphys.h"
149  include "YOMCST.h"
150  CHARACTER(LEN=80)  :: sub
151  CHARACTER(LEN=320) :: str
152  INTEGER :: vID, ncerr, n_var, ibot, iout, nn
153  INTEGER :: i, nlev_in, n_dim, itop, itrp, i0
154  LOGICAL :: lAdjTro                          !--- Need to adjust tropopause
155  REAL    :: y_frac                           !--- Elapsed year fraction
156  REAL    :: alpha, beta, al                  !--- For stretching/interpolation
157  REAL    :: SigT_in, SigT_ou                 !--- Input and output tropopauses
158  REAL    :: Sig_bot, Sig_top                 !--- Bounds of quasi-hat function
159  REAL    :: Sig_bo0, Sig_to0                 !--- Lower/upper tropopauses
160  REAL    :: Sig_in (SIZE(Pre_in))            !--- Input field sigma levels
161  REAL    :: Sig_ou (SIZE(Pre_ou,2))          !--- Output LMDZ sigma levels
162  REAL    :: phi    (SIZE(Pre_ou,2))          !--- Stretching exponent anomaly
163  REAL    :: Pstr_ou(SIZE(Pre_ou,2))          !--- Stretched pressure levels
164  REAL    :: Pres_ou(SIZE(Pre_ou,2))          !--- Pre_ou(i,:), reversed order
165  REAL, DIMENSION(nlon, nlat) :: pg1,      pt1,      ot1
166  REAL, DIMENSION(klon)       :: Pgnd_in,  Ptrp_in,  Otrp_in
167  REAL, DIMENSION(klon)       :: Ptrop_ou, Pgrnd_ou
168! * The following fields are scattered to the partial "physics" horizontal grid.
169  REAL, POINTER :: v2(:,:,:)                  !--- Current  time ozone fields
170!     In the 2D file case, the values are the same for all longitudes.
171!     "v2(i, k, l)" is at longitude-latitude "xlon(i)-xlat(i)" and name "nam(l)"
172! Both are:          * if Ploc=='I' in pressure interval "press_in_edg(k:k+1)"
173!                    * if Ploc=='C' at pressure          "press_in_cen(k)"
174  REAL, TARGET :: &
175    v2i(klon,SIZE(Pre_in)-1,SIZE(nam)), &     !--- v2 in Ploc=='I' case
176    v2c(klon,SIZE(Pre_in)  ,SIZE(nam))        !--- v2 in Ploc=='C' case
177  LOGICAL :: ll
178!--- For debug
179  REAL, DIMENSION(klon)             :: Ptrop_in, Ptrop_ef
180  REAL, DIMENSION(klon)             :: dzStrain, dzStrain0
181  REAL, DIMENSION(klon,SIZE(Pre_ou,2)) :: Pstrn_ou, phii
182!-------------------------------------------------------------------------------
183  sub="regr_pr_time_av"
184  nlev_in=SIZE(Pre_in); IF(Ploc=='I') nlev_in=nlev_in-1
185  IF(Ploc=='I') THEN; v2 => v2i; ELSE; v2 => v2c; END IF
186  CALL assert(SIZE(Pre_ou,1)==klon,TRIM(sub)//" Pre_ou klon")
187  CALL assert(SIZE(v3,1)==klon,    TRIM(sub)//" v3 klon")
188  CALL assert(SIZE(v3,2)==nlev_ou, TRIM(sub)//" v3 nlev_ou")
189  IF(Ploc=='I') CALL assert(SIZE(Pre_ou,2)==nlev_ou+1,TRIM(sub)//" Pre_ou nlev_ou+1")
190  IF(Ploc=='C') CALL assert(SIZE(Pre_ou,2)==nlev_ou  ,TRIM(sub)//" Pre_ou nlev_ou")
191  n_var = assert_eq(SIZE(nam),SIZE(v3,3),TRIM(sub)//" v3 n_var")
192  IF(PRESENT(Pgnd_ou)) CALL assert(SIZE(Pgnd_ou)==klon,TRIM(sub)//" Pgnd_ou klon")
193  IF(PRESENT(lon_in))  CALL assert(SIZE(lon_in )==klon,TRIM(sub)//" lon_in klon")
194  IF(PRESENT(lat_in))  CALL assert(SIZE(lat_in )==klon,TRIM(sub)//" lat_in klon")
195  IF(PRESENT(Ptrp_ou)) CALL assert(SIZE(Ptrp_ou)==klon,TRIM(sub)//" Ptrp_ou klon")
196  lAdjTro=PRESENT(Ptrp_ou)
197  IF(lAdjTro) THEN
198    IF(.NOT.PRESENT(lat_in)) &
199      CALL abort_physic(sub, 'Missing lat_in (required if adjust_tropopause=T)', 1)
200    IF(.NOT.PRESENT(Pgnd_ou).AND.Ploc=='C') &
201      CALL abort_physic(sub, 'Missing ground Pr(required if adjust_tropopause=T)', 1)
202    IF(PRESENT(Pgnd_ou)) THEN; Pgrnd_ou=Pgnd_ou; ELSE; Pgrnd_ou=Pre_ou(:,1); END IF
203  END IF
204
205  !$OMP MASTER
206  IF(is_mpi_root) THEN
207
208    !=== CHECK WHICH FIELDS ARE AVAILABLE IN THE INPUT FILE
209    IF(lfirst) THEN
210      lPrSfile=lAdjTro.AND.NF90_INQ_VARID(fID,"ps"                     ,vID)==NF90_NOERR
211      lPrTfile=lAdjTro.AND.NF90_INQ_VARID(fID,"tropopause_air_pressure",vID)==NF90_NOERR
212      lO3Tfile=lAdjTro.AND.NF90_INQ_VARID(fID,"tro3_at_tropopause"     ,vID)==NF90_NOERR
213      CALL NF95_INQ_DIMID(fID,"time",vID)
214      CALL NF95_INQUIRE_DIMENSION(fID,vID,nclen=ntim_in)
215      linterp=PRESENT(time_in).AND.ntim_in==14
216      ALLOCATE(v1(nlon,nlat,nlev_in,n_var))
217      IF(linterp) THEN
218        ALLOCATE(v1m(nlon,nlat,nlev_in,n_var),v1p(nlon,nlat,nlev_in,n_var))
219        IF(lPrSfile) ALLOCATE(pgm(nlon,nlat),pgp(nlon,nlat))
220        IF(lPrTfile) ALLOCATE(ptm(nlon,nlat),ptp(nlon,nlat))
221        IF(lO3Tfile) ALLOCATE(otm(nlon,nlat),otp(nlon,nlat))
222      END IF
223      !--- INITIAL INDEX: LOCATE A LAYER WELL ABOVE TROPOPAUSE (50hPa)
224      IF(lAdjTro) itrp0=locate(Pre_in,pTropUp)
225      CALL msg(linterp,'Monthly O3 files => ONLINE TIME INTERPOLATION.'    ,sub)
226      CALL msg(lPrSfile,'Using GROUND PRESSURE from input O3 forcing file.',sub)
227      CALL msg(lAdjTro ,'o3 forcing file tropopause location uses:'        ,sub)
228      IF(lPrTfile)      THEN; str='    INPUT FILE PRESSURE'
229      ELSE IF(lO3Tfile) THEN; str='    INPUT FILE O3 CONCENTRATION'
230      ELSE IF(lO3Tpara) THEN; str='    PARAMETRIZED O3 concentration'
231      ELSE;                   str='    CONSTANT O3 concentration'; END IF
232      CALL msg(lAdjTro,TRIM(str)//' at tropopause')
233    END IF
234
235    !=== UPDATE (ALWAYS FOR DAILY FILES, EACH MONTH FOR MONTHLY FILES)
236    CALL update_fields()
237
238    !=== TIME INTERPOLATION FOR MONTHLY INPUT FILES
239    IF(linterp) THEN
240      WRITE(str,'(a,f12.8,2(a,f5.1))')'Interpolating O3 at julian day ',julien,&
241        ' from fields at times ',time_in(irec),' and ', time_in(irec+1)
242      CALL msg(.TRUE.,str,sub)
243      al=(time_in(irec+1)-julien)/(time_in(irec+1)-time_in(irec))
244      v1=al*v1m+(1.-al)*v1p
245      IF(lPrSfile) pg1=al*pgm+(1.-al)*pgp
246      IF(lPrTfile) pt1=al*ptm+(1.-al)*ptp
247      IF(lO3Tfile) ot1=al*otm+(1.-al)*otp
248    END IF
249  END IF
250  !$OMP END MASTER
251  !$OMP BARRIER
252  IF(lfirst) THEN
253    lfirst=.FALSE.;       CALL bcast(lfirst)
254    IF(lAdjTro)           CALL bcast(itrp0)
255    CALL bcast(lPrSfile); CALL bcast(lPrTfile)
256    CALL bcast(lO3Tfile); CALL bcast(linterp)
257  END IF
258  CALL scatter2d(v1,v2)
259  IF(lPrSfile) CALL scatter2d(pg1,Pgnd_in)
260  IF(lPrTfile) CALL scatter2d(pt1,Ptrp_in)
261  IF(lO3Tfile) CALL scatter2d(ot1,Otrp_in)
262  !--- No ground pressure in input file => choose it to be the one of LMDZ
263  IF(lAdjTro.AND..NOT.lPrSfile) Pgnd_in(:)=Pgrnd_ou(:)
264 
265!-------------------------------------------------------------------------------
266  IF(.NOT.lAdjTro) THEN       !--- REGRID IN PRESSURE ; NO TROPOPAUSE ADJUSTMENT
267!-------------------------------------------------------------------------------
268    DO i=1,klon
269      Pres_ou=Pre_ou(i,SIZE(Pre_ou,2):1:-1)   !--- pplay & paprs are decreasing
270      IF(Ploc=='C') CALL regr_lint   (1,v2(i,:,:), LOG(Pre_in(:)),             &
271        LOG(Pres_ou(:)), v3(i,nlev_ou:1:-1,:))
272      IF(Ploc=='I') CALL regr_conserv(1,v2(i,:,:),     Pre_in(:) ,             &
273            Pres_ou(:) , v3(i,nlev_ou:1:-1,:), slopes(1,v2(i,:,:), Pre_in(:)))
274    END DO
275!-------------------------------------------------------------------------------
276  ELSE                        !--- REGRID IN PRESSURE ; TROPOPAUSE ADJUSTMENT
277!-------------------------------------------------------------------------------
278    y_frac=(REAL(days_elapsed)+jH_cur)/year_len
279
280    !--- OUTPUT SIGMA LEVELS
281    DO i=1,klon
282
283      !--- INPUT/OUTPUT (FILE/LMDZ) SIGMA LEVELS IN CURRENT COLUMN
284      Pres_ou   = Pre_ou(i,SIZE(Pre_ou,2):1:-1)!--- pplay & paprs are decreasing
285      Sig_in(:) = Pre_in (:)/Pgnd_in(i)            !--- increasing values
286      Sig_ou(:) = Pres_ou(:)/Pgnd_ou(i)            !--- increasing values
287
288      !--- INPUT (FILE) SIGMA LEVEL AT TROPOPAUSE ; extreme values are filtered
289      ! to keep tropopause pressure realistic ; high values are usually due to
290      ! ozone hole fooling the crude chemical tropopause detection algorithm.
291      SigT_in = get_SigTrop(i,itrp)
292      SigT_in=MIN(SigT_in,ChemPTrMax/Pgnd_in(i))   !--- too low  value filtered
293      SigT_in=MAX(SigT_in,ChemPTrMin/Pgnd_ou(i))   !--- too high value filtered
294
295      !--- OUTPUT (LMDZ) SIGMA LEVEL AT TROPOPAUSE ; too high variations of the
296      ! dynamical tropopause (especially in filaments) are conterbalanced with
297      ! a filter ensuring it stays within a certain distance around input (file)
298      ! tropopause, hence avoiding avoid a too thick stretched region ; a final
299      ! extra-safety filter keeps the tropopause pressure value realistic.
300      SigT_ou = Ptrp_ou(i)/Pgnd_ou(i)
301      IF(SigT_ou<SigT_in/rho) SigT_ou=SigT_in/rho  !--- too low  value w/r input
302      IF(SigT_ou>SigT_in*rho) SigT_ou=SigT_in*rho  !--- too high value w/r input
303      SigT_ou=MIN(SigT_ou,DynPTrMax/Pgnd_ou(i))    !--- too low  value filtered
304      SigT_ou=MAX(SigT_ou,DynPTrMin/Pgnd_ou(i))    !--- too high value filtered
305      Ptrop_ou(i)=SigT_ou*Pgnd_ou(i)
306      iout = locate(Sig_ou(:),SigT_ou)
307
308      !--- POWER LAW COEFFICIENT FOR TROPOPAUSES MATCHING
309      alpha = LOG(SigT_in/SigT_ou)/LOG(SigT_ou)
310
311      !--- DETERMINE STRETCHING DOMAIN UPPER AND LOWER BOUNDS
312      Sig_bo0 = MAX(SigT_in,SigT_ou)               !--- lowest  tropopause
313      Sig_to0 = MIN(SigT_in,SigT_ou)               !--- highest tropopause
314      beta    = (Sig_bo0/Sig_to0)**gamm            !--- stretching exponent
315      Sig_bot = MIN(Sig_bo0*beta,0.1*(9.+Sig_bo0)) !--- must be <1
316      ibot = locate(Sig_ou(:),Sig_bot)             !--- layer index
317      IF(ibot-iout<2) THEN                         !--- at least one layer thick
318        ibot=MIN(iout+2,nlev_ou); Sig_bot=Sig_ou(ibot)
319      END IF
320      Sig_top = Sig_to0/beta                       !--- upper bound
321      itop = locate(Sig_ou(:),Sig_top)             !--- layer index
322      IF(iout-itop<2) THEN                         !--- at least one layer thick
323        itop=MAX(iout-2,1); Sig_top=Sig_ou(itop)
324      END IF
325
326      !--- STRETCHING POWER LAW LOCALIZATION FUNCTION:
327      !    0 in [0,Sig_top]    0->1 in [Sig_top,SigT_ou]
328      !    0 in [Sig_bot,1]    1->0 in [SigT_ou, Sig_bot]
329      phi(:)=0.
330      phi(itop+1:iout) = (1.-LOG(Sig_top)/LOG(Sig_ou(itop+1:iout)))&
331                            *LOG(SigT_ou)/LOG(SigT_ou/Sig_top)
332      phi(iout+1:ibot) = (1.-LOG(Sig_bot)/LOG(Sig_ou(iout+1:ibot)))&
333                            *LOG(SigT_ou)/LOG(SigT_ou/Sig_bot)
334
335      !--- LOCALY STRECHED OUTPUT (LMDZ) PRESSURE PROFILES (INCREASING ORDER)
336      Pstr_ou(:) = Pres_ou(:) * Sig_ou(:)**(alpha*phi(:))
337
338      !--- REGRID INPUT PROFILE ON STRAINED VERTICAL OUTPUT LEVELS
339      IF(Ploc=='C') CALL regr_lint   (1, v2(i,:,:), LOG(Pre_in(:)),            &
340        LOG(Pstr_ou(:)), v3(i,nlev_ou:1:-1,:))
341      IF(Ploc=='I') CALL regr_conserv(1, v2(i,:,:),     Pre_in(:) ,            &
342            Pstr_ou(:) , v3(i,nlev_ou:1:-1,:), slopes(1,v2(i,:,:), Pre_in(:)))
343
344      !--- CHECK CONCENTRATIONS. strato: 50ppbV-15ppmV ; tropo: 5ppbV-300ppbV.
345      i0=nlev_ou-locate(Pres_ou(:),Ptrop_ou(i))+1
346      ll=check_ozone(v3(i, 1:i0-1   ,1),lon_in(i),lat_in(i),1 ,'troposphere',  &
347                     5.E-9,3.0E-7)
348!     IF(ll) CALL abort_physic(sub, 'Inconsistent O3 values in troposphere', 1)
349      ll=check_ozone(v3(i,i0:nlev_ou,1),lon_in(i),lat_in(i),i0,'stratosphere', &
350                     5.E-8,1.5E-5)
351!     IF(ll) CALL abort_physic(sub, 'Inconsistent O3 values in stratosphere', 1)
352
353      IF(ldebug) THEN
354        dzStrain0(i) = SIGN(7.*LOG(Sig_bo0/Sig_to0),SigT_in-SigT_ou)
355        dzStrain (i) = SIGN(7.*LOG(Sig_bot/Sig_top),SigT_in-SigT_ou)
356        Ptrop_in (i) = SigT_in*Pgnd_in(i)
357        Pstrn_ou(i,:)= Pstr_ou
358        phii(i,:)    = phi(:)
359        Ptrop_ef(i)  = PTrop_chem(i, itrp, locate(Pres_ou(:),PTropUp),    &
360                             Pres_ou(:), v3(:,nlev_ou:1:-1,1),o3trop=o3t0)
361      END IF
362    END DO
363  END IF
364  IF(ldebug.AND.lAdjTro) THEN
365    CALL writefield_phy('PreSt_ou' ,Pstrn_ou,SIZE(Pre_ou,2)) !--- Strained Pres
366    CALL writefield_phy('dzStrain' ,dzStrain ,1)     !--- Strained thickness
367    CALL writefield_phy('dzStrain0',dzStrain0,1)     !--- Tropopauses distance
368    CALL writefield_phy('phi',phii,nlev_ou)          !--- Localization function
369    !--- Tropopauses pressures:
370    CALL writefield_phy('PreTr_in',Ptrop_in,1)       !--- Input and effective
371    CALL writefield_phy('PreTr_ou',Ptrop_ou,1)       !--- LMDz dyn tropopause
372    CALL writefield_phy('PreTr_ef',Ptrop_ef,1)       !--- Effective chem tropop
373  END IF
374  IF(ldebug) THEN
375    CALL writefield_phy('Ozone_in',v2(:,:,1),nlev_in)!--- Raw input O3 field
376    CALL writefield_phy('Ozone_ou',v3(:,:,1),nlev_ou)!--- Output ozone field
377    CALL writefield_phy('Pres_ou' ,Pre_ou,SIZE(Pre_ou,2))!--- LMDZ Pressure
378  END IF
379
380CONTAINS
381
382
383!-------------------------------------------------------------------------------
384!
385SUBROUTINE update_fields()
386!
387!-------------------------------------------------------------------------------
388  IF(.NOT.linterp) THEN                 !=== DAILY FILES: NO TIME INTERPOLATION
389    CALL msg(.TRUE.,sub,'Updating Ozone forcing field: read from file.')
390    irec=MIN(INT(julien)+1,ntim_in)     !--- irec is just the julian day number
391    !--- MIN -> Security in the unlikely case of roundup errors.
392    CALL get_3Dfields(v1)               !--- Read ozone field(s)
393    IF(lAdjTro) THEN                    !--- Additional files for fields strain
394      IF(lPrSfile) CALL get_2Dfield(pg1,"ps")
395      IF(lPrTfile) CALL get_2Dfield(pt1,"tropopause_air_pressure")
396      IF(lO3Tfile) CALL get_2Dfield(ot1,"tro3_at_tropopause")
397    END IF
398  ELSE                                  !=== MONTHLY FILES: GET 2 NEAREST RECS
399    IF(lfirst) irec=locate(time_in,julien) !--- Need to locate surrounding times
400    IF(.NOT.lfirst.AND.julien<time_in(irec+1)) RETURN
401    CALL msg(.TRUE.,'Refreshing adjacent Ozone forcing fields.',sub)
402    IF(lfirst) THEN                     !=== READ EARLIEST TIME FIELDS
403      WRITE(str,'(a,i3,a,f12.8,a)')'Previous available field update (step 1): '&
404      //'reading record ',irec,' (time ',time_in(irec),')'
405      CALL msg(.TRUE.,str,sub)
406      CALL get_3Dfields(v1m)            !--- Read ozone field(s)
407      IF(lAdjTro) THEN                  !--- Additional files for fields strain
408        IF(lPrSfile) CALL get_2Dfield(pgm,"ps")
409        IF(lPrTfile) CALL get_2Dfield(ptm,"tropopause_air_pressure")
410        IF(lO3Tfile) CALL get_2Dfield(otm,"tro3_at_tropopause")
411      END IF
412    ELSE                                !=== SHIFT FIELDS
413      irec=irec+1
414      WRITE(str,'(a,i3,a,f12.8,a)')'Previous available field update: shifting'&
415      //' current next one (',irec,', time ',time_in(irec),')'
416      CALL msg(.TRUE.,str,sub)
417      v1m=v1p                           !--- Ozone fields
418      IF(lAdjTro) THEN                  !--- Additional files for fields strain
419        IF(lPrSfile) pgm=pgp             !--- Surface pressure
420        IF(lPrTfile) ptm=ptp             !--- Tropopause pressure
421        IF(lO3Tfile) otm=otp             !--- Tropopause ozone
422      END IF
423    END IF
424    irec=irec+1
425    WRITE(str,'(a,i3,a,f12.8,a)')'Next available field update: reading record'&
426    ,irec,' (time ',time_in(irec),')'
427    CALL msg(.TRUE.,str,sub)
428    CALL get_3Dfields(v1p)              !--- Read ozone field(s)
429    IF(lAdjTro) THEN                    !--- Additional files for fields strain
430      IF(lPrSfile) CALL get_2Dfield(pgp,"ps")
431      IF(lPrTfile) CALL get_2Dfield(ptp,"tropopause_air_pressure")
432      IF(lO3Tfile) CALL get_2Dfield(otp,"tro3_at_tropopause")
433    END IF
434    irec=irec-1
435  END IF
436
437END SUBROUTINE update_fields
438!
439!-------------------------------------------------------------------------------
440
441
442!-------------------------------------------------------------------------------
443!
444SUBROUTINE get_2Dfield(v,var)
445!
446!-------------------------------------------------------------------------------
447! Purpose: Shortcut to get the "irec"th record of the surface field named "var"
448!          from the input file.
449! Remark: In case the field is zonal, it is duplicated along first dimension.
450!-------------------------------------------------------------------------------
451! Arguments:
452  REAL,             INTENT(INOUT) :: v(:,:)
453  CHARACTER(LEN=*), INTENT(IN)    :: var
454!-------------------------------------------------------------------------------
455  CALL NF95_INQ_VARID(fID, TRIM(var), vID)
456  CALL NF95_INQUIRE_VARIABLE(fID, vID, ndims=n_dim)
457  IF(n_dim==2) ncerr=NF90_GET_VAR(fID,vID,v(1,:), start=[  1,irec])
458  IF(n_dim==3) ncerr=NF90_GET_VAR(fID,vID,v(:,:), start=[1,1,irec])
459  CALL handle_err(TRIM(sub)//" NF90_GET_VAR "//TRIM(var),ncerr,fID)
460
461  !--- Flip latitudes: ascending in input file, descending in "rlatu".
462  IF(n_dim==3) THEN
463    v(1,:) = v(1,nlat:1:-1)
464    v(2:,:)= SPREAD(v(1,:),DIM=1,ncopies=nlon-1)  !--- Duplication
465  ELSE
466    v(:,:) = v(:,nlat:1:-1)
467  END IF
468
469END SUBROUTINE get_2Dfield
470!
471!-------------------------------------------------------------------------------
472
473
474!-------------------------------------------------------------------------------
475!
476SUBROUTINE get_3Dfields(v)
477!
478!-------------------------------------------------------------------------------
479! Purpose: Shortcut to get the "irec"th record of the 3D fields named "nam"
480!          from the input file. Fields are stacked on fourth dimension.
481! Remark: In case the field is zonal, it is duplicated along first dimension.
482!-------------------------------------------------------------------------------
483! Arguments:
484  REAL, INTENT(INOUT) :: v(:,:,:,:)
485!-------------------------------------------------------------------------------
486  DO i=1,SIZE(nam)
487    CALL NF95_INQ_VARID(fID, TRIM(nam(i)), vID)
488    CALL NF95_INQUIRE_VARIABLE(fID, vID, ndims=n_dim)
489    IF(n_dim==3) ncerr=NF90_GET_VAR(fID,vID,v(1,:,:,i), start=[  1,1,irec])
490    IF(n_dim==4) ncerr=NF90_GET_VAR(fID,vID,v(:,:,:,i), start=[1,1,1,irec])
491    CALL handle_err(TRIM(sub)//" NF90_GET_VAR "//TRIM(nam(i)),ncerr,fID)
492  END DO
493
494  !--- Flip latitudes: ascending in input file, descending in "rlatu".
495  IF(n_dim==3) THEN
496    v(1,:,:,:) = v(1,nlat:1:-1,:,:)
497    v(2:,:,:,:)= SPREAD(v(1,:,:,:),DIM=1,ncopies=nlon-1)  !--- Duplication
498  ELSE
499    v(:,:,:,:) = v(:,nlat:1:-1,:,:)
500  END IF
501
502END SUBROUTINE get_3Dfields
503!
504!-------------------------------------------------------------------------------
505
506
507
508!-------------------------------------------------------------------------------
509!
510FUNCTION get_SigTrop(ih,it) RESULT(out)
511!
512!-------------------------------------------------------------------------------
513! Arguments:
514  REAL                 :: out
515  INTEGER, INTENT(IN)  :: ih
516  INTEGER, INTENT(OUT) :: it
517!-------------------------------------------------------------------------------
518  !--- Pressure at tropopause read from the forcing file
519       IF(lPrTfile) THEN; out=Ptrp_in(ih)/Pgnd_in(ih); RETURN; END IF
520
521  !--- Chemical tropopause definition based on a particular threshold
522       IF(lO3Tfile) THEN; out=PTrop_chem(ih,it,itrp0,Pre_in,v2(:,:,1),Otrp_in(ih))
523  ELSE IF(lO3Tpara) THEN; out=PTrop_chem(ih,it,itrp0,Pre_in,v2(:,:,1))
524  ELSE                  ; out=PTrop_chem(ih,it,itrp0,Pre_in,v2(:,:,1),o3t0); END IF
525  out=out/Pgnd_in(ih)
526
527END FUNCTION get_SigTrop
528!
529!-------------------------------------------------------------------------------
530
531
532!-------------------------------------------------------------------------------
533!
534FUNCTION PTrop_chem(ih,it,it0,pres,o3,o3trop) RESULT(out)
535!
536!-------------------------------------------------------------------------------
537! Purpose: Determine the tropopause using chemical criterium, ie the pressure
538!          at which the ozone concentration reaches a certain level.
539!-------------------------------------------------------------------------------
540! Remarks:
541! * Input field is upside down (increasing pressure // increasing vertical idx)
542!   The sweep is done from top to ground, starting at the 50hPa layer (idx it0),
543!   where O3 is about 1,5 ppmV, until the first layer where o3<o3t is reached:
544!   the O3 profile in this zone is decreasing with pressure.
545! * Threshold o3t can be given as an input argument ("o3trop", in ppmV) or
546!   determined using an empirical law roughly derived from ... & al.
547!-------------------------------------------------------------------------------
548! Arguments:
549  REAL                        :: out           !--- Pressure at tropopause
550  INTEGER,        INTENT(IN)  :: ih            !--- Horizontal index
551  INTEGER,        INTENT(OUT) :: it            !--- Index of tropopause layer
552  INTEGER,        INTENT(IN)  :: it0           !--- Idx: higher than tropopause
553  REAL,           INTENT(IN)  :: pres(:)       !--- Pressure profile, increasing
554  REAL,           INTENT(IN)  :: o3(:,:)       !--- Ozone field (pptV)
555  REAL, OPTIONAL, INTENT(IN)  :: o3trop        !--- Ozone at tropopause
556!-------------------------------------------------------------------------------
557! Local variables:
558  REAL :: o3t                                  !--- Ozone concent. at tropopause
559  REAL :: al                                   !--- Interpolation coefficient
560  REAL :: coef                                 !--- Coeff of latitude modulation
561  REAL, PARAMETER :: co3(3)=[91.,28.,20.]      !--- Coeff for o3 at tropopause
562!-------------------------------------------------------------------------------
563  !--- DETERMINE THE OZONE CONCENTRATION AT TROPOPAUSE IN THE CURRENT COLUMN
564  IF(PRESENT(o3trop)) THEN                     !=== THRESHOLD FROM ARGUMENTS
565    o3t=o3trop
566  ELSE                                         !=== EMPIRICAL LAW
567    coef = TANH(lat_in(ih)/co3(3))             !--- Latitude  modulation
568    coef = SIN (2.*RPI*(y_frac-1./6.)) * coef  !--- Seasonal  modulation
569    o3t  = 1.E-9 * (co3(1)+co3(2)*coef)        !--- Value from parametrization
570  END IF
571
572  !--- FROM 50hPa, GO DOWN UNTIL "it" IS SUCH THAT o3(ih,it)>o3t>o3(ih,it+1)
573  it=it0; DO WHILE(o3(ih,it+1)>=o3t); it=it+1; END DO
574  al=(o3(ih,it)-o3t)/(o3(ih,it)-o3(ih,it+1))
575  IF(Ploc=='C') out =      pres(it)**(1.-al) * pres(it+1)**al
576  IF(Ploc=='I') out = SQRT(pres(it)**(1.-al) * pres(it+2)**al *pres(it+1))
577  it = locate(pres(:), out)                    !--- pres(it)<Ptrop<pres(it+1)
578
579END FUNCTION PTrop_chem
580!
581!-------------------------------------------------------------------------------
582
583
584!-------------------------------------------------------------------------------
585!
586FUNCTION check_ozone(o3col, lon, lat, ilev0, layer, vmin, vmax) RESULT(out)
587!
588!-------------------------------------------------------------------------------
589  IMPLICIT NONE
590!-------------------------------------------------------------------------------
591! Arguments:
592  LOGICAL                      :: out          !--- .T. => some wrong values
593  REAL,             INTENT(IN) :: o3col(:), lon, lat
594  INTEGER,          INTENT(IN) :: ilev0
595  CHARACTER(LEN=*), INTENT(IN) :: layer
596  REAL, OPTIONAL,   INTENT(IN) :: vmin
597  REAL, OPTIONAL,   INTENT(IN) :: vmax
598!-------------------------------------------------------------------------------
599! Local variables:
600  INTEGER :: k
601  LOGICAL :: lmin, lmax
602  REAL    :: fac
603  CHARACTER(LEN=6) :: unt
604!-------------------------------------------------------------------------------
605  !--- NOTHING TO DO
606  lmin=.FALSE.; IF(PRESENT(vmin)) lmin=COUNT(o3col<vmin)/=0
607  lmax=.FALSE.; IF(PRESENT(vmax)) lmax=COUNT(o3col>vmax)/=0
608  out=lmin.OR.lmax; IF(.NOT.out.OR.prt_level>100) RETURN
609
610  !--- SOME TOO LOW VALUES FOUND
611  IF(lmin) THEN
612    CALL unitc(vmin,unt,fac)
613    DO k=1,SIZE(o3col); IF(o3col(k)>vmin) CYCLE
614      WRITE(*,'(a,2f7.1,i3,a,2(f8.3,a))')'WARNING: inconsistent value in '     &
615        //TRIM(layer)//': O3(',lon,lat,k+ilev0-1,')=',fac*o3col(k),unt//' < ', &
616        fac*vmin,unt//' in '//TRIM(layer)
617    END DO
618  END IF
619
620  !--- SOME TOO HIGH VALUES FOUND
621  IF(lmax) THEN
622    CALL unitc(vmax,unt,fac)
623    DO k=1,SIZE(o3col); IF(o3col(k)<vmax) CYCLE
624      WRITE(*,'(a,2f7.1,i3,a,2(f8.3,a))')'WARNING: inconsistent value in '     &
625        //TRIM(layer)//': O3(',lon,lat,k+ilev0-1,')=',fac*o3col(k),unt//' > ', &
626        fac*vmax,unt//' in '//TRIM(layer)
627    END DO
628  END IF
629
630END FUNCTION check_ozone
631!
632!-------------------------------------------------------------------------------
633
634
635!-------------------------------------------------------------------------------
636!
637SUBROUTINE unitc(val,unt,fac)
638!
639!-------------------------------------------------------------------------------
640  IMPLICIT NONE
641!-------------------------------------------------------------------------------
642! Arguments:
643  REAL,             INTENT(IN)  :: val
644  CHARACTER(LEN=6), INTENT(OUT) :: unt
645  REAL,             INTENT(OUT) :: fac
646!-------------------------------------------------------------------------------
647! Local variables:
648  INTEGER :: ndgt
649!-------------------------------------------------------------------------------
650  ndgt=3*FLOOR(LOG10(val)/3.)
651  SELECT CASE(ndgt)
652    CASE(-6);     unt=' ppmV '; fac=1.E6
653    CASE(-9);     unt=' ppbV '; fac=1.E9
654    CASE DEFAULT; unt=' vmr  '; fac=1.0
655  END SELECT
656
657END SUBROUTINE unitc
658!
659!-------------------------------------------------------------------------------
660
661
662!-------------------------------------------------------------------------------
663!
664SUBROUTINE msg(ll,str,sub)
665!
666!-------------------------------------------------------------------------------
667  USE print_control_mod, ONLY: lunout
668  IMPLICIT NONE
669!-------------------------------------------------------------------------------
670! Arguments:
671  LOGICAL,                    INTENT(IN) :: ll
672  CHARACTER(LEN=*),           INTENT(IN) :: str
673  CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sub
674!-------------------------------------------------------------------------------
675  IF(.NOT.ll) RETURN
676  IF(PRESENT(sub)) THEN
677    WRITE(lunout,*)TRIM(sub)//': '//TRIM(str)
678  ELSE
679    WRITE(lunout,*)TRIM(str)
680  END IF
681
682END SUBROUTINE msg
683!
684!-------------------------------------------------------------------------------
685
686END SUBROUTINE regr_pr_time_av
687!
688!-------------------------------------------------------------------------------
689
690END MODULE regr_pr_time_av_m
Note: See TracBrowser for help on using the repository browser.