concoutput.f90 25.3 KB
Newer Older
1
2
! SPDX-FileCopyrightText: FLEXPART 1998-2019, see flexpart_license.txt
! SPDX-License-Identifier: GPL-3.0-or-later
3

Matthias Langer's avatar
 
Matthias Langer committed
4
subroutine concoutput(itime,outnum,gridtotalunc,wetgridtotalunc, &
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
     drygridtotalunc)
!                        i     i          o             o
!       o
!*****************************************************************************
!                                                                            *
!     Output of the concentration grid and the receptor concentrations.      *
!                                                                            *
!     Author: A. Stohl                                                       *
!                                                                            *
!     24 May 1995                                                            *
!                                                                            *
!     13 April 1999, Major update: if output size is smaller, dump output    *
!                    in sparse matrix format; additional output of           *
!                    uncertainty                                             *
!                                                                            *
!     05 April 2000, Major update: output of age classes; output for backward*
!                    runs is time spent in grid cell times total mass of     *
!                    species.                                                *
!                                                                            *
!     17 February 2002, Appropriate dimensions for backward and forward runs *
!                       are now specified in file par_mod                    *
!                                                                            *
!     June 2006, write grid in sparse matrix with a single write command     *
!                in order to save disk space                                 *
!                                                                            *
!     2008 new sparse matrix format                                          *
!                                                                            *
!*****************************************************************************
!                                                                            *
! Variables:                                                                 *
! outnum          number of samples                                          *
! ncells          number of cells with non-zero concentrations               *
! sparse          .true. if in sparse matrix format, else .false.            *
! tot_mu          1 for forward, initial mass mixing ration for backw. runs  *
!                                                                            *
!*****************************************************************************
Matthias Langer's avatar
 
Matthias Langer committed
41
42
43
44
45
46

  use unc_mod
  use point_mod
  use outg_mod
  use par_mod
  use com_mod
47
  use mean_mod
Matthias Langer's avatar
 
Matthias Langer committed
48
49
50
51
52
53
54
55

  implicit none

  real(kind=dp) :: jul
  integer :: itime,i,ix,jy,kz,ks,kp,l,iix,jjy,kzz,nage,jjjjmmdd,ihmmss
  integer :: sp_count_i,sp_count_r
  real :: sp_fact
  real :: outnum,densityoutrecept(maxreceptor),xl,yl
56
57
58
! RLT
  real :: densitydryrecept(maxreceptor)
  real :: factor_dryrecept(maxreceptor)
Matthias Langer's avatar
 
Matthias Langer committed
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
!real densityoutgrid(0:numxgrid-1,0:numygrid-1,numzgrid),
!    +grid(0:numxgrid-1,0:numygrid-1,numzgrid,maxspec,maxpointspec_act,
!    +    maxageclass)
!real wetgrid(0:numxgrid-1,0:numygrid-1,maxspec,maxpointspec_act,
!    +       maxageclass)
!real drygrid(0:numxgrid-1,0:numygrid-1,maxspec,
!    +       maxpointspec_act,maxageclass)
!real gridsigma(0:numxgrid-1,0:numygrid-1,numzgrid,maxspec,
!    +       maxpointspec_act,maxageclass),
!    +     drygridsigma(0:numxgrid-1,0:numygrid-1,maxspec,
!    +     maxpointspec_act,maxageclass),
!    +     wetgridsigma(0:numxgrid-1,0:numygrid-1,maxspec,
!    +     maxpointspec_act,maxageclass)
!real factor(0:numxgrid-1,0:numygrid-1,numzgrid)
!real sparse_dump_r(numxgrid*numygrid*numzgrid)
!integer sparse_dump_i(numxgrid*numygrid*numzgrid)

!real sparse_dump_u(numxgrid*numygrid*numzgrid)
78
79
80
81
  real(dep_prec) :: auxgrid(nclassunc)
  real(sp) :: gridtotal,gridsigmatotal,gridtotalunc
  real(dep_prec) :: wetgridtotal,wetgridsigmatotal,wetgridtotalunc
  real(dep_prec) :: drygridtotal,drygridsigmatotal,drygridtotalunc
Matthias Langer's avatar
 
Matthias Langer committed
82
83
84
85
  real :: halfheight,dz,dz1,dz2,tot_mu(maxspec,maxpointspec_act)
  real,parameter :: smallnum = tiny(0.0) ! smallest number that can be handled
  real,parameter :: weightair=28.97
  logical :: sp_zer
