forked from dalesteam/dales
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodsampling.f90
1095 lines (976 loc) · 42.9 KB
/
modsampling.f90
1
2
3
4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> \file modsampling.f90
!! Calculates statistics under conditional criteria
!>
!! Calculates statistics under conditional criteria
!!for instance over updrafts or cloudy parts of the domain. Written to $sampname_fld.expnr and to $sampname_flx.expnr.
!! If netcdf is true, this module also writes in the profiles.expnr.nc output
!! Currently implemented criteria for sampling are:
!! - Updraft (w>0)
!! - Cloud (ql>0)
!! - Buoyant updraft (w>0 and thv>0)
!! - Cloud core (ql>0 and thv>0)
!!
!! \author Thijs Heus, MPI-M
!! \author Pier Siebesma, KNMI
! This file is part of DALES.
!
! DALES is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 3 of the License, or
! (at your option) any later version.
!
! DALES is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
!
! Copyright 1993-2009 Delft University of Technology, Wageningen University, Utrecht University, KNMI
!
module modsampling
use modglobal, only : longint
use modsampdata
implicit none
private
PUBLIC :: initsampling, sampling, exitsampling
save
!NetCDF variables
integer,parameter :: nvar = 32
character(80),allocatable,dimension(:,:,:) :: ncname
character(80),dimension(1,4) :: tncname
integer(kind=longint) :: idtav,itimeav,tnext,tnextwrite
integer :: nsamples,isamp,isamptot
character(20),dimension(10) :: samplname,longsamplname
real, allocatable, dimension(:,:) :: wfavl,thlfavl,thvfavl,qtfavl,qlfavl,nrsampfl,massflxhavl, &
wthlthavl,wthvthavl,wqtthavl,wqlthavl,uwthavl,vwthavl,qrfavl
real, allocatable, dimension(:,:) :: wwrhavl,wwsfavl,pfavl,dwdthavl,dwwdzhavl,dpdzhavl, &
duwdxhavl,dtaudxhavl,dtaudzhavl,thvhavl, &
fcorhavl,nrsamphl
real,allocatable, dimension(:,:) :: wh_el,sigh_el
real,allocatable, dimension(:,:) :: wadvhavl,subphavl
integer,allocatable, dimension(:,:) :: nrtsamphav
character(80) :: fname = 'sampling.xxx.nc'
integer :: ncid,nrec = 0
contains
!> Initialization routine, reads namelists and inits variables
subroutine initsampling
use modmpi, only : comm3d, my_real,mpierr,myid,mpi_logical
use modglobal, only : ladaptive, dtmax,k1,ifnamopt,fname_options,kmax, &
dtav_glob,timeav_glob,btime,tres,cexpnr,ifoutput,lwarmstart,checknamelisterror
use modstat_nc, only : lnetcdf,define_nc,ncinfo,open_nc,define_nc,ncinfo,nctiminfo,writestat_dims_nc
use modgenstat, only : idtav_prof=>idtav, itimeav_prof=>itimeav
implicit none
integer :: ierr
namelist/NAMSAMPLING/ &
dtav,timeav,lsampcl,lsampco,lsampup,lsampbuup,lsampcldup,lsamptend
dtav=dtav_glob;timeav=timeav_glob
if(myid==0)then
open(ifnamopt,file=fname_options,status='old',iostat=ierr)
read (ifnamopt,NAMSAMPLING,iostat=ierr)
call checknamelisterror(ierr, ifnamopt, 'NAMSAMPLING')
write(6 ,NAMSAMPLING)
close(ifnamopt)
end if
call MPI_BCAST(timeav ,1,MY_REAL ,0,comm3d,mpierr)
call MPI_BCAST(dtav ,1,MY_REAL ,0,comm3d,mpierr)
! call MPI_BCAST(lsampall ,1,MPI_LOGICAL,0,comm3d,mpierr)
call MPI_BCAST(lsampcl ,1,MPI_LOGICAL,0,comm3d,mpierr)
call MPI_BCAST(lsampco ,1,MPI_LOGICAL,0,comm3d,mpierr)
call MPI_BCAST(lsampup ,1,MPI_LOGICAL,0,comm3d,mpierr)
call MPI_BCAST(lsampbuup ,1,MPI_LOGICAL,0,comm3d,mpierr)
call MPI_BCAST(lsampcldup,1,MPI_LOGICAL,0,comm3d,mpierr)
call MPI_BCAST(lsamptend, 1,MPI_LOGICAL,0,comm3d,mpierr)
isamptot = 0
if (lsampall) then
isamptot = isamptot + 1
samplname (isamptot) = 'all'
longsamplname(isamptot) = 'All '
endif
if (lsampup) then
isamptot = isamptot + 1
samplname(isamptot) = 'upd'
longsamplname(isamptot) = 'Updraft '
end if
if (lsampbuup) then
isamptot = isamptot + 1
samplname(isamptot) = 'buup'
longsamplname(isamptot) = 'Buoyant Updraft '
end if
if (lsampcl) then
isamptot = isamptot + 1
samplname(isamptot) = 'cld'
longsamplname(isamptot) = 'Cloud '
end if
if (lsampco) then
isamptot = isamptot + 1
samplname(isamptot) = 'cldcr'
longsamplname(isamptot) = 'Cloud Core '
end if
if (lsampcldup) then
isamptot = isamptot + 1
samplname(isamptot) = 'cldup'
longsamplname(isamptot) = 'Cloud Updraft '
end if
if(isamptot < 2) return
idtav = dtav/tres
itimeav = timeav/tres
tnext = idtav +btime
tnextwrite = itimeav +btime
if (abs(timeav/dtav-nint(timeav/dtav))>1e-4) then
stop 'timeav must be a integer multiple of dtav'
end if
if (.not. ladaptive .and. abs(dtav/dtmax-nint(dtav/dtmax))>1e-4) then
stop 'dtav should be a integer multiple of dtmax'
end if
allocate( wfavl (k1,isamptot),thlfavl (k1,isamptot),thvfavl (k1,isamptot), &
qtfavl (k1,isamptot),qlfavl (k1,isamptot),nrsampfl (k1,isamptot), &
wthlthavl (k1,isamptot),wthvthavl (k1,isamptot),wqtthavl (k1,isamptot), &
wqlthavl (k1,isamptot),uwthavl (k1,isamptot),vwthavl (k1,isamptot), &
qrfavl (k1,isamptot))
allocate( nrsamphl (k1,isamptot),wwrhavl (k1,isamptot),wwsfavl (k1,isamptot), &
pfavl (k1,isamptot),dwdthavl (k1,isamptot),dwwdzhavl (k1,isamptot), &
dpdzhavl (k1,isamptot),duwdxhavl(k1,isamptot),dtaudxhavl(k1,isamptot), &
dtaudzhavl(k1,isamptot),thvhavl (k1,isamptot),fcorhavl (k1,isamptot))
allocate (wh_el (k1,isamptot),sigh_el (k1,isamptot),massflxhavl(k1,isamptot))
allocate (wadvhavl (k1,isamptot))
allocate (subphavl (k1,isamptot))
allocate (nrtsamphav (k1,isamptot))
!initialize variables
nrsampfl = 0.0
wfavl = 0.0
thlfavl = 0.0
thvfavl = 0.0
qtfavl = 0.0
qlfavl = 0.0
massflxhavl = 0.0
wthlthavl = 0.0
wthvthavl = 0.0
wqtthavl = 0.0
wqlthavl = 0.0
uwthavl = 0.0
vwthavl = 0.0
qrfavl = 0.0
nrsamphl = 0.0
wwrhavl = 0.0
wwsfavl = 0.0
pfavl = 0.0
dwdthavl = 0.0
dwwdzhavl = 0.0
dpdzhavl = 0.0
duwdxhavl = 0.0
dtaudxhavl = 0.0
dtaudzhavl = 0.0
thvhavl = 0.0
fcorhavl = 0.0
wh_el = 0.0
sigh_el = 0.0
wadvhavl = 0.0
subphavl = 0.0
nrtsamphav = 0
if(myid==0 .and. .not. lwarmstart) then
do isamp = 1,isamptot
open (ifoutput,file=trim(samplname(isamp))//'wbudg.'//cexpnr,status='replace')
close (ifoutput)
open (ifoutput,file=trim(samplname(isamp))//'fld.'//cexpnr,status='replace')
close (ifoutput)
open (ifoutput,file=trim(samplname(isamp))//'flx.'//cexpnr,status='replace')
close (ifoutput)
enddo
endif
if (lnetcdf) then
idtav = idtav_prof
itimeav = itimeav_prof
tnext = idtav+btime
tnextwrite = itimeav+btime
nsamples = itimeav/idtav
if (myid==0) then
allocate(ncname(nvar,4,isamptot))
call nctiminfo(tncname(1,:))
fname(10:12) = cexpnr
call open_nc(fname,ncid,nrec,n3=kmax)
call define_nc(ncid,1,tncname)
call writestat_dims_nc(ncid)
do isamp=1,isamptot
call ncinfo(ncname( 1,:,isamp),'nrsamp'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'number of points','-','tt')
call ncinfo(ncname( 2,:,isamp),'w'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'mean vertical velocity','m/s','mt')
call ncinfo(ncname( 3,:,isamp),'thl'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'mean liquid water potential temperature','K','tt')
call ncinfo(ncname( 4,:,isamp),'qt'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'mean total water content','kg/kg','tt')
call ncinfo(ncname( 5,:,isamp),'ql'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'mean liquid water content','kg/kg','tt')
call ncinfo(ncname( 6,:,isamp),'thv'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'mean virtual potential temperature','K','tt')
call ncinfo(ncname( 7,:,isamp),'massflx'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'mass flux','m^3/s','mt')
call ncinfo(ncname( 8,:,isamp),'wthl'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'theta_l flux','K m/s','mt')
call ncinfo(ncname( 9,:,isamp),'wqt'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'total water flux','kg/kg m/s','mt')
call ncinfo(ncname(10,:,isamp),'wql'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'liquid water flux','kg/kg m/s','mt')
call ncinfo(ncname(11,:,isamp),'wthv'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'theta_v flux','K m/s','mt')
call ncinfo(ncname(12,:,isamp),'uw'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'uw flux','m^2/s^2','mt')
call ncinfo(ncname(13,:,isamp),'vw'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'vw flux','m^2/s^2','mt')
call ncinfo(ncname(14,:,isamp),'nrsamph'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'number of points at halflevel','-','mt')
call ncinfo(ncname(15,:,isamp),'pf'//samplname(isamp),&
trim(longsamplname(isamp))//' '// 'pressure','kg/m/s^2','tt')
call ncinfo(ncname(16,:,isamp),'wwrh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'ww res flux','m^2/s^2','mt')
call ncinfo(ncname(17,:,isamp),'wwsf'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'ww sub flux','m^2/s^2','mt')
call ncinfo(ncname(18,:,isamp),'dwdth'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'dwdt at sampled point','m/s^2','mt')
call ncinfo(ncname(19,:,isamp),'buoyh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'buoyancy force','m/s^2','mt')
call ncinfo(ncname(20,:,isamp),'dpdzh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'vertical pressure force','m/s^2','mt')
call ncinfo(ncname(21,:,isamp),'dwwdzh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'resolved vertical w advection','m/s^2','mt')
call ncinfo(ncname(22,:,isamp),'duwdxh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'resolved horizontal w advection','m/s^2','mt')
call ncinfo(ncname(23,:,isamp),'dtaudzh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'sg vertical w advection','m/s^2','mt')
call ncinfo(ncname(24,:,isamp),'dtaudxh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'sg horizontal w advection','m/s^2','mt')
call ncinfo(ncname(25,:,isamp),'fcorh'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'coriolis force on w','m/s^2','mt')
call ncinfo(ncname(26,:,isamp),'resid'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'residual term in sampled budget eqn','m/s^2','mt')
call ncinfo(ncname(27,:,isamp),'whend'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'ws at end of sampling period','m/s','mt')
call ncinfo(ncname(28,:,isamp),'sighend'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'sigma at end of period','-','mt')
call ncinfo(ncname(29,:,isamp),'qrsamp'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'sampled qr','kg/kg','tt')
call ncinfo(ncname(30,:,isamp),'wadvhavl'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'instantaneous d/dz (w_s)^2','m/s^2','mt')
call ncinfo(ncname(31,:,isamp),'subphavl'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'subplume term in ww budget','m/s^2','mt')
call ncinfo(ncname(32,:,isamp),'nrtsamphav'//samplname(isamp),&
trim(longsamplname(isamp))//' '//'sampling fraction for gradients','-','mt')
call define_nc( ncid, NVar, ncname(:,:,isamp))
end do
end if
end if
end subroutine initsampling
!> Cleans up after the run
subroutine exitsampling
use modstat_nc, only : lnetcdf
use modmpi, only : myid
implicit none
if(isamptot < 2) return
deallocate( wfavl ,thlfavl ,thvfavl ,qtfavl ,qlfavl ,nrsampfl ,massflxhavl, &
wthlthavl,wthvthavl,wqtthavl,wqlthavl ,uwthavl ,vwthavl ,qrfavl)
deallocate( nrsamphl,wwrhavl ,wwsfavl , &
pfavl ,dwdthavl,dwwdzhavl,dpdzhavl,duwdxhavl,dtaudxhavl,dtaudzhavl, &
thvhavl ,fcorhavl,wh_el,sigh_el)
deallocate(wadvhavl,subphavl,nrtsamphav)
if (lnetcdf .and. myid==0) deallocate(ncname)
end subroutine exitsampling
!> General routine, does the timekeeping
subroutine sampling
use modglobal, only : rk3step,timee,dt_lim
implicit none
if (isamptot<2) return
if (rk3step/=3) return
if(timee<tnext .and. timee<tnextwrite) then
dt_lim = minval((/dt_lim,tnext-timee,tnextwrite-timee/))
return
end if
if (timee>=tnext) then
tnext = tnext+idtav
do isamp = 1,isamptot
call dosampling
end do
end if
if (timee>=tnextwrite) then
tnextwrite = tnextwrite+itimeav
call writesampling
end if
dt_lim = minval((/dt_lim,tnext-timee,tnextwrite-timee/))
return
end subroutine sampling
!> Performs the actual sampling
subroutine dosampling
use modglobal, only : i1,i2,j1,j2,kmax,k1,ih,jh,&
dx,dy,dzh,dzf,cp,rv,rlv,rd,ijtot, &
grav,om22,cu,nsv,zh
use modfields, only : u0,v0,w0,thl0,thl0h,qt0,qt0h,ql0,ql0h,thv0h,exnf,exnh,rhobf,rhobh,thvh, &
sv0,wp_store
use modsubgriddata,only : ekh,ekm
use modmpi, only : slabsum,my_real,mpi_integer,comm3d,mpierr,mpi_sum
use modpois, only : p
use modmicrodata, only : imicro, imicro_bulk, imicro_bin, imicro_sice,iqr
implicit none
logical, allocatable, dimension(:,:,:) :: maskf
real, allocatable, dimension(:,:,:) :: wthlth,wqtth,wqlth,wthvth,uwth,vwth
real, allocatable, dimension(:,:,:) :: w0f
real, allocatable, dimension(:,:,:) :: thv0
real, allocatable, dimension(:) :: thvav
logical, allocatable, dimension(:,:,:) :: maskh
real, allocatable, dimension(:,:,:) :: uwsh,vwsh,uwrh,vwrh,wwrh,wwsf
real, allocatable, dimension(:) :: thvhav
real, allocatable, dimension(:) :: whav0l,wwthav0l ! needed for instantaneous mean values
real, allocatable, dimension(:) :: whav0 ,wwthav0 ,wwshav0 ,sigh0
integer, allocatable, dimension (:) :: nrsamph0l, nrsamph0
integer :: i,j,k,km,kp
real :: cqt,cthl,den,ekhalf,c2,c1,t0h,qs0h,ekav
real :: wthvsh,wthvrh,wthlsh,wthlrh,wqtrh,wqtsh,wqlrh,wqls
allocate(thvav(k1))
allocate( maskf(2-ih:i1+ih,2-jh:j1+jh,k1))
allocate( wthlth(2-ih:i1+ih,2-jh:j1+jh,k1),&
wthvth(2-ih:i1+ih,2-jh:j1+jh,k1),&
wqtth(2-ih:i1+ih,2-jh:j1+jh,k1),&
wqlth(2-ih:i1+ih,2-jh:j1+jh,k1),&
uwth (2-ih:i1+ih,2-jh:j1+jh,k1),&
vwth (2-ih:i1+ih,2-jh:j1+jh,k1))
allocate( thv0 (2-ih:i1+ih,2-jh:j1+jh,k1),&
w0f (2-ih:i1+ih,2-jh:j1+jh,k1)) !Change to 3D array
allocate(maskh (2-ih:i1+ih,2-jh:j1+jh,k1)) !New
allocate(uwsh (2-ih:i1+ih,2-jh:j1+jh,k1),&
vwsh (2-ih:i1+ih,2-jh:j1+jh,k1),&
uwrh (2-ih:i1+ih,2-jh:j1+jh,k1),&
vwrh (2-ih:i1+ih,2-jh:j1+jh,k1),&
wwrh (2-ih:i1+ih,2-jh:j1+jh,k1),&
wwsf (2-ih:i1+ih,2-jh:j1+jh,k1))
allocate(thvhav(k1))
allocate(nrsamph0l(k1), &
whav0l (k1), &
wwthav0l (k1))
allocate(nrsamph0 (k1), &
whav0 (k1), &
wwthav0 (k1), &
wwshav0 (k1), &
sigh0 (k1))
nrsamph0l = 0 !reset
whav0l = 0.
wwthav0l = 0.
do k=1,k1
thv0(2:i1,2:j1,k) = (thl0(2:i1,2:j1,k)+rlv*ql0(2:i1,2:j1,k)/(cp*exnf(k))) &
*(1+(rv/rd-1)*qt0(2:i1,2:j1,k)-rv/rd*ql0(2:i1,2:j1,k))
enddo
do k=1,kmax
w0f (2:i1,2:j1,k) = 0.5*(w0 (2:i1,2:j1,k) + w0 (2:i1,2:j1,k+1))
end do
maskf = .false.
maskh= .false.
thvav = 0.0
call slabsum(thvav,1,k1,thv0,2-ih,i1+ih,2-jh,j1+jh,1,k1,2,i1,2,j1,1,k1)
thvav = thvav/ijtot
thvhav = 0.0
call slabsum(thvhav,1,k1,thv0h,2-ih,i1+ih,2-jh,j1+jh,1,k1,2,i1,2,j1,1,k1)
thvhav = thvhav/ijtot
select case (samplname(isamp))
case ('upd')
do i=2,i1
do j=2,j1
do k=1,kmax
if (w0f(i,j,k).gt.0.) then
maskf(i,j,k) = .true.
endif
if (w0(i,j,k).gt.0.) then
maskh(i,j,k) = .true.
endif
enddo
enddo
enddo
case ('buup')
do i=2,i1
do j=2,j1
do k=1,kmax
if (w0f(i,j,k)>0.0.and.thv0 (i,j,k) > thvav(k)) then
maskf(i,j,k) = .true.
endif
if (w0(i,j,k).gt.0.0.and.thv0h(i,j,k) > thvhav(k)) then
maskh(i,j,k) = .true.
endif
enddo
enddo
enddo
case ('cld')
do i=2,i1
do j=2,j1
do k=1,kmax
if (ql0(i,j,k)>epsilon(1.0)) then
maskf(i,j,k) = .true.
endif
if (ql0h(i,j,k)>epsilon(1.0)) then
maskh(i,j,k) = .true.
endif
enddo
enddo
enddo
case ('cldcr')
do i=2,i1
do j=2,j1
do k=1,kmax
if (ql0(i,j,k)>epsilon(1.0).and.thv0(i,j,k) > thvav(k)) then
maskf(i,j,k) = .true.
endif
if (ql0h(i,j,k)>epsilon(1.0).and.thv0h(i,j,k) > thvhav(k)) then
maskh(i,j,k) = .true.
endif
enddo
enddo
enddo
case ('cldup')
do i=2,i1
do j=2,j1
do k=1,kmax
if (ql0(i,j,k)>epsilon(1.0).and.w0f(i,j,k).gt.0.) then
maskf(i,j,k) = .true.
endif
if (ql0h(i,j,k)>epsilon(1.0).and.w0(i,j,k).gt.0.) then
maskh(i,j,k) = .true.
endif
enddo
enddo
enddo
case ('all')
maskf = .true.
maskh = .true.
end select
!calculate fluxes at half levels
wthlth = 0.0
wthvth = 0.0
wqtth = 0.0
wqlth = 0.0
uwth = 0.0
vwth = 0.0
uwsh = 0.0
vwsh = 0.0
uwrh = 0.0
vwrh = 0.0
wwrh = 0.0
wwsf = 0.0
do j=2,j1
do i=2,i1
do k=2,kmax
km = k-1
kp = k+1
! -----------------------------------------------------------
! calculate prefactors c1 and c2 for subgrid wthv and cthl
! cqt for wql fluxes at half levels
! -----------------------------------------------------------
qs0h = (qt0h(i,j,k) - ql0h(i,j,k))
t0h = exnh(k)*thl0h(i,j,k) + (rlv/cp)*ql0h(i,j,k)
den = 1. + (rlv**2)*qs0h/(rv*cp*(t0h**2))
cthl = (exnh(k)*cp/rlv)*((1-den)/den)
cqt = (1./den)
if (ql0h(i,j,k)>0) then
c1 = (1.-qt0h(i,j,k)+rv/rd*qs0h*(1.+rlv/(rv*t0h)))/den
c2 = c1*rlv/(t0h*cp)-1.
else
c1 = 1. + (rv/rd-1)*qt0h(i,j,k)
c2 = rv/rd-1
end if
! -----------------------------------------------------------
! calculate resolved and subgrid fluxes at half levels
! -----------------------------------------------------------
ekhalf = (ekh(i,j,k) *dzf(km)+ekh(i,j,km)*dzf(k))/(2*dzh(k) )
wthlsh = -ekhalf*(thl0(i,j,k)-thl0(i,j,km))/dzh(k)
wthlrh = w0(i,j,k)*thl0h(i,j,k)
wthlth (i,j,k) = wthlsh + wthlrh
wqtsh = -ekhalf*(qt0(i,j,k)-qt0(i,j,km))/dzh(k)
wqtrh = w0(i,j,k)*qt0h(i,j,k)
wqtth(i,j,k) = wqtsh + wqtrh
if (ql0h(i,j,k)>0) then
wqls = cthl*wthlsh+ cqt*wqtsh
else
wqls = 0.
end if
wqlrh = w0(i,j,k)*ql0h(i,j,k)
wqlth(i,j,k) = wqlrh + wqls
wthvsh = c1*wthlsh + c2*thl0h(i,j,k)*wqtsh
wthvrh = w0(i,j,k)*thv0h(i,j,k)
wthvth(i,j,k) = wthvrh + wthvsh
ekav = 0.25 * &
( ekm(i,j,k-1)+ekm(i,j,k)+ekm(i-1,j,k-1)+ekm(i-1,j,k))
uwsh(i,j,k) = - ekav * &
((u0(i,j,k)-u0(i,j,k-1))/dzf(k)+(w0(i,j,k)-w0(i-1,j,k))/dx)
uwrh(i,j,k) = (w0(i,j,k)+w0(i-1,j,k))*(u0(i,j,k-1)+u0(i,j,k))/4.
uwth(i,j,k) = uwsh(i,j,k) + uwrh(i,j,k)
ekav = 0.25 * &
( ekm(i,j,k-1)+ekm(i,j,k)+ekm(i,j-1,k-1)+ekm(i,j-1,k))
vwsh(i,j,k) = - ekav * &
((v0(i,j,k)-v0(i,j,k-1))/dzf(k)+(w0(i,j,k)-w0(i,j-1,k))/dy)
vwrh(i,j,k) = (w0(i,j,k)+w0(i,j-1,k))*(v0(i,j,k-1)+v0(i,j,k))/4.
vwth(i,j,k) = vwsh(i,j,k) + vwrh(i,j,k)
wwrh (i,j,k) = w0(i,j,k)**2
end do
end do
end do
do k=2,kmax
i=i2
do j=2,j1
ekav = 0.25 * &
( ekm(i,j,k-1)+ekm(i,j,k)+ekm(i-1,j,k-1)+ekm(i-1,j,k))
uwsh(i,j,k) = - ekav * &
((u0(i,j,k)-u0(i,j,k-1))/dzf(k)+(w0(i,j,k)-w0(i-1,j,k))/dx)
uwrh (i,j,k) = (w0(i,j,k)+w0(i-1,j,k))*(u0(i,j,k-1)+u0(i,j,k))/4.
end do
j=j2
do i=2,i1
ekav = 0.25 * &
( ekm(i,j,k-1)+ekm(i,j,k)+ekm(i,j-1,k-1)+ekm(i,j-1,k))
vwsh (i,j,k) = - ekav * &
((v0(i,j,k)-v0(i,j,k-1))/dzf(k)+(w0(i,j,k)-w0(i,j-1,k))/dy)
vwrh (i,j,k) = (w0(i,j,k)+w0(i,j-1,k))*(v0(i,j,k-1)+v0(i,j,k))/4.
end do
end do
!
! full levels
!
do j=2,j1
do i=2,i1
do k=2,kmax
km = k-1
wwsf (i,j,km) = -2 * ekm(i,j,km) * &
(w0(i,j,k)-w0(i,j,km)) / dzf(km)
end do
end do
end do
!add fields and fluxes to mean
! 1) fields on full levels
do k=1,kmax
nrsampfl(k,isamp) = nrsampfl(k,isamp)+count(maskf(2:i1,2:j1,k))
nrsamphl(k,isamp) = nrsamphl(k,isamp)+count(maskh(2:i1,2:j1,k))
wfavl (k,isamp) = wfavl (k,isamp)+sum (w0f (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
thlfavl (k,isamp) = thlfavl (k,isamp)+sum (thl0 (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
thvfavl (k,isamp) = thvfavl (k,isamp)+sum (thv0 (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
qtfavl (k,isamp) = qtfavl (k,isamp)+sum (qt0 (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
qlfavl (k,isamp) = qlfavl (k,isamp)+sum (ql0 (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
pfavl (k,isamp) = pfavl (k,isamp)+sum (p (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
wwsfavl (k,isamp) = wwsfavl (k,isamp)+sum (wwsf (2:i1,2:j1,k),maskf(2:i1,2:j1,k))
end do
if(nsv>1) then
do k=1,kmax
do j=2,j1
do i=2,i2
if(maskf(i,j,k).eqv..true.) then
qrfavl (k,isamp) = qrfavl (k,isamp)+sv0(i,j,k,iqr)
end if
end do
end do
end do
else
do k=1,kmax
qrfavl (k,isamp) = 0.
end do
end if
! 2) fluxes on half levels
do k=2,kmax
massflxhavl(k,isamp) = massflxhavl(k,isamp) + sum(w0 (2:i1,2:j1,k),maskh(2:i1,2:j1,k))
wthlthavl (k,isamp) = wthlthavl (k,isamp) + sum(wthlth(2:i1,2:j1,k),maskh(2:i1,2:j1,k))
wthvthavl (k,isamp) = wthvthavl (k,isamp) + sum(wthvth(2:i1,2:j1,k),maskh(2:i1,2:j1,k))
wqtthavl (k,isamp) = wqtthavl (k,isamp) + sum(wqtth(2:i1,2:j1,k),maskh(2:i1,2:j1,k))
wqlthavl (k,isamp) = wqlthavl (k,isamp) + sum(wqlth(2:i1,2:j1,k),maskh(2:i1,2:j1,k))
uwthavl (k,isamp) = uwthavl (k,isamp) + sum(uwth (2:i1,2:j1,k),maskh(2:i1,2:j1,k))
vwthavl (k,isamp) = vwthavl (k,isamp) + sum(vwth (2:i1,2:j1,k),maskh(2:i1,2:j1,k))
wwrhavl (k,isamp) = wwrhavl (k,isamp) + sum(wwrh (2:i1,2:j1,k),maskh(2:i1,2:j1,k))
! there may be some numerical noise in wp_store due to the Runge Kutta time integration scheme
dwdthavl (k,isamp) = dwdthavl (k,isamp) + sum(wp_store(2:i1,2:j1,k),maskh(2:i1,2:j1,k))
wh_el (k,isamp) = sum(w0 (2:i1,2:j1,k),maskh(2:i1,2:j1,k))
sigh_el (k,isamp) = count(maskh(2:i1,2:j1,k))
nrsamph0l (k) = count(maskh(2:i1,2:j1,k))
whav0l (k) = sum(w0 (2:i1,2:j1,k),maskh(2:i1,2:j1,k))
if((imicro==imicro_sice).or.(imicro==imicro_bulk).or.(imicro==imicro_bin)) then
do j=2,j1
do i=2,i1
if (maskh(i,j,k)) then
thvhavl (k,isamp) = thvhavl (k,isamp) + grav*(thv0h(i,j,k) - thvh(k))/thvh(k) &
- grav*(sv0(i,j,k,iqr)+sv0(i,j,k-1,iqr))/2.
dpdzhavl (k,isamp) = dpdzhavl (k,isamp) - (p(i,j,k)-p(i,j,k-1))/dzh(k)
dwwdzhavl (k,isamp) = dwwdzhavl (k,isamp) - (1./rhobh(k))*(rhobf(k)*w0f(i,j,k)**2-rhobf(k-1)*w0f(i,j,k-1)**2)/dzh(k)
duwdxhavl (k,isamp) = duwdxhavl (k,isamp) - (uwrh(i+1,j,k) - uwrh (i,j,k))/dx - &
(vwrh(i,j+1,k) - vwrh (i,j,k))/dy
dtaudxhavl(k,isamp) = dtaudxhavl(k,isamp) - (uwsh(i+1,j,k) - uwsh (i,j,k))/dx - &
(vwsh(i,j+1,k) - vwsh (i,j,k))/dy
dtaudzhavl(k,isamp) = dtaudzhavl(k,isamp) - (1./rhobh(k))*(rhobf(k)*wwsf(i,j,k) - rhobf(k-1)*wwsf(i,j,k-1))/dzh(k)
fcorhavl (k,isamp) = fcorhavl(k,isamp) + om22 * cu &
+( (dzf(k-1) * (u0(i,j,k) + u0(i+1,j,k) ) &
+ dzf(k) * (u0(i,j,k-1) + u0(i+1,j,k-1)) ) / dzh(k) ) &
* om22*0.25
wwthav0l (k) = wwthav0l (k) + (wwsf(i,j,k) + wwsf(i,j,k-1)) / 2. + wwrh (i,j,k)
endif
end do
end do
else
do j=2,j1
do i=2,i1
if (maskh(i,j,k)) then
thvhavl (k,isamp) = thvhavl (k,isamp) + grav*(thv0h(i,j,k) - thvh(k))/thvh(k)
dpdzhavl (k,isamp) = dpdzhavl (k,isamp) - (p(i,j,k)-p(i,j,k-1))/dzh(k)
dwwdzhavl (k,isamp) = dwwdzhavl (k,isamp) - (1./rhobh(k))*(rhobf(k)*w0f(i,j,k)**2-rhobf(k-1)*w0f(i,j,k-1)**2)/dzh(k)
duwdxhavl (k,isamp) = duwdxhavl (k,isamp) - (uwrh(i+1,j,k) - uwrh (i,j,k))/dx - &
(vwrh(i,j+1,k) - vwrh (i,j,k))/dy
dtaudxhavl(k,isamp) = dtaudxhavl(k,isamp) - (uwsh(i+1,j,k) - uwsh (i,j,k))/dx - &
(vwsh(i,j+1,k) - vwsh (i,j,k))/dy
dtaudzhavl(k,isamp) = dtaudzhavl(k,isamp) - (1./rhobh(k))*(rhobf(k)*wwsf(i,j,k) - rhobf(k-1)*wwsf(i,j,k-1))/dzh(k)
fcorhavl (k,isamp) = fcorhavl(k,isamp) + om22 * cu &
+( (dzf(k-1) * (u0(i,j,k) + u0(i+1,j,k) ) &
+ dzf(k) * (u0(i,j,k-1) + u0(i+1,j,k-1)) ) / dzh(k) ) &
* om22*0.25
wwthav0l (k) = wwthav0l (k) + (wwsf(i,j,k) + wwsf(i,j,k-1)) / 2. + wwrh (i,j,k)
endif
end do
end do
end if
enddo
call MPI_ALLREDUCE(nrsamph0l ,nrsamph0 ,k1,MPI_INTEGER,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(whav0l ,whav0 ,k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wwthav0l ,wwthav0 ,k1,MY_REAL,MPI_SUM,comm3d,mpierr)
do k=1,kmax
if (nrsamph0 (k).gt.0) then
sigh0 (k) = nrsamph0 (k) / ijtot
whav0 (k) = whav0 (k) / nrsamph0 (k)
wwthav0 (k) = wwthav0(k) / nrsamph0 (k)
wwshav0 (k) = wwthav0(k) - whav0(k)**2
endif
enddo
do k=2,kmax-1
if (nrsamph0(k-1) * nrsamph0(k) * nrsamph0(k+1).ne.0) then
nrtsamphav (k,isamp) = nrtsamphav (k,isamp) + 1
! do calculation like other *avl variables
wadvhavl (k,isamp) = wadvhavl(k,isamp) &
- 0.5/rhobf(k) * (rhobf(k+1)*whav0 (k+1)**2 - rhobf(k-1)*whav0 (k-1)**2)&
/ (zh(k+1) - zh(k-1)) * nrsamph0l(k)
subphavl (k,isamp) = subphavl (k,isamp) &
- (1./rhobf(k))*(sigh0(k+1) * rhobf(k+1)*wwshav0(k+1) - rhobf(k-1)*sigh0(k-1)*wwshav0(k-1))&
/ (zh(k+1) - zh(k-1)) / sigh0 (k) * nrsamph0l(k)
endif
enddo
deallocate(maskf,wthlth,wqtth,wqlth,wthvth,uwth,vwth,thvav,w0f,thv0)
deallocate(maskh,uwsh,vwsh,uwrh,vwrh)
deallocate(wwrh,wwsf,thvhav)
deallocate(whav0, whav0l)
deallocate(wwthav0,wwthav0l)
deallocate(wwshav0)
deallocate(nrsamph0, nrsamph0l)
deallocate(sigh0)
end subroutine dosampling
!> Write the statistics to file
subroutine writesampling
use modglobal, only : rtimee,k1,kmax,zf,zh,cexpnr,ifoutput,ijtot
use modfields, only : presf,presh
use modmpi, only : myid,my_real,comm3d,mpierr,mpi_sum
use modstat_nc, only: lnetcdf, writestat_nc,nc_fillvalue
implicit none
real,dimension(k1,nvar) :: vars
real, allocatable, dimension(:) :: wfmn,thlfmn,thvfmn,qtfmn,qlfmn,nrsampfmn,massflxhmn, &
wthlthmn,wthvthmn,wqtthmn,wqlthmn,uwthmn,vwthmn,qrfmn
real, allocatable, dimension(:,:):: wfav,thlfav,thvfav,qtfav,qlfav,nrsampf,massflxhav, &
wthlthav,wthvthav,wqtthav,wqlthav,uwthav,vwthav,qrfav
real, allocatable, dimension(:) :: nrsamphmn, wwrhmn,wwsfmn,&
pfmn,dwdthmn,dwwdzhmn,dpdzhmn,duwdxhmn,&
dtaudxhmn,dtaudzhmn,thvhmn, &
fcorhmn
real, allocatable, dimension(:,:):: nrsamph,wwrhav,wwsfav, &
pfav,dwdthav,dwwdzhav,dpdzhav,duwdxhav,&
dtaudxhav,dtaudzhav,thvhav,&
fcorhav,wh_e,sigh_e
real, allocatable, dimension(:):: wadvhmn,subphmn
real, allocatable, dimension(:,:):: wadvhav,subphav
integer :: nsecs, nhrs, nminut, k
integer :: inorm
allocate( wfmn(k1),thlfmn(k1),thvfmn(k1),qtfmn(k1),qlfmn(k1),nrsampfmn(k1),massflxhmn(k1), &
wthlthmn(k1),wthvthmn(k1),wqtthmn(k1),wqlthmn(k1),uwthmn(k1),vwthmn(k1),qrfmn(k1))
allocate( wfav(k1,isamptot),thlfav(k1,isamptot),thvfav(k1,isamptot), &
qtfav(k1,isamptot),qlfav(k1,isamptot),nrsampf(k1,isamptot),massflxhav(k1,isamptot), &
wthlthav(k1,isamptot),wthvthav(k1,isamptot),wqtthav(k1,isamptot), &
wqlthav(k1,isamptot),uwthav(k1,isamptot),vwthav(k1,isamptot),qrfav(k1,isamptot))
allocate (nrsamphmn(k1),wwrhmn(k1),wwsfmn(k1))
allocate( pfmn(k1),dwdthmn(k1),dwwdzhmn(k1),dpdzhmn(k1),duwdxhmn(k1),&
dtaudxhmn(k1),dtaudzhmn(k1),thvhmn(k1),fcorhmn(k1))
allocate( nrsamph(k1,isamptot),wwrhav(k1,isamptot),wwsfav(k1,isamptot))
allocate( pfav(k1,isamptot),dwdthav(k1,isamptot),dwwdzhav(k1,isamptot),&
dpdzhav(k1,isamptot),duwdxhav(k1,isamptot),&
dtaudxhav(k1,isamptot),dtaudzhav(k1,isamptot),thvhav(k1,isamptot),fcorhav(k1,isamptot))
allocate (wh_e(k1,isamptot),sigh_e(k1,isamptot))
allocate (wadvhav(k1,isamptot),wadvhmn(k1))
allocate (subphav(k1,isamptot),subphmn(k1))
nsecs = nint(rtimee)
nhrs = int(nsecs/3600)
nminut = int(nsecs/60)-nhrs*60
nsecs = mod(nsecs,60)
inorm = nint(ijtot*timeav/dtav)
call MPI_ALLREDUCE(nrsampfl ,nrsampf ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wfavl ,wfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(thlfavl ,thlfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(thvfavl ,thvfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(qtfavl ,qtfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(qlfavl ,qlfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(massflxhavl,massflxhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wthlthavl ,wthlthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wthvthavl ,wthvthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wqtthavl ,wqtthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wqlthavl ,wqlthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(uwthavl ,uwthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(vwthavl ,vwthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(qrfavl ,qrfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(nrsamphl ,nrsamph ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wwrhavl ,wwrhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wwsfavl ,wwsfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(pfavl ,pfav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(dwdthavl ,dwdthav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(dwwdzhavl ,dwwdzhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(dpdzhavl ,dpdzhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(duwdxhavl ,duwdxhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(dtaudxhavl ,dtaudxhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(dtaudzhavl ,dtaudzhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(thvhavl ,thvhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(fcorhavl ,fcorhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wh_el ,wh_e ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(sigh_el ,sigh_e ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(wadvhavl ,wadvhav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
call MPI_ALLREDUCE(subphavl ,subphav ,isamptot*k1,MY_REAL,MPI_SUM,comm3d,mpierr)
!reset variables
nrsampfl = 0.0
wfavl = 0.0
thlfavl = 0.0
thvfavl = 0.0
qtfavl = 0.0
qlfavl = 0.0
massflxhavl = 0.0
wthlthavl = 0.0
wthvthavl = 0.0
wqtthavl = 0.0
wqlthavl = 0.0
uwthavl = 0.0
vwthavl = 0.0
qrfavl = 0.0
nrsamphl = 0.0
wwrhavl = 0.0
wwsfavl = 0.0
pfavl = 0.0
dwdthavl = 0.0
dwwdzhavl = 0.0
dpdzhavl = 0.0
duwdxhavl = 0.0
dtaudxhavl = 0.0
dtaudzhavl = 0.0
thvhavl = 0.0
fcorhavl = 0.0
wh_el = 0.
sigh_el = 0.
wadvhavl = 0.
subphavl = 0.
if (myid==0) then
if (lnetcdf) then
call writestat_nc(ncid,1,tncname,(/rtimee/),nrec,.true.)
endif
do isamp = 1,isamptot
wfmn = 0.
thlfmn = 0.
thvfmn = 0.
qtfmn = 0.
qlfmn = 0.
pfmn = 0.
qrfmn = 0.
dwdthmn = 0.
dwwdzhmn = 0.
dpdzhmn = 0.
duwdxhmn = 0.
dtaudxhmn= 0.
dtaudzhmn= 0.
thvhmn = 0.
fcorhmn = 0.
wwrhmn = 0.
wwsfmn = 0.
wadvhmn = 0.
subphmn = 0.
!normalize variables
do k=1,kmax
if (nrsampf(k,isamp)>0) then
wfmn (k) = wfav (k,isamp)/nrsampf(k,isamp)
thlfmn (k) = thlfav(k,isamp)/nrsampf(k,isamp)
thvfmn (k) = thvfav(k,isamp)/nrsampf(k,isamp)
qtfmn (k) = qtfav (k,isamp)/nrsampf(k,isamp)
qlfmn (k) = qlfav (k,isamp)/nrsampf(k,isamp)
qrfmn (k) = qrfav (k,isamp)/nrsampf(k,isamp)
wwsfmn (k) = wwsfav (k,isamp)/nrsampf(k,isamp)
endif
if (sigh_e(k,isamp).gt.0) then
wh_e (k,isamp) = wh_e (k,isamp) / sigh_e(k,isamp)
sigh_e(k,isamp) = sigh_e(k,isamp) / ijtot
endif
if (nrsamph(k,isamp).gt.0) then
pfmn (k) = pfav (k,isamp)/nrsamph(k,isamp)
dwdthmn (k) = dwdthav (k,isamp)/nrsamph(k,isamp)
dwwdzhmn (k) = dwwdzhav (k,isamp)/nrsamph(k,isamp)
dpdzhmn (k) = dpdzhav (k,isamp)/nrsamph(k,isamp)
duwdxhmn (k) = duwdxhav (k,isamp)/nrsamph(k,isamp)
dtaudxhmn(k) = dtaudxhav (k,isamp)/nrsamph(k,isamp)
dtaudzhmn(k) = dtaudzhav (k,isamp)/nrsamph(k,isamp)
thvhmn (k) = thvhav (k,isamp)/nrsamph(k,isamp)
fcorhmn (k) = fcorhav (k,isamp)/nrsamph(k,isamp)
wwrhmn (k) = wwrhav (k,isamp)/nrsamph(k,isamp)
wadvhmn (k) = wadvhav (k,isamp)/nrsamph(k,isamp)
subphmn (k) = subphav (k,isamp)/nrsamph(k,isamp)
endif
nrsampfmn (k) = nrsampf (k,isamp)/inorm
massflxhmn (k) = massflxhav(k,isamp)/inorm
wthlthmn (k) = wthlthav (k,isamp)/inorm
wthvthmn (k) = wthvthav (k,isamp)/inorm
wqtthmn (k) = wqtthav (k,isamp)/inorm
wqlthmn (k) = wqlthav (k,isamp)/inorm
uwthmn (k) = uwthav (k,isamp)/inorm
vwthmn (k) = vwthav (k,isamp)/inorm
nrsamphmn (k) = nrsamph (k,isamp)/inorm
enddo
!write files
open (ifoutput,file=trim(samplname(isamp))//'fld.'//cexpnr,position='append')
write(ifoutput,'(//3A,/A,F5.0,A,I4,A,I2,A,I2,A)') &
'#------------------------------ ',trim(longsamplname(isamp)),' -------------------------------' &
,'#',timeav,'--- AVERAGING TIMESTEP --- ' &
,nhrs,':',nminut,':',nsecs &
,' HRS:MIN:SEC AFTER INITIALIZATION '
write (ifoutput,'(2A/2A)') &
'#------------------------------------------------------' &
,'------------------------------' &
,'# LEV HGHT_F HGHT_H PRES COV_F COV_H W THL QT ' &
,'QL THV P WW_RES_H WW_SUB_F'
do k=1,kmax
write(ifoutput,'(i5,2F8.0,F7.1,2F10.5,5F11.5,E14.5,2F14.5)') &
k, &
zf (k), &
zh (k), &
presf (k)/100., &
nrsampfmn(k), &
nrsamphmn(k),&
wfmn (k), &
thlfmn (k), &
qtfmn (k)*1000., &
qlfmn (k)*1000., &
thvfmn (k),&
pfmn (k), &
wwrhmn (k),&
wwsfmn (k)
end do
close(ifoutput)
open (ifoutput,file=trim(samplname(isamp))//'flx.'//cexpnr,position='append')
write(ifoutput,'(//3A,/A,F5.0,A,I4,A,I2,A,I2,A)') &
'#------------------------------- ',trim(longsamplname(isamp)),' ----------------------------------------' &
,'#',timeav,'--- AVERAGING TIMESTEP --- ' &
,nhrs,':',nminut,':',nsecs &
,' HRS:MIN:SEC AFTER INITIALIZATION '
write (ifoutput,'(2A/2A)') &
'#------------------------------------------------------' &
,'------------------------------' &
,'# LEV HGHT PRES AW WTHL ' &
,'WQT WQL WTHV UW VW'
do k=1,kmax
write(ifoutput,'(i5,F8.0,F7.1,7E16.8)') &
k, &
zh (k), &
presh (k)/100., &
massflxhmn (k), &
wthlthmn (k), &
wqtthmn (k), &
wqlthmn (k), &
wthvthmn (k), &
uwthmn (k), &
vwthmn (k)
end do
close(ifoutput)
open (ifoutput,file=trim(samplname(isamp))//'wbudg.'//cexpnr,position='append')
write(ifoutput,'(//3A,/A,F5.0,A,I4,A,I2,A,I2,A)') &
'#------------------------------ ',trim(samplname(isamp)),' -------------------------------' &
,'#',timeav,'--- AVERAGING TIMESTEP --- ' &
,nhrs,':',nminut,':',nsecs &