SURFEX  V8_0
Surfex V8_0 release
 All Classes Files Functions Variables
soda.F90
Go to the documentation of this file.
1 !SFX_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
2 !SFX_LIC This is part of the SURFEX software governed by the CeCILL-C licence
3 !SFX_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4 !SFX_LIC for details. version 1.
5 ! *****************************************************************************************
6 PROGRAM soda
7 
8 #ifdef USE_SODA
9 !
10 ! ------------------------------------------------------------------------------------------
11 !!
12 !! SODA: SURFEX Offline Data Assimilation
13 !!
14 !! PURPOSE
15 !! -------
16 !! Program to perform surface data assimilation within SURFEX
17 !!
18 !!
19 !! METHOD
20 !! ------
21 !! Different methods for different tiles
22 !!
23 !! EXTERNAL
24 !! --------
25 !!
26 !! IMPLICIT ARGUMENTS
27 !! ------------------
28 !!
29 !! REFERENCE
30 !! ---------
31 !!
32 !! AUTHOR
33 !! ------
34 !!
35 !! T. Aspelien met.no
36 !!
37 !! MODIFICATION
38 !! ------------
39 !!
40 !! Original 04/2012
41 !!
42 !! 03/2014 E. Martin change indices names in OMP module according to GMAP changes
43 ! 05/2013 B. Decharme New coupling variables XTSURF (for AGCM)
44 ! 02/2016 B. Decharme MODD_IO_SURF_ARO not used
45 !----------------------------------------------------------------------------
46 !
49 !
50 USE modd_surfex_mpi, ONLY : nrank, npio, wlog_mpi, prep_log_mpi, nproc, ncomm, &
51  nindex, nsize_task, end_log_mpi, nsize
52 USE modd_surfex_omp, ONLY : nindx2sfx, nwork, xwork, xwork2, xwork3, &
53  nwork_full, xwork_full, xwork2_full, nwork2, &
54  nwork2_full
55 !
56 USE modd_mask, ONLY: nmask_full
57 !
59 !
60 USE modd_surf_conf, ONLY : cprogname, csoftware
61 USE modd_surf_par, ONLY : xundef,nundef
62 !
63 USE modd_assim, ONLY : lassim, larome, laladsurf, cassim_isba, nvar, xf, xf_patch, &
64  nobstype, xat2m_isba, xahu2m_isba, cvar, cobs, nechgu, xi, &
65  xlai_pass, xbio_pass, cbio, nivar, xyo, nific, nprintlev, &
66  nobs, nprintlev, lread_all, nens,lbias_correction, &
67  lextrap_sea,lextrap_water,lextrap_nature,lobsheader,nobsmax, &
68  cfile_format_fg,cfile_format_lsm,cfile_format_obs,cfile_format_clim,&
69  nnco,lwatertg2,lobsnat
70 !
71 USE modd_forc_atm, ONLY : csv, xdir_alb, xsca_alb, xemis, xtsrad, xtsun, xzs, &
72  xzref, xuref, xta, xqa, xsv, xu, xv, xsw_bands, &
73  xzenith, xazim, xco2, xrhoa, xtsurf
74 !
75 USE modd_write_bin, ONLY : nwrite
76 !
77 #ifdef SFX_OL
78 USE modd_io_surf_ol, ONLY : xstart, xcount, xstride, xstartw, xcountw, ltime_written, &
79  ldefined_nature, ldefined_sea, ldefined_water, &
80  ldefined_town, ldefined_surf_atm, lpartw
81 
82 #endif
83 !
84 #ifdef SFX_NC
85 USE modd_io_surf_nc, ONLY : cfilein_nc, cfilein_nc_save, cfilepgd_nc, cfileout_nc, ldef, &
86  cluout_nc
87 #endif
88 #ifdef SFX_ASC
89 USE modd_io_surf_asc, ONLY : cfilein, cfilein_save, cfilepgd, cfileout, lcreated
90 #endif
91 #ifdef SFX_FA
92 USE modd_io_surf_fa, ONLY : cfilein_fa, cfilein_fa_save, cfilepgd_fa, cdnomc, cfileout_fa, &
93  nunit_fa, iverbfa, lfanocompact
94 #endif
95 #ifdef SFX_LFI
96 USE modd_io_surf_lfi, ONLY : cfilein_lfi, cfilein_lfi_save, &
97  cfilepgd_lfi, cfile_lfi, cluout_lfi, cfileout_lfi
98 #endif
99 !
100 USE modn_io_offline, ONLY : nam_io_offline, cnamelist, cpgdfile, cprepfile, csurffile, &
101  csurf_filetype, ctimeseries_filetype, lland_use, yalg_mpi, &
102  ldiag_fa_nocompact, lout_timename, xio_frac, lrestart_2m
103 !
104 USE mode_pos_surf, ONLY : posnam
105 !
106 USE modi_dealloc_surf_atm_n
107 USE modi_init_output_ol_n
108 USE modi_set_surfex_filein
109 USE modi_init_index_mpi
112 USE modi_abor1_sfx
113 USE modi_get_luout
114 USE modi_open_namelist
115 USE modi_close_namelist
116 USE modi_read_all_namelists
117 USE modi_init_io_surf_n
118 USE modi_end_io_surf_n
119 USE modi_read_surf
120 USE modi_io_buff_clean
121 USE modi_get_size_full_n
122 USE modi_init_surf_atm_n
123 USE modi_assim_surf_atm_n
124 USE modi_write_surf_atm_n
125 USE modi_write_diag_surf_atm_n
126 USE modi_assim_set_sst
127 USE modi_add_forecast_to_date_surf
128 USE modi_flag_update
129 USE modi_flag_diag_update
131 USE modi_init_output_nc_n
132 USE modi_close_fileout_ol
133 !
134 USE mode_ekf, ONLY : get_file_name, set_filein
135 !
136 USE yomhook, ONLY : lhook,dr_hook
137 USE parkind1, ONLY : jprb
138 !
139 IMPLICIT NONE
140 !
141 #ifdef SFX_MPI
142 include 'mpif.h'
143 #endif
144 !
145 !* 0. Declaration of local variables
146 ! ------------------------------
147 !
148  CHARACTER(LEN=200) :: ymfile ! Name of the observation, perturbed or reference file!
149  CHARACTER(LEN=3) :: yinit
150  CHARACTER(LEN=2), PARAMETER :: ytest = 'OK' ! must be equal to 'OK'
151  CHARACTER(LEN=28) :: yatmfile =' ' ! name of the Atmospheric file
152  CHARACTER(LEN=6) :: yatmfiletype =' ' ! type of the Atmospheric file
153  CHARACTER(LEN=28) :: yluout ='LISTING_SODA ' ! name of listing
154  CHARACTER(LEN=6) :: yprogram2 = 'FA '
155  CHARACTER(LEN=28) :: yfilein
156  CHARACTER(LEN=3) :: yvar
157 !
158  CHARACTER(LEN=100) :: yname
159  CHARACTER(LEN=10) :: yrank
160  CHARACTER(LEN=3) :: yens
161  CHARACTER(LEN=10),DIMENSION(:), ALLOCATABLE :: cobsinfile ! Identifier for simulated observations in file
162 !
163 REAL, ALLOCATABLE, DIMENSION(:,:) :: zyo_nat
164 REAL, ALLOCATABLE, DIMENSION(:) :: znature
165 !
166 REAL,ALLOCATABLE, DIMENSION(:,:) :: zwork
167 REAL,ALLOCATABLE, DIMENSION(:) :: zlsm ! Land-Sea mask
168 REAL,ALLOCATABLE, DIMENSION(:) :: zcon_rain ! Amount of convective liquid precipitation
169 REAL,ALLOCATABLE, DIMENSION(:) :: zstrat_rain ! Amount of stratiform liquid precipitation
170 REAL,ALLOCATABLE, DIMENSION(:) :: zcon_snow ! Amount of convective solid precipitation
171 REAL,ALLOCATABLE, DIMENSION(:) :: zstrat_snow ! Amount of stratiform solid precipitation
172 REAL,ALLOCATABLE, DIMENSION(:) :: zclouds ! Cloudcover
173 REAL,ALLOCATABLE, DIMENSION(:) :: zevaptr ! Evaporation
174 REAL,ALLOCATABLE, DIMENSION(:) :: zevap ! Evaporation
175 REAL,ALLOCATABLE, DIMENSION(:) :: ztsc ! Climatological surface temperature
176 REAL,ALLOCATABLE, DIMENSION(:) :: zts ! Surface temperature
177 REAL,ALLOCATABLE, DIMENSION(:) :: zt2m ! Screen level temperature
178 REAL,ALLOCATABLE, DIMENSION(:) :: zhu2m ! Screen level relative humidity
179 REAL,ALLOCATABLE, DIMENSION(:) :: zsnc
180 REAL,ALLOCATABLE, DIMENSION(:) :: zswe ! Snow water equvivalent (amount of snow on the ground)
181 REAL,ALLOCATABLE, DIMENSION(:) :: zswec ! Climatological snow water equvivalent (amount of snow on the ground)
182 REAL,ALLOCATABLE, DIMENSION(:) :: zucls
183 REAL,ALLOCATABLE, DIMENSION(:) :: zvcls
184 REAL,ALLOCATABLE, DIMENSION(:) :: zsst ! SST from external file
185 REAL,ALLOCATABLE, DIMENSION(:) :: zsic ! SIC from external file
186 REAL,ALLOCATABLE, DIMENSION(:) :: zlat
187 REAL,ALLOCATABLE, DIMENSION(:) :: zlon
188 !
189 REAL :: ztime
190 REAL :: ztime_out ! output time since start of the run (s)
191 !
192 LOGICAL, ALLOCATABLE, DIMENSION(:) :: gd_maskext
193 LOGICAL :: glkeepextzone
194 LOGICAL :: gfound
195 !
196 TYPE (date_time) :: ttime ! Current date and time
197 !
198  CHARACTER(LEN=14) :: ytag
199 !
200 INTEGER, DIMENSION(11) :: idatef
201 INTEGER :: idim_full
202 INTEGER :: isv ! Number of scalar species
203 INTEGER :: isw ! Number of radiative bands
204 INTEGER :: iyear, imonth, iday, ihour
205 INTEGER :: iyear_out, imonth_out, iday_out
206 INTEGER :: jl,ji,jj,inb,icpt
207 INTEGER :: inw, jnw
208 INTEGER :: istep
209 INTEGER :: iobs
210 INTEGER :: igpcomp
211 INTEGER :: iluout
212 INTEGER :: ilunam
213 INTEGER :: iret, inbfa
214 INTEGER :: iresp, istat ! Response value
215 INTEGER :: infompi, ilevel
216 INTEGER :: isize, iens, isize_full
217 !
218 ! Flag diag :
219 !
220 INTEGER :: i2m, ibeq, idsteq
221 LOGICAL :: gfrac, gdiag_grid, gsurf_budget, grad_budget, gcoef, &
222  gsurf_vars, gdiag_ocean, gdiag_seaice, gwater_profile, &
223  gsurf_evap_budget, gflood, gpgd_isba, gch_no_flux_isba,&
224  gsurf_misc_budget_isba, gpgd_teb, gsurf_misc_budget_teb
225 !
226 REAL(KIND=JPRB) :: zhook_handle
227 ! ******************************************************************************************
228 !
229 infompi=1
230 !
231 #ifdef SFX_MPI
232  CALL mpi_init_thread(mpi_thread_multiple,ilevel,infompi)
233 #endif
234 !
235 IF (lhook) CALL dr_hook('SODA',0,zhook_handle)
236 !
237  csoftware = 'SODA'
238 !
239 #ifdef SFX_MPI
240 ncomm = mpi_comm_world
241  CALL mpi_comm_size(ncomm,nproc,infompi)
242  CALL mpi_comm_rank(ncomm,nrank,infompi)
243 #endif
244 !
245  CALL prep_log_mpi
246 !
247 !--------------------------------------
248 !
249 IF (nrank==npio) THEN
250  WRITE(*,*)
251  WRITE(*,*) ' ------------------------------------'
252  WRITE(*,*) ' | SODA |'
253  WRITE(*,*) ' | SURFEX OFFLINE DATA ASSIMILATION |'
254  WRITE(*,*) ' ------------------------------------'
255  WRITE(*,*)
256 ENDIF
257 !
258 WRITE(yrank,fmt='(I10)') nrank
259 yname=trim(yluout)//adjustl(yrank)
260 !
261 ! Open ascii outputfile for writing
262 #ifdef SFX_LFI
263  cluout_lfi = adjustl(adjustr(yluout)//'.txt')
264 #endif
265 #ifdef SFX_NC
266  cluout_nc = adjustl(adjustr(yluout)//'.txt')
267 #endif
268  CALL get_luout('ASCII ',iluout)
269 OPEN(unit=iluout,file=adjustl(adjustr(yname)//'.txt'),form='FORMATTED',action='WRITE')
270 !
271 ! Read offline specific things
272  CALL open_namelist('ASCII ',ilunam,cnamelist)
273  CALL posnam(ilunam,'NAM_IO_OFFLINE',gfound)
274 IF (gfound) READ (unit=ilunam,nml=nam_io_offline)
275  CALL close_namelist('ASCII ',ilunam)
276 !
277 IF (nproc==1) THEN
278  xio_frac=1.
279 ELSE
280  xio_frac = max(min(xio_frac,1.),0.)
281 ENDIF
282 !
283 ! Check validity of NAM_IO_OFFLINE settings
284  CALL test_nam_var_surf(iluout,'CSURF_FILETYPE',csurf_filetype,'ASCII ','LFI ','FA ','NC ')
285  CALL test_nam_var_surf(iluout,'CTIMESERIES_FILETYPE',ctimeseries_filetype,'NETCDF','TEXTE ','BINARY',&
286  'ASCII ','LFI ','FA ',&
287  'NONE ','OFFLIN','NC ')
288 !
289 IF (ctimeseries_filetype=='NETCDF') ctimeseries_filetype='OFFLIN'
290 !
291 ! Setting input files read from namelist
292 IF ( csurf_filetype == "LFI " ) THEN
293 #ifdef SFX_LFI
294  cfilein_lfi = cprepfile
295  cfile_lfi = cprepfile
296  cfilein_lfi_save = cprepfile
297  cfilepgd_lfi = cpgdfile
298 #endif
299 ELSEIF ( csurf_filetype == "FA " ) THEN
300 #ifdef SFX_FA
301  cfilein_fa = adjustl(adjustr(cprepfile)//'.fa')
302  cfilein_fa_save = adjustl(adjustr(cprepfile)//'.fa')
303  cfilepgd_fa = adjustl(adjustr(cpgdfile)//'.fa')
304 #endif
305 ELSEIF ( csurf_filetype == "ASCII " ) THEN
306 #ifdef SFX_ASC
307  cfilein = adjustl(adjustr(cprepfile)//'.txt')
308  cfilein_save = adjustl(adjustr(cprepfile)//'.txt')
309  cfilepgd = adjustl(adjustr(cpgdfile)//'.txt')
310 #endif
311 ELSEIF ( csurf_filetype == "NC " ) THEN
312 #ifdef SFX_NC
313  cfilein_nc = adjustl(adjustr(cprepfile)//'.nc')
314  cfilein_nc_save = adjustl(adjustr(cprepfile)//'.nc')
315  cfilepgd_nc = adjustl(adjustr(cpgdfile)//'.nc')
316 #endif
317 ELSE
318  CALL abor1_sfx(trim(csurf_filetype)//" is not implemented!")
319 ENDIF
320 !
321 ! Allocation of Surfex Types
322  CALL surfex_alloc_list(1)
323  ysurf_cur => ysurf_list(1)
324 !
325 ! Reading all namelist (also assimilation)
326  CALL read_all_namelists(ysurf_cur, &
327  csurf_filetype,'ALL',.false.)
328 !
329 !* 0.2. Goto model of Surfex Types
330 !
331 icurrent_model = 1
332 !
333  cprogname = csurf_filetype
334 lread_all = .true.
335 !
336 ! Initialization netcdf file handling
337 IF (nrank==npio) THEN
338  !
339  xstart = nundef
340  xstride = nundef
341  xcount = nundef
342  xstartw = 0
343  xcountw = 1
344  lpartw = .true.
345  ldefined_surf_atm = .false.
346  ldefined_nature = .false.
347  ldefined_town = .false.
348  ldefined_water = .false.
349  ldefined_sea = .false.
350  !
351 ENDIF
352 !
353  CALL init_index_mpi(ysurf_cur, &
354  csurf_filetype, yalg_mpi, xio_frac, .false.)
355 !
356 !
357 ! Initialize time information
358 iyear = nundef
359 imonth = nundef
360 iday = nundef
361 ztime = xundef
362  CALL set_surfex_filein(csurf_filetype,'PREP')
363  CALL init_io_surf_n(ysurf_cur%DTCO, ysurf_cur%DGU, ysurf_cur%U, &
364  csurf_filetype,'FULL ','SURF ','READ ')
365  CALL read_surf(&
366  csurf_filetype,'DIM_FULL ',idim_full, iresp)
367  CALL read_surf(&
368  csurf_filetype,'DTCUR ',ttime, iresp)
369  CALL end_io_surf_n(csurf_filetype)
370 !
371  CALL get_size_full_n(ysurf_cur%U,csurf_filetype,idim_full,isize_full)
372 !
373 IF (ALLOCATED(nmask_full)) DEALLOCATE(nmask_full)
374 !
375 isw = 0
376 isv = 0
377 ALLOCATE(csv(isv))
378 ALLOCATE(xco2(isize_full))
379 ALLOCATE(xrhoa(isize_full))
380 ALLOCATE(xzenith(isize_full))
381 ALLOCATE(xazim(isize_full))
382 ALLOCATE(xsw_bands(isw))
383 ALLOCATE(xdir_alb(isize_full,isw))
384 ALLOCATE(xsca_alb(isize_full,isw))
385 ALLOCATE(xemis(isize_full))
386 ALLOCATE(xtsrad(isize_full))
387 ALLOCATE(xtsurf(isize_full))
388 !
389 ! Indicate that zenith and azimuth angles are not initialized
390 xzenith = xundef
391 xazim = xundef
392 xco2 = 0.
393 xrhoa = 1.
394 !
395 ! Sanity check
396 IF ( .NOT. lassim ) CALL abor1_sfx("YOU CAN'T RUN SODA WITHOUT SETTING LASSIM=.TRUE. IN THE ASSIM NAMELIST")
397 !
398 ! Set the number of initializations to be done
399 ! Default is one
400 inb = 1
401 IF ( cassim_isba == 'EKF ' ) THEN
402  ! Has to do initialization for all the perturbations +
403  ! control + the real run at last
404  inb = nvar + 2
405  isize = nvar
406 ELSEIF ( cassim_isba == 'ENKF ' ) THEN
407  inb = nens
408  IF (lbias_correction) inb = inb + 1
409  isize = nens
410 ENDIF
411 !
412 IF (nrank==npio) WRITE(*,*) "INITIALIZING SURFEX..."
413 !
414 yinit = 'ALL'
415 !
416 iyear = ttime%TDATE%YEAR
417 imonth = ttime%TDATE%MONTH
418 iday = ttime%TDATE%DAY
419 ztime = ttime%TIME
420 ihour = nint(ztime/3600.)
421 !
422 nobs = 0
423 !
424 lread_all = .false.
425 !
426 DO nific = inb,1,-1
427  !
428  ! If we have more than one initialization to do
429  ! For last initialization, we must re-do the first.
430  ! Could be avoided by introducing knowlegde of LASSIM on this level
431  IF ( cassim_isba == 'EKF ' .OR. cassim_isba == 'ENKF ' ) THEN
432  !
433  IF (cassim_isba == 'EKF ') THEN
434  IF ( nific<inb ) THEN
435  ymfile = "PREP_"
436  CALL get_file_name(iyear,imonth,iday,ihour,ymfile)
437  WRITE(yvar,'(I1.1)') nific-1
438  yfilein = trim(ymfile)//"_EKF_PERT"//adjustl(yvar)
439  ELSE
440  yfilein = "PREP_INIT"
441  ENDIF
442  ELSEIF (cassim_isba == 'ENKF ') THEN
443  ymfile = "PREP_"
444  CALL get_file_name(iyear,imonth,iday,ihour,ymfile)
445  WRITE(yvar,'(I1.1)') nific
446  yfilein = trim(ymfile)//"_EKF_ENS"//adjustl(yvar)
447  ENDIF
448  !
449  CALL set_filein(yfilein)
450  !
451  ENDIF
452  !
453  CALL dealloc_surf_atm_n(ysurf_cur)
454  !
455  IF ( (cassim_isba=='EKF '.OR.cassim_isba=='ENKF ') .AND. nific==1 ) lread_all = .true.
456  !
457  ! Initialize the SURFEX interface
458  CALL io_buff_clean
459  CALL init_surf_atm_n(ysurf_cur, &
460  csurf_filetype,yinit, lland_use, isize_full, isv, isw, &
461  csv, xco2, xrhoa, xzenith, xazim, xsw_bands, &
462  xdir_alb, xsca_alb, xemis, xtsrad, xtsurf, &
463  iyear, imonth, iday, ztime, &
464  yatmfile, yatmfiletype, ytest )
465  !
466  IF ( cassim_isba=='EKF ' .OR. cassim_isba=='ENKF ' ) THEN
467  !
468  IF ( nific==inb ) THEN
469  ALLOCATE(xlai_pass(ysurf_cur%U%NSIZE_NATURE,ysurf_cur%IM%I%NPATCH))
470  ALLOCATE(xbio_pass(ysurf_cur%U%NSIZE_NATURE,ysurf_cur%IM%I%NPATCH))
471  IF (cassim_isba=='EKF ') ALLOCATE(xi(ysurf_cur%U%NSIZE_NATURE,ysurf_cur%IM%I%NPATCH,isize))
472  ALLOCATE(xf(ysurf_cur%U%NSIZE_NATURE,ysurf_cur%IM%I%NPATCH,isize+1,nvar))
473  ALLOCATE(xf_patch(ysurf_cur%U%NSIZE_NATURE,ysurf_cur%IM%I%NPATCH,isize+1,nobstype))
474  ENDIF
475  !
476  IF ( cassim_isba=='EKF ' .AND. nific<inb .OR. cassim_isba=='ENKF ') THEN
477  !
478  ! Set the global state values for this control value
479  DO iobs = 1,nobstype
480  DO ji=1,ysurf_cur%IM%I%NPATCH
481  SELECT CASE (trim(cobs(iobs)))
482  CASE("T2M")
483  xf_patch(:,ji,nific,iobs) = xat2m_isba(:,1)
484  CASE("HU2M")
485  xf_patch(:,ji,nific,iobs) = xahu2m_isba(:,1)
486  CASE("WG1")
487  xf_patch(:,ji,nific,iobs) = ysurf_cur%IM%I%XWG(:,1,ji)
488  CASE("LAI")
489  xf_patch(:,ji,nific,iobs) = ysurf_cur%IM%I%XLAI(:,ji)
490  CASE default
491  CALL abor1_sfx("Mapping of "//cobs(iobs)//" is not defined in SODA!")
492  END SELECT
493  ENDDO
494  ENDDO
495  !
496  ! Prognostic fields for assimilation (Control vector)
497  DO jl = 1,nvar
498  SELECT CASE (trim(cvar(jl)))
499  CASE("TG1")
500  xf(:,:,nific,jl) = ysurf_cur%IM%I%XTG(:,1,:)
501  CASE("TG2")
502  xf(:,:,nific,jl) = ysurf_cur%IM%I%XTG(:,2,:)
503  CASE("WG1")
504  xf(:,:,nific,jl) = ysurf_cur%IM%I%XWG(:,1,:)
505  CASE("WG2")
506  xf(:,:,nific,jl) = ysurf_cur%IM%I%XWG(:,2,:)
507  CASE("WG3")
508  xf(:,:,nific,jl) = ysurf_cur%IM%I%XWG(:,3,:)
509  CASE("LAI")
510  xf(:,:,nific,jl) = ysurf_cur%IM%I%XLAI(:,:)
511  CASE default
512  CALL abor1_sfx("Mapping of "//trim(cvar(jl))//" is not defined in SODA!")
513  END SELECT
514  ENDDO
515  !
516  IF ( nific==1 ) THEN
517  !
518  DO jl = 1,nvar
519  IF (trim(cvar(jl))=="LAI") THEN
520  IF ( ysurf_cur%IM%I%NPATCH==1 .AND. trim(cbio)/="LAI" ) THEN
521  CALL abor1_sfx("Mapping of "//cbio//" is not defined in EKF with NPATCH=1!")
522  ENDIF
523  SELECT CASE (trim(cbio))
524  CASE("BIOMA1","BIOMASS1")
525  xbio_pass(:,:) = ysurf_cur%IM%I%XBIOMASS(:,1,:)
526  CASE("BIOMA2","BIOMASS2")
527  xbio_pass(:,:) = ysurf_cur%IM%I%XBIOMASS(:,2,:)
528  CASE("RESPI1","RESP_BIOM1")
529  xbio_pass(:,:) = ysurf_cur%IM%I%XRESP_BIOMASS(:,1,:)
530  CASE("RESPI2","RESP_BIOM2")
531  xbio_pass(:,:) = ysurf_cur%IM%I%XRESP_BIOMASS(:,2,:)
532  CASE("LAI")
533  xbio_pass(:,:) = ysurf_cur%IM%I%XLAI(:,:)
534  CASE default
535  CALL abor1_sfx("Mapping of "//cbio//" is not defined in EKF!")
536  END SELECT
537  !
538  xlai_pass(:,:) = ysurf_cur%IM%I%XLAI(:,:)
539  !
540  ENDIF
541  !
542  ENDDO
543  ENDIF
544  !
545  ELSE
546  !
547  DO jl = 1,nvar
548  SELECT CASE (trim(cvar(jl)))
549  CASE("TG1")
550  xi(:,:,jl) = ysurf_cur%IM%I%XTG(:,1,:)
551  CASE("TG2")
552  xi(:,:,jl) = ysurf_cur%IM%I%XTG(:,2,:)
553  CASE("WG1")
554  xi(:,:,jl) = ysurf_cur%IM%I%XWG(:,1,:)
555  CASE("WG2")
556  xi(:,:,jl) = ysurf_cur%IM%I%XWG(:,2,:)
557  CASE("WG3")
558  xi(:,:,jl) = ysurf_cur%IM%I%XWG(:,3,:)
559  CASE("LAI")
560  xi(:,:,jl) = ysurf_cur%IM%I%XLAI(:,:)
561  CASE default
562  CALL abor1_sfx("Mapping of "//trim(cvar(jl))//" is not defined in SODA!")
563  END SELECT
564  ENDDO
565  !
566  ENDIF
567  !
568  ENDIF
569  !
570 ENDDO
571 !
572 ! Allocate input fields to the assimilation interface
573 ALLOCATE(zlsm(isize_full))
574 ALLOCATE(zcon_rain(isize_full))
575 ALLOCATE(zstrat_rain(isize_full))
576 ALLOCATE(zcon_snow(isize_full))
577 ALLOCATE(zstrat_snow(isize_full))
578 ALLOCATE(zclouds(isize_full))
579 ALLOCATE(zevaptr(isize_full))
580 ALLOCATE(zevap(isize_full))
581 ALLOCATE(ztsc(isize_full))
582 ALLOCATE(zswec(isize_full))
583 ALLOCATE(zts(isize_full))
584 ALLOCATE(zucls(isize_full))
585 ALLOCATE(zvcls(isize_full))
586 ALLOCATE(zsst(isize_full))
587 ALLOCATE(zsic(isize_full))
588 zts(:) = xundef
589 
590 ! Allocate observations
591 ALLOCATE(zt2m(isize_full))
592 ALLOCATE(zhu2m(isize_full))
593 ALLOCATE(zswe(isize_full))
594 !
595 ! OI needs first guess values used in oi_cacsts
596 IF (cassim_isba=="OI ") THEN
597  !
598  IF ( trim(cfile_format_fg) == "ASCII" ) THEN
599 
600  ALLOCATE(zwork(ysurf_cur%U%NDIM_FULL,8))
601 
602  IF (nrank==npio) THEN
603 
604  ymfile = 'FIRST_GUESS_'
605  CALL get_file_name(iyear,imonth,iday,ihour,ymfile)
606  WRITE(*,*) "READING first guess from file "//trim(ymfile)//".DAT"
607  istat = 0
608  OPEN(unit=55,file=trim(ymfile)//".DAT",form='FORMATTED',status='OLD',iostat=istat)
609  IF ( istat /= 0 ) CALL abor1_sfx("Can not open "//trim(ymfile))
610 
611  zwork(:,:) = xundef
612 
613  ! Read first guess
614  DO ji = 1,ysurf_cur%U%NDIM_FULL
615  READ (55,*,iostat=istat) (zwork(ji,jj),jj=1,8)
616  IF ( istat /= 0 ) CALL abor1_sfx("Error reading file "//trim(ymfile))
617  ENDDO
618  CLOSE(55)
619  ENDIF
620 
621  ! Distribute values on processors
622  IF (nproc>1) THEN
623  CALL read_and_send_mpi(zwork(:,1),zcon_rain(:))
624  CALL read_and_send_mpi(zwork(:,2),zstrat_snow(:))
625  CALL read_and_send_mpi(zwork(:,3),zcon_snow(:))
626  CALL read_and_send_mpi(zwork(:,4),zstrat_snow(:))
627  CALL read_and_send_mpi(zwork(:,5),zclouds(:))
628  CALL read_and_send_mpi(zwork(:,6),zlsm(:))
629  CALL read_and_send_mpi(zwork(:,7),zevap(:))
630  CALL read_and_send_mpi(zwork(:,8),zevaptr(:))
631  ELSE
632  ! Set First-Guess variables
633  zcon_rain(:) = zwork(:,1)
634  zstrat_snow(:) = zwork(:,2)
635  zcon_snow(:) = zwork(:,3)
636  zstrat_snow(:) = zwork(:,4)
637  zclouds(:) = zwork(:,5)
638  zlsm(:) = zwork(:,6)
639  zevap(:) = zwork(:,7)
640  zevaptr(:) = zwork(:,8)
641  ENDIF
642 
643  DEALLOCATE(zwork)
644 
645  ELSEIF ( trim(cfile_format_fg) == "FA" ) THEN
646  !
647  ! Read atmospheric forecast fields from FA files
648 #ifdef SFX_FA
649  cfilein_fa = 'FG_OI_MAIN'
650  cdnomc = 'oimain' ! new frame name
651 
652  ! Open FA file (LAM version with extension zone)
653  CALL init_io_surf_n(ysurf_cur%DTCO, ysurf_cur%DGU, ysurf_cur%U, &
654  yprogram2,'EXTZON','SURF ','READ ')
655  !
656  ! Read model forecast quantities
657  IF (larome) THEN
658  CALL read_surf(yprogram2,'SURFACCPLUIE', zstrat_rain,iresp)
659  CALL read_surf(yprogram2,'SURFACCNEIGE', zstrat_snow,iresp)
660  CALL read_surf(yprogram2,'SURFACCGRAUPEL',zcon_snow,iresp)
661  ! So far graupel has not been used
662  !ZCON_SNOW=ZCON_SNOW+ZCON_GRAUPEL
663  zcon_rain(:) = 0.0
664  ELSE
665  CALL read_surf(yprogram2,'SURFPREC.EAU.CON',zcon_rain ,iresp)
666  CALL read_surf(yprogram2,'SURFPREC.EAU.GEC',zstrat_rain ,iresp)
667  CALL read_surf(yprogram2,'SURFPREC.NEI.CON',zcon_snow ,iresp)
668  CALL read_surf(yprogram2,'SURFPREC.NEI.GEC',zstrat_snow ,iresp)
669  ENDIF
670  !
671  CALL read_surf(yprogram2,'ATMONEBUL.BASSE ',zclouds,iresp)
672  CALL read_surf(yprogram2,'SURFIND.TERREMER',zlsm ,iresp)
673  CALL read_surf(yprogram2,'SURFFLU.LAT.MEVA',zevap ,iresp) ! accumulated fluxes (not available in LFI)
674  !
675  IF (.NOT.laladsurf) THEN
676  CALL read_surf(yprogram2,'SURFXEVAPOTRANSP',zevaptr,iresp) ! not in ALADIN SURFEX
677  ELSE
678  zevaptr(:) = 0.0
679  ENDIF
680  !
681  ! Close FA file
682  CALL end_io_surf_n(yprogram2)
683  CALL io_buff_clean
684 #else
685  CALL abor1_sfx("The first guess is supposed to be an FA file. You must compile with FA support enabled: -DSFX_FA")
686 #endif
687  ELSE
688  CALL abor1_sfx("CFILE_FORMAT_FG="//trim(cfile_format_fg)//" not implemented!")
689  ENDIF
690  IF (nrank==npio .AND. nprintlev>0) WRITE(*,*)'READ FIRST GUESS OK'
691 ENDIF
692 !
693 ! If we want to extrapolate values, we need to have a land-sea-mask available
694 IF (lextrap_sea .OR. lextrap_water .OR. lextrap_nature .OR. .NOT.lwatertg2) THEN
695 
696  IF (nrank==npio .AND. nprintlev>0) WRITE(*,*) "READING Land-Sea mask"
697 
698  IF ( trim(cfile_format_lsm) == "ASCII" ) THEN
699 
700  ALLOCATE(zwork(ysurf_cur%U%NDIM_FULL,1))
701 
702  IF (nrank==npio) THEN
703  ymfile = 'LSM.DAT'
704  IF (nprintlev>0) WRITE(*,*) "READING LSM from file "//trim(ymfile)
705  OPEN(unit=55,file=trim(ymfile),form='FORMATTED',status='OLD',iostat=istat)
706  IF ( istat /= 0 ) CALL abor1_sfx("Can not open "//trim(ymfile))
707 
708  zwork(:,:) = xundef
709  ! Read LSM
710  DO ji = 1,ysurf_cur%U%NDIM_FULL
711  READ (55,*,iostat=istat) zwork(ji,1)
712  IF ( istat /= 0 ) CALL abor1_sfx("Error reading file "//trim(ymfile))
713  ENDDO
714  CLOSE(55)
715  ENDIF
716 
717  ! Distribute values on processors
718  IF (nproc>1) THEN
719  CALL read_and_send_mpi(zwork(:,1),zlsm(:))
720  ELSE
721  ! Set First-Guess variables
722  zlsm(:)=zwork(:,1)
723  ENDIF
724  DEALLOCATE(zwork)
725 
726  ELSEIF ( trim(cfile_format_lsm) == "FA" ) THEN
727  ! Read atmospheric forecast fields from FA files
728 #ifdef SFX_FA
729  cfilein_fa = 'FG_OI_MAIN'
730  cdnomc = 'oimain' ! new frame name
731 
732  ! Open FA file (LAM version with extension zone)
733  CALL init_io_surf_n(ysurf_cur%DTCO, ysurf_cur%DGU, ysurf_cur%U, &
734  yprogram2,'EXTZON','SURF ','READ ')
735  CALL read_surf(yprogram2,'SURFIND.TERREMER',zlsm ,iresp)
736  IF (iresp /=0) CALL abor1_sfx("Can not read Land-Sea mask from FA file "//trim(cfilein_fa))
737  ! Close FA file
738  CALL end_io_surf_n(yprogram2)
739  CALL io_buff_clean
740 #else
741  CALL abor1_sfx("The Land-sea mask file is assumed to be an FA file. You must compile with FA support enabled: -DSFX_FA")
742 #endif
743  ELSE
744  CALL abor1_sfx("CFILE_FORMAT_LSM="//trim(cfile_format_lsm)//" not implemented!")
745  ENDIF
746  IF (nrank==npio.AND.nprintlev>0) WRITE(*,*)'READ LSM OK'
747 ENDIF
748 !
749 ! The observations used in the analysis is read.
750 ! The options are either from a CANARI analysis or from a file
751 IF ( cassim_isba=="EKF " .OR. cassim_isba=="ENKF " ) THEN
752  ALLOCATE(xyo(ysurf_cur%U%NSIZE_NATURE,nobstype))
753 ENDIF
754 
755 IF ( trim(cfile_format_obs) == "ASCII") THEN
756 
757  ALLOCATE(zwork(ysurf_cur%U%NDIM_FULL,nobstype))
758 
759  IF (nrank==npio) THEN
760 
761  ymfile = 'OBSERVATIONS_'
762  CALL get_file_name(iyear,imonth,iday,ihour,ymfile)
763  ymfile=trim(ymfile)//".DAT"
764  istat = 0
765  OPEN(unit=55,file=trim(ymfile),form='FORMATTED',status='OLD',iostat=istat)
766  IF ( istat /=0 ) CALL abor1_sfx("Error opening file "//trim(ymfile))
767 
768  ! Get the nature points from processors
769  ! If the file has an header, we check for consistency
770  IF ( lobsheader ) THEN
771  ! Read in first line and check if variables are consistent
772  ALLOCATE(cobsinfile(nobstype))
773  READ (55,*,iostat=istat) (cobsinfile(jj),jj=1,nobstype)
774  IF ( istat /= 0 ) CALL abor1_sfx("Error reading header in "//trim(ymfile))
775 
776  DO iobs = 1,nobstype
777  IF ( trim(cobs(iobs)) /= trim(cobsinfile(iobs))) THEN
778  CALL abor1_sfx("Mapping of observations in "//trim(ymfile)//&
779  " is not consistent with setup! "//trim(cobs(iobs))//" /= "//trim(cobsinfile(iobs)))
780  ENDIF
781  ENDDO
782  DEALLOCATE(cobsinfile)
783  ENDIF
784 
785  zwork(:,:) = xundef
786 
787  ENDIF
788 
789  ! Read all observations (NDIM_FULL)
790  IF (lobsnat) THEN
791 
792  IF (nrank==npio) THEN
793  ALLOCATE(zyo_nat(ysurf_cur%U%NDIM_NATURE,nobstype))
794  DO ji = 1,ysurf_cur%U%NDIM_NATURE
795  READ (55,*,iostat=istat) (zyo_nat(ji,jj),jj=1,nobstype)
796  IF ( istat /= 0 ) CALL abor1_sfx("Error reading file "//trim(ymfile))
797  ENDDO
798  ALLOCATE(znature(ysurf_cur%U%NDIM_FULL))
799  ENDIF
800 
801  IF (nproc>1) THEN
802  CALL gather_and_write_mpi(ysurf_cur%U%XNATURE,znature)
803  ELSEIF (nrank==npio) THEN
804  znature(:) = ysurf_cur%U%XNATURE
805  ENDIF
806 
807  IF (nrank==npio) THEN
808  icpt = 0
809  DO ji = 1,ysurf_cur%U%NDIM_FULL
810  IF (znature(ji)>0.) THEN
811  icpt = icpt + 1
812  zwork(ji,:) = zyo_nat(icpt,:)
813  ENDIF
814  ENDDO
815  DEALLOCATE(znature,zyo_nat)
816  ENDIF
817 
818  ELSEIF (nrank==npio) THEN
819 
820  DO ji = 1,ysurf_cur%U%NDIM_FULL
821  READ (55,*,iostat=istat) (zwork(ji,jj),jj=1,nobstype)
822  IF ( istat /= 0 ) CALL abor1_sfx("Error reading file "//trim(ymfile))
823  ENDDO
824  ENDIF
825 
826  IF (nrank==npio) THEN
827 
828  CLOSE(55)
829  IF (nprintlev>0) WRITE(*,*) 'Read observation file OK'
830 
831  ENDIF
832 
833  ! Initialize possible observations
834  zt2m = 999.
835  zhu2m = 999.
836  zswe = 999.
837 
838  IF (nproc>1) THEN
839 
840  ! Running on more CPU's
841  ! For EKF/EnKF we must distribute variables for nature tile
842  IF ( cassim_isba=="EKF " .OR. cassim_isba=="ENKF " ) THEN
843  DO jj=1,nobstype
844  CALL read_and_send_mpi(zwork(:,jj),xyo(:,jj),ysurf_cur%U%NR_NATURE)
845  ENDDO
846  ENDIF
847 
848  ! Set observations used for possibly other tiles than nature
849  ! Distribute read variables
850  jj = 1
851  DO ji = 1,nobsmax
852  IF (nnco(ji) == 1 .AND. jj <= nobstype ) THEN
853  SELECT CASE (ji)
854  CASE (1)
855  CALL read_and_send_mpi(zwork(:,jj),zt2m(:))
856  CASE (2)
857  CALL read_and_send_mpi(zwork(:,jj),zhu2m(:))
858  CASE (5)
859  CALL read_and_send_mpi(zwork(:,jj),zswe(:))
860  END SELECT
861  jj = jj + 1
862  ENDIF
863  ENDDO
864 
865  ELSE
866 
867  ! Running on one CPU
868  IF ( cassim_isba=="EKF " .OR. cassim_isba=="ENKF " ) THEN
869  DO ji = 1,ysurf_cur%U%NSIZE_NATURE
870  xyo(ji,:) = zwork(ysurf_cur%U%NR_NATURE(ji),:)
871  ENDDO
872  ENDIF
873  ! Set observations used for possibly other tiles than nature
874  jj = 1
875  DO ji = 1,nobsmax
876  IF (nnco(ji) == 1 .AND. jj <= nobstype ) THEN
877  SELECT CASE (ji)
878  CASE (1)
879  zt2m(:)=zwork(:,jj)
880  CASE (2)
881  zhu2m(:)=zwork(:,jj)
882  CASE (5)
883  zswe(:)=zwork(:,jj)
884  END SELECT
885  jj = jj + 1
886  ENDIF
887  ENDDO
888  ENDIF
889  DEALLOCATE(zwork)
890 
891  nobs = nobs + nobstype
892  IF (( cassim_isba=="EKF " .OR. cassim_isba=="ENKF " ) .AND. ( nprintlev > 2 )) WRITE(iluout,*) 'read in obs: ', xyo(1,:), nobs
893 
894 ELSEIF ( trim(cfile_format_obs) == "FA") THEN
895  !
896  nobs = nobstype
897  !
898  !
899  ! Define FA file name for CANARI analysis
900 #ifdef SFX_FA
901  cfilein_fa = 'CANARI' ! input CANARI analysis
902  cdnomc = 'canari' ! new frame name
903 
904 ! Open FA file
905  CALL init_io_surf_n(ysurf_cur%DTCO, ysurf_cur%DGU, ysurf_cur%U, &
906  yprogram2,'EXTZON','SURF ','READ ')
907 !
908 ! Read CANARI analysis
909  CALL read_surf(yprogram2,'CLSTEMPERATURE ',zt2m ,iresp)
910  CALL read_surf(yprogram2,'CLSHUMI.RELATIVE',zhu2m,iresp)
911  CALL read_surf(yprogram2,'SURFTEMPERATURE ',zts ,iresp)
912  CALL read_surf(yprogram2,'SURFRESERV.NEIGE',zswe ,iresp)
913  CALL read_surf(yprogram2,'CLSVENT.ZONAL ',zucls,iresp)
914  CALL read_surf(yprogram2,'CLSVENT.MERIDIEN',zvcls,iresp)
915 
916 ! Close CANARI file
917  CALL end_io_surf_n(yprogram2)
918  CALL io_buff_clean
919  IF (nrank==npio.AND.nprintlev>0) WRITE(*,*) 'READ CANARI OK'
920 #else
921  CALL abor1_sfx("CANARI analyis is supposed to be an FA file. You must compile with FA support enabled: -DSFX_FA")
922 #endif
923 ELSE
924  CALL abor1_sfx("CFILE_FORMAT_OBS="//trim(cfile_format_obs)//" not implemented!")
925 ENDIF
926 !
927 ! Climatological fields are only used in OI
928 IF (cassim_isba=="OI ") THEN
929  !
930  IF (trim(cfile_format_clim) == "ASCII" ) THEN
931 
932  ALLOCATE(zwork(ysurf_cur%U%NDIM_FULL,2))
933 
934  IF (nrank==npio) THEN
935  ymfile = 'CLIMATE.DAT'
936  WRITE(*,*) "READING CLIM from file "//trim(ymfile)
937  OPEN(unit=55,file=trim(ymfile),form='FORMATTED',status='OLD',iostat=istat)
938  IF ( istat /= 0 ) CALL abor1_sfx("Can not open "//trim(ymfile))
939 
940  zwork(:,:) = xundef
941  ! Read CLIMATE file
942  DO ji = 1,ysurf_cur%U%NDIM_FULL
943  READ (55,*,iostat=istat) (zwork(ji,jj),jj=1,2)
944  IF ( istat /= 0 ) CALL abor1_sfx("Error reading file "//trim(ymfile))
945  ENDDO
946  CLOSE(55)
947  ENDIF
948 
949  ! Distribute values on processors
950  IF (nproc>1) THEN
951  CALL read_and_send_mpi(zwork(:,1),zswec(:))
952  CALL read_and_send_mpi(zwork(:,2),ztsc(:))
953  ELSE
954  ! Set First-Guess variables
955  zswec(:)=zwork(:,1)
956  ztsc=zwork(:,2)
957  ENDIF
958  DEALLOCATE(zwork)
959 
960  ELSEIF (trim(cfile_format_clim) == "FA" ) THEN
961 
962  ! Define FA file name for surface climatology
963 #ifdef SFX_FA
964  cfilein_fa = 'clim_isba' ! input climatology
965  cdnomc = 'climat' ! new frame name
966 
967 ! Open FA file
968  CALL init_io_surf_n(ysurf_cur%DTCO, ysurf_cur%DGU, ysurf_cur%U, &
969  yprogram2,'EXTZON','SURF ','READ ')
970 !
971 ! Read climatology file
972  CALL read_surf(yprogram2,'SURFRESERV.NEIGE',zswec,iresp)
973  CALL read_surf(yprogram2,'SURFTEMPERATURE' ,ztsc ,iresp)
974 
975 ! Close climatology file
976  CALL end_io_surf_n(yprogram2)
977  CALL io_buff_clean
978 #else
979  CALL abor1_sfx("The climate file is supposed to be an FA file. You must compile with FA support enabled: -DSFX_FA")
980 #endif
981  ELSE
982  CALL abor1_sfx("CFILE_FORMAT_CLIM="//trim(cfile_format_clim)//" not implemented!")
983  ENDIF
984  IF (nrank==npio.AND.nprintlev>0) WRITE(*,*) 'READ CLIMATOLOGY OK'
985  !
986 ENDIF
987 !
988  CALL assim_set_sst(ysurf_cur%DTCO, ysurf_cur%DGU, ysurf_cur%SM%S, ysurf_cur%U, &
989  isize_full,zlsm,zsst,zsic,ytest)
990 
991 IF ( .NOT. lassim ) CALL abor1_sfx("YOU CAN'T RUN SODA WITHOUT SETTING LASSIM=.TRUE. IN THE ASSIM NAMELIST")
992 !
993 ALLOCATE(gd_maskext(isize_full))
994 gd_maskext(:) = .false.
995 !
996 ALLOCATE(zlon(isize_full))
997 ALLOCATE(zlat(isize_full))
998 zlon(:) = ysurf_cur%UG%XLON(:)
999 zlat(:) = ysurf_cur%UG%XLAT(:)
1000 !
1001 glkeepextzone = .true.
1002 !
1003 IF (nrank==npio) WRITE(*,*) 'PERFORMIMG OFFLINE SURFEX DATA ASSIMILATION...'
1004  CALL assim_surf_atm_n(ysurf_cur%IM%DGMI, ysurf_cur%IM%IG, ysurf_cur%IM%I, ysurf_cur%SM%S, &
1005  ysurf_cur%U, ysurf_cur%TM%T, ysurf_cur%TM%TOP, ysurf_cur%WM%W, &
1006  csurf_filetype,isize_full, &
1007  zcon_rain, zstrat_rain, zcon_snow, zstrat_snow, &
1008  zclouds, zlsm, zevaptr, zevap, &
1009  zswec, ztsc, &
1010  zts, zt2m, zhu2m, zswe, &
1011  zsst, zsic, zucls, zvcls, &
1012  ytest, gd_maskext, zlon, zlat, glkeepextzone )
1013 !
1014 DEALLOCATE(zcon_rain)
1015 DEALLOCATE(zstrat_rain)
1016 DEALLOCATE(zcon_snow)
1017 DEALLOCATE(zstrat_snow)
1018 DEALLOCATE(zclouds)
1019 DEALLOCATE(zlsm)
1020 DEALLOCATE(zevaptr)
1021 DEALLOCATE(zevap)
1022 DEALLOCATE(ztsc)
1023 DEALLOCATE(zswec)
1024 DEALLOCATE(zts)
1025 DEALLOCATE(zt2m)
1026 DEALLOCATE(zhu2m)
1027 DEALLOCATE(zswe)
1028 DEALLOCATE(zucls)
1029 DEALLOCATE(zvcls)
1030 DEALLOCATE(zsst)
1031 DEALLOCATE(zsic)
1032 !
1033 !
1034 IF (ctimeseries_filetype=="OFFLIN") THEN
1035  CALL init_output_ol_n(ysurf_cur)
1036 ENDIF
1037 !
1038 ztime_out = ztime
1039 iday_out = iday
1040 imonth_out = imonth
1041 iyear_out = iyear
1042 !
1043 IF (nrank==npio) THEN
1044  !
1045  IF(lout_timename)THEN
1046  ! if true, change the name of output file at the end of a day
1047  ! (ex: 19860502_00h00 -> 19860501_24h00)
1048  IF(ztime==0.0)THEN
1049  ztime_out = 86400.
1050  iday_out = iday-1
1051  IF(iday_out==0)THEN
1052  imonth_out = imonth - 1
1053  IF(imonth_out==0)THEN
1054  imonth_out=12
1055  iyear_out = iyear - 1
1056  ENDIF
1057  SELECT CASE (imonth_out)
1058  CASE(4,6,9,11)
1059  iday_out=30
1060  CASE(1,3,5,7:8,10,12)
1061  iday_out=31
1062  CASE(2)
1063  IF( ((mod(iyear_out,4)==0).AND.(mod(iyear_out,100)/=0)) .OR. (mod(iyear_out,400)==0))THEN
1064  iday_out=29
1065  ELSE
1066  iday_out=28
1067  ENDIF
1068  END SELECT
1069  ENDIF
1070  ENDIF
1071  !
1072  ENDIF
1073  !
1074  WRITE(ytag,fmt='(I4.4,I2.2,I2.2,A1,I2.2,A1,I2.2)') iyear_out,imonth_out,iday_out,&
1075  '_',int(ztime_out/3600.),'h',nint(ztime_out)/60-60*int(ztime_out/3600.)
1076  cfileout = adjustl(adjustr(csurffile)//'.'//ytag//'.txt')
1077 #ifdef SFX_LFI
1078  cfileout_lfi= adjustl(adjustr(csurffile)//'.'//ytag)
1079 #endif
1080 #ifdef SFX_FA
1081  cfileout_fa = adjustl(adjustr(csurffile)//'.'//ytag//'.fa')
1082 #endif
1083 #ifdef SFX_NC
1084  cfileout_nc = adjustl(adjustr(csurffile)//'.'//ytag//'.nc')
1085 #endif
1086  !
1087  IF (csurf_filetype=='FA ') THEN
1088 #ifdef SFX_FA
1089  cdnomc = 'header'
1090  lfanocompact = ldiag_fa_nocompact
1091  idatef(1)= iyear_out
1092  idatef(2)= imonth_out
1093  idatef(3)= iday_out
1094  idatef(4)= floor(ztime_out/3600.)
1095  idatef(5)= floor(ztime_out/60.) - idatef(4) * 60
1096  idatef(6)= nint(ztime_out) - idatef(4) * 3600 - idatef(5) * 60
1097  idatef(7:11) = 0
1098  CALL faitou(iret,nunit_fa,.true.,cfileout_fa,'UNKNOWN',.true.,.false.,iverbfa,0,inb,cdnomc)
1099  CALL fandar(iret,nunit_fa,idatef)
1100 #endif
1101  END IF
1102  !
1103 ENDIF
1104 !
1105 isize = 1
1106 IF (cassim_isba=="ENKF ") THEN
1107  isize = nens
1108  IF (lbias_correction) isize = isize + 1
1109 ENDIF
1110 !
1111 nwrite = 1
1112 xstartw = 1
1113 ltime_written(:)=.false.
1114 !
1115 DO iens = 1,isize
1116  !
1117  IF (cassim_isba=="ENKF ") THEN
1118  !
1119  ymfile = "PREP_"
1120  CALL get_file_name(iyear,imonth,iday,ihour,ymfile)
1121  WRITE(yvar,'(I3)') iens
1122  yfilein = trim(ymfile)//"_EKF_ENS"//adjustl(yvar)
1123  CALL set_filein(yfilein)
1124  !
1125  lread_all = .true.
1126  !
1127  CALL dealloc_surf_atm_n(ysurf_cur)
1128  !
1129  ! Initialize the SURFEX interface
1130  CALL io_buff_clean
1131  CALL init_surf_atm_n(ysurf_cur, &
1132  csurf_filetype,yinit, lland_use, isize_full, isv, isw, &
1133  csv, xco2, xrhoa, xzenith, xazim, xsw_bands, &
1134  xdir_alb, xsca_alb, xemis, xtsrad, xtsurf, &
1135  iyear, imonth, iday, ztime, &
1136  yatmfile, yatmfiletype, ytest )
1137  !
1138  !
1139  DO jl=1,nvar
1140  !
1141  ! Update the modified values
1142  SELECT CASE (trim(cvar(jl)))
1143  CASE("TG1")
1144  ysurf_cur%IM%I%XTG(:,1,:) = xf(:,:,iens,jl)
1145  CASE("TG2")
1146  ysurf_cur%IM%I%XTG(:,2,:) = xf(:,:,iens,jl)
1147  CASE("WG1")
1148  ysurf_cur%IM%I%XWG(:,1,:) = xf(:,:,iens,jl)
1149  CASE("WG2")
1150  ysurf_cur%IM%I%XWG(:,2,:) = xf(:,:,iens,jl)
1151  CASE("WG3")
1152  ysurf_cur%IM%I%XWG(:,3,:) = xf(:,:,iens,jl)
1153  CASE("LAI")
1154  ysurf_cur%IM%I%XLAI(:,:) = xf(:,:,iens,jl)
1155  CASE default
1156  CALL abor1_sfx("Mapping of "//trim(cvar(jl))//" is not defined in EKF!")
1157  END SELECT
1158  ENDDO
1159  !
1160  ENDIF
1161  !
1162 #ifdef SFX_NC
1163  ldef = .true.
1164 #endif
1165  !
1166  IF (ctimeseries_filetype=="NC ") THEN
1167  CALL init_output_nc_n(ysurf_cur%TM%BDD, ysurf_cur%CHE, ysurf_cur%CHN, ysurf_cur%CHU, &
1168  ysurf_cur%SM%DTS, ysurf_cur%TM%DTT, ysurf_cur%DTZ, ysurf_cur%IM%I, &
1169  ysurf_cur%UG, ysurf_cur%U, ysurf_cur%DGU)
1170  ENDIF
1171  !
1172  inw = 1
1173  IF (ctimeseries_filetype=="NC ") inw = 2
1174  !
1175  DO jnw = 1,inw
1176  CALL io_buff_clean
1177  CALL write_surf_atm_n(ysurf_cur, &
1178  ctimeseries_filetype,'ALL',lland_use)
1179  CALL write_diag_surf_atm_n(ysurf_cur, &
1180  ctimeseries_filetype,'ALL')
1181 #ifdef SFX_NC
1182  ldef = .false.
1183 #endif
1184  ENDDO
1185  !
1186  CALL flag_update(ysurf_cur%IM%DGI, ysurf_cur%DGU,.false.,.true.,.false.,.false.)
1187  !
1188  IF (lrestart_2m) THEN
1189  i2m = 1
1190  gpgd_isba = .true.
1191  ELSE
1192  i2m = 0
1193  gpgd_isba = .false.
1194  ENDIF
1195  gfrac = .true.
1196  gdiag_grid = .true.
1197  gsurf_budget = .false.
1198  grad_budget = .false.
1199  gcoef = .false.
1200  gsurf_vars = .false.
1201  ibeq = 0
1202  idsteq = 0
1203  gdiag_ocean = .false.
1204  gdiag_seaice = .false.
1205  gwater_profile = .false.
1206  gsurf_evap_budget = .false.
1207  gflood = .false.
1208  gpgd_isba = .false.
1209  gch_no_flux_isba = .false.
1210  gsurf_misc_budget_isba = .false.
1211  gpgd_teb = .false.
1212  gsurf_misc_budget_teb = .false.
1213  !
1214  CALL flag_diag_update(ysurf_cur%FM%CHF, ysurf_cur%IM%CHI, ysurf_cur%SM%CHS, ysurf_cur%TM%CHT, &
1215  ysurf_cur%WM%CHW, ysurf_cur%IM%DGEI, ysurf_cur%FM%DGF, ysurf_cur%IM%DGI, &
1216  ysurf_cur%FM%DGMF, ysurf_cur%IM%DGMI, ysurf_cur%TM%DGMTO, ysurf_cur%SM%DGO, &
1217  ysurf_cur%SM%DGS, ysurf_cur%SM%DGSI, ysurf_cur%DGU, ysurf_cur%TM%DGT, &
1218  ysurf_cur%WM%DGW, ysurf_cur%IM%I, ysurf_cur%U, &
1219  gfrac, gdiag_grid, i2m, gsurf_budget, grad_budget, gcoef, &
1220  gsurf_vars, ibeq, idsteq, gdiag_ocean, gdiag_seaice, &
1221  gwater_profile, &
1222  gsurf_evap_budget, gflood, gpgd_isba, gch_no_flux_isba, &
1223  gsurf_misc_budget_isba, gpgd_teb, gsurf_misc_budget_teb )
1224  !
1225  yens = ' '
1226  IF (isize>1) WRITE(yens,'(I3)') iens
1227  !
1228  IF ( csurf_filetype == "LFI " ) THEN
1229 #ifdef SFX_LFI
1230  cfileout_lfi = trim(trim(csurffile)//adjustl(yens))
1231 #endif
1232  ELSEIF ( csurf_filetype == "FA " ) THEN
1233 #ifdef SFX_FA
1234  cfileout_fa = adjustl(trim(adjustr(csurffile)//adjustl(yens))//'.fa')
1235 #endif
1236  ELSEIF ( csurf_filetype == "ASCII " ) THEN
1237 #ifdef SFX_ASC
1238  cfileout = adjustl(trim(adjustr(csurffile)//adjustl(yens))//'.txt')
1239  lcreated = .false.
1240 #endif
1241  ELSEIF ( csurf_filetype == "NC " ) THEN
1242 #ifdef SFX_NC
1243  cfileout_nc = adjustl(trim(adjustr(csurffile)//adjustl(yens))//'.nc')
1244 #endif
1245  ELSE
1246  CALL abor1_sfx(trim(csurf_filetype)//" is not implemented!")
1247  ENDIF
1248  !
1249 #ifdef SFX_NC
1250  ldef = .true.
1251 #endif
1252  !
1253  IF (csurf_filetype=="NC ") THEN
1254  CALL init_output_nc_n(ysurf_cur%TM%BDD, ysurf_cur%CHE, ysurf_cur%CHN, ysurf_cur%CHU, &
1255  ysurf_cur%SM%DTS, ysurf_cur%TM%DTT, ysurf_cur%DTZ, ysurf_cur%IM%I, &
1256  ysurf_cur%UG, ysurf_cur%U, ysurf_cur%DGU)
1257  ENDIF
1258  !
1259  inw = 1
1260  IF (csurf_filetype=="NC ") inw = 2
1261  !
1262  DO jnw = 1,inw
1263  !
1264  CALL io_buff_clean
1265  !
1266  ! Store results from assimilation
1267  CALL write_surf_atm_n(ysurf_cur, &
1268  csurf_filetype,'ALL',lland_use)
1269  IF (ysurf_cur%DGU%LREAD_BUDGETC.AND..NOT.ysurf_cur%IM%DGEI%LRESET_BUDGETC) THEN
1270  CALL write_diag_surf_atm_n(ysurf_cur, &
1271  csurf_filetype,'ALL')
1272  ENDIF
1273 #ifdef SFX_NC
1274  ldef = .false.
1275 #endif
1276  !
1277  ENDDO
1278  !
1279 ENDDO
1280 !
1281 IF (nrank==npio .AND. csurf_filetype=='FA ') THEN
1282 #ifdef SFX_FA
1283  CALL fairme(iret,nunit_fa,'UNKNOWN')
1284 #endif
1285 END IF
1286 !
1287 !* 3. Close parallelized I/O
1288 ! ----------------------
1289 !
1290 IF (ctimeseries_filetype=='OFFLIN') CALL close_fileout_ol
1291 !
1292 IF (nrank==npio) THEN
1293  WRITE(iluout,*) ' '
1294  WRITE(iluout,*) ' -----------------------'
1295  WRITE(iluout,*) ' | SODA ENDS CORRECTLY |'
1296  WRITE(iluout,*) ' -----------------------'
1297  !
1298  WRITE(*,*) ' '
1299  WRITE(*,*) ' -----------------------'
1300  WRITE(*,*) ' | SODA ENDS CORRECTLY |'
1301  WRITE(*,*) ' -----------------------'
1302  !
1303 ENDIF
1304 !
1305  CLOSE(iluout)
1306 !
1307  CALL surfex_deallo_list
1308 !
1309 IF (ALLOCATED(nindex)) DEALLOCATE(nindex)
1310 IF (ALLOCATED(nsize_task)) DEALLOCATE(nsize_task)
1311 !
1312 IF (ASSOCIATED(nwork)) DEALLOCATE(nwork)
1313 IF (ASSOCIATED(xwork)) DEALLOCATE(xwork)
1314 IF (ASSOCIATED(nwork2)) DEALLOCATE(nwork2)
1315 IF (ASSOCIATED(xwork2)) DEALLOCATE(xwork2)
1316 IF (ASSOCIATED(xwork3)) DEALLOCATE(xwork3)
1317 IF (ASSOCIATED(nwork_full)) DEALLOCATE(nwork_full)
1318 IF (ASSOCIATED(xwork_full)) DEALLOCATE(xwork_full)
1319 IF (ASSOCIATED(nwork2_full)) DEALLOCATE(nwork2_full)
1320 IF (ASSOCIATED(xwork2_full)) DEALLOCATE(xwork2_full)
1321 !
1322  CALL end_log_mpi
1323 !
1324 IF (lhook) CALL dr_hook('SODA',1,zhook_handle)
1325 !
1326 #ifdef SFX_MPI
1327  CALL mpi_finalize(infompi)
1328 #endif
1329 !
1330 !-------------------------------------------------------------------------------
1331 
1332 #endif
1333 END PROGRAM soda
subroutine init_io_surf_n(DTCO, DGU, U, HPROGRAM, HMASK, HSCHEME, HACTION)
subroutine init_output_nc_n(BDD, CHE, CHN, CHU, DTS, DTT, DTZ, I, UG, U, DGU)
subroutine dealloc_surf_atm_n(YSC)
subroutine init_output_ol_n(YSC)
subroutine set_surfex_filein(HPROGRAM, HMASK)
subroutine close_fileout_ol
subroutine io_buff_clean
subroutine prep_log_mpi
subroutine init_index_mpi(YSC, HPROGRAM, HALG, PIO_FRAC, OSHADOWS)
subroutine abor1_sfx(YTEXT)
Definition: abor1_sfx.F90:6
subroutine end_log_mpi
subroutine flag_update(DGI, DGU, ONOWRITE_CANOPY, OPGD, OPROVAR_TO_DIAG, OSELECT)
Definition: flag_update.F90:6
subroutine write_diag_surf_atm_n(YSC, HPROGRAM, HWRITE)
subroutine init_surf_atm_n(YSC, HPROGRAM, HINIT, OLAND_USE, KI, KSV, KSW, HSV, PCO2, PRHOA, PZENITH, PAZIM, PSW_BANDS, PDIR_ALB, PSCA_ALB, PEMIS, PTSRAD, PTSURF, KYEAR, KMONTH, KDAY, PTIME, HATMFILE, HATMFILETYPE, HTEST)
subroutine surfex_deallo_list
subroutine close_namelist(HPROGRAM, KLUNAM)
subroutine read_all_namelists(YSC, HPROGRAM, HINIT, ONAM_READ)
subroutine posnam(KULNAM, HDNAML, OFOUND, KLUOUT)
subroutine flag_diag_update(CHF, CHI, CHS, CHT, CHW, DGEI, DGF, DGI, DGMF, DGMI, DGMTO, DGO, DGS, DGSI, DGU, DGT, DGW, I, U, OFRAC, ODIAG_GRID, K2M, OSURF_BUDGET, ORAD_BUDGET, OCOEF, OSURF_VARS, KBEQ, KDSTEQ, ODIAG_OCEAN, ODIAG_SEAICE, OWATER_PROFILE, OSURF_EVAP_BUDGET, OFLOOD, OPGD_ISBA, OCH_NO_FLUX_ISBA, OSURF_MISC_BUDGET_ISBA, OPGD_TEB, OSURF_MISC_BUDGET_TEB)
subroutine end_io_surf_n(HPROGRAM)
Definition: end_io_surfn.F90:6
subroutine get_luout(HPROGRAM, KLUOUT)
Definition: get_luout.F90:6
subroutine write_surf_atm_n(YSC, HPROGRAM, HWRITE, OLAND_USE)
subroutine wlog_mpi(HLOG, PLOG, KLOG, KLOG2, OLOG)
subroutine surfex_alloc_list(KMODEL)
subroutine assim_set_sst(DTCO, DGU, S, U, KI, PITM, PSST, PSIC, HTEST)
subroutine get_size_full_n(U, HPROGRAM, KDIM_FULL, KSIZE_FULL)
subroutine assim_surf_atm_n(DGMI, IG, I, S, U, T, TOP, W, HPROGRAM, KI, PCON_RAIN, PSTRAT_RAIN, PCON_SNOW, PSTRAT_SNOW, PCLOUDS, PLSM, PEVAPTR, PEVAP, PSWEC, PTSC, PTS, PT2M, PHU2M, PSWE, PSST, PSIC, PUCLS, PVCLS, HTEST, OD_MASKEXT, PLON, PLAT, OLKEEPEXTZONE)
subroutine open_namelist(HPROGRAM, KLUNAM, HFILE)
program soda
Definition: soda.F90:6