86
  logical,save :: init=.true.
Matthias Langer's avatar
 
Matthias Langer committed
87
88
  character :: adate*8,atime*6
  character(len=3) :: anspec
Espen Sollum's avatar
Espen Sollum committed
89
90
  integer :: mind
! mind        eso:added to ensure identical results between 2&3-fields versions
91
92
  character(LEN=8),save :: file_stat='REPLACE'
  logical :: ldates_file
93
  logical :: lexist
94
95
  integer :: ierr
  character(LEN=100) :: dates_char
Matthias Langer's avatar
 
Matthias Langer committed
96

97
98
! Determine current calendar date, needed for the file name
!**********************************************************
Matthias Langer's avatar
 
Matthias Langer committed
99
100
101
102
103

  jul=bdate+real(itime,kind=dp)/86400._dp
  call caldate(jul,jjjjmmdd,ihmmss)
  write(adate,'(i8.8)') jjjjmmdd
  write(atime,'(i6.6)') ihmmss
104
105
106
107

! Overwrite existing dates file on first call, later append to it
! This fixes a bug where the dates file kept growing across multiple runs

108
! If 'dates' file exists in output directory, make a backup
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  inquire(file=path(2)(1:length(2))//'dates', exist=ldates_file)
  if (ldates_file.and.init) then
    open(unit=unitdates, file=path(2)(1:length(2))//'dates',form='formatted', &
         &access='sequential', status='old', action='read', iostat=ierr)
    open(unit=unittmp, file=path(2)(1:length(2))//'dates.bak', access='sequential', &
         &status='replace', action='write', form='formatted', iostat=ierr)
    do while (.true.)
      read(unitdates, '(a)', iostat=ierr) dates_char
      if (ierr.ne.0) exit
      write(unit=unittmp, fmt='(a)', iostat=ierr, advance='yes') trim(dates_char)
    end do
    close(unit=unitdates)
    close(unit=unittmp)
  end if

  open(unitdates,file=path(2)(1:length(2))//'dates', ACCESS='APPEND', STATUS=file_stat)
Matthias Langer's avatar
 
Matthias Langer committed
125
  write(unitdates,'(a)') adate//atime
126
  close(unitdates)  
Matthias Langer's avatar
 
Matthias Langer committed
127

128
129
  ! Overwrite existing dates file on first call, later append to it
  ! This fixes a bug where the dates file kept growing across multiple runs
130
131
132
133
134
135
  IF (init) THEN
    file_stat='OLD'
    init=.false.
  END IF


136
137
138
139
! For forward simulations, output fields have dimension MAXSPEC,
! for backward simulations, output fields have dimension MAXPOINT.
! Thus, make loops either about nspec, or about numpoint
!*****************************************************************
Matthias Langer's avatar
 
Matthias Langer committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156


  if (ldirect.eq.1) then
    do ks=1,nspec
      do kp=1,maxpointspec_act
        tot_mu(ks,kp)=1
      end do
    end do
  else
    do ks=1,nspec
      do kp=1,maxpointspec_act
        tot_mu(ks,kp)=xmass(kp,ks)
      end do
    end do
  endif


157
158
159
160
161
162
!*******************************************************************
! Compute air density: sufficiently accurate to take it
! from coarse grid at some time
! Determine center altitude of output layer, and interpolate density
! data to that altitude
!*******************************************************************
Matthias Langer's avatar
 
Matthias Langer committed
163

Espen Sollum's avatar
Espen Sollum committed
164
  mind=memind(2)
Matthias Langer's avatar
 
Matthias Langer committed
165
166
167
168
169
170
171
172
173
174
  do kz=1,numzgrid
    if (kz.eq.1) then
      halfheight=outheight(1)/2.
    else
      halfheight=(outheight(kz)+outheight(kz-1))/2.
    endif
    do kzz=2,nz
      if ((height(kzz-1).lt.halfheight).and. &
           (height(kzz).gt.halfheight)) goto 46
    end do
175
46  kzz=max(min(kzz,nz),2)
Matthias Langer's avatar
 
Matthias Langer committed
176
177
178
179
180
181
182
183
    dz1=halfheight-height(kzz-1)
    dz2=height(kzz)-halfheight
    dz=dz1+dz2
    do jy=0,numygrid-1
      do ix=0,numxgrid-1
        xl=outlon0+real(ix)*dxout
        yl=outlat0+real(jy)*dyout
        xl=(xl-xlon0)/dx
184
        yl=(yl-ylat0)/dy !v9.1.1 
Matthias Langer's avatar
 
Matthias Langer committed
185
186
        iix=max(min(nint(xl),nxmin1),0)
        jjy=max(min(nint(yl),nymin1),0)
187
188
! densityoutgrid(ix,jy,kz)=(rho(iix,jjy,kzz,2)*dz1+ &
!      rho(iix,jjy,kzz-1,2)*dz2)/dz
Espen Sollum's avatar
Espen Sollum committed
189
190
        densityoutgrid(ix,jy,kz)=(rho(iix,jjy,kzz,mind)*dz1+ &
             rho(iix,jjy,kzz-1,mind)*dz2)/dz
191
192
193
! RLT
        densitydrygrid(ix,jy,kz)=(rho_dry(iix,jjy,kzz,mind)*dz1+ &
             rho_dry(iix,jjy,kzz-1,mind)*dz2)/dz  
Matthias Langer's avatar
 
Matthias Langer committed
194
195
196
197
      end do
    end do
  end do

Espen Sollum's avatar
Espen Sollum committed
198
199
200
201
202
  do i=1,numreceptor
    xl=xreceptor(i)
    yl=yreceptor(i)
    iix=max(min(nint(xl),nxmin1),0)
    jjy=max(min(nint(yl),nymin1),0)
203
!densityoutrecept(i)=rho(iix,jjy,1,2)
Espen Sollum's avatar
Espen Sollum committed
204
    densityoutrecept(i)=rho(iix,jjy,1,mind)
205
206
! RLT
    densitydryrecept(i)=rho_dry(iix,jjy,1,mind)
Espen Sollum's avatar
Espen Sollum committed
207
  end do
Matthias Langer's avatar
 
Matthias Langer committed
208

209
210
211
212
! RLT
! conversion factor for output relative to dry air
  factor_drygrid=densityoutgrid/densitydrygrid
  factor_dryrecept=densityoutrecept/densitydryrecept
Matthias Langer's avatar
 
Matthias Langer committed
213

214
215
216
217
218
219
220
221
222
! Output is different for forward and backward simulations
  do kz=1,numzgrid
    do jy=0,numygrid-1
      do ix=0,numxgrid-1
        if (ldirect.eq.1) then
          factor3d(ix,jy,kz)=1.e12/volume(ix,jy,kz)/outnum
        else
          factor3d(ix,jy,kz)=real(abs(loutaver))/outnum
        endif
Matthias Langer's avatar
 
Matthias Langer committed
223
224
      end do
    end do
225
  end do
Matthias Langer's avatar
 
Matthias Langer committed
226

227
228
229
230
!*********************************************************************
! Determine the standard deviation of the mean concentration or mixing
! ratio (uncertainty of the output) and the dry and wet deposition
!*********************************************************************
Matthias Langer's avatar
 
Matthias Langer committed
231
232
233
234
235
236
237
238
239
240
241
242
243

  gridtotal=0.
  gridsigmatotal=0.
  gridtotalunc=0.
  wetgridtotal=0.
  wetgridsigmatotal=0.
  wetgridtotalunc=0.
  drygridtotal=0.
  drygridsigmatotal=0.
  drygridtotalunc=0.

  do ks=1,nspec

244
    write(anspec,'(i3.3)') ks
Matthias Langer's avatar
 
Matthias Langer committed
245

246
247
248
    if (DRYBKDEP.or.WETBKDEP) then !scavdep output
      if (DRYBKDEP) & 
      open(unitoutgrid,file=path(2)(1:length(2))//'grid_drydep_'//adate// &
249
           atime//'_'//anspec,form='unformatted')
250
251
      if (WETBKDEP) & 
      open(unitoutgrid,file=path(2)(1:length(2))//'grid_wetdep_'//adate// &
252
253
           atime//'_'//anspec,form='unformatted')
      write(unitoutgrid) itime
254
255
256
257
    else
      if ((iout.eq.1).or.(iout.eq.3).or.(iout.eq.5)) then
        if (ldirect.eq.1) then
          open(unitoutgrid,file=path(2)(1:length(2))//'grid_conc_'//adate// &
258
             atime//'_'//anspec,form='unformatted')
259
260
        else
          open(unitoutgrid,file=path(2)(1:length(2))//'grid_time_'//adate// &
261
             atime//'_'//anspec,form='unformatted')
262
263
        endif
        write(unitoutgrid) itime
264
      endif
265
266
      if ((iout.eq.2).or.(iout.eq.3)) then      ! mixing ratio
        open(unitoutgridppt,file=path(2)(1:length(2))//'grid_pptv_'//adate// &
267
           atime//'_'//anspec,form='unformatted')
268
269
270
        write(unitoutgridppt) itime
      endif
    endif ! if deposition output
Matthias Langer's avatar
 
Matthias Langer committed
271

272
273
    do kp=1,maxpointspec_act
      do nage=1,nageclass
Matthias Langer's avatar
 
Matthias Langer committed
274

275
276
        do jy=0,numygrid-1
          do ix=0,numxgrid-1
Matthias Langer's avatar
 
Matthias Langer committed
277

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
! WET DEPOSITION
            if ((WETDEP).and.(ldirect.gt.0)) then
              do l=1,nclassunc
                auxgrid(l)=wetgridunc(ix,jy,ks,kp,l,nage)
              end do
              call mean(auxgrid,wetgrid(ix,jy), &
                   wetgridsigma(ix,jy),nclassunc)
! Multiply by number of classes to get total concentration
              wetgrid(ix,jy)=wetgrid(ix,jy) &
                   *nclassunc
              wetgridtotal=wetgridtotal+wetgrid(ix,jy)
! Calculate standard deviation of the mean
              wetgridsigma(ix,jy)= &
                   wetgridsigma(ix,jy)* &
                   sqrt(real(nclassunc))
              wetgridsigmatotal=wetgridsigmatotal+ &
                   wetgridsigma(ix,jy)
            endif

! DRY DEPOSITION
            if ((DRYDEP).and.(ldirect.gt.0)) then
              do l=1,nclassunc
                auxgrid(l)=drygridunc(ix,jy,ks,kp,l,nage)
              end do
              call mean(auxgrid,drygrid(ix,jy), &
                   drygridsigma(ix,jy),nclassunc)
! Multiply by number of classes to get total concentration
              drygrid(ix,jy)=drygrid(ix,jy)* &
                   nclassunc
              drygridtotal=drygridtotal+drygrid(ix,jy)
! Calculate standard deviation of the mean
              drygridsigma(ix,jy)= &
                   drygridsigma(ix,jy)* &
                   sqrt(real(nclassunc))
312
              drygridsigmatotal=drygridsigmatotal+ &
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
                   drygridsigma(ix,jy)
            endif

! CONCENTRATION OR MIXING RATIO
            do kz=1,numzgrid
              do l=1,nclassunc
                auxgrid(l)=gridunc(ix,jy,kz,ks,kp,l,nage)
              end do
              call mean(auxgrid,grid(ix,jy,kz), &
                   gridsigma(ix,jy,kz),nclassunc)
! Multiply by number of classes to get total concentration
              grid(ix,jy,kz)= &
                   grid(ix,jy,kz)*nclassunc
              gridtotal=gridtotal+grid(ix,jy,kz)
! Calculate standard deviation of the mean
              gridsigma(ix,jy,kz)= &
                   gridsigma(ix,jy,kz)* &
                   sqrt(real(nclassunc))
              gridsigmatotal=gridsigmatotal+ &
                   gridsigma(ix,jy,kz)
Matthias Langer's avatar
 
Matthias Langer committed
333
            end do
334
          end do
Matthias Langer's avatar
 
Matthias Langer committed
335
336
337
338
339
        end do




340
341
342
343
344
345
346
!*******************************************************************
! Generate output: may be in concentration (ng/m3) or in mixing
! ratio (ppt) or both
! Output the position and the values alternated multiplied by
! 1 or -1, first line is number of values, number of positions
! For backward simulations, the unit is seconds, stored in grid_time
!*******************************************************************
Matthias Langer's avatar
 
Matthias Langer committed
347

348
349
! Concentration output
!*********************
350
        if ((iout.eq.1).or.(iout.eq.3).or.(iout.eq.5)) then
Matthias Langer's avatar
 
Matthias Langer committed
351

352
353
354
355
356
357
358
359
360
361
362
! Wet deposition
          sp_count_i=0
          sp_count_r=0
          sp_fact=-1.
          sp_zer=.true.
          if ((ldirect.eq.1).and.(WETDEP)) then
            do jy=0,numygrid-1
              do ix=0,numxgrid-1
!oncentraion greater zero
                if (wetgrid(ix,jy).gt.smallnum) then
                  if (sp_zer.eqv..true.) then ! first non zero value
Matthias Langer's avatar
 
Matthias Langer committed
363
364
365
366
                    sp_count_i=sp_count_i+1
                    sparse_dump_i(sp_count_i)=ix+jy*numxgrid
                    sp_zer=.false.
                    sp_fact=sp_fact*(-1.)
367
368
369
370
371
372
373
                  endif
                  sp_count_r=sp_count_r+1
                  sparse_dump_r(sp_count_r)= &
                       sp_fact*1.e12*wetgrid(ix,jy)/area(ix,jy)
!                sparse_dump_u(sp_count_r)=
!+                1.e12*wetgridsigma(ix,jy,ks,kp,nage)/area(ix,jy)
                else ! concentration is zero
Matthias Langer's avatar
 
Matthias Langer committed
374
                  sp_zer=.true.
375
376
                endif
              end do
Matthias Langer's avatar
 
Matthias Langer committed
377
            end do
378
          else
Matthias Langer's avatar
 
Matthias Langer committed
379
380
            sp_count_i=0
            sp_count_r=0
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
          endif
          write(unitoutgrid) sp_count_i
          write(unitoutgrid) (sparse_dump_i(i),i=1,sp_count_i)
          write(unitoutgrid) sp_count_r
          write(unitoutgrid) (sparse_dump_r(i),i=1,sp_count_r)
!       write(unitoutgrid) sp_count_u
!       write(unitoutgrid) (sparse_dump_u(i),i=1,sp_count_r)

! Dry deposition
          sp_count_i=0
          sp_count_r=0
          sp_fact=-1.
          sp_zer=.true.
          if ((ldirect.eq.1).and.(DRYDEP)) then
            do jy=0,numygrid-1
              do ix=0,numxgrid-1
                if (drygrid(ix,jy).gt.smallnum) then
                  if (sp_zer.eqv..true.) then ! first non zero value
Matthias Langer's avatar
 
Matthias Langer committed
399
400
401
402
                    sp_count_i=sp_count_i+1
                    sparse_dump_i(sp_count_i)=ix+jy*numxgrid
                    sp_zer=.false.
                    sp_fact=sp_fact*(-1.)
403
404
405
406
407
408
409
410
                  endif
                  sp_count_r=sp_count_r+1
                  sparse_dump_r(sp_count_r)= &
                       sp_fact* &
                       1.e12*drygrid(ix,jy)/area(ix,jy)
!                sparse_dump_u(sp_count_r)=
!+                1.e12*drygridsigma(ix,jy,ks,kp,nage)/area(ix,jy)
                else ! concentration is zero
Matthias Langer's avatar
 
Matthias Langer committed
411
                  sp_zer=.true.
412
413
                endif
              end do
Matthias Langer's avatar
 
Matthias Langer committed
414
            end do
415
          else
Matthias Langer's avatar
 
Matthias Langer committed
416
417
            sp_count_i=0
            sp_count_r=0
418
419
420
421
422
423
424
          endif
          write(unitoutgrid) sp_count_i
          write(unitoutgrid) (sparse_dump_i(i),i=1,sp_count_i)
          write(unitoutgrid) sp_count_r
          write(unitoutgrid) (sparse_dump_r(i),i=1,sp_count_r)
!       write(*,*) sp_count_u
!       write(unitoutgrid) (sparse_dump_u(i),i=1,sp_count_r)
Matthias Langer's avatar
 
Matthias Langer committed
425
426
427



428
429
430
431
432
! Concentrations
          sp_count_i=0
          sp_count_r=0
          sp_fact=-1.
          sp_zer=.true.
Matthias Langer's avatar
 
Matthias Langer committed
433
434
435
436
437
438
439
440
441
442
          do kz=1,numzgrid
            do jy=0,numygrid-1
              do ix=0,numxgrid-1
                if (grid(ix,jy,kz).gt.smallnum) then
                  if (sp_zer.eqv..true.) then ! first non zero value
                    sp_count_i=sp_count_i+1
                    sparse_dump_i(sp_count_i)= &
                         ix+jy*numxgrid+kz*numxgrid*numygrid
                    sp_zer=.false.
                    sp_fact=sp_fact*(-1.)
443
444
                  endif
                  sp_count_r=sp_count_r+1
445
446
447
448
449
450
451
452
453
454
455
456
                  if (lparticlecountoutput) then
                    sparse_dump_r(sp_count_r)= &
                         sp_fact* &
                         grid(ix,jy,kz)
                  else
                    sparse_dump_r(sp_count_r)= &
                         sp_fact* &
                         grid(ix,jy,kz)* &
                         factor3d(ix,jy,kz)/tot_mu(ks,kp)
                  end if


457
458
459
460
461
462
!                 if ((factor(ix,jy,kz)/tot_mu(ks,kp)).eq.0)
!    +              write (*,*) factor(ix,jy,kz),tot_mu(ks,kp),ks,kp
!                sparse_dump_u(sp_count_r)=
!+               ,gridsigma(ix,jy,kz,ks,kp,nage)*
!+               factor(ix,jy,kz)/tot_mu(ks,kp)
                else ! concentration is zero
Matthias Langer's avatar
 
Matthias Langer committed
463
                  sp_zer=.true.
464
                endif
Matthias Langer's avatar
 
Matthias Langer committed
465
466
467
              end do
            end do
          end do
468
469
470
471
472
473
          write(unitoutgrid) sp_count_i
          write(unitoutgrid) (sparse_dump_i(i),i=1,sp_count_i)
          write(unitoutgrid) sp_count_r
          write(unitoutgrid) (sparse_dump_r(i),i=1,sp_count_r)
!       write(unitoutgrid) sp_count_u
!       write(unitoutgrid) (sparse_dump_u(i),i=1,sp_count_r)
Matthias Langer's avatar
 
Matthias Langer committed
474
475
476



477
        endif !  concentration output
Matthias Langer's avatar
 
Matthias Langer committed
478

479
480
! Mixing ratio output
!********************
Matthias Langer's avatar
 
Matthias Langer committed
481

482
        if ((iout.eq.2).or.(iout.eq.3)) then      ! mixing ratio
Matthias Langer's avatar
 
Matthias Langer committed
483

484
485
486
487
488
489
490
491
! Wet deposition
          sp_count_i=0
          sp_count_r=0
          sp_fact=-1.
          sp_zer=.true.
          if ((ldirect.eq.1).and.(WETDEP)) then
            do jy=0,numygrid-1
              do ix=0,numxgrid-1
Matthias Langer's avatar
 
Matthias Langer committed
492
493
494
495
496
497
498
                if (wetgrid(ix,jy).gt.smallnum) then
                  if (sp_zer.eqv..true.) then ! first non zero value
                    sp_count_i=sp_count_i+1
                    sparse_dump_i(sp_count_i)= &
                         ix+jy*numxgrid
                    sp_zer=.false.
                    sp_fact=sp_fact*(-1.)
499
500
501
502
503
504
505
506
                  endif
                  sp_count_r=sp_count_r+1
                  sparse_dump_r(sp_count_r)= &
                       sp_fact* &
                       1.e12*wetgrid(ix,jy)/area(ix,jy)
!                sparse_dump_u(sp_count_r)=
!    +            ,1.e12*wetgridsigma(ix,jy,ks,kp,nage)/area(ix,jy)
                else ! concentration is zero
Matthias Langer's avatar
 
Matthias Langer committed
507
                  sp_zer=.true.
508
509
                endif
              end do
Matthias Langer's avatar
 
Matthias Langer committed
510
            end do
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
          else
            sp_count_i=0
            sp_count_r=0
          endif
          write(unitoutgridppt) sp_count_i
          write(unitoutgridppt) (sparse_dump_i(i),i=1,sp_count_i)
          write(unitoutgridppt) sp_count_r
          write(unitoutgridppt) (sparse_dump_r(i),i=1,sp_count_r)
!       write(unitoutgridppt) sp_count_u
!       write(unitoutgridppt) (sparse_dump_u(i),i=1,sp_count_r)


! Dry deposition
          sp_count_i=0
          sp_count_r=0
          sp_fact=-1.
          sp_zer=.true.
          if ((ldirect.eq.1).and.(DRYDEP)) then
            do jy=0,numygrid-1
              do ix=0,numxgrid-1
Matthias Langer's avatar
 
Matthias Langer committed
531
532
533
534
535
536
537
                if (drygrid(ix,jy).gt.smallnum) then
                  if (sp_zer.eqv..true.) then ! first non zero value
                    sp_count_i=sp_count_i+1
                    sparse_dump_i(sp_count_i)= &
                         ix+jy*numxgrid
                    sp_zer=.false.
                    sp_fact=sp_fact*(-1)
538
539
540
541
542
543
544
545
                  endif
                  sp_count_r=sp_count_r+1
                  sparse_dump_r(sp_count_r)= &
                       sp_fact* &
                       1.e12*drygrid(ix,jy)/area(ix,jy)
!                sparse_dump_u(sp_count_r)=
!    +            ,1.e12*drygridsigma(ix,jy,ks,kp,nage)/area(ix,jy)
                else ! concentration is zero
Matthias Langer's avatar
 
Matthias Langer committed
546
                  sp_zer=.true.
547
548
                endif
              end do
Matthias Langer's avatar
 
Matthias Langer committed
549
            end do
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
          else
            sp_count_i=0
            sp_count_r=0
          endif
          write(unitoutgridppt) sp_count_i
          write(unitoutgridppt) (sparse_dump_i(i),i=1,sp_count_i)
          write(unitoutgridppt) sp_count_r
          write(unitoutgridppt) (sparse_dump_r(i),i=1,sp_count_r)
!       write(unitoutgridppt) sp_count_u
!       write(unitoutgridppt) (sparse_dump_u(i),i=1,sp_count_r)


! Mixing ratios
          sp_count_i=0
          sp_count_r=0
          sp_fact=-1.
          sp_zer=.true.
Matthias Langer's avatar
 
Matthias Langer committed
567
568
569
570
571
572
573
574
575
576
          do kz=1,numzgrid
            do jy=0,numygrid-1
              do ix=0,numxgrid-1
                if (grid(ix,jy,kz).gt.smallnum) then
                  if (sp_zer.eqv..true.) then ! first non zero value
                    sp_count_i=sp_count_i+1
                    sparse_dump_i(sp_count_i)= &
                         ix+jy*numxgrid+kz*numxgrid*numygrid
                    sp_zer=.false.
                    sp_fact=sp_fact*(-1.)
577
578
579
580
581
582
583
584
585
586
587
588
                  endif
                  sp_count_r=sp_count_r+1
                  sparse_dump_r(sp_count_r)= &
                       sp_fact* &
                       1.e12*grid(ix,jy,kz) &
                       /volume(ix,jy,kz)/outnum* &
                       weightair/weightmolar(ks)/densityoutgrid(ix,jy,kz)
!                sparse_dump_u(sp_count_r)=
!+              ,1.e12*gridsigma(ix,jy,kz,ks,kp,nage)/volume(ix,jy,kz)/
!+              outnum*weightair/weightmolar(ks)/
!+              densityoutgrid(ix,jy,kz)
                else ! concentration is zero
Matthias Langer's avatar
 
Matthias Langer committed
589
                  sp_zer=.true.
590
                endif
Matthias Langer's avatar
 
Matthias Langer committed
591
592
593
              end do
            end do
          end do
594
595
596
597
598
599
          write(unitoutgridppt) sp_count_i
          write(unitoutgridppt) (sparse_dump_i(i),i=1,sp_count_i)
          write(unitoutgridppt) sp_count_r
          write(unitoutgridppt) (sparse_dump_r(i),i=1,sp_count_r)
!       write(unitoutgridppt) sp_count_u
!       write(unitoutgridppt) (sparse_dump_u(i),i=1,sp_count_r)
Matthias Langer's avatar
 
Matthias Langer committed
600

601
        endif ! output for ppt
Matthias Langer's avatar
 
Matthias Langer committed
602

603
604
      end do
    end do
Matthias Langer's avatar
 
Matthias Langer committed
605
606
607
608
609
610

    close(unitoutgridppt)
    close(unitoutgrid)

  end do

611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
! RLT Aug 2017
! Write out conversion factor for dry air
  inquire(file=path(2)(1:length(2))//'factor_drygrid',exist=lexist)
  if (lexist) then
    ! open and append
    open(unitoutfactor,file=path(2)(1:length(2))//'factor_drygrid',form='unformatted',&
            status='old',action='write',access='append')
  else
    ! create new
    open(unitoutfactor,file=path(2)(1:length(2))//'factor_drygrid',form='unformatted',&
            status='new',action='write')
  endif
  sp_count_i=0
  sp_count_r=0
  sp_fact=-1.
  sp_zer=.true.
  do kz=1,numzgrid
    do jy=0,numygrid-1
      do ix=0,numxgrid-1
        if (factor_drygrid(ix,jy,kz).gt.(1.+smallnum).or.factor_drygrid(ix,jy,kz).lt.(1.-smallnum)) then
          if (sp_zer.eqv..true.) then ! first value not equal to one
            sp_count_i=sp_count_i+1
            sparse_dump_i(sp_count_i)= &
                  ix+jy*numxgrid+kz*numxgrid*numygrid
            sp_zer=.false.
            sp_fact=sp_fact*(-1.)
          endif
          sp_count_r=sp_count_r+1
          sparse_dump_r(sp_count_r)= &
               sp_fact*factor_drygrid(ix,jy,kz)
        else ! factor is one
          sp_zer=.true.
        endif
      end do
    end do
  end do
  write(unitoutfactor) sp_count_i
  write(unitoutfactor) (sparse_dump_i(i),i=1,sp_count_i)
  write(unitoutfactor) sp_count_r
  write(unitoutfactor) (sparse_dump_r(i),i=1,sp_count_r)
  close(unitoutfactor)


Matthias Langer's avatar
 
Matthias Langer committed
654
655
656
657
658
659
  if (gridtotal.gt.0.) gridtotalunc=gridsigmatotal/gridtotal
  if (wetgridtotal.gt.0.) wetgridtotalunc=wetgridsigmatotal/ &
       wetgridtotal
  if (drygridtotal.gt.0.) drygridtotalunc=drygridsigmatotal/ &
       drygridtotal

660
! Dump of receptor concentrations
Matthias Langer's avatar
 
Matthias Langer committed
661

662
663
664
665
666
667
668
  if (numreceptor.gt.0 .and. (iout.eq.2 .or. iout.eq.3)  ) then
    write(unitoutreceptppt) itime
    do ks=1,nspec
      write(unitoutreceptppt) (1.e12*creceptor(i,ks)/outnum* &
           weightair/weightmolar(ks)/densityoutrecept(i),i=1,numreceptor)
    end do
  endif
Matthias Langer's avatar
 
Matthias Langer committed
669

670
! Dump of receptor concentrations
Matthias Langer's avatar
 
Matthias Langer committed
671

672
673
674
675
676
677
678
  if (numreceptor.gt.0) then
    write(unitoutrecept) itime
    do ks=1,nspec
      write(unitoutrecept) (1.e12*creceptor(i,ks)/outnum, &
           i=1,numreceptor)
    end do
  endif
Matthias Langer's avatar
 
Matthias Langer committed
679

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
! RLT Aug 2017
! Write out conversion factor for dry air
  if (numreceptor.gt.0) then
    inquire(file=path(2)(1:length(2))//'factor_dryreceptor',exist=lexist)
     if (lexist) then
     ! open and append
      open(unitoutfactor,file=path(2)(1:length(2))//'factor_dryreceptor',form='unformatted',&
              status='old',action='write',access='append')
    else
      ! create new
      open(unitoutfactor,file=path(2)(1:length(2))//'factor_dryreceptor',form='unformatted',&
              status='new',action='write')
    endif
    write(unitoutfactor) itime
    write(unitoutfactor) (factor_dryrecept(i),i=1,numreceptor)
    close(unitoutfactor)
  endif
Matthias Langer's avatar
 
Matthias Langer committed
697

698
699
! Reinitialization of grid
!*************************
Matthias Langer's avatar
 
Matthias Langer committed
700

701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
! do ks=1,nspec
!   do kp=1,maxpointspec_act
!     do i=1,numreceptor
!       creceptor(i,ks)=0.
!     end do
!     do jy=0,numygrid-1
!       do ix=0,numxgrid-1
!         do l=1,nclassunc
!           do nage=1,nageclass
!             do kz=1,numzgrid
!               gridunc(ix,jy,kz,ks,kp,l,nage)=0.
!             end do
!           end do
!         end do
!       end do
!     end do
!   end do
! end do
719
720
  creceptor(:,:)=0.
  gridunc(:,:,:,:,:,:,:)=0.
Matthias Langer's avatar
 
Matthias Langer committed
721
722
723


end subroutine concoutput