New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
diaobs.F90 in branches/UKMO/dev_r5518_obs_oper_update_bgc3d/NEMOGCM/NEMO/OPA_SRC/OBS – NEMO

source: branches/UKMO/dev_r5518_obs_oper_update_bgc3d/NEMOGCM/NEMO/OPA_SRC/OBS/diaobs.F90 @ 9202

Last change on this file since 9202 was 9202, checked in by dford, 6 years ago

Modify obs_rea_prof to loop over an arbitrary number of variables.

File size: 83.8 KB
Line 
1MODULE diaobs
2   !!======================================================================
3   !!                       ***  MODULE diaobs  ***
4   !! Observation diagnostics: Computation of the misfit between data and
5   !!                          their model equivalent
6   !!======================================================================
7
8   !!----------------------------------------------------------------------
9   !!   dia_obs_init : Reading and prepare observations
10   !!   dia_obs      : Compute model equivalent to observations
11   !!   dia_obs_wri  : Write observational diagnostics
12   !!   ini_date     : Compute the initial date YYYYMMDD.HHMMSS
13   !!   fin_date     : Compute the final date YYYYMMDD.HHMMSS
14   !!----------------------------------------------------------------------
15   !! * Modules used
16   USE wrk_nemo                 ! Memory Allocation
17   USE par_kind                 ! Precision variables
18   USE in_out_manager           ! I/O manager
19   USE par_oce
20   USE dom_oce                  ! Ocean space and time domain variables
21   USE obs_read_prof            ! Reading and allocation of profile obs
22   USE obs_read_surf            ! Reading and allocation of surface obs
23   USE obs_readmdt              ! Reading and allocation of MDT for SLA.
24   USE obs_prep                 ! Preparation of obs. (grid search etc).
25   USE obs_oper                 ! Observation operators
26   USE obs_write                ! Writing of observation related diagnostics
27   USE obs_grid                 ! Grid searching
28   USE obs_read_altbias         ! Bias treatment for altimeter
29   USE obs_sstbias              ! Bias correction routine for SST
30   USE obs_profiles_def         ! Profile data definitions
31   USE obs_surf_def             ! Surface data definitions
32   USE obs_types                ! Definitions for observation types
33   USE mpp_map                  ! MPP mapping
34   USE lib_mpp                  ! For ctl_warn/stop
35
36   IMPLICIT NONE
37
38   !! * Routine accessibility
39   PRIVATE
40   PUBLIC dia_obs_init, &  ! Initialize and read observations
41      &   dia_obs,      &  ! Compute model equivalent to observations
42      &   dia_obs_wri,  &  ! Write model equivalent to observations
43      &   dia_obs_dealloc  ! Deallocate dia_obs data
44
45   !! * Module variables
46   LOGICAL, PUBLIC :: &
47      &       lk_diaobs = .TRUE.   !: Include this for backwards compatibility at NEMO 3.6.
48   LOGICAL :: ln_diaobs            !: Logical switch for the obs operator
49   LOGICAL :: ln_sstnight          !: Logical switch for night mean SST obs
50   LOGICAL :: ln_default_fp_indegs !: T=> Default obs footprint size specified in degrees, F=> in metres
51   LOGICAL :: ln_sla_fp_indegs     !: T=>     SLA obs footprint size specified in degrees, F=> in metres
52   LOGICAL :: ln_sst_fp_indegs     !: T=>     SST obs footprint size specified in degrees, F=> in metres
53   LOGICAL :: ln_sss_fp_indegs     !: T=>     SSS obs footprint size specified in degrees, F=> in metres
54   LOGICAL :: ln_sic_fp_indegs     !: T=> sea-ice obs footprint size specified in degrees, F=> in metres
55
56   REAL(wp) :: rn_default_avglamscl !: Default E/W diameter of observation footprint
57   REAL(wp) :: rn_default_avgphiscl !: Default N/S diameter of observation footprint
58   REAL(wp) :: rn_sla_avglamscl     !: E/W diameter of SLA observation footprint
59   REAL(wp) :: rn_sla_avgphiscl     !: N/S diameter of SLA observation footprint
60   REAL(wp) :: rn_sst_avglamscl     !: E/W diameter of SST observation footprint
61   REAL(wp) :: rn_sst_avgphiscl     !: N/S diameter of SST observation footprint
62   REAL(wp) :: rn_sss_avglamscl     !: E/W diameter of SSS observation footprint
63   REAL(wp) :: rn_sss_avgphiscl     !: N/S diameter of SSS observation footprint
64   REAL(wp) :: rn_sic_avglamscl     !: E/W diameter of sea-ice observation footprint
65   REAL(wp) :: rn_sic_avgphiscl     !: N/S diameter of sea-ice observation footprint
66
67   INTEGER :: nn_1dint         !: Vertical interpolation method
68   INTEGER :: nn_2dint_default !: Default horizontal interpolation method
69   INTEGER :: nn_2dint_sla     !: SLA horizontal interpolation method (-1 = default)
70   INTEGER :: nn_2dint_sst     !: SST horizontal interpolation method (-1 = default)
71   INTEGER :: nn_2dint_sss     !: SSS horizontal interpolation method (-1 = default)
72   INTEGER :: nn_2dint_sic     !: Seaice horizontal interpolation method (-1 = default)
73 
74   INTEGER, DIMENSION(imaxavtypes) :: &
75      & nn_profdavtypes      !: Profile data types representing a daily average
76   INTEGER :: nproftypes     !: Number of profile obs types
77   INTEGER :: nsurftypes     !: Number of surface obs types
78   INTEGER, DIMENSION(:), ALLOCATABLE :: &
79      & nvarsprof, &         !: Number of profile variables
80      & nvarssurf            !: Number of surface variables
81   INTEGER, DIMENSION(:), ALLOCATABLE :: &
82      & nextrprof, &         !: Number of profile extra variables
83      & nextrsurf            !: Number of surface extra variables
84   INTEGER, DIMENSION(:), ALLOCATABLE :: &
85      & n2dintsurf           !: Interpolation option for surface variables
86   REAL(wp), DIMENSION(:), ALLOCATABLE :: &
87      & ravglamscl, &        !: E/W diameter of averaging footprint for surface variables
88      & ravgphiscl           !: N/S diameter of averaging footprint for surface variables
89   LOGICAL, DIMENSION(:), ALLOCATABLE :: &
90      & lfpindegs, &         !: T=> surface obs footprint size specified in degrees, F=> in metres
91      & llnightav            !: Logical for calculating night-time averages
92
93   TYPE(obs_surf), PUBLIC, POINTER, DIMENSION(:) :: &
94      & surfdata, &          !: Initial surface data
95      & surfdataqc           !: Surface data after quality control
96   TYPE(obs_prof), PUBLIC, POINTER, DIMENSION(:) :: &
97      & profdata, &          !: Initial profile data
98      & profdataqc           !: Profile data after quality control
99
100   CHARACTER(len=8), PUBLIC, DIMENSION(:), ALLOCATABLE :: &
101      & cobstypesprof, &     !: Profile obs types
102      & cobstypessurf        !: Surface obs types
103
104   !!----------------------------------------------------------------------
105   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
106   !! $Id$
107   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
108   !!----------------------------------------------------------------------
109
110   !! * Substitutions
111#  include "domzgr_substitute.h90"
112CONTAINS
113
114   SUBROUTINE dia_obs_init
115      !!----------------------------------------------------------------------
116      !!                    ***  ROUTINE dia_obs_init  ***
117      !!         
118      !! ** Purpose : Initialize and read observations
119      !!
120      !! ** Method  : Read the namelist and call reading routines
121      !!
122      !! ** Action  : Read the namelist and call reading routines
123      !!
124      !! History :
125      !!        !  06-03  (K. Mogensen) Original code
126      !!        !  06-05  (A. Weaver) Reformatted
127      !!        !  06-10  (A. Weaver) Cleaning and add controls
128      !!        !  07-03  (K. Mogensen) General handling of profiles
129      !!        !  14-08  (J.While) Incorporated SST bias correction
130      !!        !  15-02  (M. Martin) Simplification of namelist and code
131      !!----------------------------------------------------------------------
132
133      IMPLICIT NONE
134
135      !! * Local declarations
136      INTEGER, PARAMETER :: &
137         & jpmaxnfiles = 1000    ! Maximum number of files for each obs type
138      INTEGER, DIMENSION(:), ALLOCATABLE :: &
139         & ifilesprof, &         ! Number of profile files
140         & ifilessurf            ! Number of surface files
141      INTEGER :: ios             ! Local integer output status for namelist read
142      INTEGER :: jtype           ! Counter for obs types
143      INTEGER :: jvar            ! Counter for variables
144      INTEGER :: jfile           ! Counter for files
145      INTEGER :: jnumsstbias     ! Number of SST bias files to read and apply
146      INTEGER :: n2dint_type     ! Local version of nn_2dint*
147
148      CHARACTER(len=128), DIMENSION(jpmaxnfiles) :: &
149         & cn_profbfiles,      & ! T/S profile input filenames
150         & cn_sstfbfiles,      & ! Sea surface temperature input filenames
151         & cn_slafbfiles,      & ! Sea level anomaly input filenames
152         & cn_sicfbfiles,      & ! Seaice concentration input filenames
153         & cn_velfbfiles,      & ! Velocity profile input filenames
154         & cn_sssfbfiles,      & ! Sea surface salinity input filenames
155         & cn_slchltotfbfiles, & ! Surface total              log10(chlorophyll) input filenames
156         & cn_slchldiafbfiles, & ! Surface diatom             log10(chlorophyll) input filenames
157         & cn_slchlnonfbfiles, & ! Surface non-diatom         log10(chlorophyll) input filenames
158         & cn_slchldinfbfiles, & ! Surface dinoflagellate     log10(chlorophyll) input filenames
159         & cn_slchlmicfbfiles, & ! Surface microphytoplankton log10(chlorophyll) input filenames
160         & cn_slchlnanfbfiles, & ! Surface nanophytoplankton  log10(chlorophyll) input filenames
161         & cn_slchlpicfbfiles, & ! Surface picophytoplankton  log10(chlorophyll) input filenames
162         & cn_schltotfbfiles,  & ! Surface total              chlorophyll        input filenames
163         & cn_sspmfbfiles,     & ! Surface suspended particulate matter input filenames
164         & cn_sfco2fbfiles,    & ! Surface fugacity         of carbon dioxide input filenames
165         & cn_spco2fbfiles,    & ! Surface partial pressure of carbon dioxide input filenames
166         & cn_plchltotfbfiles, & ! Profile total log10(chlorophyll) input filenames
167         & cn_pchltotfbfiles,  & ! Profile total chlorophyll input filenames
168         & cn_pno3fbfiles,     & ! Profile nitrate input filenames
169         & cn_psi4fbfiles,     & ! Profile silicate input filenames
170         & cn_ppo4fbfiles,     & ! Profile phosphate input filenames
171         & cn_pdicfbfiles,     & ! Profile dissolved inorganic carbon input filenames
172         & cn_palkfbfiles,     & ! Profile alkalinity input filenames
173         & cn_pphfbfiles,      & ! Profile pH input filenames
174         & cn_po2fbfiles,      & ! Profile dissolved oxygen input filenames
175         & cn_sstbiasfiles       ! SST bias input filenames
176
177      CHARACTER(LEN=128) :: &
178         & cn_altbiasfile        ! Altimeter bias input filename
179
180
181      LOGICAL :: ln_t3d          ! Logical switch for temperature profiles
182      LOGICAL :: ln_s3d          ! Logical switch for salinity profiles
183      LOGICAL :: ln_sla          ! Logical switch for sea level anomalies
184      LOGICAL :: ln_sst          ! Logical switch for sea surface temperature
185      LOGICAL :: ln_sic          ! Logical switch for sea ice concentration
186      LOGICAL :: ln_sss          ! Logical switch for sea surface salinity obs
187      LOGICAL :: ln_vel3d        ! Logical switch for velocity (u,v) obs
188      LOGICAL :: ln_slchltot     ! Logical switch for surface total              log10(chlorophyll) obs
189      LOGICAL :: ln_slchldia     ! Logical switch for surface diatom             log10(chlorophyll) obs
190      LOGICAL :: ln_slchlnon     ! Logical switch for surface non-diatom         log10(chlorophyll) obs
191      LOGICAL :: ln_slchldin     ! Logical switch for surface dinoflagellate     log10(chlorophyll) obs
192      LOGICAL :: ln_slchlmic     ! Logical switch for surface microphytoplankton log10(chlorophyll) obs
193      LOGICAL :: ln_slchlnan     ! Logical switch for surface nanophytoplankton  log10(chlorophyll) obs
194      LOGICAL :: ln_slchlpic     ! Logical switch for surface picophytoplankton  log10(chlorophyll) obs
195      LOGICAL :: ln_schltot      ! Logical switch for surface total              chlorophyll        obs
196      LOGICAL :: ln_sspm         ! Logical switch for surface suspended particulate matter obs
197      LOGICAL :: ln_sfco2        ! Logical switch for surface fugacity         of carbon dioxide obs
198      LOGICAL :: ln_spco2        ! Logical switch for surface partial pressure of carbon dioxide obs
199      LOGICAL :: ln_plchltot     ! Logical switch for profile total log10(chlorophyll) obs
200      LOGICAL :: ln_pchltot      ! Logical switch for profile total chlorophyll obs
201      LOGICAL :: ln_pno3         ! Logical switch for profile nitrate obs
202      LOGICAL :: ln_psi4         ! Logical switch for profile silicate obs
203      LOGICAL :: ln_ppo4         ! Logical switch for profile phosphate obs
204      LOGICAL :: ln_pdic         ! Logical switch for profile dissolved inorganic carbon obs
205      LOGICAL :: ln_palk         ! Logical switch for profile alkalinity obs
206      LOGICAL :: ln_pph          ! Logical switch for profile pH obs
207      LOGICAL :: ln_po2          ! Logical switch for profile dissolved oxygen obs
208      LOGICAL :: ln_nea          ! Logical switch to remove obs near land
209      LOGICAL :: ln_altbias      ! Logical switch for altimeter bias
210      LOGICAL :: ln_sstbias      ! Logical switch for bias correction of SST
211      LOGICAL :: ln_ignmis       ! Logical switch for ignoring missing files
212      LOGICAL :: ln_s_at_t       ! Logical switch to compute model S at T obs
213      LOGICAL :: ln_bound_reject ! Logical switch for rejecting obs near the boundary
214
215      REAL(dp) :: rn_dobsini     ! Obs window start date YYYYMMDD.HHMMSS
216      REAL(dp) :: rn_dobsend     ! Obs window end date   YYYYMMDD.HHMMSS
217
218      REAL(wp) :: rtype_avglamscl ! Local version of rn_*_avglamscl
219      REAL(wp) :: rtype_avgphiscl ! Local version of rn_*_avgphiscl
220
221      CHARACTER(len=128), DIMENSION(:,:), ALLOCATABLE :: &
222         & clproffiles, &        ! Profile filenames
223         & clsurffiles           ! Surface filenames
224
225      LOGICAL, DIMENSION(:), ALLOCATABLE :: llvar   ! Logical for profile variable read
226      LOGICAL :: ltype_fp_indegs ! Local version of ln_*_fp_indegs
227      LOGICAL :: ltype_night     ! Local version of ln_sstnight (false for other variables)
228
229      REAL(wp), POINTER, DIMENSION(:,:) :: &
230         & zglam1, &             ! Model longitudes for profile variable 1
231         & zglam2                ! Model longitudes for profile variable 2
232      REAL(wp), POINTER, DIMENSION(:,:) :: &
233         & zgphi1, &             ! Model latitudes for profile variable 1
234         & zgphi2                ! Model latitudes for profile variable 2
235      REAL(wp), POINTER, DIMENSION(:,:,:) :: &
236         & zmask1, &             ! Model land/sea mask associated with variable 1
237         & zmask2                ! Model land/sea mask associated with variable 2
238
239
240      NAMELIST/namobs/ln_diaobs, ln_t3d, ln_s3d, ln_sla,              &
241         &            ln_sst, ln_sic, ln_sss, ln_vel3d,               &
242         &            ln_slchltot, ln_slchldia, ln_slchlnon,          &
243         &            ln_slchldin, ln_slchlmic, ln_slchlnan,          &
244         &            ln_slchlpic, ln_schltot,                        &
245         &            ln_sspm,     ln_sfco2,    ln_spco2,             &
246         &            ln_plchltot, ln_pchltot,  ln_pno3,              &
247         &            ln_psi4,     ln_ppo4,     ln_pdic,              &
248         &            ln_palk,     ln_pph,      ln_po2,               &
249         &            ln_altbias, ln_sstbias, ln_nea,                 &
250         &            ln_grid_global, ln_grid_search_lookup,          &
251         &            ln_ignmis, ln_s_at_t, ln_bound_reject,          &
252         &            ln_sstnight, ln_default_fp_indegs,              &
253         &            ln_sla_fp_indegs, ln_sst_fp_indegs,             &
254         &            ln_sss_fp_indegs, ln_sic_fp_indegs,             &
255         &            cn_profbfiles, cn_slafbfiles,                   &
256         &            cn_sstfbfiles, cn_sicfbfiles,                   &
257         &            cn_velfbfiles, cn_sssfbfiles,                   &
258         &            cn_slchltotfbfiles, cn_slchldiafbfiles,         &
259         &            cn_slchlnonfbfiles, cn_slchldinfbfiles,         &
260         &            cn_slchlmicfbfiles, cn_slchlnanfbfiles,         &
261         &            cn_slchlpicfbfiles,                             &
262         &            cn_schltotfbfiles, cn_sspmfbfiles,              &
263         &            cn_sfco2fbfiles, cn_spco2fbfiles,               &
264         &            cn_plchltotfbfiles, cn_pchltotfbfiles,          &
265         &            cn_pno3fbfiles, cn_psi4fbfiles, cn_ppo4fbfiles, &
266         &            cn_pdicfbfiles, cn_palkfbfiles, cn_pphfbfiles,  &
267         &            cn_po2fbfiles,                                  &
268         &            cn_sstbiasfiles, cn_altbiasfile,                &
269         &            cn_gridsearchfile, rn_gridsearchres,            &
270         &            rn_dobsini, rn_dobsend,                         &
271         &            rn_default_avglamscl, rn_default_avgphiscl,     &
272         &            rn_sla_avglamscl, rn_sla_avgphiscl,             &
273         &            rn_sst_avglamscl, rn_sst_avgphiscl,             &
274         &            rn_sss_avglamscl, rn_sss_avgphiscl,             &
275         &            rn_sic_avglamscl, rn_sic_avgphiscl,             &
276         &            nn_1dint, nn_2dint_default,                     &
277         &            nn_2dint_sla, nn_2dint_sst,                     &
278         &            nn_2dint_sss, nn_2dint_sic,                     &
279         &            nn_msshc, rn_mdtcorr, rn_mdtcutoff,             &
280         &            nn_profdavtypes
281
282      CALL wrk_alloc( jpi, jpj, zglam1 )
283      CALL wrk_alloc( jpi, jpj, zglam2 )
284      CALL wrk_alloc( jpi, jpj, zgphi1 )
285      CALL wrk_alloc( jpi, jpj, zgphi2 )
286      CALL wrk_alloc( jpi, jpj, jpk, zmask1 )
287      CALL wrk_alloc( jpi, jpj, jpk, zmask2 )
288
289      !-----------------------------------------------------------------------
290      ! Read namelist parameters
291      !-----------------------------------------------------------------------
292
293      ! Some namelist arrays need initialising
294      cn_profbfiles(:)      = ''
295      cn_slafbfiles(:)      = ''
296      cn_sstfbfiles(:)      = ''
297      cn_sicfbfiles(:)      = ''
298      cn_velfbfiles(:)      = ''
299      cn_sssfbfiles(:)      = ''
300      cn_slchltotfbfiles(:) = ''
301      cn_slchldiafbfiles(:) = ''
302      cn_slchlnonfbfiles(:) = ''
303      cn_slchldinfbfiles(:) = ''
304      cn_slchlmicfbfiles(:) = ''
305      cn_slchlnanfbfiles(:) = ''
306      cn_slchlpicfbfiles(:) = ''
307      cn_schltotfbfiles(:)  = ''
308      cn_sspmfbfiles(:)     = ''
309      cn_sfco2fbfiles(:)    = ''
310      cn_spco2fbfiles(:)    = ''
311      cn_plchltotfbfiles(:) = ''
312      cn_pchltotfbfiles(:)  = ''
313      cn_pno3fbfiles(:)     = ''
314      cn_psi4fbfiles(:)     = ''
315      cn_ppo4fbfiles(:)     = ''
316      cn_pdicfbfiles(:)     = ''
317      cn_palkfbfiles(:)     = ''
318      cn_pphfbfiles(:)      = ''
319      cn_po2fbfiles(:)      = ''
320      cn_sstbiasfiles(:)    = ''
321      nn_profdavtypes(:)    = -1
322
323      CALL ini_date( rn_dobsini )
324      CALL fin_date( rn_dobsend )
325
326      ! Read namelist namobs : control observation diagnostics
327      REWIND( numnam_ref )   ! Namelist namobs in reference namelist
328      READ  ( numnam_ref, namobs, IOSTAT = ios, ERR = 901)
329901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namobs in reference namelist', lwp )
330
331      REWIND( numnam_cfg )   ! Namelist namobs in configuration namelist
332      READ  ( numnam_cfg, namobs, IOSTAT = ios, ERR = 902 )
333902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namobs in configuration namelist', lwp )
334      IF(lwm) WRITE ( numond, namobs )
335
336      lk_diaobs = .FALSE.
337#if defined key_diaobs
338      IF ( ln_diaobs ) lk_diaobs = .TRUE.
339#endif
340
341      IF ( .NOT. lk_diaobs ) THEN
342         IF(lwp) WRITE(numout,cform_war)
343         IF(lwp) WRITE(numout,*)' ln_diaobs is set to false or key_diaobs is not set, so not calling dia_obs'
344         RETURN
345      ENDIF
346
347      IF(lwp) THEN
348         WRITE(numout,*)
349         WRITE(numout,*) 'dia_obs_init : Observation diagnostic initialization'
350         WRITE(numout,*) '~~~~~~~~~~~~'
351         WRITE(numout,*) '          Namelist namobs : set observation diagnostic parameters' 
352         WRITE(numout,*) '             Logical switch for T profile observations                ln_t3d = ', ln_t3d
353         WRITE(numout,*) '             Logical switch for S profile observations                ln_s3d = ', ln_s3d
354         WRITE(numout,*) '             Logical switch for SLA observations                      ln_sla = ', ln_sla
355         WRITE(numout,*) '             Logical switch for SST observations                      ln_sst = ', ln_sst
356         WRITE(numout,*) '             Logical switch for Sea Ice observations                  ln_sic = ', ln_sic
357         WRITE(numout,*) '             Logical switch for velocity observations               ln_vel3d = ', ln_vel3d
358         WRITE(numout,*) '             Logical switch for SSS observations                      ln_sss = ', ln_sss
359         WRITE(numout,*) '             Logical switch for surface total logchl obs         ln_slchltot = ', ln_slchltot
360         WRITE(numout,*) '             Logical switch for surface diatom logchl obs        ln_slchldia = ', ln_slchldia
361         WRITE(numout,*) '             Logical switch for surface non-diatom logchl obs    ln_slchlnon = ', ln_slchlnon
362         WRITE(numout,*) '             Logical switch for surface dino logchl obs          ln_slchldin = ', ln_slchldin
363         WRITE(numout,*) '             Logical switch for surface micro logchl obs         ln_slchlmic = ', ln_slchlmic
364         WRITE(numout,*) '             Logical switch for surface nano logchl obs          ln_slchlnan = ', ln_slchlnan
365         WRITE(numout,*) '             Logical switch for surface pico logchl obs          ln_slchlpic = ', ln_slchlpic
366         WRITE(numout,*) '             Logical switch for surface total chl obs             ln_schltot = ', ln_schltot
367         WRITE(numout,*) '             Logical switch for surface SPM observations             ln_sspm = ', ln_sspm
368         WRITE(numout,*) '             Logical switch for surface fCO2 observations           ln_sfco2 = ', ln_sfco2
369         WRITE(numout,*) '             Logical switch for surface pCO2 observations           ln_spco2 = ', ln_spco2
370         WRITE(numout,*) '             Logical switch for profile total logchl obs         ln_plchltot = ', ln_plchltot
371         WRITE(numout,*) '             Logical switch for profile total chl obs             ln_pchltot = ', ln_pchltot
372         WRITE(numout,*) '             Logical switch for profile nitrate obs                  ln_pno3 = ', ln_pno3
373         WRITE(numout,*) '             Logical switch for profile silicate obs                 ln_psi4 = ', ln_psi4
374         WRITE(numout,*) '             Logical switch for profile phosphate obs                ln_ppo4 = ', ln_ppo4
375         WRITE(numout,*) '             Logical switch for profile DIC obs                      ln_pdic = ', ln_pdic
376         WRITE(numout,*) '             Logical switch for profile alkalinity obs               ln_palk = ', ln_palk
377         WRITE(numout,*) '             Logical switch for profile pH obs                        ln_pph = ', ln_pph
378         WRITE(numout,*) '             Logical switch for profile oxygen obs                    ln_po2 = ', ln_po2
379         WRITE(numout,*) '             Global distribution of observations              ln_grid_global = ', ln_grid_global
380         WRITE(numout,*) '             Logical switch for obs grid search lookup ln_grid_search_lookup = ', ln_grid_search_lookup
381         IF (ln_grid_search_lookup) &
382            WRITE(numout,*) '             Grid search lookup file header                cn_gridsearchfile = ', cn_gridsearchfile
383         WRITE(numout,*) '             Initial date in window YYYYMMDD.HHMMSS               rn_dobsini = ', rn_dobsini
384         WRITE(numout,*) '             Final date in window YYYYMMDD.HHMMSS                 rn_dobsend = ', rn_dobsend
385         WRITE(numout,*) '             Type of vertical interpolation method                  nn_1dint = ', nn_1dint
386         WRITE(numout,*) '             Default horizontal interpolation method        nn_2dint_default = ', nn_2dint_default
387         WRITE(numout,*) '             Type of horizontal interpolation method for SLA    nn_2dint_sla = ', nn_2dint_sla
388         WRITE(numout,*) '             Type of horizontal interpolation method for SST    nn_2dint_sst = ', nn_2dint_sst
389         WRITE(numout,*) '             Type of horizontal interpolation method for SSS    nn_2dint_sss = ', nn_2dint_sss
390         WRITE(numout,*) '             Type of horizontal interpolation method for SIC    nn_2dint_sic = ', nn_2dint_sic
391         WRITE(numout,*) '             Default E/W diameter of obs footprint      rn_default_avglamscl = ', rn_default_avglamscl
392         WRITE(numout,*) '             Default N/S diameter of obs footprint      rn_default_avgphiscl = ', rn_default_avgphiscl
393         WRITE(numout,*) '             Default obs footprint in deg [T] or m [F]  ln_default_fp_indegs = ', ln_default_fp_indegs
394         WRITE(numout,*) '             SLA E/W diameter of obs footprint              rn_sla_avglamscl = ', rn_sla_avglamscl
395         WRITE(numout,*) '             SLA N/S diameter of obs footprint              rn_sla_avgphiscl = ', rn_sla_avgphiscl
396         WRITE(numout,*) '             SLA obs footprint in deg [T] or m [F]          ln_sla_fp_indegs = ', ln_sla_fp_indegs
397         WRITE(numout,*) '             SST E/W diameter of obs footprint              rn_sst_avglamscl = ', rn_sst_avglamscl
398         WRITE(numout,*) '             SST N/S diameter of obs footprint              rn_sst_avgphiscl = ', rn_sst_avgphiscl
399         WRITE(numout,*) '             SST obs footprint in deg [T] or m [F]          ln_sst_fp_indegs = ', ln_sst_fp_indegs
400         WRITE(numout,*) '             SIC E/W diameter of obs footprint              rn_sic_avglamscl = ', rn_sic_avglamscl
401         WRITE(numout,*) '             SIC N/S diameter of obs footprint              rn_sic_avgphiscl = ', rn_sic_avgphiscl
402         WRITE(numout,*) '             SIC obs footprint in deg [T] or m [F]          ln_sic_fp_indegs = ', ln_sic_fp_indegs
403         WRITE(numout,*) '             Rejection of observations near land switch               ln_nea = ', ln_nea
404         WRITE(numout,*) '             Rejection of obs near open bdys                 ln_bound_reject = ', ln_bound_reject
405         WRITE(numout,*) '             MSSH correction scheme                                 nn_msshc = ', nn_msshc
406         WRITE(numout,*) '             MDT  correction                                      rn_mdtcorr = ', rn_mdtcorr
407         WRITE(numout,*) '             MDT cutoff for computed correction                 rn_mdtcutoff = ', rn_mdtcutoff
408         WRITE(numout,*) '             Logical switch for alt bias                          ln_altbias = ', ln_altbias
409         WRITE(numout,*) '             Logical switch for sst bias                          ln_sstbias = ', ln_sstbias
410         WRITE(numout,*) '             Logical switch for ignoring missing files             ln_ignmis = ', ln_ignmis
411         WRITE(numout,*) '             Daily average types                             nn_profdavtypes = ', nn_profdavtypes
412         WRITE(numout,*) '             Logical switch for night-time SST obs               ln_sstnight = ', ln_sstnight
413      ENDIF
414      !-----------------------------------------------------------------------
415      ! Set up list of observation types to be used
416      ! and the files associated with each type
417      !-----------------------------------------------------------------------
418
419      nproftypes = COUNT( (/ln_t3d .OR. ln_s3d, ln_vel3d, ln_plchltot,          &
420         &                  ln_pchltot,  ln_pno3,     ln_psi4,     ln_ppo4,     &
421         &                  ln_pdic,     ln_palk,     ln_pph,      ln_po2 /) )
422      nsurftypes = COUNT( (/ln_sla, ln_sst, ln_sic, ln_sss,                     &
423         &                  ln_slchltot, ln_slchldia, ln_slchlnon, ln_slchldin, &
424         &                  ln_slchlmic, ln_slchlnan, ln_slchlpic, ln_schltot,  &
425         &                  ln_sspm,     ln_sfco2,    ln_spco2 /) )
426
427      IF ( nproftypes == 0 .AND. nsurftypes == 0 ) THEN
428         IF(lwp) WRITE(numout,cform_war)
429         IF(lwp) WRITE(numout,*) ' ln_diaobs is set to true, but all obs operator logical flags', &
430            &                    ' are set to .FALSE. so turning off calls to dia_obs'
431         nwarn = nwarn + 1
432         lk_diaobs = .FALSE.
433         RETURN
434      ENDIF
435
436      IF(lwp) WRITE(numout,*) '          Number of profile obs types: ',nproftypes
437      IF ( nproftypes > 0 ) THEN
438
439         ALLOCATE( cobstypesprof(nproftypes) )
440         ALLOCATE( ifilesprof(nproftypes) )
441         ALLOCATE( clproffiles(nproftypes,jpmaxnfiles) )
442
443         jtype = 0
444         IF (ln_t3d .OR. ln_s3d) THEN
445            jtype = jtype + 1
446            cobstypesprof(jtype) = 'prof'
447            clproffiles(jtype,:) = cn_profbfiles
448         ENDIF
449         IF (ln_vel3d) THEN
450            jtype = jtype + 1
451            cobstypesprof(jtype) =  'vel'
452            clproffiles(jtype,:) = cn_velfbfiles
453         ENDIF
454         IF (ln_plchltot) THEN
455            jtype = jtype + 1
456            cobstypesprof(jtype) = 'plchltot'
457            clproffiles(jtype,:) = cn_plchltotfbfiles
458         ENDIF
459         IF (ln_pchltot) THEN
460            jtype = jtype + 1
461            cobstypesprof(jtype) = 'pchltot'
462            clproffiles(jtype,:) = cn_pchltotfbfiles
463         ENDIF
464         IF (ln_pno3) THEN
465            jtype = jtype + 1
466            cobstypesprof(jtype) = 'pno3'
467            clproffiles(jtype,:) = cn_pno3fbfiles
468         ENDIF
469         IF (ln_psi4) THEN
470            jtype = jtype + 1
471            cobstypesprof(jtype) = 'psi4'
472            clproffiles(jtype,:) = cn_psi4fbfiles
473         ENDIF
474         IF (ln_ppo4) THEN
475            jtype = jtype + 1
476            cobstypesprof(jtype) = 'ppo4'
477            clproffiles(jtype,:) = cn_ppo4fbfiles
478         ENDIF
479         IF (ln_pdic) THEN
480            jtype = jtype + 1
481            cobstypesprof(jtype) = 'pdic'
482            clproffiles(jtype,:) = cn_pdicfbfiles
483         ENDIF
484         IF (ln_palk) THEN
485            jtype = jtype + 1
486            cobstypesprof(jtype) = 'palk'
487            clproffiles(jtype,:) = cn_palkfbfiles
488         ENDIF
489         IF (ln_pph) THEN
490            jtype = jtype + 1
491            cobstypesprof(jtype) = 'pph'
492            clproffiles(jtype,:) = cn_pphfbfiles
493         ENDIF
494         IF (ln_po2) THEN
495            jtype = jtype + 1
496            cobstypesprof(jtype) = 'po2'
497            clproffiles(jtype,:) = cn_po2fbfiles
498         ENDIF
499
500         CALL obs_settypefiles( nproftypes, jpmaxnfiles, ifilesprof, cobstypesprof, clproffiles )
501
502      ENDIF
503
504      IF(lwp) WRITE(numout,*)'          Number of surface obs types: ',nsurftypes
505      IF ( nsurftypes > 0 ) THEN
506
507         ALLOCATE( cobstypessurf(nsurftypes) )
508         ALLOCATE( ifilessurf(nsurftypes) )
509         ALLOCATE( clsurffiles(nsurftypes, jpmaxnfiles) )
510         ALLOCATE(n2dintsurf(nsurftypes))
511         ALLOCATE(ravglamscl(nsurftypes))
512         ALLOCATE(ravgphiscl(nsurftypes))
513         ALLOCATE(lfpindegs(nsurftypes))
514         ALLOCATE(llnightav(nsurftypes))
515
516         jtype = 0
517         IF (ln_sla) THEN
518            jtype = jtype + 1
519            cobstypessurf(jtype) = 'sla'
520            clsurffiles(jtype,:) = cn_slafbfiles
521         ENDIF
522         IF (ln_sst) THEN
523            jtype = jtype + 1
524            cobstypessurf(jtype) = 'sst'
525            clsurffiles(jtype,:) = cn_sstfbfiles
526         ENDIF
527         IF (ln_sic) THEN
528            jtype = jtype + 1
529            cobstypessurf(jtype) = 'sic'
530            clsurffiles(jtype,:) = cn_sicfbfiles
531         ENDIF
532         IF (ln_sss) THEN
533            jtype = jtype + 1
534            cobstypessurf(jtype) = 'sss'
535            clsurffiles(jtype,:) = cn_sssfbfiles
536         ENDIF
537         IF (ln_slchltot) THEN
538            jtype = jtype + 1
539            cobstypessurf(jtype) = 'slchltot'
540            clsurffiles(jtype,:) = cn_slchltotfbfiles
541         ENDIF
542         IF (ln_slchldia) THEN
543            jtype = jtype + 1
544            cobstypessurf(jtype) = 'slchldia'
545            clsurffiles(jtype,:) = cn_slchldiafbfiles
546         ENDIF
547         IF (ln_slchlnon) THEN
548            jtype = jtype + 1
549            cobstypessurf(jtype) = 'slchlnon'
550            clsurffiles(jtype,:) = cn_slchlnonfbfiles
551         ENDIF
552         IF (ln_slchldin) THEN
553            jtype = jtype + 1
554            cobstypessurf(jtype) = 'slchldin'
555            clsurffiles(jtype,:) = cn_slchldinfbfiles
556         ENDIF
557         IF (ln_slchlmic) THEN
558            jtype = jtype + 1
559            cobstypessurf(jtype) = 'slchlmic'
560            clsurffiles(jtype,:) = cn_slchlmicfbfiles
561         ENDIF
562         IF (ln_slchlnan) THEN
563            jtype = jtype + 1
564            cobstypessurf(jtype) = 'slchlnan'
565            clsurffiles(jtype,:) = cn_slchlnanfbfiles
566         ENDIF
567         IF (ln_slchlpic) THEN
568            jtype = jtype + 1
569            cobstypessurf(jtype) = 'slchlpic'
570            clsurffiles(jtype,:) = cn_slchlpicfbfiles
571         ENDIF
572         IF (ln_schltot) THEN
573            jtype = jtype + 1
574            cobstypessurf(jtype) = 'schltot'
575            clsurffiles(jtype,:) = cn_schltotfbfiles
576         ENDIF
577         IF (ln_sspm) THEN
578            jtype = jtype + 1
579            cobstypessurf(jtype) = 'sspm'
580            clsurffiles(jtype,:) = cn_sspmfbfiles
581         ENDIF
582         IF (ln_sfco2) THEN
583            jtype = jtype + 1
584            cobstypessurf(jtype) = 'sfco2'
585            clsurffiles(jtype,:) = cn_sfco2fbfiles
586         ENDIF
587         IF (ln_spco2) THEN
588            jtype = jtype + 1
589            cobstypessurf(jtype) = 'spco2'
590            clsurffiles(jtype,:) = cn_spco2fbfiles
591         ENDIF
592
593         CALL obs_settypefiles( nsurftypes, jpmaxnfiles, ifilessurf, cobstypessurf, clsurffiles )
594
595         DO jtype = 1, nsurftypes
596
597            IF ( TRIM(cobstypessurf(jtype)) == 'sla' ) THEN
598               IF ( nn_2dint_sla == -1 ) THEN
599                  n2dint_type  = nn_2dint_default
600               ELSE
601                  n2dint_type  = nn_2dint_sla
602               ENDIF
603               rtype_avglamscl = rn_sla_avglamscl
604               rtype_avgphiscl = rn_sla_avgphiscl
605               ltype_fp_indegs = ln_sla_fp_indegs
606               ltype_night     = .FALSE.
607            ELSE IF ( TRIM(cobstypessurf(jtype)) == 'sst' ) THEN
608               IF ( nn_2dint_sst == -1 ) THEN
609                  n2dint_type  = nn_2dint_default
610               ELSE
611                  n2dint_type  = nn_2dint_sst
612               ENDIF
613               rtype_avglamscl = rn_sst_avglamscl
614               rtype_avgphiscl = rn_sst_avgphiscl
615               ltype_fp_indegs = ln_sst_fp_indegs
616               ltype_night     = ln_sstnight
617            ELSE IF ( TRIM(cobstypessurf(jtype)) == 'sic' ) THEN
618               IF ( nn_2dint_sic == -1 ) THEN
619                  n2dint_type  = nn_2dint_default
620               ELSE
621                  n2dint_type  = nn_2dint_sic
622               ENDIF
623               rtype_avglamscl = rn_sic_avglamscl
624               rtype_avgphiscl = rn_sic_avgphiscl
625               ltype_fp_indegs = ln_sic_fp_indegs
626               ltype_night     = .FALSE.
627            ELSE IF ( TRIM(cobstypessurf(jtype)) == 'sss' ) THEN
628               IF ( nn_2dint_sss == -1 ) THEN
629                  n2dint_type  = nn_2dint_default
630               ELSE
631                  n2dint_type  = nn_2dint_sss
632               ENDIF
633               rtype_avglamscl = rn_sss_avglamscl
634               rtype_avgphiscl = rn_sss_avgphiscl
635               ltype_fp_indegs = ln_sss_fp_indegs
636               ltype_night     = .FALSE.
637            ELSE
638               n2dint_type     = nn_2dint_default
639               rtype_avglamscl = rn_default_avglamscl
640               rtype_avgphiscl = rn_default_avgphiscl
641               ltype_fp_indegs = ln_default_fp_indegs
642               ltype_night     = .FALSE.
643            ENDIF
644           
645            CALL obs_setinterpopts( nsurftypes, jtype, TRIM(cobstypessurf(jtype)), &
646               &                    nn_2dint_default, n2dint_type,                 &
647               &                    rtype_avglamscl, rtype_avgphiscl,              &
648               &                    ltype_fp_indegs, ltype_night,                  &
649               &                    n2dintsurf, ravglamscl, ravgphiscl,            &
650               &                    lfpindegs, llnightav )
651
652         END DO
653
654      ENDIF
655
656      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~'
657
658
659      !-----------------------------------------------------------------------
660      ! Obs operator parameter checking and initialisations
661      !-----------------------------------------------------------------------
662
663      IF ( ln_vel3d .AND. ( .NOT. ln_grid_global ) ) THEN
664         CALL ctl_stop( 'Velocity data only works with ln_grid_global=.true.' )
665         RETURN
666      ENDIF
667
668      IF ( ( nn_1dint < 0 ) .OR. ( nn_1dint > 1 ) ) THEN
669         CALL ctl_stop(' Choice of vertical (1D) interpolation method', &
670            &                    ' is not available')
671      ENDIF
672
673      IF ( ( nn_2dint_default < 0 ) .OR. ( nn_2dint_default > 6 ) ) THEN
674         CALL ctl_stop(' Choice of default horizontal (2D) interpolation method', &
675            &                    ' is not available')
676      ENDIF
677
678      CALL obs_typ_init
679
680      CALL mppmap_init
681
682      CALL obs_grid_setup( )
683
684      !-----------------------------------------------------------------------
685      ! Depending on switches read the various observation types
686      !-----------------------------------------------------------------------
687
688      IF ( nproftypes > 0 ) THEN
689
690         ALLOCATE(profdata(nproftypes))
691         ALLOCATE(profdataqc(nproftypes))
692         ALLOCATE(nvarsprof(nproftypes))
693         ALLOCATE(nextrprof(nproftypes))
694
695         DO jtype = 1, nproftypes
696
697            IF ( TRIM(cobstypesprof(jtype)) == 'prof' ) THEN
698               nvarsprof(jtype) = 2
699               nextrprof(jtype) = 1
700               ALLOCATE(llvar(nvarsprof(jtype)))
701               llvar(1) = ln_t3d
702               llvar(2) = ln_s3d
703               zglam1 = glamt
704               zgphi1 = gphit
705               zmask1 = tmask
706               zglam2 = glamt
707               zgphi2 = gphit
708               zmask2 = tmask
709            ELSE IF ( TRIM(cobstypesprof(jtype)) == 'vel' )  THEN
710               nvarsprof(jtype) = 2
711               nextrprof(jtype) = 2
712               ALLOCATE(llvar(nvarsprof(jtype)))
713               llvar(1) = ln_vel3d
714               llvar(2) = ln_vel3d
715               zglam1 = glamu
716               zgphi1 = gphiu
717               zmask1 = umask
718               zglam2 = glamv
719               zgphi2 = gphiv
720               zmask2 = vmask
721            ELSE
722               nvarsprof(jtype) = 1
723               nextrprof(jtype) = 0
724               ALLOCATE(llvar(nvarsprof(jtype)))
725               llvar(1) = .TRUE.
726               zglam1 = glamt
727               zgphi1 = gphit
728               zmask1 = tmask
729               zglam2 = glamt
730               zgphi2 = gphit
731               zmask2 = tmask
732            ENDIF
733
734            !Read in profile or profile obs types
735            CALL obs_rea_prof( profdata(jtype), ifilesprof(jtype),       &
736               &               clproffiles(jtype,1:ifilesprof(jtype)), &
737               &               nvarsprof(jtype), nextrprof(jtype), nitend-nit000+2, &
738               &               rn_dobsini, rn_dobsend, llvar, &
739               &               ln_ignmis, ln_s_at_t, .FALSE., &
740               &               kdailyavtypes = nn_profdavtypes )
741
742            DO jvar = 1, nvarsprof(jtype)
743               CALL obs_prof_staend( profdata(jtype), jvar )
744            END DO
745
746            CALL obs_pre_prof( profdata(jtype), profdataqc(jtype), &
747               &               llvar(1), llvar(2), &
748               &               jpi, jpj, jpk, &
749               &               zmask1, zglam1, zgphi1, zmask2, zglam2, zgphi2,  &
750               &               ln_nea, ln_bound_reject, &
751               &               kdailyavtypes = nn_profdavtypes )
752           
753            ! Is allocating and deallocating repeatedly in a loop good practice?
754            DEALLOCATE(llvar)
755
756         END DO
757
758         DEALLOCATE( ifilesprof, clproffiles )
759
760      ENDIF
761
762      IF ( nsurftypes > 0 ) THEN
763
764         ALLOCATE(surfdata(nsurftypes))
765         ALLOCATE(surfdataqc(nsurftypes))
766         ALLOCATE(nvarssurf(nsurftypes))
767         ALLOCATE(nextrsurf(nsurftypes))
768
769         DO jtype = 1, nsurftypes
770
771            nvarssurf(jtype) = 1
772            nextrsurf(jtype) = 0
773            IF ( TRIM(cobstypessurf(jtype)) == 'sla' ) nextrsurf(jtype) = 2
774
775            !Read in surface obs types
776            CALL obs_rea_surf( surfdata(jtype), ifilessurf(jtype), &
777               &               clsurffiles(jtype,1:ifilessurf(jtype)), &
778               &               nvarssurf(jtype), nextrsurf(jtype), nitend-nit000+2, &
779               &               rn_dobsini, rn_dobsend, ln_ignmis, .FALSE., llnightav(jtype) )
780
781            CALL obs_pre_surf( surfdata(jtype), surfdataqc(jtype), ln_nea, ln_bound_reject )
782
783            IF ( TRIM(cobstypessurf(jtype)) == 'sla' ) THEN
784               CALL obs_rea_mdt( surfdataqc(jtype), n2dintsurf(jtype) )
785               IF ( ln_altbias ) &
786                  & CALL obs_rea_altbias ( surfdataqc(jtype), n2dintsurf(jtype), cn_altbiasfile )
787            ENDIF
788
789            IF ( TRIM(cobstypessurf(jtype)) == 'sst' .AND. ln_sstbias ) THEN
790               jnumsstbias = 0
791               DO jfile = 1, jpmaxnfiles
792                  IF ( TRIM(cn_sstbiasfiles(jfile)) /= '' ) &
793                     &  jnumsstbias = jnumsstbias + 1
794               END DO
795               IF ( jnumsstbias == 0 ) THEN
796                  CALL ctl_stop("ln_sstbias set but no bias files to read in")   
797               ENDIF
798
799               CALL obs_app_sstbias( surfdataqc(jtype), n2dintsurf(jtype), & 
800                  &                  jnumsstbias, cn_sstbiasfiles(1:jnumsstbias) ) 
801
802            ENDIF
803
804         END DO
805
806         DEALLOCATE( ifilessurf, clsurffiles )
807
808      ENDIF
809
810      CALL wrk_dealloc( jpi, jpj, zglam1 )
811      CALL wrk_dealloc( jpi, jpj, zglam2 )
812      CALL wrk_dealloc( jpi, jpj, zgphi1 )
813      CALL wrk_dealloc( jpi, jpj, zgphi2 )
814      CALL wrk_dealloc( jpi, jpj, jpk, zmask1 )
815      CALL wrk_dealloc( jpi, jpj, jpk, zmask2 )
816
817   END SUBROUTINE dia_obs_init
818
819   SUBROUTINE dia_obs( kstp )
820      !!----------------------------------------------------------------------
821      !!                    ***  ROUTINE dia_obs  ***
822      !!         
823      !! ** Purpose : Call the observation operators on each time step
824      !!
825      !! ** Method  : Call the observation operators on each time step to
826      !!              compute the model equivalent of the following data:
827      !!               - Profile data, currently T/S or U/V
828      !!               - Surface data, currently SST, SLA or sea-ice concentration.
829      !!
830      !! ** Action  :
831      !!
832      !! History :
833      !!        !  06-03  (K. Mogensen) Original code
834      !!        !  06-05  (K. Mogensen) Reformatted
835      !!        !  06-10  (A. Weaver) Cleaning
836      !!        !  07-03  (K. Mogensen) General handling of profiles
837      !!        !  07-04  (G. Smith) Generalized surface operators
838      !!        !  08-10  (M. Valdivieso) obs operator for velocity profiles
839      !!        !  15-08  (M. Martin) Combined surface/profile routines.
840      !!----------------------------------------------------------------------
841      !! * Modules used
842      USE phycst, ONLY : &         ! Physical constants
843         & rday
844      USE oce, ONLY : &            ! Ocean dynamics and tracers variables
845         & tsn,       &
846         & un,        &
847         & vn,        &
848         & sshn
849#if defined  key_lim3
850      USE ice, ONLY : &            ! LIM3 Ice model variables
851         & frld
852#endif
853#if defined key_lim2
854      USE ice_2, ONLY : &          ! LIM2 Ice model variables
855         & frld
856#endif
857#if defined key_cice
858      USE sbc_oce, ONLY : fr_i     ! ice fraction
859#endif
860#if defined key_hadocc
861      USE trc, ONLY :  &           ! HadOCC variables
862         & trn, &
863         & HADOCC_CHL, &
864         & HADOCC_FCO2, &
865         & HADOCC_PCO2, &
866         & HADOCC_FILL_FLT
867      USE par_hadocc
868#elif defined key_medusa && defined key_foam_medusa
869      USE trc, ONLY :  &           ! MEDUSA variables
870         & trn
871      USE par_medusa
872#if defined key_roam
873      USE sms_medusa, ONLY: &
874         & f2_pco2w, &
875         & f2_fco2w, &
876         & f3_pH
877#endif
878#elif defined key_fabm
879      USE fabm
880      USE par_fabm
881#endif
882#if defined key_spm
883      USE par_spm, ONLY: &         ! ERSEM/SPM sediments
884         & jp_spm
885      USE trc, ONLY :  &
886         & trn
887#endif
888
889      IMPLICIT NONE
890
891      !! * Arguments
892      INTEGER, INTENT(IN) :: kstp  ! Current timestep
893      !! * Local declarations
894      INTEGER :: idaystp           ! Number of timesteps per day
895      INTEGER :: jtype             ! Data loop variable
896      INTEGER :: jvar              ! Variable number
897      INTEGER :: ji, jj            ! Loop counters
898      REAL(wp) :: tiny             ! small number
899      REAL(wp), POINTER, DIMENSION(:,:,:) :: &
900         & zprofvar1, &            ! Model values for 1st variable in a prof ob
901         & zprofvar2               ! Model values for 2nd variable in a prof ob
902      REAL(wp), POINTER, DIMENSION(:,:,:) :: &
903         & zprofmask1, &           ! Mask associated with zprofvar1
904         & zprofmask2              ! Mask associated with zprofvar2
905      REAL(wp), POINTER, DIMENSION(:,:) :: &
906         & zsurfvar, &             ! Model values equivalent to surface ob.
907         & zsurfmask               ! Mask associated with surface variable
908      REAL(wp), POINTER, DIMENSION(:,:) :: &
909         & zglam1,    &            ! Model longitudes for prof variable 1
910         & zglam2,    &            ! Model longitudes for prof variable 2
911         & zgphi1,    &            ! Model latitudes for prof variable 1
912         & zgphi2                  ! Model latitudes for prof variable 2
913      LOGICAL :: llog10            ! Perform log10 transform of variable
914
915
916      !Allocate local work arrays
917      CALL wrk_alloc( jpi, jpj, jpk, zprofvar1 )
918      CALL wrk_alloc( jpi, jpj, jpk, zprofvar2 )
919      CALL wrk_alloc( jpi, jpj, jpk, zprofmask1 )
920      CALL wrk_alloc( jpi, jpj, jpk, zprofmask2 )
921      CALL wrk_alloc( jpi, jpj, zsurfvar )
922      CALL wrk_alloc( jpi, jpj, zsurfmask )
923      CALL wrk_alloc( jpi, jpj, zglam1 )
924      CALL wrk_alloc( jpi, jpj, zglam2 )
925      CALL wrk_alloc( jpi, jpj, zgphi1 )
926      CALL wrk_alloc( jpi, jpj, zgphi2 )
927
928      IF(lwp) THEN
929         WRITE(numout,*)
930         WRITE(numout,*) 'dia_obs : Call the observation operators', kstp
931         WRITE(numout,*) '~~~~~~~'
932         CALL FLUSH(numout)
933      ENDIF
934
935      idaystp = NINT( rday / rdt )
936
937      !-----------------------------------------------------------------------
938      ! Call the profile and surface observation operators
939      !-----------------------------------------------------------------------
940
941      IF ( nproftypes > 0 ) THEN
942
943         DO jtype = 1, nproftypes
944
945            SELECT CASE ( TRIM(cobstypesprof(jtype)) )
946            CASE('prof')
947               zprofvar1(:,:,:) = tsn(:,:,:,jp_tem)
948               zprofvar2(:,:,:) = tsn(:,:,:,jp_sal)
949               zprofmask1(:,:,:) = tmask(:,:,:)
950               zprofmask2(:,:,:) = tmask(:,:,:)
951               zglam1(:,:) = glamt(:,:)
952               zglam2(:,:) = glamt(:,:)
953               zgphi1(:,:) = gphit(:,:)
954               zgphi2(:,:) = gphit(:,:)
955            CASE('vel')
956               zprofvar1(:,:,:) = un(:,:,:)
957               zprofvar2(:,:,:) = vn(:,:,:)
958               zprofmask1(:,:,:) = umask(:,:,:)
959               zprofmask2(:,:,:) = vmask(:,:,:)
960               zglam1(:,:) = glamu(:,:)
961               zglam2(:,:) = glamv(:,:)
962               zgphi1(:,:) = gphiu(:,:)
963               zgphi2(:,:) = gphiv(:,:)
964
965            CASE('plchltot')
966#if defined key_hadocc
967               ! Chlorophyll from HadOCC
968               zprofvar1(:,:,:) = HADOCC_CHL(:,:,:)
969#elif defined key_medusa && defined key_foam_medusa
970               ! Add non-diatom and diatom chlorophyll from MEDUSA
971               zprofvar1(:,:,:) = trn(:,:,:,jpchn) + trn(:,:,:,jpchd)
972#elif defined key_fabm
973               ! Add all chlorophyll groups from ERSEM
974               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_chl1) + trn(:,:,:,jp_fabm_chl2) + &
975                  &               trn(:,:,:,jp_fabm_chl3) + trn(:,:,:,jp_fabm_chl4)
976#else
977               CALL ctl_stop( ' Trying to run plchltot observation operator', &
978                  &           ' but no biogeochemical model appears to have been defined' )
979#endif
980               zprofmask1(:,:,:) = tmask(:,:,:)
981               ! Take the log10 where we can, otherwise exclude
982               tiny = 1.0e-20
983               WHERE(zprofvar1(:,:,:) > tiny .AND. zprofvar1(:,:,:) /= obfillflt )
984                  zprofvar1(:,:,:)  = LOG10(zprofvar1(:,:,:))
985               ELSEWHERE
986                  zprofvar1(:,:,:)  = obfillflt
987                  zprofmask1(:,:,:) = 0
988               END WHERE
989               zglam1(:,:) = glamt(:,:)
990               zgphi1(:,:) = gphit(:,:)
991
992            CASE('pchltot')
993#if defined key_hadocc
994               ! Chlorophyll from HadOCC
995               zprofvar1(:,:,:) = HADOCC_CHL(:,:,:)
996#elif defined key_medusa && defined key_foam_medusa
997               ! Add non-diatom and diatom chlorophyll from MEDUSA
998               zprofvar1(:,:,:) = trn(:,:,:,jpchn) + trn(:,:,:,jpchd)
999#elif defined key_fabm
1000               ! Add all chlorophyll groups from ERSEM
1001               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_chl1) + trn(:,:,:,jp_fabm_chl2) + &
1002                  &               trn(:,:,:,jp_fabm_chl3) + trn(:,:,:,jp_fabm_chl4)
1003#else
1004               CALL ctl_stop( ' Trying to run pchltot observation operator', &
1005                  &           ' but no biogeochemical model appears to have been defined' )
1006#endif
1007               zprofmask1(:,:,:) = tmask(:,:,:)
1008               zglam1(:,:) = glamt(:,:)
1009               zgphi1(:,:) = gphit(:,:)
1010
1011            CASE('pno3')
1012#if defined key_hadocc
1013               ! Dissolved inorganic nitrogen from HadOCC
1014               zprofvar1(:,:,:) = trn(:,:,:,jp_had_nut)
1015#elif defined key_medusa && defined key_foam_medusa
1016               ! Dissolved inorganic nitrogen from MEDUSA
1017               zprofvar1(:,:,:) = trn(:,:,:,jpdin)
1018#elif defined key_fabm
1019               ! Nitrate from ERSEM
1020               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_n3n)
1021#else
1022               CALL ctl_stop( ' Trying to run pno3 observation operator', &
1023                  &           ' but no biogeochemical model appears to have been defined' )
1024#endif
1025               zprofmask1(:,:,:) = tmask(:,:,:)
1026               zglam1(:,:) = glamt(:,:)
1027               zgphi1(:,:) = gphit(:,:)
1028
1029            CASE('psi4')
1030#if defined key_hadocc
1031               CALL ctl_stop( ' Trying to run psi4 observation operator', &
1032                  &           ' but HadOCC does not simulate silicate' )
1033#elif defined key_medusa && defined key_foam_medusa
1034               ! Silicate from MEDUSA
1035               zprofvar1(:,:,:) = trn(:,:,:,jpsil)
1036#elif defined key_fabm
1037               ! Silicate from ERSEM
1038               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_n5s)
1039#else
1040               CALL ctl_stop( ' Trying to run psi4 observation operator', &
1041                  &           ' but no biogeochemical model appears to have been defined' )
1042#endif
1043               zprofmask1(:,:,:) = tmask(:,:,:)
1044               zglam1(:,:) = glamt(:,:)
1045               zgphi1(:,:) = gphit(:,:)
1046
1047            CASE('ppo4')
1048#if defined key_hadocc
1049               CALL ctl_stop( ' Trying to run ppo4 observation operator', &
1050                  &           ' but HadOCC does not simulate phosphate' )
1051#elif defined key_medusa && defined key_foam_medusa
1052               CALL ctl_stop( ' Trying to run ppo4 observation operator', &
1053                  &           ' but MEDUSA does not simulate phosphate' )
1054#elif defined key_fabm
1055               ! Phosphate from ERSEM
1056               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_n1p)
1057#else
1058               CALL ctl_stop( ' Trying to run ppo4 observation operator', &
1059                  &           ' but no biogeochemical model appears to have been defined' )
1060#endif
1061               zprofmask1(:,:,:) = tmask(:,:,:)
1062               zglam1(:,:) = glamt(:,:)
1063               zgphi1(:,:) = gphit(:,:)
1064
1065            CASE('pdic')
1066#if defined key_hadocc
1067               ! Dissolved inorganic carbon from HadOCC
1068               zprofvar1(:,:,:) = trn(:,:,:,jp_had_dic)
1069#elif defined key_medusa && defined key_foam_medusa
1070               ! Dissolved inorganic carbon from MEDUSA
1071               zprofvar1(:,:,:) = trn(:,:,:,jpdic)
1072#elif defined key_fabm
1073               ! Dissolved inorganic carbon from ERSEM
1074               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_o3c)
1075#else
1076               CALL ctl_stop( ' Trying to run pdic observation operator', &
1077                  &           ' but no biogeochemical model appears to have been defined' )
1078#endif
1079               zprofmask1(:,:,:) = tmask(:,:,:)
1080               zglam1(:,:) = glamt(:,:)
1081               zgphi1(:,:) = gphit(:,:)
1082
1083            CASE('palk')
1084#if defined key_hadocc
1085               ! Alkalinity from HadOCC
1086               zprofvar1(:,:,:) = trn(:,:,:,jp_had_alk)
1087#elif defined key_medusa && defined key_foam_medusa
1088               ! Alkalinity from MEDUSA
1089               zprofvar1(:,:,:) = trn(:,:,:,jpalk)
1090#elif defined key_fabm
1091               ! Alkalinity from ERSEM
1092               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_o3a)
1093#else
1094               CALL ctl_stop( ' Trying to run palk observation operator', &
1095                  &           ' but no biogeochemical model appears to have been defined' )
1096#endif
1097               zprofmask1(:,:,:) = tmask(:,:,:)
1098               zglam1(:,:) = glamt(:,:)
1099               zgphi1(:,:) = gphit(:,:)
1100
1101            CASE('pph')
1102#if defined key_hadocc
1103               CALL ctl_stop( ' Trying to run pph observation operator', &
1104                  &           ' but HadOCC has no pH diagnostic defined' )
1105#elif defined key_medusa && defined key_foam_medusa
1106               ! pH from MEDUSA
1107               zprofvar1(:,:,:) = f3_pH(:,:,:)
1108#elif defined key_fabm
1109               ! pH from ERSEM
1110               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_o3ph)
1111#else
1112               CALL ctl_stop( ' Trying to run pph observation operator', &
1113                  &           ' but no biogeochemical model appears to have been defined' )
1114#endif
1115               zprofmask1(:,:,:) = tmask(:,:,:)
1116               zglam1(:,:) = glamt(:,:)
1117               zgphi1(:,:) = gphit(:,:)
1118
1119            CASE('po2')
1120#if defined key_hadocc
1121               CALL ctl_stop( ' Trying to run po2 observation operator', &
1122                  &           ' but HadOCC does not simulate oxygen' )
1123#elif defined key_medusa && defined key_foam_medusa
1124               ! Oxygen from MEDUSA
1125               zprofvar1(:,:,:) = trn(:,:,:,jpoxy)
1126#elif defined key_fabm
1127               ! Oxygen from ERSEM
1128               zprofvar1(:,:,:) = trn(:,:,:,jp_fabm_o2o)
1129#else
1130               CALL ctl_stop( ' Trying to run po2 observation operator', &
1131                  &           ' but no biogeochemical model appears to have been defined' )
1132#endif
1133               zprofmask1(:,:,:) = tmask(:,:,:)
1134               zglam1(:,:) = glamt(:,:)
1135               zgphi1(:,:) = gphit(:,:)
1136
1137            CASE DEFAULT
1138               CALL ctl_stop( 'Unknown profile observation type '//TRIM(cobstypesprof(jtype))//' in dia_obs' )
1139            END SELECT
1140           
1141            IF ( ( TRIM(cobstypesprof(jtype)) /= 'prof' ) .AND. ( TRIM(cobstypesprof(jtype)) /= 'vel' ) ) THEN
1142               zprofvar2(:,:,:)  = zprofvar1(:,:,:)
1143               zprofmask2(:,:,:) = zprofmask1(:,:,:)
1144               zglam2(:,:)       = zglam1(:,:)
1145               zgphi2(:,:)       = zgphi1(:,:)
1146            ENDIF
1147
1148            CALL obs_prof_opt( profdataqc(jtype), kstp, jpi, jpj, jpk,  &
1149               &               nit000, idaystp,                         &
1150               &               zprofvar1, zprofvar2,                    &
1151               &               fsdept(:,:,:), fsdepw(:,:,:),            & 
1152               &               zprofmask1, zprofmask2,                  &
1153               &               zglam1, zglam2, zgphi1, zgphi2,          &
1154               &               nn_1dint, nn_2dint_default,              &
1155               &               kdailyavtypes = nn_profdavtypes )
1156
1157         END DO
1158
1159      ENDIF
1160
1161      IF ( nsurftypes > 0 ) THEN
1162
1163         DO jtype = 1, nsurftypes
1164
1165            !Defaults which might be changed
1166            zsurfmask(:,:) = tmask(:,:,1)
1167            llog10 = .FALSE.
1168
1169            SELECT CASE ( TRIM(cobstypessurf(jtype)) )
1170            CASE('sst')
1171               zsurfvar(:,:) = tsn(:,:,1,jp_tem)
1172            CASE('sla')
1173               zsurfvar(:,:) = sshn(:,:)
1174            CASE('sss')
1175               zsurfvar(:,:) = tsn(:,:,1,jp_sal)
1176            CASE('sic')
1177               IF ( kstp == 0 ) THEN
1178                  IF ( lwp .AND. surfdataqc(jtype)%nsstpmpp(1) > 0 ) THEN
1179                     CALL ctl_warn( 'Sea-ice not initialised on zeroth '// &
1180                        &           'time-step but some obs are valid then.' )
1181                     WRITE(numout,*)surfdataqc(jtype)%nsstpmpp(1), &
1182                        &           ' sea-ice obs will be missed'
1183                  ENDIF
1184                  surfdataqc(jtype)%nsurfup = surfdataqc(jtype)%nsurfup + &
1185                     &                        surfdataqc(jtype)%nsstp(1)
1186                  CYCLE
1187               ELSE
1188#if defined key_cice
1189                  zsurfvar(:,:) = fr_i(:,:)
1190#elif defined key_lim2 || defined key_lim3
1191                  zsurfvar(:,:) = 1._wp - frld(:,:)
1192#else
1193               CALL ctl_stop( ' Trying to run sea-ice observation operator', &
1194                  &           ' but no sea-ice model appears to have been defined' )
1195#endif
1196               ENDIF
1197
1198            CASE('slchltot')
1199#if defined key_hadocc
1200               ! Surface chlorophyll from HadOCC
1201               zsurfvar(:,:) = HADOCC_CHL(:,:,1)
1202#elif defined key_medusa && defined key_foam_medusa
1203               ! Add non-diatom and diatom surface chlorophyll from MEDUSA
1204               zsurfvar(:,:) = trn(:,:,1,jpchn) + trn(:,:,1,jpchd)
1205#elif defined key_fabm
1206               ! Add all surface chlorophyll groups from ERSEM
1207               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl1) + trn(:,:,1,jp_fabm_chl2) + &
1208                  &            trn(:,:,1,jp_fabm_chl3) + trn(:,:,1,jp_fabm_chl4)
1209#else
1210               CALL ctl_stop( ' Trying to run slchltot observation operator', &
1211                  &           ' but no biogeochemical model appears to have been defined' )
1212#endif
1213               llog10 = .TRUE.
1214
1215            CASE('slchldia')
1216#if defined key_hadocc
1217               CALL ctl_stop( ' Trying to run slchldia observation operator', &
1218                  &           ' but HadOCC does not explicitly simulate diatoms' )
1219#elif defined key_medusa && defined key_foam_medusa
1220               ! Diatom surface chlorophyll from MEDUSA
1221               zsurfvar(:,:) = trn(:,:,1,jpchd)
1222#elif defined key_fabm
1223               ! Diatom surface chlorophyll from ERSEM
1224               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl1)
1225#else
1226               CALL ctl_stop( ' Trying to run slchldia observation operator', &
1227                  &           ' but no biogeochemical model appears to have been defined' )
1228#endif
1229               llog10 = .TRUE.
1230
1231            CASE('slchlnon')
1232#if defined key_hadocc
1233               CALL ctl_stop( ' Trying to run slchlnon observation operator', &
1234                  &           ' but HadOCC does not explicitly simulate non-diatoms' )
1235#elif defined key_medusa && defined key_foam_medusa
1236               ! Non-diatom surface chlorophyll from MEDUSA
1237               zsurfvar(:,:) = trn(:,:,1,jpchn)
1238#elif defined key_fabm
1239               ! Add all non-diatom surface chlorophyll groups from ERSEM
1240               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl2) + &
1241                  &            trn(:,:,1,jp_fabm_chl3) + trn(:,:,1,jp_fabm_chl4)
1242#else
1243               CALL ctl_stop( ' Trying to run slchlnon observation operator', &
1244                  &           ' but no biogeochemical model appears to have been defined' )
1245#endif
1246               llog10 = .TRUE.
1247
1248            CASE('slchldin')
1249#if defined key_hadocc
1250               CALL ctl_stop( ' Trying to run slchldin observation operator', &
1251                  &           ' but HadOCC does not explicitly simulate dinoflagellates' )
1252#elif defined key_medusa && defined key_foam_medusa
1253               CALL ctl_stop( ' Trying to run slchldin observation operator', &
1254                  &           ' but MEDUSA does not explicitly simulate dinoflagellates' )
1255#elif defined key_fabm
1256               ! Dinoflagellate surface chlorophyll from ERSEM
1257               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl4)
1258#else
1259               CALL ctl_stop( ' Trying to run slchldin observation operator', &
1260                  &           ' but no biogeochemical model appears to have been defined' )
1261#endif
1262               llog10 = .TRUE.
1263
1264            CASE('slchlmic')
1265#if defined key_hadocc
1266               CALL ctl_stop( ' Trying to run slchlmic observation operator', &
1267                  &           ' but HadOCC does not explicitly simulate microphytoplankton' )
1268#elif defined key_medusa && defined key_foam_medusa
1269               CALL ctl_stop( ' Trying to run slchlmic observation operator', &
1270                  &           ' but MEDUSA does not explicitly simulate microphytoplankton' )
1271#elif defined key_fabm
1272               ! Add diatom and dinoflagellate surface chlorophyll from ERSEM
1273               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl1) + trn(:,:,1,jp_fabm_chl4)
1274#else
1275               CALL ctl_stop( ' Trying to run slchlmic observation operator', &
1276                  &           ' but no biogeochemical model appears to have been defined' )
1277#endif
1278               llog10 = .TRUE.
1279
1280            CASE('slchlnan')
1281#if defined key_hadocc
1282               CALL ctl_stop( ' Trying to run slchlnan observation operator', &
1283                  &           ' but HadOCC does not explicitly simulate nanophytoplankton' )
1284#elif defined key_medusa && defined key_foam_medusa
1285               CALL ctl_stop( ' Trying to run slchlnan observation operator', &
1286                  &           ' but MEDUSA does not explicitly simulate nanophytoplankton' )
1287#elif defined key_fabm
1288               ! Nanophytoplankton surface chlorophyll from ERSEM
1289               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl2)
1290#else
1291               CALL ctl_stop( ' Trying to run slchlnan observation operator', &
1292                  &           ' but no biogeochemical model appears to have been defined' )
1293#endif
1294               llog10 = .TRUE.
1295
1296            CASE('slchlpic')
1297#if defined key_hadocc
1298               CALL ctl_stop( ' Trying to run slchlpic observation operator', &
1299                  &           ' but HadOCC does not explicitly simulate picophytoplankton' )
1300#elif defined key_medusa && defined key_foam_medusa
1301               CALL ctl_stop( ' Trying to run slchlpic observation operator', &
1302                  &           ' but MEDUSA does not explicitly simulate picophytoplankton' )
1303#elif defined key_fabm
1304               ! Picophytoplankton surface chlorophyll from ERSEM
1305               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl3)
1306#else
1307               CALL ctl_stop( ' Trying to run slchlpic observation operator', &
1308                  &           ' but no biogeochemical model appears to have been defined' )
1309#endif
1310               llog10 = .TRUE.
1311
1312            CASE('schltot')
1313#if defined key_hadocc
1314               ! Surface chlorophyll from HadOCC
1315               zsurfvar(:,:) = HADOCC_CHL(:,:,1)
1316#elif defined key_medusa && defined key_foam_medusa
1317               ! Add non-diatom and diatom surface chlorophyll from MEDUSA
1318               zsurfvar(:,:) = trn(:,:,1,jpchn) + trn(:,:,1,jpchd)
1319#elif defined key_fabm
1320               ! Add all surface chlorophyll groups from ERSEM
1321               zsurfvar(:,:) = trn(:,:,1,jp_fabm_chl1) + trn(:,:,1,jp_fabm_chl2) + &
1322                  &            trn(:,:,1,jp_fabm_chl3) + trn(:,:,1,jp_fabm_chl4)
1323#else
1324               CALL ctl_stop( ' Trying to run schltot observation operator', &
1325                  &           ' but no biogeochemical model appears to have been defined' )
1326#endif
1327
1328            CASE('sspm')
1329#if defined key_spm
1330               zsurfvar(:,:) = 0.0
1331               DO jn = 1, jp_spm
1332                  zsurfvar(:,:) = zsurfvar(:,:) + trn(:,:,1,jn)   ! sum SPM sizes
1333               END DO
1334#else
1335               CALL ctl_stop( ' Trying to run sspm observation operator', &
1336                  &           ' but no spm model appears to have been defined' )
1337#endif
1338
1339            CASE('sfco2')
1340#if defined key_hadocc
1341               zsurfvar(:,:) = HADOCC_FCO2(:,:)    ! fCO2 from HadOCC
1342               IF ( ( MINVAL( HADOCC_FCO2 ) == HADOCC_FILL_FLT ) .AND. &
1343                  & ( MAXVAL( HADOCC_FCO2 ) == HADOCC_FILL_FLT ) ) THEN
1344                  zsurfvar(:,:) = obfillflt
1345                  zsurfmask(:,:) = 0
1346                  CALL ctl_warn( ' HadOCC fCO2 values masked out for observation operator', &
1347                     &           ' as HADOCC_FCO2(:,:) == HADOCC_FILL_FLT' )
1348               ENDIF
1349#elif defined key_medusa && defined key_foam_medusa && defined key_roam
1350               zsurfvar(:,:) = f2_fco2w(:,:)
1351#elif defined key_fabm
1352               ! First, get pCO2 from FABM
1353               pco2_3d(:,:,:) = fabm_get_bulk_diagnostic_data(model, jp_fabm_o3pc)
1354               zsurfvar(:,:) = pco2_3d(:,:,1)
1355               ! Now, convert pCO2 to fCO2, based on SST in K. This follows the standard methodology of:
1356               ! Pierrot et al. (2009), Recommendations for autonomous underway pCO2 measuring systems
1357               ! and data reduction routines, Deep-Sea Research II, 56: 512-522.
1358               ! and
1359               ! Weiss (1974), Carbon dioxide in water and seawater: the solubility of a non-ideal gas,
1360               ! Marine Chemistry, 2: 203-215.
1361               ! In the implementation below, atmospheric pressure has been assumed to be 1 atm and so
1362               ! not explicitly included - atmospheric pressure is not necessarily available so this is
1363               ! the best assumption.
1364               ! Further, the (1-xCO2)^2 term has been neglected. This is common practice
1365               ! (see e.g. Zeebe and Wolf-Gladrow (2001), CO2 in Seawater: Equilibrium, Kinetics, Isotopes)
1366               ! because xCO2 in atm is ~0, and so this term will only affect the result to the 3rd decimal
1367               ! place for typical values, and xCO2 would need to be approximated from pCO2 anyway.
1368               zsurfvar(:,:) = zsurfvar(:,:) * EXP((-1636.75                                                          + &
1369                  &            12.0408      * (tsn(:,:,1,jp_tem)+rt0)                                                 - &
1370                  &            0.0327957    * (tsn(:,:,1,jp_tem)+rt0)*(tsn(:,:,1,jp_tem)+rt0)                         + &
1371                  &            0.0000316528 * (tsn(:,:,1,jp_tem)+rt0)*(tsn(:,:,1,jp_tem)+rt0)*(tsn(:,:,1,jp_tem)+rt0) + &
1372                  &            2.0 * (57.7 - 0.118 * (tsn(:,:,1,jp_tem)+rt0)))                                        / &
1373                  &            (82.0578 * (tsn(:,:,1,jp_tem)+rt0)))
1374#else
1375               CALL ctl_stop( ' Trying to run sfco2 observation operator', &
1376                  &           ' but no biogeochemical model appears to have been defined' )
1377#endif
1378
1379            CASE('spco2')
1380#if defined key_hadocc
1381               zsurfvar(:,:) = HADOCC_PCO2(:,:)    ! pCO2 from HadOCC
1382               IF ( ( MINVAL( HADOCC_PCO2 ) == HADOCC_FILL_FLT ) .AND. &
1383                  & ( MAXVAL( HADOCC_PCO2 ) == HADOCC_FILL_FLT ) ) THEN
1384                  zsurfvar(:,:) = obfillflt
1385                  zsurfmask(:,:) = 0
1386                  CALL ctl_warn( ' HadOCC pCO2 values masked out for observation operator', &
1387                     &           ' as HADOCC_PCO2(:,:) == HADOCC_FILL_FLT' )
1388               ENDIF
1389#elif defined key_medusa && defined key_foam_medusa && defined key_roam
1390               zsurfvar(:,:) = f2_pco2w(:,:)
1391#elif defined key_fabm
1392               pco2_3d(:,:,:) = fabm_get_bulk_diagnostic_data(model, jp_fabm_o3pc)
1393               zsurfvar(:,:) = pco2_3d(:,:,1)
1394#else
1395               CALL ctl_stop( ' Trying to run spco2 observation operator', &
1396                  &           ' but no biogeochemical model appears to have been defined' )
1397#endif
1398
1399            CASE DEFAULT
1400
1401               CALL ctl_stop( 'Unknown surface observation type '//TRIM(cobstypessurf(jtype))//' in dia_obs' )
1402
1403            END SELECT
1404           
1405            IF ( llog10 ) THEN
1406               ! Take the log10 where we can, otherwise exclude
1407               tiny = 1.0e-20
1408               WHERE(zsurfvar(:,:) > tiny .AND. zsurfvar(:,:) /= obfillflt )
1409                  zsurfvar(:,:)  = LOG10(zsurfvar(:,:))
1410               ELSEWHERE
1411                  zsurfvar(:,:)  = obfillflt
1412                  zsurfmask(:,:) = 0
1413               END WHERE
1414            ENDIF
1415
1416            CALL obs_surf_opt( surfdataqc(jtype), kstp, jpi, jpj,       &
1417               &               nit000, idaystp, zsurfvar, zsurfmask,    &
1418               &               n2dintsurf(jtype), llnightav(jtype),     &
1419               &               ravglamscl(jtype), ravgphiscl(jtype),     &
1420               &               lfpindegs(jtype) )
1421
1422         END DO
1423
1424      ENDIF
1425
1426      CALL wrk_dealloc( jpi, jpj, jpk, zprofvar1 )
1427      CALL wrk_dealloc( jpi, jpj, jpk, zprofvar2 )
1428      CALL wrk_dealloc( jpi, jpj, jpk, zprofmask1 )
1429      CALL wrk_dealloc( jpi, jpj, jpk, zprofmask2 )
1430      CALL wrk_dealloc( jpi, jpj, zsurfvar )
1431      CALL wrk_dealloc( jpi, jpj, zsurfmask )
1432      CALL wrk_dealloc( jpi, jpj, zglam1 )
1433      CALL wrk_dealloc( jpi, jpj, zglam2 )
1434      CALL wrk_dealloc( jpi, jpj, zgphi1 )
1435      CALL wrk_dealloc( jpi, jpj, zgphi2 )
1436
1437   END SUBROUTINE dia_obs
1438
1439   SUBROUTINE dia_obs_wri
1440      !!----------------------------------------------------------------------
1441      !!                    ***  ROUTINE dia_obs_wri  ***
1442      !!         
1443      !! ** Purpose : Call observation diagnostic output routines
1444      !!
1445      !! ** Method  : Call observation diagnostic output routines
1446      !!
1447      !! ** Action  :
1448      !!
1449      !! History :
1450      !!        !  06-03  (K. Mogensen) Original code
1451      !!        !  06-05  (K. Mogensen) Reformatted
1452      !!        !  06-10  (A. Weaver) Cleaning
1453      !!        !  07-03  (K. Mogensen) General handling of profiles
1454      !!        !  08-09  (M. Valdivieso) Velocity component (U,V) profiles
1455      !!        !  15-08  (M. Martin) Combined writing for prof and surf types
1456      !!----------------------------------------------------------------------
1457      !! * Modules used
1458      USE obs_rot_vel          ! Rotation of velocities
1459
1460      IMPLICIT NONE
1461
1462      !! * Local declarations
1463      INTEGER :: jtype                    ! Data set loop variable
1464      INTEGER :: jo, jvar, jk
1465      REAL(wp), DIMENSION(:), ALLOCATABLE :: &
1466         & zu, &
1467         & zv
1468
1469      !-----------------------------------------------------------------------
1470      ! Depending on switches call various observation output routines
1471      !-----------------------------------------------------------------------
1472
1473      IF ( nproftypes > 0 ) THEN
1474
1475         DO jtype = 1, nproftypes
1476
1477            IF ( TRIM(cobstypesprof(jtype)) == 'vel' ) THEN
1478
1479               ! For velocity data, rotate the model velocities to N/S, E/W
1480               ! using the compressed data structure.
1481               ALLOCATE( &
1482                  & zu(profdataqc(jtype)%nvprot(1)), &
1483                  & zv(profdataqc(jtype)%nvprot(2))  &
1484                  & )
1485
1486               CALL obs_rotvel( profdataqc(jtype), nn_2dint_default, zu, zv )
1487
1488               DO jo = 1, profdataqc(jtype)%nprof
1489                  DO jvar = 1, 2
1490                     DO jk = profdataqc(jtype)%npvsta(jo,jvar), profdataqc(jtype)%npvend(jo,jvar)
1491
1492                        IF ( jvar == 1 ) THEN
1493                           profdataqc(jtype)%var(jvar)%vmod(jk) = zu(jk)
1494                        ELSE
1495                           profdataqc(jtype)%var(jvar)%vmod(jk) = zv(jk)
1496                        ENDIF
1497
1498                     END DO
1499                  END DO
1500               END DO
1501
1502               DEALLOCATE( zu )
1503               DEALLOCATE( zv )
1504
1505            END IF
1506
1507            CALL obs_prof_decompress( profdataqc(jtype), &
1508               &                      profdata(jtype), .TRUE., numout )
1509
1510            CALL obs_wri_prof( profdata(jtype) )
1511
1512         END DO
1513
1514      ENDIF
1515
1516      IF ( nsurftypes > 0 ) THEN
1517
1518         DO jtype = 1, nsurftypes
1519
1520            CALL obs_surf_decompress( surfdataqc(jtype), &
1521               &                      surfdata(jtype), .TRUE., numout )
1522
1523            CALL obs_wri_surf( surfdata(jtype) )
1524
1525         END DO
1526
1527      ENDIF
1528
1529   END SUBROUTINE dia_obs_wri
1530
1531   SUBROUTINE dia_obs_dealloc
1532      IMPLICIT NONE
1533      !!----------------------------------------------------------------------
1534      !!                    *** ROUTINE dia_obs_dealloc ***
1535      !!
1536      !!  ** Purpose : To deallocate data to enable the obs_oper online loop.
1537      !!               Specifically: dia_obs_init --> dia_obs --> dia_obs_wri
1538      !!
1539      !!  ** Method : Clean up various arrays left behind by the obs_oper.
1540      !!
1541      !!  ** Action :
1542      !!
1543      !!----------------------------------------------------------------------
1544      ! obs_grid deallocation
1545      CALL obs_grid_deallocate
1546
1547      ! diaobs deallocation
1548      IF ( nproftypes > 0 ) &
1549         &   DEALLOCATE( cobstypesprof, profdata, profdataqc, nvarsprof, nextrprof )
1550
1551      IF ( nsurftypes > 0 ) &
1552         &   DEALLOCATE( cobstypessurf, surfdata, surfdataqc, nvarssurf, nextrsurf, &
1553         &               n2dintsurf, ravglamscl, ravgphiscl, lfpindegs, llnightav )
1554
1555   END SUBROUTINE dia_obs_dealloc
1556
1557   SUBROUTINE ini_date( ddobsini )
1558      !!----------------------------------------------------------------------
1559      !!                    ***  ROUTINE ini_date  ***
1560      !!
1561      !! ** Purpose : Get initial date in double precision YYYYMMDD.HHMMSS format
1562      !!
1563      !! ** Method  : Get initial date in double precision YYYYMMDD.HHMMSS format
1564      !!
1565      !! ** Action  : Get initial date in double precision YYYYMMDD.HHMMSS format
1566      !!
1567      !! History :
1568      !!        !  06-03  (K. Mogensen)  Original code
1569      !!        !  06-05  (K. Mogensen)  Reformatted
1570      !!        !  06-10  (A. Weaver) Cleaning
1571      !!        !  06-10  (G. Smith) Calculates initial date the same as method for final date
1572      !!        !  10-05  (D. Lea) Update to month length calculation for NEMO vn3.2
1573      !!----------------------------------------------------------------------
1574      USE phycst, ONLY : &            ! Physical constants
1575         & rday
1576      USE dom_oce, ONLY : &           ! Ocean space and time domain variables
1577         & rdt
1578
1579      IMPLICIT NONE
1580
1581      !! * Arguments
1582      REAL(dp), INTENT(OUT) :: ddobsini  ! Initial date in YYYYMMDD.HHMMSS
1583
1584      !! * Local declarations
1585      INTEGER :: iyea        ! date - (year, month, day, hour, minute)
1586      INTEGER :: imon
1587      INTEGER :: iday
1588      INTEGER :: ihou
1589      INTEGER :: imin
1590      INTEGER :: imday       ! Number of days in month.
1591      INTEGER, DIMENSION(12) :: &
1592         &       imonth_len  ! Length in days of the months of the current year
1593      REAL(wp) :: zdayfrc    ! Fraction of day
1594
1595      !----------------------------------------------------------------------
1596      ! Initial date initialization (year, month, day, hour, minute)
1597      ! (This assumes that the initial date is for 00z))
1598      !----------------------------------------------------------------------
1599      iyea =   ndate0 / 10000
1600      imon = ( ndate0 - iyea * 10000 ) / 100
1601      iday =   ndate0 - iyea * 10000 - imon * 100
1602      ihou = 0
1603      imin = 0
1604
1605      !----------------------------------------------------------------------
1606      ! Compute number of days + number of hours + min since initial time
1607      !----------------------------------------------------------------------
1608      iday = iday + ( nit000 -1 ) * rdt / rday
1609      zdayfrc = ( nit000 -1 ) * rdt / rday
1610      zdayfrc = zdayfrc - aint(zdayfrc)
1611      ihou = int( zdayfrc * 24 )
1612      imin = int( (zdayfrc * 24 - ihou) * 60 )
1613
1614      !-----------------------------------------------------------------------
1615      ! Convert number of days (iday) into a real date
1616      !----------------------------------------------------------------------
1617
1618      CALL calc_month_len( iyea, imonth_len )
1619
1620      DO WHILE ( iday > imonth_len(imon) )
1621         iday = iday - imonth_len(imon)
1622         imon = imon + 1 
1623         IF ( imon > 12 ) THEN
1624            imon = 1
1625            iyea = iyea + 1
1626            CALL calc_month_len( iyea, imonth_len )  ! update month lengths
1627         ENDIF
1628      END DO
1629
1630      !----------------------------------------------------------------------
1631      ! Convert it into YYYYMMDD.HHMMSS format.
1632      !----------------------------------------------------------------------
1633      ddobsini = iyea * 10000_dp + imon * 100_dp + &
1634         &       iday + ihou * 0.01_dp + imin * 0.0001_dp
1635
1636
1637   END SUBROUTINE ini_date
1638
1639   SUBROUTINE fin_date( ddobsfin )
1640      !!----------------------------------------------------------------------
1641      !!                    ***  ROUTINE fin_date  ***
1642      !!
1643      !! ** Purpose : Get final date in double precision YYYYMMDD.HHMMSS format
1644      !!
1645      !! ** Method  : Get final date in double precision YYYYMMDD.HHMMSS format
1646      !!
1647      !! ** Action  : Get final date in double precision YYYYMMDD.HHMMSS format
1648      !!
1649      !! History :
1650      !!        !  06-03  (K. Mogensen)  Original code
1651      !!        !  06-05  (K. Mogensen)  Reformatted
1652      !!        !  06-10  (A. Weaver) Cleaning
1653      !!        !  10-05  (D. Lea) Update to month length calculation for NEMO vn3.2
1654      !!----------------------------------------------------------------------
1655      USE phycst, ONLY : &            ! Physical constants
1656         & rday
1657      USE dom_oce, ONLY : &           ! Ocean space and time domain variables
1658         & rdt
1659
1660      IMPLICIT NONE
1661
1662      !! * Arguments
1663      REAL(dp), INTENT(OUT) :: ddobsfin ! Final date in YYYYMMDD.HHMMSS
1664
1665      !! * Local declarations
1666      INTEGER :: iyea        ! date - (year, month, day, hour, minute)
1667      INTEGER :: imon
1668      INTEGER :: iday
1669      INTEGER :: ihou
1670      INTEGER :: imin
1671      INTEGER :: imday       ! Number of days in month.
1672      INTEGER, DIMENSION(12) :: &
1673         &       imonth_len  ! Length in days of the months of the current year
1674      REAL(wp) :: zdayfrc    ! Fraction of day
1675
1676      !-----------------------------------------------------------------------
1677      ! Initial date initialization (year, month, day, hour, minute)
1678      ! (This assumes that the initial date is for 00z)
1679      !-----------------------------------------------------------------------
1680      iyea =   ndate0 / 10000
1681      imon = ( ndate0 - iyea * 10000 ) / 100
1682      iday =   ndate0 - iyea * 10000 - imon * 100
1683      ihou = 0
1684      imin = 0
1685
1686      !-----------------------------------------------------------------------
1687      ! Compute number of days + number of hours + min since initial time
1688      !-----------------------------------------------------------------------
1689      iday    = iday +  nitend  * rdt / rday
1690      zdayfrc =  nitend  * rdt / rday
1691      zdayfrc = zdayfrc - AINT( zdayfrc )
1692      ihou    = INT( zdayfrc * 24 )
1693      imin    = INT( ( zdayfrc * 24 - ihou ) * 60 )
1694
1695      !-----------------------------------------------------------------------
1696      ! Convert number of days (iday) into a real date
1697      !----------------------------------------------------------------------
1698
1699      CALL calc_month_len( iyea, imonth_len )
1700
1701      DO WHILE ( iday > imonth_len(imon) )
1702         iday = iday - imonth_len(imon)
1703         imon = imon + 1 
1704         IF ( imon > 12 ) THEN
1705            imon = 1
1706            iyea = iyea + 1
1707            CALL calc_month_len( iyea, imonth_len )  ! update month lengths
1708         ENDIF
1709      END DO
1710
1711      !-----------------------------------------------------------------------
1712      ! Convert it into YYYYMMDD.HHMMSS format
1713      !-----------------------------------------------------------------------
1714      ddobsfin = iyea * 10000_dp + imon * 100_dp    + iday &
1715         &     + ihou * 0.01_dp  + imin * 0.0001_dp
1716
1717    END SUBROUTINE fin_date
1718
1719    SUBROUTINE obs_settypefiles( ntypes, jpmaxnfiles, ifiles, cobstypes, cfiles )
1720
1721       INTEGER, INTENT(IN) :: ntypes      ! Total number of obs types
1722       INTEGER, INTENT(IN) :: jpmaxnfiles ! Maximum number of files allowed for each type
1723       INTEGER, DIMENSION(ntypes), INTENT(OUT) :: &
1724          &                   ifiles      ! Out number of files for each type
1725       CHARACTER(len=8), DIMENSION(ntypes), INTENT(IN) :: &
1726          &                   cobstypes   ! List of obs types
1727       CHARACTER(len=128), DIMENSION(ntypes, jpmaxnfiles), INTENT(IN) :: &
1728          &                   cfiles      ! List of files for all types
1729
1730       !Local variables
1731       INTEGER :: jfile
1732       INTEGER :: jtype
1733
1734       DO jtype = 1, ntypes
1735
1736          ifiles(jtype) = 0
1737          DO jfile = 1, jpmaxnfiles
1738             IF ( trim(cfiles(jtype,jfile)) /= '' ) &
1739                       ifiles(jtype) = ifiles(jtype) + 1
1740          END DO
1741
1742          IF ( ifiles(jtype) == 0 ) THEN
1743               CALL ctl_stop( 'Logical for observation type '//TRIM(cobstypes(jtype))//   &
1744                  &           ' set to true but no files available to read' )
1745          ENDIF
1746
1747          IF(lwp) THEN   
1748             WRITE(numout,*) '             '//cobstypes(jtype)//' input observation file names:'
1749             DO jfile = 1, ifiles(jtype)
1750                WRITE(numout,*) '                '//TRIM(cfiles(jtype,jfile))
1751             END DO
1752          ENDIF
1753
1754       END DO
1755
1756    END SUBROUTINE obs_settypefiles
1757
1758    SUBROUTINE obs_setinterpopts( ntypes, jtype, ctypein,             &
1759               &                  n2dint_default, n2dint_type,        &
1760               &                  ravglamscl_type, ravgphiscl_type,   &
1761               &                  lfp_indegs_type, lavnight_type,     &
1762               &                  n2dint, ravglamscl, ravgphiscl,     &
1763               &                  lfpindegs, lavnight )
1764
1765       INTEGER, INTENT(IN)  :: ntypes             ! Total number of obs types
1766       INTEGER, INTENT(IN)  :: jtype              ! Index of the current type of obs
1767       INTEGER, INTENT(IN)  :: n2dint_default     ! Default option for interpolation type
1768       INTEGER, INTENT(IN)  :: n2dint_type        ! Option for interpolation type
1769       REAL(wp), INTENT(IN) :: &
1770          &                    ravglamscl_type, & !E/W diameter of obs footprint for this type
1771          &                    ravgphiscl_type    !N/S diameter of obs footprint for this type
1772       LOGICAL, INTENT(IN)  :: lfp_indegs_type    !T=> footprint in degrees, F=> in metres
1773       LOGICAL, INTENT(IN)  :: lavnight_type      !T=> obs represent night time average
1774       CHARACTER(len=8), INTENT(IN) :: ctypein 
1775
1776       INTEGER, DIMENSION(ntypes), INTENT(INOUT) :: &
1777          &                    n2dint 
1778       REAL(wp), DIMENSION(ntypes), INTENT(INOUT) :: &
1779          &                    ravglamscl, ravgphiscl
1780       LOGICAL, DIMENSION(ntypes), INTENT(INOUT) :: &
1781          &                    lfpindegs, lavnight
1782
1783       lavnight(jtype) = lavnight_type
1784
1785       IF ( (n2dint_type >= 0) .AND. (n2dint_type <= 6) ) THEN
1786          n2dint(jtype) = n2dint_type
1787       ELSE IF ( n2dint_type == -1 ) THEN
1788          n2dint(jtype) = n2dint_default
1789       ELSE
1790          CALL ctl_stop(' Choice of '//TRIM(ctypein)//' horizontal (2D) interpolation method', &
1791            &                    ' is not available')
1792       ENDIF
1793
1794       ! For averaging observation footprints set options for size of footprint
1795       IF ( (n2dint(jtype) > 4) .AND. (n2dint(jtype) <= 6) ) THEN
1796          IF ( ravglamscl_type > 0._wp ) THEN
1797             ravglamscl(jtype) = ravglamscl_type
1798          ELSE
1799             CALL ctl_stop( 'Incorrect value set for averaging footprint '// &
1800                            'scale (ravglamscl) for observation type '//TRIM(ctypein) )     
1801          ENDIF
1802
1803          IF ( ravgphiscl_type > 0._wp ) THEN
1804             ravgphiscl(jtype) = ravgphiscl_type
1805          ELSE
1806             CALL ctl_stop( 'Incorrect value set for averaging footprint '// &
1807                            'scale (ravgphiscl) for observation type '//TRIM(ctypein) )     
1808          ENDIF
1809
1810          lfpindegs(jtype) = lfp_indegs_type 
1811
1812       ENDIF
1813
1814       ! Write out info
1815       IF(lwp) THEN
1816          IF ( n2dint(jtype) <= 4 ) THEN
1817             WRITE(numout,*) '             '//TRIM(ctypein)// &
1818                &            ' model counterparts will be interpolated horizontally'
1819          ELSE IF ( n2dint(jtype) <= 6 ) THEN
1820             WRITE(numout,*) '             '//TRIM(ctypein)// &
1821                &            ' model counterparts will be averaged horizontally'
1822             WRITE(numout,*) '             '//'    with E/W scale: ',ravglamscl(jtype)
1823             WRITE(numout,*) '             '//'    with N/S scale: ',ravgphiscl(jtype)
1824             IF ( lfpindegs(jtype) ) THEN
1825                 WRITE(numout,*) '             '//'    (in degrees)'
1826             ELSE
1827                 WRITE(numout,*) '             '//'    (in metres)'
1828             ENDIF
1829          ENDIF
1830       ENDIF
1831
1832    END SUBROUTINE obs_setinterpopts
1833
1834END MODULE diaobs
Note: See TracBrowser for help on using the repository browser.