-
Notifications
You must be signed in to change notification settings - Fork 153
/
setupw.f90
1894 lines (1661 loc) · 74.7 KB
/
setupw.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
module w_setup
implicit none
private
public:: setup
interface setup; module procedure setupw; end interface
contains
!-------------------------------------------------------------------------
! NOAA/NCEP, National Centers for Environmental Prediction GSI !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: setupw --- Compute rhs of oi for wind component obs
!
! !INTERFACE:
!
subroutine setupw(obsLL,odiagLL,lunin,mype,bwork,awork,nele,nobs,is,conv_diagsave)
! !USES:
use mpeu_util, only: die,perr,getindex
use state_vectors, only: svars3d, levels
use kinds, only: r_kind,r_single,r_double,i_kind
use m_obsdiagNode, only: obs_diag
use m_obsdiagNode, only: obs_diags
use m_obsdiagNode, only: obsdiagLList_nextNode
use m_obsdiagNode, only: obsdiagNode_set
use m_obsdiagNode, only: obsdiagNode_get
use m_obsdiagNode, only: obsdiagNode_assert
use obsmod, only: rmiss_single,perturb_obs,oberror_tune,lobsdiag_forenkf,&
lobsdiagsave,nobskeep,lobsdiag_allocated,&
time_offset,bmiss,ianldate,aircraft_recon
use m_obsNode, only: obsNode
use m_wNode, only: wNode
use m_wNode, only: wNode_appendto
use m_wNode, only: wNode_ich0
use m_wNode, only: wNode_ich0_PBL_pseudo
use m_obsLList, only: obsLList
use obsmod, only: luse_obsdiag
use obsmod, only: netcdf_diag, binary_diag, dirname
use obsmod, only: l_obsprvdiag
use obsmod, only: neutral_stability_windfact_2dvar,use_similarity_2dvar
use nc_diag_write_mod, only: nc_diag_init, nc_diag_header, nc_diag_metadata, &
nc_diag_write, nc_diag_data2d
use nc_diag_read_mod, only: nc_diag_read_init, nc_diag_read_get_dim, nc_diag_read_close
use gsi_4dvar, only: nobs_bins,hr_obsbin,min_offset
use qcmod, only: npres_print,ptop,pbot,dfact,dfact1,qc_satwnds,njqc,vqc
use qcmod, only: nvqc
use oneobmod, only: oneobtest,oneob_type,magoberr,maginnov
use gridmod, only: get_ijk,nsig,twodvar_regional,regional,wrf_nmm_regional,&
rotate_wind_xy2ll,pt_ll,fv3_regional
use guess_grids, only: nfldsig,hrdifsig,geop_hgtl,sfcmod_gfs
use guess_grids, only: tropprs,sfcmod_mm5
use guess_grids, only: ges_lnprsl,comp_fact10,pbl_height
use constants, only: zero,half,one,tiny_r_kind,two, &
three,rd,grav,four,five,huge_single,r1000,wgtlim,r10,r400
use constants, only: grav_ratio,flattening,deg2rad, &
grav_equator,somigliana,semi_major_axis,eccentricity
use jfunc, only: jiter,last,jiterstart,miter
use convinfo, only: nconvtype,cermin,cermax,cgross,cvar_b,cvar_pg,ictype
use convinfo, only: icsubtype,ibeta,ikapa
use converr_uv, only: ptabl_uv
use converr, only: ptabl
use rapidrefresh_cldsurf_mod, only: l_PBL_pseudo_SurfobsUV, pblH_ration,pps_press_incr
use rapidrefresh_cldsurf_mod, only: l_closeobs, i_gsdqc
use m_dtime, only: dtime_setup, dtime_check
use gsi_bundlemod, only : gsi_bundlegetpointer
use hdraobmod, only: nhduv,hduvlist
use gsi_metguess_mod, only : gsi_metguess_get,gsi_metguess_bundle
use sparsearr, only: sparr2, new, size, writearray, fullarray
use aux2dvarflds, only: rtma_comp_fact10
! The following variables are the coefficients that describe the
! linear regression fits that are used to define the dynamic
! observation error (DOE) specifications for all reconnissance
! observations collected within hurricanes/tropical cyclones; these
! apply only to the regional forecast models (e.g., HWRF); Henry
! R. Winterbottom ([email protected]).
use obsmod, only: uv_doe_a_236,uv_doe_a_237,uv_doe_b_236,uv_doe_b_237
implicit none
type(obsLList ),target,dimension(:),intent(in):: obsLL
type(obs_diags),target,dimension(:),intent(in):: odiagLL
! !INPUT PARAMETERS:
integer(i_kind) ,intent(in ) :: lunin ! unit from which to read observations
integer(i_kind) ,intent(in ) :: mype ! mpi task id
integer(i_kind) ,intent(in ) :: nele ! number of data elements per observation
integer(i_kind) ,intent(in ) :: nobs ! number of observations
integer(i_kind) ,intent(in ) :: is ! ndat index
logical ,intent(in ) :: conv_diagsave ! logical to save innovation dignostics
! !INPUT/OUTPUT PARAMETERS:
real(r_kind),dimension(npres_print,nconvtype,5,3),intent(inout) :: bwork ! obs-ges stats
real(r_kind),dimension(100+7*nsig) ,intent(inout) :: awork ! data counts and gross checks
!
! !DESCRIPTION: For wind component observations, this routine
! \begin{enumerate}
! \item reads obs assigned to given mpi task (geographic region),
! \item simulates obs from guess,
! \item apply some quality control to obs,
! \item load weight and innovation arrays used in minimization
! \item collects statistics for runtime diagnostic output
! \item writes additional diagnostic information to output file
! \end{enumerate}
!
! !REVISION HISTORY:
!
! 1990-10-06 parrish
! 1998-04-10 weiyu yang
! 1999-03-01 wu - ozone processing moved into setuprhs from setupoz
! 1999-08-24 derber, j., treadon, r., yang, w., first frozen mpp version
! 2004-06-17 treadon - update documentation
! 2004-07-15 todling - protex-compliant prologue; added intent/only's
! 2004-10-06 parrish - increase size of vwork array for nonlinear qc
! 2004-11-22 derber - remove weight, add logical for boundary point
! 2004-12-22 treadon - move logical conv_diagsave from obsmod to argument list
! 2005-03-02 dee - remove garbage from diagnostic file
! 2005-03-09 parrish - nonlinear qc change to account for inflated obs error
! 2005-05-27 derber - level output change
! 2005-07-22 jung - add modis winds
! 2005-07-27 derber - add print of monitoring and reject data
! 2005-09-28 derber - combine with prep,spr,remove tran and clean up
! 2005-10-14 derber - input grid location and fix regional lat/lon
! 2005-10-21 su - modified variational qc and diagnose output
! 2005-11-03 treadon - correct error in ilone,ilate data array indices
! 2005-11-22 wu - add option to perturb conventional obs
! 2005-11-29 derber - remove psfcg and use ges_lnps instead
! 2006-01-13 treadon - correct bugs in modis wind qc
! 2006-01-31 todling - storing wgt/wgtlim in diag file instead of wgt only
! 2006-02-02 treadon - rename lnprsl as ges_lnprsl
! 2006-02-08 treadon - correct vertical dimension (nsig) in call tintrp2a(ges_tv...)
! 2006-02-15 treadon - use height when processing type 223, 224, 229 winds
! 2006-02-24 derber - modify to take advantage of convinfo module
! 2006-03-21 treadon - modify optional perturbation to observation
! 2006-04-03 derber - fix bugs and move all surface data to height calculation
! 2006-05-30 su,derber,treadon - modify diagnostic output
! 2006-06-06 su - move to wgtlim to constants module
! 2006-07-28 derber - modify to use new inner loop obs data structure
! - modify handling of multiple data at same location
! 2006-07-31 kleist - use ges_ps instead of ln(ps)
! 2006-08-28 su - fix a bug in variational qc
! 2006-11-30 jung/sienkiewicz - add type 259 for modis winds
! 2006-10-28 su - turn off rawinsonde Vqc at south hemisphere
! 2007-03-09 su - modify observation pertabation for adjusting obs error
! 2007-03-19 tremolet - binning of observations
! 2007-03-27 li.bi - add qc for type 289 windsat winds
! 2007-06-05 tremolet - add observation diagnostics structure
! 2007-08-28 su - modify observation gross check error
! 2008-03-24 wu - oberror tuning and perturb obs
! 2008-03-31 li.bi - add qc for type 290 ascat winds
! 2008-05-20 safford - rm unused vars
! 2008-09-08 lueken - merged ed's changes into q1fy09 code
! 2008-12-03 todling - changed handle of tail%time
! 2009-08-19 guo - changed for multi-pass setup with dtime_check().
! 2009-02-06 pondeca - for each observation site, add the following to the
! diagnostic file: local terrain height, dominate surface
! type, station provider name, and station subprovider name
! 2010-11-25 su - data items to hold quality mark for satellite wind
! 2011-03-08 parrish - for regional=.true., convert wind components in rdiagbuf from grid relative
! to earth relative, using subroutine rotate_wind_xy2ll.
! 2011-05-05 su - ome quality control for satellite satellite winds
! 2012-01-10 hu - add additional quality control for PBL profiler 223, 224, 227
! 2011-12-14 wu - add code for rawinsonde level enhancement ( ext_sonde )
! 2012-07-19 todling - add qc_satwnds flag to allow bypass QC-satwinds (QC not good for GMAO)
! 2011-10-14 Hu - add code for producing pseudo-obs in PBL
! layer based on surface obs UV
! 2013-01-08 Su -add more quality control for satellite winds and profiler winds
! 2013-01-26 parrish - change grdcrd to grdcrd1, intrp2a to intrp2a11, tintrp2a to tintrp2a1, tintrp2a11,
! tintrp3 to tintrp31 (so debug compile works on WCOSS)
! 2013-02-15 parrish - WCOSS debug runtime error--ikx outside range 1 to nconvtype. Add counter
! num_bad_ikx and print 1st 10 instances of ikx out of range
! and also print num_bad_ikx after all data processed if > 0 .
! 2013-05-24 wu - move rawinsonde level enhancement ( ext_sonde ) to read_prepbufr
! 2013-07-19 Hu/Olson/Carley - Add tall tower (type=261) winds
! 2013-10-19 todling - metguess now holds background
! 2014-01-28 todling - write sensitivity slot indicator (ioff) to header of diagfile
! 2014-04-12 su - add non linear qc from Purser's scheme
! 2014-12-30 derber - Modify for possibility of not using obsdiag
! 2015-05-01 Liu Ling - Added ISS Rapidscat wind (u,v) qc
! 2015-03-14 Nebuda - add departure check and near surface check for clear air WV AMV (WVCS) from GOES type 247
! 2015-10-01 guo - full res obvsr: index to allow redistribution of obsdiags
! 2015-12-21 yang - Parrish's correction to the previous code in new varqc.
! 2016-05-18 guo - replaced ob_type with polymorphic obsNode through type casting
! 2016-06-24 guo - fixed the default value of obsdiags(:,:)%tail%luse to luse(i)
! . removed (%dlat,%dlon) debris.
! 2016-11-29 shlyaeva - save linearized H(x) for EnKF
! 2016-12-09 mccarty - add netcdf_diag capability
! 2016-12-13 pondeca - add Tyndall & Horel QC for mesonet winds (WAF 2013, Vol. 8, pg. 285) to GSI's 2dvar option
! 2017-03-31 Hu - addd option l_closeobs to use closest obs to analysis
! time in analysis
! 2017-02-09 guo - Remove m_alloc, n_alloc.
! . Remove my_node with corrected typecast().
! 2018-04-09 pondeca - introduce duplogic to correctly handle the characterization of
! duplicate obs in twodvar_regional applications
! 2019-06-07 levine/pondeca - for twodvar_regional applications, switch from processing surface obs
! with reported pressure to surface obs with reported geometric height.
! This is mainly to account for mesonets with sensor height < 10 m.
! 2019-07-12 levine - introduce logic to read in mesonet wind sensor heights from prepbufr
! rather than assuming sensor height is 10 m AGL.
! 2019-08-12 zhang/levine/pondeca - add option to adjust 10-m bckg wind with the help of similarity
! theory in twodvar_regional applications
! 2019-09-20 Su - remove current VQC part and add subroutine call on VQC
! 2019-09-25 Su - put hibert curve on aircraft data
! 2020-01-27 Winterbottom - moved the linear regression derived
! coefficients for the dynamic observation
! error (DOE) calculation to the namelist
! level; they are now loaded by
! aircraftinfo.
! 2020-05-04 wu - no rotate_wind for fv3_regional
! 2021-07-25 Genkova - write AMVQ in diagnostic files
! 2021-10-xx pondeca/morris/zhao - added observation provider/subprovider
! information in diagonostic file, which is used
! in offline observation quality control program (AutoObsQC)
! for 3D-RTMA (if l_obsprvdiag is true).
!
! REMARKS:
! language: f90
! machine: ibm RS/6000 SP; SGI Origin 2000; Compaq HP
!
! !AUTHOR: parrish org: np22 date: 1990-10-06
!
!EOP
!-------------------------------------------------------------------------
! Declare local parameters
real(r_kind),parameter:: r0_7=0.7_r_kind
real(r_kind),parameter:: r0_1=1.0_r_kind
real(r_kind),parameter:: r6=6.0_r_kind
real(r_kind),parameter:: r7=7.0_r_kind
real(r_kind),parameter:: r15=15.0_r_kind
real(r_kind),parameter:: r20=20.0_r_kind
real(r_kind),parameter:: r50=50.0_r_kind
real(r_kind),parameter:: r200=200.0_r_kind
real(r_kind),parameter:: r360=360.0_r_kind
real(r_kind),parameter:: r0_1_bmiss=0.1_r_kind*bmiss
character(len=*),parameter:: myname='setupw'
! Declare external calls for code analysis
external:: intrp2a11,tintrp2a1,tintrp2a11
external:: tintrp31
external:: grdcrd1
external:: stop2
! Declare local variables
real(r_double) rstation_id
real(r_kind) qcu,qcv,trop5,tfact,fact
real(r_kind) scale,ratio,obserror,obserrlm
real(r_kind) residual,ressw,ress,vals,val2,dudiff,dvdiff,rat_err2u
real(r_kind) valqc,valu,valv,dx10,rlow,rhgh,drpx,prsfc,var_jb,rat_err2v
real(r_kind) cg_t,cvar,wgt,term,qcgross,valqcu,valqcv
real(r_kind) presw,factw,dpres,ugesin,vgesin,rwgt,dpressave
real(r_kind) sfcchk,prsln2,error,dtime,dlon,dlat,r0_001,rsig,thirty,rsigp
real(r_kind) ratio_errors,goverrd,spdges,spdob,ten,psges,zsges
real(r_kind) slat,sin2,termg,termr,termrg,pobl,uob,vob
real(r_kind) uob_reg,vob_reg,uob_e,vob_e,dlon_e,uges_e,vges_e,dudiff_e,dvdiff_e
real(r_kind) dz,zob,z1,z2,p1,p2,dz21,dlnp21,spdb,dstn
real(r_kind) errinv_input,errinv_adjst,errinv_final
real(r_kind) err_input,err_adjst,err_final,skint,sfcr
real(r_kind) dudiff_opp, dvdiff_opp, vecdiff, vecdiff_opp
real(r_kind) dudiff_opp_rs, dvdiff_opp_rs, vecdiff_rs, vecdiff_opp_rs
real(r_kind) oscat_vec,ascat_vec,rapidscat_vec
real(r_kind),dimension(nele,nobs):: data
real(r_kind),dimension(nobs):: dup
real(r_kind),dimension(nsig)::prsltmp,tges,zges
real(r_kind) wdirob,wdirgesin,wdirdiffmax
real(r_kind),dimension(34)::ptabluv
real(r_single),allocatable,dimension(:,:)::rdiagbuf
integer(i_kind) i,nchar,nreal,k,j,l,ii,itype,ijb
! Variables needed for new polar winds QC based on Log Normalized Vector Departure (LNVD)
real(r_kind) LNVD_wspd
real(r_kind) LNVD_omb
real(r_kind) LNVD_ratio
real(r_kind) LNVD_threshold
integer(i_kind) jsig,mm1,iptrbu,iptrbv,jj,icat
integer(i_kind) k1,k2,ikxx,nn,isli,ibin,ioff,ioff0
integer(i_kind) ier,ilon,ilat,ipres,iuob,ivob,id,itime,ikx,ielev,iqc
integer(i_kind) ihgt,ier2,iuse,ilate,ilone
integer(i_kind) izz,iprvd,isprvd
integer(i_kind) idomsfc,isfcr,iskint,iff10
integer(i_kind) ibb,ikk,ihil,idddd,iamvq
integer(i_kind) num_bad_ikx,iprev_station
character(8) station_id
character(8),allocatable,dimension(:):: cdiagbuf
character(8),allocatable,dimension(:):: cprvstg,csprvstg
character(8) c_prvstg,c_sprvstg
real(r_double) r_prvstg,r_sprvstg
type(sparr2) :: dhx_dx_u, dhx_dx_v
integer(i_kind) :: iz, u_ind, v_ind, nnz, nind
real(r_kind) :: delz
logical z_height,sfc_data
logical,dimension(nobs):: luse,muse
logical:: muse_u,muse_v
integer(i_kind),dimension(nobs):: ioid ! initial (pre-distribution) obs ID
logical lowlevelsat,duplogic
logical msonetob
logical proceed
logical:: l_pbl_pseudo_itype
integer(i_kind):: ich0
logical:: in_curbin, in_anybin, save_jacobian
type(wNode),pointer :: my_head
type(obs_diag),pointer :: jj_diag
type(obs_diag),pointer :: my_diagu, my_diagu_pbl
type(obs_diag),pointer :: my_diagv, my_diagv_pbl
type(obs_diags),pointer :: my_diagLL
real(r_kind) :: thisPBL_height,ratio_PBL_height,prest,prestsfc,dudiffsfc,dvdiffsfc
real(r_kind) :: hr_offset
real(r_kind) :: magomb
equivalence(rstation_id,station_id)
equivalence(r_prvstg,c_prvstg)
equivalence(r_sprvstg,c_sprvstg)
real(r_kind),allocatable,dimension(:,:,: ) :: ges_ps
real(r_kind),allocatable,dimension(:,:,: ) :: ges_z
real(r_kind),allocatable,dimension(:,:,:,:) :: ges_u
real(r_kind),allocatable,dimension(:,:,:,:) :: ges_v
real(r_kind),allocatable,dimension(:,:,:,:) :: ges_tv
type(obsLList),pointer,dimension(:):: whead
whead => obsLL(:)
save_jacobian = conv_diagsave .and. jiter==jiterstart .and. lobsdiag_forenkf
! Check to see if required guess fields are available
call check_vars_(proceed)
if(.not.proceed) return ! not all vars available, simply return
! If require guess vars available, extract from bundle ...
call init_vars_
!******************************************************************************
! Read and reformat observations in work arrays.
spdb=zero
read(lunin)data,luse,ioid
! index information for data array (see reading routine)
ier=1 ! index of obs error
ilon=2 ! index of grid relative obs location (x)
ilat=3 ! index of grid relative obs location (y)
ipres=4 ! index of pressure
ihgt=5 ! index of height
iuob=6 ! index of u observation
ivob=7 ! index of v observation
id=8 ! index of station id
itime=9 ! index of observation time in data array
ikxx=10 ! index of ndex ob type in convinfo file
ielev=11 ! index of station elevation
iqc=12 ! index of quality mark
ier2=13 ! index of original-original obs error ratio
iuse=14 ! index of use parameter
idomsfc=15 ! index of dominant surface type
iskint=16 ! index of surface skin temperature
iff10=17 ! index of 10 meter wind factor
isfcr=18 ! index of surface roughness
ilone=19 ! index of longitude (degrees)
ilate=20 ! index of latitude (degrees)
izz=21 ! index of surface height
iprvd=22 ! index of observation provider
isprvd=23 ! index of observation subprovider
icat=24 ! index of data level category
ijb=25 ! index of non linear qc parameter
ihil=26 ! index of hilbert curve weight
iamvq=27 ! index of AMVQ
iptrbu=28 ! index of u perturbation
iptrbv=29 ! index of v perturbation
mm1=mype+1
scale=one
rsig=nsig
thirty = 30.0_r_kind
ten = 10.0_r_kind
r0_001=0.001_r_kind
rsigp=rsig+one
goverrd=grav/rd
var_jb=zero
! If requested, save select data for output to diagnostic file
if(conv_diagsave)then
ii=0
nchar=1
ioff0=26
nreal=ioff0
if (lobsdiagsave) nreal=nreal+7*miter+2
if (twodvar_regional .or. l_obsprvdiag) then
nreal=nreal+2 ! account for idomsfc,izz
allocate(cprvstg(nobs),csprvstg(nobs)) ! obs provider info
endif
if (save_jacobian) then
nnz = 2 ! number of non-zero elements in dH(x)/dx profile
nind = 1
call new(dhx_dx_u, nnz, nind)
call new(dhx_dx_v, nnz, nind)
nreal = nreal + 2*size(dhx_dx_u)
endif
allocate(cdiagbuf(nobs),rdiagbuf(nreal,nobs))
if (netcdf_diag) call init_netcdf_diag_
end if
num_bad_ikx=0
do i=1,nobs
muse(i)=nint(data(iuse,i)) <= jiter
ikx=nint(data(ikxx,i))
if(ikx < 1 .or. ikx > nconvtype) then
num_bad_ikx=num_bad_ikx+1
if(num_bad_ikx<=10) write(6,*)' in setupw ',ikx,i,nconvtype,mype
end if
end do
! If HD raobs available move prepbufr version to monitor
if(nhduv > 0)then
iprev_station=0
do i=1,nobs
ikx=nint(data(ikxx,i))
itype=ictype(ikx)
if(itype == 220 .or. itype == 221) then
rstation_id = data(id,i)
read(station_id,'(i5,3x)',err=1200) idddd
if(idddd == iprev_station)then
data(iuse,i)=108._r_kind
muse(i) = .false.
else
stn_loop:do j=1,nhduv
if(idddd == hduvlist(j))then
iprev_station=idddd
data(iuse,i)=108._r_kind
muse(i) = .false.
exit stn_loop
end if
end do stn_loop
end if
end if
1200 continue
end do
end if
! handle multiple-report observations at a station
hr_offset=min_offset/60.0_r_kind
dup=one
do k=1,nobs
do l=k+1,nobs
if (twodvar_regional) then
duplogic=data(ilat,k) == data(ilat,l) .and. &
data(ilon,k) == data(ilon,l) .and. &
data(ier,k) < r1000 .and. data(ier,l) < r1000 .and. &
muse(k) .and. muse(l)
else
duplogic=data(ilat,k) == data(ilat,l) .and. &
data(ilon,k) == data(ilon,l) .and. &
data(ipres,k) == data(ipres,l) .and. &
data(ier,k) < r1000 .and. data(ier,l) < r1000 .and. &
muse(k) .and. muse(l)
end if
if (duplogic) then
if(l_closeobs) then
if(abs(data(itime,k)-hr_offset)<abs(data(itime,l)-hr_offset)) then
muse(l)=.false.
else
muse(k)=.false.
endif
! write(*,'(a,2f10.5,2I8,2L10)') 'chech wind obs time==',&
! data(itime,k)-hr_offset,data(itime,l)-hr_offset,k,l,&
! muse(k),muse(l)
else
tfact=min(one,abs(data(itime,k)-data(itime,l))/dfact1)
dup(k)=dup(k)+one-tfact*tfact*(one-dfact)
dup(l)=dup(l)+one-tfact*tfact*(one-dfact)
endif
end if
end do
end do
call dtime_setup()
num_bad_ikx=0
loop_for_all_obs: &
do i=1,nobs
dtime=data(itime,i)
call dtime_check(dtime, in_curbin, in_anybin)
if(.not.in_anybin) cycle
ikx=nint(data(ikxx,i))
if(in_curbin) then
dlat=data(ilat,i)
dlon=data(ilon,i)
rstation_id = data(id,i)
error=data(ier2,i)
var_jb=data(ijb,i)
if(ikx < 1 .or. ikx > nconvtype) then
num_bad_ikx=num_bad_ikx+1
if(num_bad_ikx<=10) write(6,*)' in setupw, bad ikx, ikx,i,nconvtype=',ikx,i,nconvtype,mype
cycle
end if
isli = data(idomsfc,i)
endif
if(ikx < 1 .or. ikx > nconvtype) cycle
itype=ictype(ikx)
! Link observation to appropriate observation bin
if (nobs_bins>1) then
ibin = NINT( dtime/hr_obsbin ) + 1
else
ibin = 1
endif
IF (ibin<1.OR.ibin>nobs_bins) write(6,*)mype,'Error nobs_bins,ibin= ',nobs_bins,ibin
! Link obs to diagnostics structure
if (luse_obsdiag) my_diagLL => odiagLL(ibin)
! Flag static conditions to turn pbl_pseudo_surfobs on
l_pbl_pseudo_itype = l_PBL_pseudo_SurfobsUV .and. &
( itype==281 .or. itype==283 .or.itype==287 )
if (luse_obsdiag) then
my_diagu => null()
my_diagv => null()
my_diagu_pbl => null()
my_diagv_pbl => null()
ich0 = wNode_ich0
if(l_pbl_pseudo_itype) ich0 = wNode_ich0_pbl_pseudo
do jj=1,ich0+2 ! "2" for there are u and v components
jj_diag => obsdiagLList_nextNode(my_diagLL, &
create = .not.lobsdiag_allocated ,&
idv = is ,&
iob = ioid(i) ,&
ich = jj ,&
elat = data(ilate,i) ,&
elon = data(ilone,i) ,&
luse = luse(i) ,&
miter = miter )
if(.not.associated(jj_diag)) then
call perr(myname,'obsdiagLList_nextNode(), create =',.not.lobsdiag_allocated)
call perr(myname,' ich0 =',ich0)
call perr(myname,' jj =',jj)
call die(myname)
endif
select case(jj)
case(1); my_diagu => jj_diag
case(2); my_diagv => jj_diag
case(3); my_diagu_pbl => jj_diag
case(4); my_diagv_pbl => jj_diag
end select
enddo
endif
if(.not.in_curbin) cycle
! Load observation error and values into local variables
obserror = max(cermin(ikx),min(cermax(ikx),data(ier,i)))
uob = data(iuob,i)
vob = data(ivob,i)
spdob=sqrt(uob*uob+vob*vob)
call tintrp2a11(ges_ps,psges,dlat,dlon,dtime,hrdifsig,&
mype,nfldsig)
call tintrp2a1(ges_lnprsl,prsltmp,dlat,dlon,dtime,hrdifsig,&
nsig,mype,nfldsig)
! Type 221=pibal winds contain a mixture of wind observations reported
! by pressure and others by height. Those levels only reported by
! pressure have a missing value (ie, large) value for the reported
! height. The logic below determines whether to process type 221
! wind observations using height or pressure as the vertical coordinate.
! If height is not bad (less than r0_1_bmiss), we use height in the
! forward model. Otherwise, use reported pressure.
z_height = .false.
if ((itype>=221 .and. itype <= 229) .and. (data(ihgt,i)<r0_1_bmiss)) z_height = .true.
if ((itype==261) .and. (data(ihgt,i)<r0_1_bmiss)) z_height = .true.
if (itype == 218) z_height = .true.
! Process observations reported with height differently than those
! reported with pressure. Type 223=profiler and 224=vadwnd are
! encoded in NCEP prepbufr files with geometric height above
! sea level. Type 229=pibal profiler is reported using
! geopotenital height. Some type 221=pibal wind observations are
! also repoted using geopotential height.
sfc_data = (itype >=280 .and. itype < 300)
if (z_height .or. sfc_data) then
drpx = zero
dpres = data(ihgt,i)
dstn = data(ielev,i)
call tintrp2a11(ges_z,zsges,dlat,dlon,dtime,hrdifsig,&
mype,nfldsig)
! Subtract off combination of surface station elevation and
! model elevation depending on how close to surface
fact = zero
if(dpres-dstn > 10._r_kind)then
if(dpres-dstn > r1000)then
fact = one
else
fact=(dpres-dstn)/990._r_kind
end if
end if
dpres=dpres-(dstn+fact*(zsges-dstn))
if(itype==261) dpres = data(ihgt,i)
! Get guess surface elevation and geopotential height profile
! at observation location.
call tintrp2a1(geop_hgtl,zges,dlat,dlon,dtime,hrdifsig,&
nsig,mype,nfldsig)
! For observation reported with geometric height above sea level,
! convert geopotential to geometric height.
if (((itype>=223 .and. itype<=228) .or. itype == 218 .or. sfc_data) .and. .not.twodvar_regional) then
! Convert geopotential height at layer midpoints to geometric
! height using equations (17, 20, 23) in MJ Mahoney's note
! "A discussion of various measures of altitude" (2001).
! Available on the web at
! http://mtp.jpl.nasa.gov/notes/altitude/altitude.html
!
! termg = equation 17
! termr = equation 21
! termrg = first term in the denominator of equation 23
! zges = equation 23
slat = data(ilate,i)*deg2rad
sin2 = sin(slat)*sin(slat)
termg = grav_equator * &
((one+somigliana*sin2)/sqrt(one-eccentricity*eccentricity*sin2))
termr = semi_major_axis /(one + flattening + grav_ratio - &
two*flattening*sin2)
termrg = (termg/grav)*termr
do k=1,nsig
zges(k) = (termr*zges(k)) / (termrg-zges(k)) ! eq (23)
end do
else if (twodvar_regional) then
zges(1) = ten
endif
! Given observation height, (1) adjust 10 meter wind factor if
! necessary, (2) convert height to grid relative units, (3) compute
! compute observation pressure (for diagnostic purposes only), and
! (4) compute location of midpoint of first model layer above surface
! in grid relative units
! Adjust 10m wind factor if necessary. Rarely do we have a
! profiler/vad obs within 10 meters of the surface. Almost always,
! the code below resets the 10m wind factor to 1.0 (i.e., no
! reduction in wind speed due to surface friction).
! Convert observation height (in dpres) from meters to grid relative
! units. Save the observation height in zob for later use.
zob = dpres
call grdcrd1(dpres,zges,nsig,1)
! Interpolate guess u and v to observation location and time.
call tintrp31(ges_u,ugesin,dlat,dlon,dpres,dtime, &
hrdifsig,mype,nfldsig)
call tintrp31(ges_v,vgesin,dlat,dlon,dpres,dtime, &
hrdifsig,mype,nfldsig)
iz = max(1, min( int(dpres), nsig))
delz = max(zero, min(dpres - float(iz), one))
if (save_jacobian) then
u_ind = getindex(svars3d, 'u')
if (u_ind < 0) then
print *, 'Error: no variable u in state vector. Exiting.'
call stop2(1300)
endif
v_ind = getindex(svars3d, 'v')
if (v_ind < 0) then
print *, 'Error: no variable v in state vector. Exiting.'
call stop2(1300)
endif
dhx_dx_u%st_ind(1) = iz + sum(levels(1:u_ind-1))
dhx_dx_u%end_ind(1) = min(iz + 1,nsig) + sum(levels(1:u_ind-1))
dhx_dx_v%st_ind(1) = iz + sum(levels(1:v_ind-1))
dhx_dx_v%end_ind(1) = min(iz + 1,nsig) + sum(levels(1:v_ind-1))
dhx_dx_u%val(1) = one - delz
dhx_dx_u%val(2) = delz
dhx_dx_v%val = dhx_dx_u%val
endif
msonetob=itype==288.or.itype==295
if (zob <= zero .and. twodvar_regional) zob=ten !trap for stations with negative zob
if (zob > zges(1)) then
factw=one
else
factw = data(iff10,i)
if(sfcmod_gfs .or. sfcmod_mm5) then
sfcr = data(isfcr,i)
skint = data(iskint,i)
call comp_fact10(dlat,dlon,dtime,skint,sfcr,isli,mype,factw)
end if
if (zob <= ten) then
if(zob < ten)then
if (msonetob .and. twodvar_regional .and. use_similarity_2dvar) then
if (neutral_stability_windfact_2dvar) then
sfcr = data(isfcr,i)
factw=log(max(sfcr,zob)/sfcr)/log(ten/sfcr)
else
sfcr = data(isfcr,i)
skint = data(iskint,i)
call rtma_comp_fact10(dlat,dlon,dtime,zob,skint,sfcr,isli,mype,factw)
endif
else
term = max(zob,zero)/ten
factw = term*factw
endif
end if
else
term = (zges(1)-zob)/(zges(1)-ten)
factw = one-term+factw*term
end if
ugesin=factw*ugesin
vgesin=factw*vgesin
if (save_jacobian) then
dhx_dx_u%val = factw * dhx_dx_u%val
dhx_dx_v%val = factw * dhx_dx_v%val
endif
endif
if(sfc_data .or. dpres < one) then
drpx=0.005_r_kind*abs(dstn-zsges)*(one-fact)
end if
! Compute observation pressure (only used for diagnostics)
if (twodvar_regional) then
presw = ten*exp(data(ipres,i))
else
! Set indices of model levels below (k1) and above (k2) observation.
if (dpres<one) then
z1=zero; p1=log(psges)
z2=zges(1); p2=prsltmp(1)
elseif (dpres>nsig) then
z1=zges(nsig-1); p1=prsltmp(nsig-1)
z2=zges(nsig); p2=prsltmp(nsig)
drpx = 1.e6_r_kind
else
k=dpres
k1=min(max(1,k),nsig)
k2=max(1,min(k+1,nsig))
z1=zges(k1); p1=prsltmp(k1)
z2=zges(k2); p2=prsltmp(k2)
endif
dz21 = z2-z1
dlnp21 = p2-p1
dz = zob-z1
pobl = p1 + (dlnp21/dz21)*dz
presw = ten*exp(pobl)
endif
! Determine location in terms of grid units for midpoint of
! first layer above surface
sfcchk=zero
! call grdcrd1(sfcchk,zges,nsig,1)
! Process observations with reported pressure
else
dpres = data(ipres,i)
presw = ten*exp(dpres)
dpres = dpres-log(psges)
drpx=zero
prsfc=psges
prsln2=log(exp(prsltmp(1))/prsfc)
dpressave=dpres
! Put obs pressure in correct units to get grid coord. number
dpres=log(exp(dpres)*prsfc)
call grdcrd1(dpres,prsltmp(1),nsig,-1)
! Interpolate guess u and v to observation location and time.
call tintrp31(ges_u,ugesin,dlat,dlon,dpres,dtime, &
hrdifsig,mype,nfldsig)
call tintrp31(ges_v,vgesin,dlat,dlon,dpres,dtime, &
hrdifsig,mype,nfldsig)
iz = max(1, min( int(dpres), nsig))
delz = max(zero, min(dpres - float(iz), one))
if (save_jacobian) then
u_ind = getindex(svars3d, 'u')
if (u_ind < 0) then
print *, 'Error: no variable u in state vector. Exiting.'
call stop2(1300)
endif
v_ind = getindex(svars3d, 'v')
if (v_ind < 0) then
print *, 'Error: no variable v in state vector. Exiting.'
call stop2(1300)
endif
dhx_dx_u%st_ind(1) = iz + sum(levels(1:u_ind-1))
dhx_dx_u%end_ind(1) = min(iz + 1,nsig) + sum(levels(1:u_ind-1))
dhx_dx_v%st_ind(1) = iz + sum(levels(1:v_ind-1))
dhx_dx_v%end_ind(1) = min(iz + 1,nsig) + sum(levels(1:v_ind-1))
dhx_dx_u%val(1) = one - delz
dhx_dx_u%val(2) = delz
dhx_dx_v%val = dhx_dx_u%val
endif
if(dpressave <= prsln2)then
factw=one
else
factw = data(iff10,i)
if(sfcmod_gfs .or. sfcmod_mm5) then
sfcr = data(isfcr,i)
skint = data(iskint,i)
call comp_fact10(dlat,dlon,dtime,skint,sfcr,isli,mype,factw)
end if
call tintrp2a1(ges_tv,tges,dlat,dlon,dtime,hrdifsig,&
nsig,mype,nfldsig)
! Apply 10-meter wind reduction factor to guess winds
dx10=-goverrd*ten/tges(1)
if (dpressave < dx10)then
term=(prsln2-dpressave)/(prsln2-dx10)
factw=one-term+factw*term
end if
ugesin=factw*ugesin
vgesin=factw*vgesin
if (save_jacobian) then
dhx_dx_u%val = factw * dhx_dx_u%val
dhx_dx_v%val = factw * dhx_dx_v%val
endif
end if
! Get approx k value of sfc by using surface pressure
sfcchk=log(psges)
call grdcrd1(sfcchk,prsltmp(1),nsig,-1)
endif
! Checks based on observation location relative to model surface and top
rlow=max(sfcchk-dpres,zero)
rhgh=max(dpres-r0_001-rsigp,zero)
if(luse(i))then
awork(1) = awork(1) + one
if(rlow/=zero) awork(2) = awork(2) + one
if(rhgh/=zero) awork(3) = awork(3) + one
end if
ratio_errors=error/(data(ier,i)+drpx+1.0e6_r_kind*rhgh+four*rlow)
! Compute innovations
lowlevelsat=itype==242.or.itype==243.or.itype==245.or.itype==246.or. &
itype==247.or.itype==250.or.itype==251.or.itype==252.or. &
itype==253.or.itype==254.or.itype==257.or.itype==258.or. &
itype==259
if (lowlevelsat .and. twodvar_regional) then
call windfactor(presw,factw)
data(iuob,i)=factw*data(iuob,i)
data(ivob,i)=factw*data(ivob,i)
uob = data(iuob,i)
vob = data(ivob,i)
endif
dudiff=uob-ugesin
dvdiff=vob-vgesin
spdb=sqrt(uob**2+vob**2)-sqrt(ugesin**2+vgesin**2)
! Setup dynamic ob error specification for aircraft recon in hurricanes
if (aircraft_recon) then
if (itype==236) then
magomb=sqrt(dudiff*dudiff+dvdiff*dvdiff)
ratio_errors=error/((uv_doe_a_236*magomb+uv_doe_b_236)+drpx+1.0e6_r_kind*rhgh+four*rlow)
endif
if (itype==237) then
magomb=sqrt(dudiff*dudiff+dvdiff*dvdiff)
ratio_errors=error/((uv_doe_a_237*magomb+uv_doe_b_237)+drpx+1.0e6_r_kind*rhgh+four*rlow)
endif
endif
! Invert observation error
error=one/error
! Check to see if observation below model surface or above model top.
! If so, don't use observation
if (dpres > rsig )then
if( regional .and. presw > pt_ll )then
dpres=rsig
else
ratio_errors=zero
endif
endif
if ( (itype>=221 .and. itype<=229).and. (dpres<zero) ) ratio_errors=zero
! QC PBL profiler 227 and 223, 224
if(itype==227 .or. itype==223 .or. itype==224 .or. itype==228 .or. itype==229) then
if(abs(uob) < 1.0_r_kind .and. abs(vob) <1.0_r_kind ) then
muse(i)=.false.
error=zero
endif
endif
! VADWND and aircraft winds quality control
if( itype ==224 .and. presw < 226.0_r_kind) then
error=zero
endif
if(itype >=230 .and. itype <=239 .and. presw <126.0_r_kind ) then
error=zero
endif
! Quality control for satellite winds
if ( qc_satwnds ) then
if (itype >=240 .and. itype <=260) then
call intrp2a11(tropprs,trop5,dlat,dlon,mype)
if(presw < trop5-r50) error=zero ! tropopose check for all satellite winds
endif
if(itype >=240 .and. itype <=260) then
if(i_gsdqc==2) then
prsfc = r10*psges
if( prsfc-presw < 100.0_r_kind) error =zero ! add check for obs within 100 hPa of sfc
else
if( presw >950.0_r_kind) error =zero ! screen data beloww 950mb
endif
endif
if(itype ==242 .or. itype ==243 ) then ! visible winds from JMA and EUMETSAT
if(presw <700.0_r_kind) error=zero ! no visible winds above 700mb
endif
if(itype ==245 ) then
if( presw >399.0_r_kind .and. presw <801.0_r_kind) then !GOES IR winds
error=zero ! no data between 400-800mb
endif
endif
if(itype == 252 .and. presw >499.0_r_kind .and. presw <801.0_r_kind) then ! JMA IR winds
error=zero
endif
if(itype == 253 ) then
if(presw >401.0_r_kind .and. presw <801.0_r_kind) then ! EUMET IR winds
error=zero
endif
endif
if( itype == 246 .or. itype == 250 .or. itype == 254 ) then ! water vapor cloud top
if(presw >399.0_r_kind) error=zero
endif
if(itype ==257 .and. presw <249.0_r_kind) error=zero
if(itype ==258 .and. presw >600.0_r_kind) error=zero
if(itype ==259 .and. presw >600.0_r_kind) error=zero
if(itype ==259 .and. presw <249.0_r_kind) error=zero
endif ! qc_satwnds
! QC GOES CAWV - some checks above as well
if (itype==247) then
prsfc = r10*psges ! surface pressure in hPa
! Compute observed and guess wind speeds (m/s).
spdges = sqrt(ugesin* ugesin +vgesin* vgesin )
! Set and compute GOES CAWV specific departure parameters
LNVD_wspd = spdob
LNVD_omb = sqrt(dudiff*dudiff + dvdiff*dvdiff)
LNVD_ratio = LNVD_omb / log(LNVD_wspd)
LNVD_threshold = 3.0_r_kind
if( .not. wrf_nmm_regional) then ! LNVD check not use for CAWV winds in HWRF
if(LNVD_ratio >= LNVD_threshold .or. & ! LNVD check
(presw > prsfc-110.0_r_kind .and. isli /= 0))then ! near surface check 110 ~1km
error = zero