FV3 Bundle
read_diag.f90
Go to the documentation of this file.
1 !$$$ subprogram documentation block
2 ! . . . .
3 ! subprogram: read_raddiag read rad diag file
4 ! prgmmr: tahara org: np20 date: 2003-01-01
5 !
6 ! abstract: This module contains code to process radiance
7 ! diagnostic files. The module defines structures
8 ! to contain information from the radiance
9 ! diagnostic files and then provides two routines
10 ! to access contents of the file.
11 !
12 ! program history log:
13 ! 2005-07-22 treadon - add this doc block
14 ! 2010-10-05 treadon - refactor code to GSI standard
15 ! 2010-10-08 zhu - use data_tmp to handle various npred values
16 ! 2011-02-22 kleist - changes related to memory allocate/deallocate
17 ! 2011-04-08 li - add tref, dtw, dtc to diag_data_fix_list, add tb_tz to diag_data_chan_list
18 ! - correspondingly, change ireal_radiag (26 -> 30) and ipchan_radiag (7 -> 8)
19 ! 2011-07-24 safford - make structure size for reading data_fix data version dependent
20 ! 2013-11-21 todling - revisit how versions are set (add set/get_radiag)
21 ! 2014-01-27 todling - add ob sensitivity index
22 ! 2017-07-13 mccarty - incorporate hooks for nc4/binary diag reading
23 !
24 ! contains
25 ! read_radiag_header - read radiance diagnostic file header
26 ! read_radiag_data - read radiance diagnostic file data
27 ! set_netcdf_read - call set_netcdf_read(.true.) to use nc4 hooks, otherwise read file as
28 ! traditional binary format
29 !
30 ! attributes:
31 ! language: f90
32 ! machine: ibm RS/6000 SP
33 !
34 !$$$
35 
36 module read_diag
37 
38  use ncd_kinds, only: i_kind,r_single,r_kind
39  use nc_diag_read_mod, only: nc_diag_read_get_var, nc_diag_read_get_global_attr
40  use nc_diag_read_mod, only: nc_diag_read_get_dim
42  implicit none
43 
44 ! Declare public and private
45  private
46 
47  public :: diag_header_fix_list
48  public :: diag_header_chan_list
49  public :: diag_data_name_list
50  public :: diag_data_fix_list
51  public :: diag_data_chan_list
52  public :: diag_data_extra_list
53  public :: open_radiag
54  public :: close_radiag
55  public :: read_radiag_header
56  public :: read_radiag_data
57  public :: set_netcdf_read
58 ! public :: iversion_radiag
59 ! public :: iversion_radiag_1
60 ! public :: iversion_radiag_2
61 ! public :: iversion_radiag_3
62 ! public :: iversion_radiag_4
63  public :: ireal_radiag
64  public :: ipchan_radiag
65  public :: set_radiag
66  public :: get_radiag
67  public :: read_all_radiag
68 
69  interface set_radiag
70  module procedure set_radiag_int_ ! internal procedure for integers
71  end interface
72  interface get_radiag
73  module procedure get_radiag_int_ ! internal procedure for integers
74  end interface
75 
76  integer(i_kind),parameter :: ireal_radiag = 30 ! number of real entries per spot in radiance diagnostic file
77  integer(i_kind),parameter :: ireal_old_radiag = 26 ! number of real entries per spot in versions older than iversion_radiag_2
78  integer(i_kind),parameter :: ipchan_radiag = 8 ! number of entries per channel per spot in radiance diagnostic file
79 
80 ! Declare structures for radiance diagnostic file information
82  character(len=20) :: isis ! sat and sensor type
83  character(len=10) :: id ! sat type
84  character(len=10) :: obstype ! observation type
85  integer(i_kind) :: jiter ! outer loop counter
86  integer(i_kind) :: nchan ! number of channels in the sensor
87  integer(i_kind) :: npred ! number of updating bias correction predictors
88  integer(i_kind) :: idate ! time (yyyymmddhh)
89  integer(i_kind) :: ireal ! # of real elements in the fix part of a data record
90  integer(i_kind) :: ipchan ! # of elements for each channel except for bias correction terms
91  integer(i_kind) :: iextra ! # of extra elements for each channel
92  integer(i_kind) :: jextra ! # of extra elements
93  integer(i_kind) :: idiag ! first dimension of diag_data_chan
94  integer(i_kind) :: angord ! order of polynomial for adp_anglebc option
95  integer(i_kind) :: iversion ! radiance diagnostic file version number
96  integer(i_kind) :: inewpc ! indicator of newpc4pred (1 on, 0 off)
97  integer(i_kind) :: isens ! sensitivity index
98  end type diag_header_fix_list
99 
101  character(len=10),dimension(ireal_radiag) :: fix
102  character(len=10),dimension(:),allocatable :: chn
103  end type diag_data_name_list
104 
106  real(r_single) :: freq ! frequency (Hz)
107  real(r_single) :: polar ! polarization
108  real(r_single) :: wave ! wave number (cm^-1)
109  real(r_single) :: varch ! error variance (or SD error?)
110  real(r_single) :: tlapmean ! mean lapse rate
111  integer(i_kind):: iuse ! use flag
112  integer(i_kind):: nuchan ! sensor relative channel number
113  integer(i_kind):: iochan ! satinfo relative channel number
114  end type diag_header_chan_list
115 
117  real(r_single) :: lat ! latitude (deg)
118  real(r_single) :: lon ! longitude (deg)
119  real(r_single) :: zsges ! guess elevation at obs location (m)
120  real(r_single) :: obstime ! observation time relative to analysis
121  real(r_single) :: senscn_pos ! sensor scan position (integer(i_kind))
122  real(r_single) :: senscn_ang ! sensor scan angle
123  real(r_single) :: satzen_ang ! satellite zenith angle (deg)
124  real(r_single) :: satazm_ang ! satellite azimuth angle (deg)
125  real(r_single) :: solzen_ang ! solar zenith angle (deg)
126  real(r_single) :: solazm_ang ! solar azimumth angle (deg)
127  real(r_single) :: sungln_ang ! sun glint angle (deg)
128  real(r_single) :: water_frac ! fractional coverage by water
129  real(r_single) :: land_frac ! fractional coverage by land
130  real(r_single) :: ice_frac ! fractional coverage by ice
131  real(r_single) :: snow_frac ! fractional coverage by snow
132  real(r_single) :: water_temp ! surface temperature over water (K)
133  real(r_single) :: land_temp ! surface temperature over land (K)
134  real(r_single) :: ice_temp ! surface temperature over ice (K)
135  real(r_single) :: snow_temp ! surface temperature over snow (K)
136  real(r_single) :: soil_temp ! soil temperature (K)
137  real(r_single) :: soil_mois ! soil moisture
138  real(r_single) :: land_type ! land type (integer(i_kind))
139  real(r_single) :: veg_frac ! vegetation fraction
140  real(r_single) :: snow_depth ! snow depth
141  real(r_single) :: sfc_wndspd ! surface wind speed
142  real(r_single) :: qcdiag1 ! ir=cloud fraction, mw=cloud liquid water
143  real(r_single) :: qcdiag2 ! ir=cloud top pressure, mw=total column water
144  real(r_single) :: tref ! reference temperature (Tr) in NSST
145  real(r_single) :: dtw ! dt_warm at zob
146  real(r_single) :: dtc ! dt_cool at zob
147  real(r_single) :: tz_tr ! d(Tz)/d(Tr)
148  end type diag_data_fix_list
149 
151  real(r_single) :: tbobs ! Tb (obs) (K)
152  real(r_single) :: omgbc ! Tb_(obs) - Tb_(simulated w/ bc) (K)
153  real(r_single) :: omgnbc ! Tb_(obs) - Tb_(simulated_w/o bc) (K)
154  real(r_single) :: errinv ! inverse error (K**(-1))
155  real(r_single) :: qcmark ! quality control mark
156  real(r_single) :: emiss ! surface emissivity
157  real(r_single) :: tlap ! temperature lapse rate
158  real(r_single) :: tb_tz ! d(Tb)/d(Tz)
159  real(r_single) :: bicons ! constant bias correction term
160  real(r_single) :: biang ! scan angle bias correction term
161  real(r_single) :: biclw ! CLW bias correction term
162  real(r_single) :: bilap2 ! square lapse rate bias correction term
163  real(r_single) :: bilap ! lapse rate bias correction term
164  real(r_single) :: bicos ! node*cos(lat) bias correction term
165  real(r_single) :: bisin ! sin(lat) bias correction term
166  real(r_single) :: biemis ! emissivity sensitivity bias correction term
167  real(r_single),dimension(:),allocatable :: bifix ! angle dependent bias
168  real(r_single) :: bisst ! SST bias correction term
169  end type diag_data_chan_list
170 
172  real(r_single) :: extra ! extra information
173  end type diag_data_extra_list
174 
175  integer(i_kind),save :: iversion_radiag ! Current version (see set routine)
176  integer(i_kind),parameter:: iversion_radiag_1 = 11104 ! Version when bias-correction entries were modified
177  integer(i_kind),parameter:: iversion_radiag_2 = 13784 ! Version when NSST entries were added
178  integer(i_kind),parameter:: iversion_radiag_3 = 19180 ! Version when SSMIS added
179  integer(i_kind),parameter:: iversion_radiag_4 = 30303 ! Version when emissivity predictor added
180 
181  real(r_single),parameter:: rmiss_radiag = -9.9e11_r_single
182 
183  logical,save :: netcdf = .false.
184 
186  logical :: nc_read
187  integer(i_kind) :: cur_ob_idx
188  integer(i_kind) :: num_records
189  type(diag_data_fix_list), allocatable :: all_data_fix(:)
190  type(diag_data_chan_list), allocatable :: all_data_chan(:,:)
191  type(diag_data_extra_list), allocatable :: all_data_extra(:,:,:)
192  end type ncdiag_status
193 
194  integer(i_kind), parameter :: max_open_ncdiag = 2
195  integer(i_kind), save :: nopen_ncdiag = 0
196  integer(i_kind), dimension(MAX_OPEN_NCDIAG), save :: ncdiag_open_id = (/-1, -1/)
197  type(ncdiag_status), dimension(MAX_OPEN_NCDIAG), save :: ncdiag_open_status
198 
199 contains
200 
201 subroutine set_radiag_int_ (what,iv,ier)
202 character(len=*),intent(in) :: what
203 integer(i_kind),intent(in) :: iv
204 integer(i_kind),intent(out):: ier
205 ier=-1
206 if(trim(what)=='version') then
207  iversion_radiag = iv
208  ier=0
209 endif
210 end subroutine set_radiag_int_
211 
212 subroutine get_radiag_int_ (what,iv,ier)
213 character(len=*),intent(in) :: what
214 integer(i_kind),intent(out):: iv
215 integer(i_kind),intent(out):: ier
216 ier=-1
217 if(trim(what)=='version') then
218  iv = iversion_radiag
219  ier=0
220 endif
221 end subroutine get_radiag_int_
222 
223 subroutine set_netcdf_read(use_netcdf)
224 ! . . . .
225 ! subprogram: read_diag_header_bin read rad diag header
226 ! prgmmr: mccarty org: gmao date: 2015-08-06
227 !
228 ! abstract: This routine sets the routines to read from a netcdf file.
229 ! The default currently is to read binary files
230 !
231 ! program history log:
232 ! 2015-08-06 mccarty - created routine
233 !
234 ! input argument list:
235 ! use_netcdf - logical .true. tells routine to read netcdf diag
236 ! attributes:
237 ! language: f90
238 ! machine: ibm RS/6000 SP
239 !
240 !$$$
241  logical,intent(in) :: use_netcdf
242 
243  netcdf = use_netcdf
244 end subroutine set_netcdf_read
245 
246 
247 subroutine open_radiag(filename, ftin)
248  character*500, intent(in) :: filename
249  integer(i_kind), intent(inout) :: ftin
250 
251  integer(i_kind) :: i
252 
253  if (netcdf) then
254  if (nopen_ncdiag >= max_open_ncdiag) then
255  write(6,*) 'OPEN_RADIAG: ***ERROR*** Cannot open more than ', &
256  max_open_ncdiag, ' netcdf diag files.'
257  call abort
258  endif
259  call nc_diag_read_init(filename,ftin)
260  do i = 1, max_open_ncdiag
261  if (ncdiag_open_id(i) < 0) then
262  ncdiag_open_id(i) = ftin
263  ncdiag_open_status(i)%nc_read = .false.
264  ncdiag_open_status(i)%cur_ob_idx = -9999
265  ncdiag_open_status(i)%num_records = -9999
266  if (allocated(ncdiag_open_status(i)%all_data_fix)) then
267  deallocate(ncdiag_open_status(i)%all_data_fix)
268  endif
269  if (allocated(ncdiag_open_status(i)%all_data_chan)) then
270  deallocate(ncdiag_open_status(i)%all_data_chan)
271  endif
272  if (allocated(ncdiag_open_status(i)%all_data_extra)) then
273  deallocate(ncdiag_open_status(i)%all_data_extra)
274  endif
276  exit
277  endif
278  enddo
279  else
280  open(ftin,form="unformatted",file=filename)
281  rewind(ftin)
282  endif
283 
284 end subroutine open_radiag
285 
286 subroutine close_radiag(filename, ftin)
287  character*500, intent(in) :: filename
288  integer(i_kind), intent(inout) :: ftin
289 
290  integer(i_kind) :: id
291 
292  if (netcdf) then
293  id = find_ncdiag_id(ftin)
294  if (id < 0) then
295  write(6,*) 'CLOSE_RADIAG: ***ERROR*** ncdiag file ', filename, &
296  ' was not opened'
297  call abort
298  endif
299  call nc_diag_read_close(filename)
300  ncdiag_open_id(id) = -1
301  ncdiag_open_status(id)%nc_read = .false.
302  ncdiag_open_status(id)%cur_ob_idx = -9999
303  ncdiag_open_status(id)%num_records = -9999
304  if (allocated(ncdiag_open_status(id)%all_data_fix)) then
305  deallocate(ncdiag_open_status(id)%all_data_fix)
306  endif
307  if (allocated(ncdiag_open_status(id)%all_data_chan)) then
308  deallocate(ncdiag_open_status(id)%all_data_chan)
309  endif
310  if (allocated(ncdiag_open_status(id)%all_data_extra)) then
311  deallocate(ncdiag_open_status(id)%all_data_extra)
312  endif
314  else
315  close(ftin)
316  endif
317 
318 end subroutine close_radiag
319 
320 subroutine read_radiag_header(ftin,npred_radiag,retrieval,header_fix,header_chan,data_name,iflag,lverbose)
321 ! . . . .
322 ! subprogram: read_diag_header_bin read rad diag header
323 ! prgmmr: mccarty org: gmao date: 2015-08-06
324 !
325 ! abstract: This routine reads the header record from a radiance
326 ! diagnostic file
327 !
328 ! program history log:
329 ! 2015-08-06 mccarty - created routine w/ fork for ncdiag or binary
330 !
331 ! input argument list:
332 ! ftin - unit number connected to diagnostic file
333 ! npred_radiag - number of bias correction terms
334 ! retrieval - .true. if sst retrieval
335 !
336 ! output argument list:
337 ! header_fix - header information structure
338 ! header_chan - channel information structure
339 ! data_name - diag file data names
340 ! iflag - error code
341 ! lverbose - optional flag to turn off default output to standard out
342 !
343 ! attributes:
344 ! language: f90
345 ! machine: ibm RS/6000 SP
346 !
347 !$$$
348 
349 ! Declare passed arguments
350  integer(i_kind),intent(in) :: ftin
351  integer(i_kind),intent(in) :: npred_radiag
352  logical,intent(in) :: retrieval
353  type(diag_header_fix_list ),intent(out):: header_fix
354  type(diag_header_chan_list),allocatable :: header_chan(:)
355  type(diag_data_name_list) :: data_name
356  integer(i_kind),intent(out) :: iflag
357  logical,optional,intent(in) :: lverbose
358 
359  iflag = 0
360  if (netcdf) then
361  print *,'netcdf slot'
362  call read_radiag_header_nc(ftin,npred_radiag,retrieval,header_fix,header_chan,data_name,iflag,lverbose)
363  else
364  call read_radiag_header_bin(ftin,npred_radiag,retrieval,header_fix,header_chan,data_name,iflag,lverbose)
365  endif
366 
367 end subroutine read_radiag_header
368 
369 subroutine read_radiag_header_nc(ftin,npred_radiag,retrieval,header_fix,header_chan,data_name,iflag,lverbose)
370 ! . . . .
371 ! subprogram: read_diag_header_nc read rad diag header
372 ! prgmmr: mccarty org: gmao date: 2003-01-01
373 !
374 ! abstract: This routine reads the header record from a radiance
375 ! diagnostic file
376 !
377 ! program history log:
378 ! 2015-08-06 mccarty - Created routine for ncdiag header reading
379 !
380 ! input argument list:
381 ! ftin - unit number connected to diagnostic file
382 ! npred_radiag - number of bias correction terms
383 ! retrieval - .true. if sst retrieval
384 !
385 ! output argument list:
386 ! header_fix - header information structure
387 ! header_chan - channel information structure
388 ! data_name - diag file data names
389 ! iflag - error code
390 ! lverbose - optional flag to turn off default output to standard out
391 !
392 ! attributes:
393 ! language: f90
394 ! machine: ibm RS/6000 SP
395 !
396 !$$$
397 ! Declare passed arguments
398  integer(i_kind),intent(in) :: ftin
399  integer(i_kind),intent(in) :: npred_radiag
400  logical,intent(in) :: retrieval
401  type(diag_header_fix_list ),intent(out):: header_fix
402  type(diag_header_chan_list),allocatable :: header_chan(:)
403  type(diag_data_name_list) :: data_name
404  integer(i_kind),intent(out) :: iflag
405  logical,optional,intent(in) :: lverbose
406 
407 ! local variables
408  integer(i_kind) :: nchan_dim
409  real(r_kind),allocatable,dimension(:) :: r_var_stor
410  integer(i_kind),allocatable,dimension(:) :: i_var_stor
411  character(20) :: isis
412  character(10) :: id, obstype
413 ! integer(i_kind),dimension(:),allocatable :: jiter, nchan_diag, npred, idate, &
414  integer(i_kind) :: jiter, nchan_diag, npred, idate, &
415  ireal, ipchan, iextra, jextra, &
416  idiag, angord, iversion, inewpc, &
417  isens
418 
419  iflag = 0
420 ! allocate(nchan_diag(1) )
421  nchan_dim = nc_diag_read_get_dim(ftin,'nchans')
422  write(*,*)'Number of channels=',nchan_dim
423  header_fix%nchan = nchan_dim
424 
425  call nc_diag_read_get_global_attr(ftin, "Number_of_channels", nchan_diag)
426 
427  if (nchan_dim .ne. nchan_diag) then
428  write(*,*)'ERROR: Number of channels from dimension do not match those from header, aborting.'
429  call abort
430  endif
431 
432  call nc_diag_read_get_global_attr(ftin, "Satellite_Sensor", isis) ; header_fix%isis = isis
433  call nc_diag_read_get_global_attr(ftin, "Satellite", id) ; header_fix%id = id
434  call nc_diag_read_get_global_attr(ftin, "Observation_type", obstype) ; header_fix%obstype = obstype
435  call nc_diag_read_get_global_attr(ftin, "Outer_Loop_Iteration", jiter) ; header_fix%jiter = jiter
436  call nc_diag_read_get_global_attr(ftin, "Number_of_Predictors", npred) ; header_fix%npred = npred
437  call nc_diag_read_get_global_attr(ftin, "date_time", idate) ; header_fix%idate = idate
438  call nc_diag_read_get_global_attr(ftin, "ireal_radiag", ireal) ; header_fix%ireal = ireal
439  call nc_diag_read_get_global_attr(ftin, "ipchan_radiag", ipchan) ; header_fix%ipchan = ipchan
440  call nc_diag_read_get_global_attr(ftin, "iextra", iextra) ; header_fix%iextra = iextra
441  call nc_diag_read_get_global_attr(ftin, "jextra", jextra) ; header_fix%jextra = jextra
442  call nc_diag_read_get_global_attr(ftin, "idiag", idiag) ; header_fix%idiag = idiag
443  call nc_diag_read_get_global_attr(ftin, "angord", angord) ; header_fix%angord = angord
444  call nc_diag_read_get_global_attr(ftin, "iversion_radiag", iversion) ; header_fix%iversion = iversion
445  call nc_diag_read_get_global_attr(ftin, "New_pc4pred", inewpc) ; header_fix%inewpc = inewpc
446  call nc_diag_read_get_global_attr(ftin, "ioff0", isens) ; header_fix%isens = isens
447 
448 
449  allocate(header_chan(nchan_dim) )
450 
451  allocate(r_var_stor(nchan_dim), &
452  i_var_stor(nchan_dim) )
453 ! call nc_diag_read_get_var('Var', var_stor)
454  call nc_diag_read_get_var(ftin, 'frequency',r_var_stor) ; header_chan%freq = r_var_stor
455  call nc_diag_read_get_var(ftin, 'polarization',i_var_stor) ; header_chan%polar = i_var_stor
456  call nc_diag_read_get_var(ftin, 'wavenumber',r_var_stor) ; header_chan%wave = r_var_stor
457  call nc_diag_read_get_var(ftin, 'error_variance',r_var_stor) ; header_chan%varch = r_var_stor
458  call nc_diag_read_get_var(ftin, 'mean_lapse_rate',r_var_stor); header_chan%tlapmean = r_var_stor
459  call nc_diag_read_get_var(ftin, 'use_flag',i_var_stor) ; header_chan%iuse = i_var_stor
460  call nc_diag_read_get_var(ftin, 'sensor_chan',i_var_stor) ; header_chan%nuchan = i_var_stor
461  call nc_diag_read_get_var(ftin, 'satinfo_chan',i_var_stor) ; header_chan%iochan = i_var_stor
462 
463 
464 end subroutine read_radiag_header_nc
465 
466 subroutine read_radiag_header_bin(ftin,npred_radiag,retrieval,header_fix,header_chan,data_name,iflag,lverbose)
467 ! . . . .
468 ! subprogram: read_diag_header_bin read rad diag header
469 ! prgmmr: tahara org: np20 date: 2003-01-01
470 !
471 ! abstract: This routine reads the header record from a radiance
472 ! diagnostic file
473 !
474 ! program history log:
475 ! 2010-10-05 treadon - add this doc block
476 ! 2011-02-22 kleist - changes related to memory allocation and standard output
477 ! 2014-07-25 sienkiewicz - supress warning if npred_radiag == 0
478 ! 2017-07-17 mccarty - renamed routine to _bin suffix for ncdiag
479 !
480 ! input argument list:
481 ! ftin - unit number connected to diagnostic file
482 ! npred_radiag - number of bias correction terms
483 ! retrieval - .true. if sst retrieval
484 !
485 ! output argument list:
486 ! header_fix - header information structure
487 ! header_chan - channel information structure
488 ! data_name - diag file data names
489 ! iflag - error code
490 ! lverbose - optional flag to turn off default output to standard out
491 !
492 ! attributes:
493 ! language: f90
494 ! machine: ibm RS/6000 SP
495 !
496 !$$$
497 
498 ! Declare passed arguments
499  integer(i_kind),intent(in) :: ftin
500  integer(i_kind),intent(in) :: npred_radiag
501  logical,intent(in) :: retrieval
502  type(diag_header_fix_list ),intent(out):: header_fix
503  type(diag_header_chan_list),allocatable :: header_chan(:)
504  type(diag_data_name_list) :: data_name
505  integer(i_kind),intent(out) :: iflag
506  logical,optional,intent(in) :: lverbose
507 
508 ! Declare local variables
509  character(len=2):: string
510  character(len=10):: satid,sentype
511  character(len=20):: sensat
512  integer(i_kind) :: i,ich
513  integer(i_kind):: jiter,nchanl,npred,ianldate,ireal,ipchan,iextra,jextra
514  integer(i_kind):: idiag,angord,iversion,inewpc,isens
515  integer(i_kind):: iuse_tmp,nuchan_tmp,iochan_tmp
516  real(r_single) :: freq_tmp,polar_tmp,wave_tmp,varch_tmp,tlapmean_tmp
517  logical loutall
518 
519  loutall=.true.
520  if(present(lverbose)) loutall=lverbose
521 
522 ! Read header (fixed_part).
523  read(ftin,iostat=iflag) sensat,satid,sentype,jiter,nchanl,npred,ianldate,&
524  ireal,ipchan,iextra,jextra,idiag,angord,iversion,inewpc,isens
525  if (iflag/=0) then
526  rewind(ftin)
527  read(ftin,iostat=iflag) sensat,satid,sentype,jiter,nchanl,npred,ianldate,&
528  ireal,ipchan,iextra,jextra,idiag,angord,iversion,inewpc
529  isens=0
530  end if
531 
532  if (iflag/=0) then
533  rewind(ftin)
534  read(ftin,iostat=iflag) sensat,satid,sentype,jiter,nchanl,npred,ianldate,&
535  ireal,ipchan,iextra,jextra
536  idiag=ipchan+npred+1
537  angord=0
538  iversion=0
539  inewpc=0
540  isens=0
541  if (iflag/=0) then
542  write(6,*)'READ_RADIAG_HEADER: ***ERROR*** Unknown file format. Cannot read'
543  return
544  endif
545  endif
546 
547  header_fix%isis = sensat
548  header_fix%id = satid
549  header_fix%obstype = sentype
550  header_fix%jiter = jiter
551  header_fix%nchan = nchanl
552  header_fix%npred = npred
553  header_fix%idate = ianldate
554  header_fix%ireal = ireal
555  header_fix%ipchan = ipchan
556  header_fix%iextra = iextra
557  header_fix%jextra = jextra
558  header_fix%idiag = idiag
559  header_fix%angord = angord
560  header_fix%iversion= iversion
561  header_fix%inewpc = inewpc
562  header_fix%isens = isens
563 
564  if (loutall) then
565  write(6,*)'READ_RADIAG_HEADER: isis=',header_fix%isis,&
566  ' nchan=',header_fix%nchan,&
567  ' npred=',header_fix%npred,&
568  ' angord=',header_fix%angord,&
569  ' idiag=',header_fix%idiag,&
570  ' iversion=',header_fix%iversion,&
571  ' inewpc=',header_fix%inewpc,&
572  ' isens=',header_fix%isens
573 
574  if ( header_fix%iextra /= 0) &
575  write(6,*)'READ_RADIAG_HEADER: extra diagnostic information available, ',&
576  'iextra=',header_fix%iextra
577  end if
578 
579  if (header_fix%npred /= npred_radiag .and. npred_radiag /= 0) &
580  write(6,*) 'READ_RADIAG_HEADER: **WARNING** header_fix%npred,npred=',&
581  header_fix%npred,npred_radiag
582 
583 ! Allocate and initialize as needed
584  if (allocated(header_chan)) deallocate(header_chan)
585  if (allocated(data_name%chn)) deallocate(data_name%chn)
586 
587  allocate(header_chan( header_fix%nchan))
588  allocate(data_name%chn(header_fix%idiag))
589 
590  data_name%fix(1) ='lat '
591  data_name%fix(2) ='lon '
592  data_name%fix(3) ='zsges '
593  data_name%fix(4) ='obstim '
594  data_name%fix(5) ='scanpos '
595  data_name%fix(6) ='satzen '
596  data_name%fix(7) ='satazm '
597  data_name%fix(8) ='solzen '
598  data_name%fix(9) ='solazm '
599  data_name%fix(10)='sungln '
600  data_name%fix(11)='fwater '
601  data_name%fix(12)='fland '
602  data_name%fix(13)='fice '
603  data_name%fix(14)='fsnow '
604  data_name%fix(15)='twater '
605  data_name%fix(16)='tland '
606  data_name%fix(17)='tice '
607  data_name%fix(18)='tsnow '
608  data_name%fix(19)='tsoil '
609  data_name%fix(20)='soilmoi '
610  data_name%fix(21)='landtyp '
611  data_name%fix(22)='vegfrac '
612  data_name%fix(23)='snowdep '
613  data_name%fix(24)='wndspd '
614  data_name%fix(25)='qc1 '
615  data_name%fix(26)='qc2 '
616  data_name%fix(27)='tref '
617  data_name%fix(28)='dtw '
618  data_name%fix(29)='dtc '
619  data_name%fix(30)='tz_tr '
620 
621  data_name%chn(1)='obs '
622  data_name%chn(2)='omgbc '
623  data_name%chn(3)='omgnbc '
624  data_name%chn(4)='errinv '
625  data_name%chn(5)='qcmark '
626  data_name%chn(6)='emiss '
627  data_name%chn(7)='tlap '
628  data_name%chn(8)='tb_tz '
629 
630  if (header_fix%iversion < iversion_radiag_1) then
631  data_name%chn( 8)= 'bifix '
632  data_name%chn( 9)= 'bilap '
633  data_name%chn(10)= 'bilap2 '
634  data_name%chn(11)= 'bicons '
635  data_name%chn(12)= 'biang '
636  data_name%chn(13)= 'biclw '
637  if (retrieval) data_name%chn(13)= 'bisst '
638  elseif ( header_fix%iversion < iversion_radiag_2 .and. header_fix%iversion >= iversion_radiag_1 ) then
639  data_name%chn( 8)= 'bicons '
640  data_name%chn( 9)= 'biang '
641  data_name%chn(10)= 'biclw '
642  data_name%chn(11)= 'bilap2 '
643  data_name%chn(12)= 'bilap '
644  do i=1,header_fix%angord
645  write(string,'(i2.2)') header_fix%angord-i+1
646  data_name%chn(12+i)= 'bifix' // string
647  end do
648  data_name%chn(12+header_fix%angord+1)= 'bifix '
649  data_name%chn(12+header_fix%angord+2)= 'bisst '
650  elseif ( header_fix%iversion < iversion_radiag_3 .and. header_fix%iversion >= iversion_radiag_2 ) then
651  data_name%chn( 9)= 'bicons '
652  data_name%chn(10)= 'biang '
653  data_name%chn(11)= 'biclw '
654  data_name%chn(12)= 'bilap2 '
655  data_name%chn(13)= 'bilap '
656  do i=1,header_fix%angord
657  write(string,'(i2.2)') header_fix%angord-i+1
658  data_name%chn(13+i)= 'bifix' // string
659  end do
660  data_name%chn(13+header_fix%angord+1)= 'bifix '
661  data_name%chn(13+header_fix%angord+2)= 'bisst '
662  elseif ( header_fix%iversion < iversion_radiag_4 .and. header_fix%iversion >= iversion_radiag_3 ) then
663  data_name%chn( 9)= 'bicons '
664  data_name%chn(10)= 'biang '
665  data_name%chn(11)= 'biclw '
666  data_name%chn(12)= 'bilap2 '
667  data_name%chn(13)= 'bilap '
668  data_name%chn(14)= 'bicos '
669  data_name%chn(15)= 'bisin '
670  do i=1,header_fix%angord
671  write(string,'(i2.2)') header_fix%angord-i+1
672  data_name%chn(15+i)= 'bifix' // string
673  end do
674  data_name%chn(15+header_fix%angord+1)= 'bifix '
675  data_name%chn(15+header_fix%angord+2)= 'bisst '
676  else
677  data_name%chn( 9)= 'bicons '
678  data_name%chn(10)= 'biang '
679  data_name%chn(11)= 'biclw '
680  data_name%chn(12)= 'bilap2 '
681  data_name%chn(13)= 'bilap '
682  data_name%chn(14)= 'bicos '
683  data_name%chn(15)= 'bisin '
684  data_name%chn(16)= 'biemis '
685  do i=1,header_fix%angord
686  write(string,'(i2.2)') header_fix%angord-i+1
687  data_name%chn(16+i)= 'bifix' // string
688  end do
689  data_name%chn(16+header_fix%angord+1)= 'bifix '
690  data_name%chn(16+header_fix%angord+2)= 'bisst '
691  endif
692 
693 ! Read header (channel part)
694  do ich=1, header_fix%nchan
695  read(ftin,iostat=iflag) freq_tmp,polar_tmp,wave_tmp,varch_tmp,tlapmean_tmp,iuse_tmp,nuchan_tmp,iochan_tmp
696  header_chan(ich)%freq = freq_tmp
697  header_chan(ich)%polar = polar_tmp
698  header_chan(ich)%wave = wave_tmp
699  header_chan(ich)%varch = varch_tmp
700  header_chan(ich)%tlapmean = tlapmean_tmp
701  header_chan(ich)%iuse = iuse_tmp
702  header_chan(ich)%nuchan = nuchan_tmp
703  header_chan(ich)%iochan = iochan_tmp
704  if (iflag/=0) return
705  end do
706 
707 ! Construct array containing menonics for data record entries
708 
709 
710 end subroutine read_radiag_header_bin
711 
712 integer(i_kind) function find_ncdiag_id(ftin)
713  integer, intent(in) :: ftin
714 
715  integer :: i
716 
717  find_ncdiag_id = -1
718  do i = 1, max_open_ncdiag
719  if (ncdiag_open_id(i) == ftin) then
720  find_ncdiag_id = i
721  return
722  endif
723  enddo
724  return
725 
726 end function find_ncdiag_id
727 
728 subroutine read_radiag_data(ftin,header_fix,retrieval,data_fix,data_chan,data_extra,iflag )
729 ! . . . .
730 ! subprogram: read_radiag_dat read rad diag data
731 ! prgmmr: tahara org: np20 date: 2003-01-01
732 !
733 ! abstract: This routine reads the data record from a radiance
734 ! diagnostic file
735 !
736 ! program history log:
737 ! 2010-10-05 treadon - add this doc block
738 ! 2011-02-22 kleist - changes related to memory allocation
739 ! 2017-07-17 mccarty - change routine to be generalized for bin/nc4 files
740 !
741 ! input argument list:
742 ! ftin - unit number connected to diagnostic file
743 ! header_fix - header information structure
744 !
745 ! output argument list:
746 ! data_fix - spot header information structure
747 ! data_chan - spot channel information structure
748 ! data_extra - spot extra information
749 ! iflag - error code
750 !
751 ! attributes:
752 ! language: f90
753 ! machine: ibm RS/6000 SP
754 !
755 !$$$
756 
757 
758 ! Declare passed arguments
759  integer(i_kind),intent(in) :: ftin
760  type(diag_header_fix_list ),intent(in) :: header_fix
761  logical,intent(in) :: retrieval
762  type(diag_data_fix_list) ,intent(out):: data_fix
763  type(diag_data_chan_list) ,allocatable :: data_chan(:)
764  type(diag_data_extra_list) ,allocatable :: data_extra(:,:)
765  integer(i_kind),intent(out) :: iflag
766 
767  integer(i_kind) :: id
768 
769  if (netcdf) then
770 
771  id = find_ncdiag_id(ftin)
772  if (id < 0) then
773  write(6,*) 'READ_RADIAG_DATA: ***ERROR*** netcdf diag file ', ftin, ' has not been opened yet.'
774  call abort
775  endif
776 
777  if (.not. ncdiag_open_status(id)%nc_read) then
778  call read_radiag_data_nc_init(ftin, ncdiag_open_status(id), header_fix, retrieval)
779  endif
780 
781  if (ncdiag_open_status(id)%cur_ob_idx .eq. ncdiag_open_status(id)%num_records ) then
782  iflag = 0
783  else if (ncdiag_open_status(id)%cur_ob_idx .gt. ncdiag_open_status(id)%num_records) then
784  iflag = -1
785  else
786  iflag = 1
787  endif
788 
789  if (iflag .ge. 0) then
790  call read_radiag_data_nc(ftin,ncdiag_open_status(id),header_fix,retrieval,data_fix,data_chan,data_extra,iflag)
791  endif
792 
793  else
794  call read_radiag_data_bin(ftin,header_fix,retrieval,data_fix,data_chan,data_extra,iflag )
795  endif
796 
797 end subroutine read_radiag_data
798 
799 subroutine read_all_radiag(ftin, header_fix, retrieval, all_data_fix, &
800  all_data_chan, all_data_extra, &
801  nobs, iflag)
803  integer(i_kind),intent(in) :: ftin
804  type(diag_header_fix_list ),intent(in) :: header_fix
805  logical,intent(in) :: retrieval
806  integer(i_kind),intent(out) :: iflag
807  type(diag_data_fix_list), allocatable :: all_data_fix(:)
808  type(diag_data_chan_list), allocatable :: all_data_chan(:,:)
809  type(diag_data_extra_list), allocatable :: all_data_extra(:,:,:)
810  integer(i_kind), intent(out) :: nobs
811 
812  integer(i_kind) :: id
813 
814  if (netcdf) then
815  id = find_ncdiag_id(ftin)
816  if (id < 0) then
817  write(6,*) 'READ_RADIAG_DATA: ***ERROR*** netcdf diag file ', ftin, ' has not been opened yet.'
818  call abort
819  endif
820 
821  if (.not. ncdiag_open_status(id)%nc_read) then
822  call read_radiag_data_nc_init(ftin, ncdiag_open_status(id), header_fix, retrieval)
823  endif
824  nobs = ncdiag_open_status(id)%num_records
825 
826  iflag = 0
827  if (.not. allocated(all_data_fix)) allocate(all_data_fix(nobs))
828  if (.not. allocated(all_data_chan)) allocate(all_data_chan(nobs,header_fix%nchan) )
829  if (.not. allocated(all_data_extra)) allocate(all_data_extra(nobs,header_fix%iextra, header_fix%nchan) )
830 
831  all_data_fix = ncdiag_open_status(id)%all_data_fix
832  all_data_chan = ncdiag_open_status(id)%all_data_chan
833  all_data_extra = ncdiag_open_status(id)%all_data_extra
834  else
835  iflag = -1
836  endif
837 end subroutine read_all_radiag
838 
839 subroutine read_radiag_data_nc_init(ftin, diag_status, header_fix, retrieval)
840 ! . . . .
841 ! subprogram: read_radiag_data_nc_init read rad diag data
842 ! prgmmr: mccarty org: np20 date: 2015-08-10
843 !
844 ! abstract: This routine reads the data record from a netcdf radiance
845 ! diagnostic file
846 !
847 ! program history log:
848 ! 2015-06-10 mccarty - create routine
849 !
850 ! input argument list:
851 ! ftin - unit number connected to diagnostic file
852 ! header_fix - header information structure
853 !
854 ! output argument list:
855 ! data_fix - spot header information structure
856 ! data_chan - spot channel information structure
857 ! data_extra - spot extra information
858 ! iflag - error code
859 !
860 ! attributes:
861 ! language: f90
862 ! machine: ibm RS/6000 SP
863 !
864 !$$$
865 
866 ! Declare passed arguments
867  integer(i_kind),intent(in) :: ftin
868  type(ncdiag_status), intent(inout) :: diag_status
869  type(diag_header_fix_list ),intent(in) :: header_fix
870  logical,intent(in) :: retrieval
871 
872 ! Declare local variables
873  integer(i_kind) :: nrecord, ndatum, nangord
874  integer(i_kind) :: cch, ic, ir, cdatum
875  real(r_kind), allocatable, dimension(:) :: Latitude, Longitude, Elevation, Obs_Time, Scan_Position, Scan_Angle, &
876  Sat_Zenith_Angle, Sat_Azimuth_Angle, Sol_Zenith_Angle, Sol_Azimuth_Angle, &
877  Sun_Glint_Angle, Water_Fraction, Land_Fraction, Ice_Fraction, &
878  Snow_Fraction, Water_Temperature, Land_Temperature, Ice_Temperature, &
879  Snow_Temperature, Soil_Temperature, Soil_Moisture, &
880  tsavg5, sstcu, sstph, sstnv, dta, dqa, dtp_avh, Vegetation_Fraction, &
881  Snow_Depth, tpwc_amsua, clw_guess_retrieval, Sfc_Wind_Speed, &
882  Cloud_Frac, CTP, CLW, TPWC, clw_obs, clw_guess, Foundation_Temperature, SST_Warm_layer_dt, &
883  SST_Cool_layer_tdrop, SST_dTz_dTfound, Observation, Obs_Minus_Forecast_adjusted, &
884  Obs_Minus_Forecast_unadjusted, Inverse_Observation_Error, QC_Flag, Emissivity, &
885  Weighted_Lapse_Rate, dTb_dTs, BC_Constant, BC_Scan_Angle, &
886  BC_Cloud_Liquid_Water, BC_Lapse_Rate_Squared, BC_Lapse_Rate, BC_Cosine_Latitude_times_Node, &
887  BC_Sine_Latitude,BC_Emissivity,BC_Fixed_Scan_Position
888  integer(i_kind), allocatable, dimension(:) :: Channel_Index, Land_Type_Index
889  real(r_kind), allocatable, dimension(:,:) :: BC_angord ! (nobs, BC_angord_arr_dim) ;
890 
891  real(r_kind) :: clat, clon
892 
893  ndatum = nc_diag_read_get_dim(ftin,'nobs')
894  if (header_fix%angord > 0) then
895  nangord = nc_diag_read_get_dim(ftin,'BC_angord_arr_dim')
896  end if
897 
898  nrecord = ndatum / header_fix%nchan
899  diag_status%num_records = nrecord
900 
901  allocate( channel_index(ndatum), &
902  latitude(ndatum), longitude(ndatum), elevation(ndatum), &
903  obs_time(ndatum), scan_position(ndatum), sat_zenith_angle(ndatum), &
904  sat_azimuth_angle(ndatum), sol_zenith_angle(ndatum), sol_azimuth_angle(ndatum), &
905  sun_glint_angle(ndatum), water_fraction(ndatum), land_fraction(ndatum), &
906  ice_fraction(ndatum), snow_fraction(ndatum), water_temperature(ndatum), &
907  land_temperature(ndatum), ice_temperature(ndatum), snow_temperature(ndatum), &
908  soil_temperature(ndatum), soil_moisture(ndatum), tsavg5(ndatum), &
909  sstcu(ndatum), sstph(ndatum), sstnv(ndatum), &
910  dta(ndatum), dqa(ndatum), dtp_avh(ndatum), &
911  vegetation_fraction(ndatum), snow_depth(ndatum), tpwc_amsua(ndatum), &
912  clw_guess_retrieval(ndatum), sfc_wind_speed(ndatum), cloud_frac(ndatum), &
913  ctp(ndatum), clw(ndatum), tpwc(ndatum), &
914  clw_obs(ndatum), clw_guess(ndatum), foundation_temperature(ndatum), &
915  sst_warm_layer_dt(ndatum), sst_cool_layer_tdrop(ndatum), sst_dtz_dtfound(ndatum), &
916  observation(ndatum), obs_minus_forecast_adjusted(ndatum),obs_minus_forecast_unadjusted(ndatum), &
917  inverse_observation_error(ndatum),qc_flag(ndatum), emissivity(ndatum), &
918  weighted_lapse_rate(ndatum), dtb_dts(ndatum), bc_constant(ndatum), &
919  bc_scan_angle(ndatum), bc_cloud_liquid_water(ndatum), bc_lapse_rate_squared(ndatum), &
920  bc_lapse_rate(ndatum), bc_cosine_latitude_times_node(ndatum), bc_sine_latitude(ndatum), &
921  bc_emissivity(ndatum), bc_fixed_scan_position(ndatum), land_type_index(ndatum), &
922  scan_angle(ndatum) )
923 
924  if (header_fix%angord > 0) then
925  allocate( bc_angord(nangord, ndatum) )
926  end if
927 
928  if (allocated(diag_status%all_data_fix)) deallocate(diag_status%all_data_fix)
929  if (allocated(diag_status%all_data_chan)) deallocate(diag_status%all_data_chan)
930  if (allocated(diag_status%all_data_extra)) deallocate(diag_status%all_data_extra)
931  allocate( diag_status%all_data_fix(nrecord) )
932  allocate( diag_status%all_data_chan(nrecord, header_fix%nchan))
933  allocate( diag_status%all_data_extra(nrecord, header_fix%iextra, header_fix%jextra) )
934 
935  call nc_diag_read_get_var(ftin, 'Channel_Index', channel_index)
936  call nc_diag_read_get_var(ftin, 'Latitude', latitude)
937  call nc_diag_read_get_var(ftin, 'Longitude', longitude)
938  call nc_diag_read_get_var(ftin, 'Elevation', elevation)
939  call nc_diag_read_get_var(ftin, 'Obs_Time', obs_time)
940  call nc_diag_read_get_var(ftin, 'Scan_Position', scan_position)
941  call nc_diag_read_get_var(ftin, 'Scan_Angle', scan_angle)
942  call nc_diag_read_get_var(ftin, 'Sat_Zenith_Angle', sat_zenith_angle)
943  call nc_diag_read_get_var(ftin, 'Sat_Azimuth_Angle', sat_azimuth_angle)
944  call nc_diag_read_get_var(ftin, 'Sol_Zenith_Angle', sol_zenith_angle)
945  call nc_diag_read_get_var(ftin, 'Sol_Azimuth_Angle', sol_azimuth_angle)
946  call nc_diag_read_get_var(ftin, 'Sun_Glint_Angle', sun_glint_angle)
947  call nc_diag_read_get_var(ftin, 'Water_Fraction', water_fraction)
948  call nc_diag_read_get_var(ftin, 'Land_Fraction', land_fraction)
949  call nc_diag_read_get_var(ftin, 'Ice_Fraction', ice_fraction)
950  call nc_diag_read_get_var(ftin, 'Snow_Fraction', snow_fraction)
951  call nc_diag_read_get_var(ftin, 'Water_Temperature', water_temperature)
952  call nc_diag_read_get_var(ftin, 'Land_Temperature', land_temperature)
953  call nc_diag_read_get_var(ftin, 'Ice_Temperature', ice_temperature)
954  call nc_diag_read_get_var(ftin, 'Snow_Temperature', snow_temperature)
955  call nc_diag_read_get_var(ftin, 'Soil_Temperature', soil_temperature)
956  call nc_diag_read_get_var(ftin, 'Soil_Moisture', soil_moisture)
957  call nc_diag_read_get_var(ftin, 'tsavg5', tsavg5)
958  call nc_diag_read_get_var(ftin, 'sstcu', sstcu)
959  call nc_diag_read_get_var(ftin, 'sstph', sstph)
960  call nc_diag_read_get_var(ftin, 'sstnv', sstnv)
961  call nc_diag_read_get_var(ftin, 'dta', dta)
962  call nc_diag_read_get_var(ftin, 'dqa', dqa)
963  call nc_diag_read_get_var(ftin, 'dtp_avh', dtp_avh)
964  call nc_diag_read_get_var(ftin, 'Vegetation_Fraction', vegetation_fraction)
965  call nc_diag_read_get_var(ftin, 'Snow_Depth', snow_depth)
966  call nc_diag_read_get_var(ftin, 'tpwc_amsua', tpwc_amsua)
967  call nc_diag_read_get_var(ftin, 'clw_guess_retrieval', clw_guess_retrieval)
968  call nc_diag_read_get_var(ftin, 'Sfc_Wind_Speed', sfc_wind_speed)
969  call nc_diag_read_get_var(ftin, 'Cloud_Frac', cloud_frac)
970  call nc_diag_read_get_var(ftin,'CTP', ctp)
971  call nc_diag_read_get_var(ftin, 'CLW', clw)
972  call nc_diag_read_get_var(ftin, 'TPWC', tpwc)
973  call nc_diag_read_get_var(ftin, 'clw_obs', clw_obs)
974  call nc_diag_read_get_var(ftin, 'clw_guess', clw_guess)
975  call nc_diag_read_get_var(ftin, 'Foundation_Temperature', foundation_temperature)
976  call nc_diag_read_get_var(ftin, 'SST_Warm_layer_dt', sst_warm_layer_dt)
977  call nc_diag_read_get_var(ftin, 'SST_Cool_layer_tdrop', sst_cool_layer_tdrop)
978  call nc_diag_read_get_var(ftin, 'SST_dTz_dTfound', sst_dtz_dtfound)
979  call nc_diag_read_get_var(ftin, 'Observation', observation)
980  call nc_diag_read_get_var(ftin, 'Obs_Minus_Forecast_adjusted', obs_minus_forecast_adjusted)
981  call nc_diag_read_get_var(ftin, 'Obs_Minus_Forecast_unadjusted', obs_minus_forecast_unadjusted)
982  call nc_diag_read_get_var(ftin, 'Inverse_Observation_Error', inverse_observation_error)
983  call nc_diag_read_get_var(ftin, 'QC_Flag', qc_flag)
984  call nc_diag_read_get_var(ftin, 'Emissivity', emissivity)
985  call nc_diag_read_get_var(ftin, 'Weighted_Lapse_Rate', weighted_lapse_rate)
986  call nc_diag_read_get_var(ftin, 'dTb_dTs', dtb_dts)
987  call nc_diag_read_get_var(ftin, 'BC_Constant', bc_constant)
988  call nc_diag_read_get_var(ftin, 'BC_Scan_Angle', bc_scan_angle)
989  call nc_diag_read_get_var(ftin, 'BC_Cloud_Liquid_Water', bc_cloud_liquid_water)
990  call nc_diag_read_get_var(ftin, 'BC_Lapse_Rate_Squared', bc_lapse_rate_squared)
991  call nc_diag_read_get_var(ftin, 'BC_Lapse_Rate', bc_lapse_rate)
992  call nc_diag_read_get_var(ftin, 'BC_Cosine_Latitude_times_Node', bc_cosine_latitude_times_node)
993  call nc_diag_read_get_var(ftin, 'BC_Sine_Latitude', bc_sine_latitude)
994  call nc_diag_read_get_var(ftin, 'BC_Emissivity', bc_emissivity)
995  call nc_diag_read_get_var(ftin, 'BC_Fixed_Scan_Position', bc_fixed_scan_position)
996  call nc_diag_read_get_var(ftin, 'Land_Type_Index', land_type_index)
997  if (header_fix%angord > 0) then
998  call nc_diag_read_get_var(ftin, 'BC_angord ', bc_angord )
999  end if
1000  cdatum = 1
1001 
1002 ! allocate( all_data_fix(nrecord) )
1003 ! allocate( all_data_chan(nrecord, nchan))
1004 
1005 
1006  do ir=1,nrecord
1007  clat = latitude(cdatum)
1008  clon = longitude(cdatum)
1009  diag_status%all_data_fix(ir)%lat = latitude(cdatum)
1010  diag_status%all_data_fix(ir)%lon = longitude(cdatum)
1011  diag_status%all_data_fix(ir)%zsges = elevation(cdatum)
1012  diag_status%all_data_fix(ir)%obstime = obs_time(cdatum)
1013  diag_status%all_data_fix(ir)%senscn_pos = scan_position(cdatum)
1014  diag_status%all_data_fix(ir)%senscn_ang = scan_angle(cdatum)
1015  diag_status%all_data_fix(ir)%satzen_ang = sat_zenith_angle(cdatum)
1016  diag_status%all_data_fix(ir)%satazm_ang = sat_azimuth_angle(cdatum)
1017  diag_status%all_data_fix(ir)%solzen_ang = sol_zenith_angle(cdatum)
1018  diag_status%all_data_fix(ir)%solazm_ang = sol_azimuth_angle(cdatum)
1019  diag_status%all_data_fix(ir)%sungln_ang = sun_glint_angle(cdatum)
1020  diag_status%all_data_fix(ir)%water_frac = water_fraction(cdatum)
1021  diag_status%all_data_fix(ir)%land_frac = land_fraction(cdatum)
1022  diag_status%all_data_fix(ir)%ice_frac = ice_fraction(cdatum)
1023  diag_status%all_data_fix(ir)%snow_frac = snow_fraction(cdatum)
1024  diag_status%all_data_fix(ir)%water_temp = water_temperature(cdatum)
1025  diag_status%all_data_fix(ir)%land_temp = land_temperature(cdatum)
1026  diag_status%all_data_fix(ir)%ice_temp = ice_temperature(cdatum)
1027  diag_status%all_data_fix(ir)%snow_temp = snow_temperature(cdatum)
1028  diag_status%all_data_fix(ir)%soil_temp = soil_temperature(cdatum)
1029  diag_status%all_data_fix(ir)%soil_mois = soil_moisture(cdatum)
1030  diag_status%all_data_fix(ir)%land_type = land_type_index(cdatum)
1031  diag_status%all_data_fix(ir)%veg_frac = vegetation_fraction(cdatum)
1032  diag_status%all_data_fix(ir)%snow_depth = snow_depth(cdatum)
1033  diag_status%all_data_fix(ir)%sfc_wndspd = sfc_wind_speed(cdatum)
1034  diag_status%all_data_fix(ir)%qcdiag1 = cloud_frac(cdatum)
1035  diag_status%all_data_fix(ir)%qcdiag2 = ctp(cdatum)
1036  diag_status%all_data_fix(ir)%tref = foundation_temperature(cdatum)
1037  diag_status%all_data_fix(ir)%dtw = sst_warm_layer_dt(cdatum)
1038  diag_status%all_data_fix(ir)%dtc = sst_cool_layer_tdrop(cdatum)
1039  diag_status%all_data_fix(ir)%tz_tr = sst_dtz_dtfound(cdatum)
1040 
1041  if (retrieval) then
1042  diag_status%all_data_fix(ir)%water_temp = tsavg5(cdatum)
1043  diag_status%all_data_fix(ir)%land_temp = sstcu(cdatum)
1044  diag_status%all_data_fix(ir)%ice_temp = sstph(cdatum)
1045  diag_status%all_data_fix(ir)%snow_temp = sstnv(cdatum)
1046  diag_status%all_data_fix(ir)%soil_temp = dta(cdatum)
1047  diag_status%all_data_fix(ir)%soil_mois = dqa(cdatum)
1048  diag_status%all_data_fix(ir)%land_type = dtp_avh(cdatum)
1049  endif
1050 
1051  do ic=1,header_fix%nchan
1052  if (clat .ne. latitude(cdatum) .or. clon .ne. longitude(cdatum)) then
1053  write(*,*) 'ERROR: Lats & Lons are mismatched. This is bad'
1054  print *,'irecord=',ir
1055  print *,'clat,clon=',clat,clon
1056  print *,'lat/lon(datum)=',latitude(cdatum), longitude(cdatum)
1057  call abort
1058  endif
1059  cch = channel_index(cdatum)
1060  if (allocated(diag_status%all_data_chan(ir,cch)%bifix)) deallocate(diag_status%all_data_chan(ir,cch)%bifix )
1061  if (header_fix%angord > 0) then
1062  allocate(diag_status%all_data_chan(ir,cch)%bifix(nangord))
1063  else
1064  allocate(diag_status%all_data_chan(ir,cch)%bifix(1))
1065  end if
1066 
1067  diag_status%all_data_chan(ir,cch)%tbobs = observation(cdatum)
1068  diag_status%all_data_chan(ir,cch)%omgbc = obs_minus_forecast_adjusted(cdatum)
1069  diag_status%all_data_chan(ir,cch)%omgnbc= obs_minus_forecast_unadjusted(cdatum)
1070  diag_status%all_data_chan(ir,cch)%errinv= inverse_observation_error(cdatum)
1071  diag_status%all_data_chan(ir,cch)%qcmark= qc_flag(cdatum)
1072  diag_status%all_data_chan(ir,cch)%emiss = emissivity(cdatum)
1073  diag_status%all_data_chan(ir,cch)%tlap = weighted_lapse_rate(cdatum)
1074  diag_status%all_data_chan(ir,cch)%tb_tz = dtb_dts(cdatum)
1075  diag_status%all_data_chan(ir,cch)%bicons= bc_constant(cdatum)
1076  diag_status%all_data_chan(ir,cch)%biang = bc_scan_angle(cdatum)
1077  diag_status%all_data_chan(ir,cch)%biclw = bc_cloud_liquid_water(cdatum)
1078  diag_status%all_data_chan(ir,cch)%bilap2= bc_lapse_rate_squared(cdatum)
1079  diag_status%all_data_chan(ir,cch)%bilap = bc_lapse_rate(cdatum)
1080  diag_status%all_data_chan(ir,cch)%bicos = bc_cosine_latitude_times_node(cdatum)
1081  diag_status%all_data_chan(ir,cch)%bisin = bc_sine_latitude(cdatum)
1082  diag_status%all_data_chan(ir,cch)%biemis= bc_emissivity(cdatum)
1083  if (header_fix%angord > 0) then
1084  diag_status%all_data_chan(ir,cch)%bifix = bc_angord(1:nangord,cdatum)
1085  else
1086  diag_status%all_data_chan(ir,cch)%bifix(1) = bc_fixed_scan_position(cdatum)
1087  endif
1088  ! placeholder for SST BC
1089  cdatum = cdatum + 1
1090  enddo
1091  enddo
1092 
1093  diag_status%nc_read = .true.
1094  diag_status%cur_ob_idx = 1
1095 end subroutine read_radiag_data_nc_init
1096 
1097 subroutine read_radiag_data_nc(ftin,diag_status,header_fix,retrieval,data_fix,data_chan,data_extra,iflag )
1098 ! . . . .
1099 ! subprogram: read_radiag_dat read rad diag data
1100 ! prgmmr: tahara org: np20 date: 2015-08-10
1101 !
1102 ! abstract: This routine reads the data record from a netcdf radiance
1103 ! diagnostic file
1104 !
1105 ! program history log:
1106 ! 2015-08-10 mccarty - create routine
1107 !
1108 ! input argument list:
1109 ! ftin - unit number connected to diagnostic file
1110 ! header_fix - header information structure
1111 !
1112 ! output argument list:
1113 ! data_fix - spot header information structure
1114 ! data_chan - spot channel information structure
1115 ! data_extra - spot extra information
1116 ! iflag - error code
1117 !
1118 ! attributes:
1119 ! language: f90
1120 ! machine: ibm RS/6000 SP
1121 !
1122 !$$$
1123 
1124 ! Declare passed arguments
1125  integer(i_kind),intent(in) :: ftin
1126  type(ncdiag_status), intent(inout) :: diag_status
1127  type(diag_header_fix_list ),intent(in) :: header_fix
1128  logical,intent(in) :: retrieval
1129  type(diag_data_fix_list) ,intent(out):: data_fix
1130  type(diag_data_chan_list) ,allocatable :: data_chan(:)
1131  type(diag_data_extra_list) ,allocatable :: data_extra(:,:)
1132  integer(i_kind),intent(out) :: iflag
1133 
1134  iflag = 0
1135  if (.not. allocated(data_chan)) allocate(data_chan(header_fix%nchan) )
1136  if (.not. allocated(data_extra)) allocate(data_extra(header_fix%iextra, header_fix%nchan) )
1137 
1138  data_fix = diag_status%all_data_fix(diag_status%cur_ob_idx)
1139  data_chan(:) = diag_status%all_data_chan(diag_status%cur_ob_idx,:)
1140  data_extra(:,:) = diag_status%all_data_extra(diag_status%cur_ob_idx,:,:)
1141 
1142  diag_status%cur_ob_idx = diag_status%cur_ob_idx + 1
1143 
1144 end subroutine read_radiag_data_nc
1145 
1146 subroutine read_radiag_data_bin(ftin,header_fix,retrieval,data_fix,data_chan,data_extra,iflag )
1147 ! . . . .
1148 ! subprogram: read_radiag_dat read rad diag data
1149 ! prgmmr: tahara org: np20 date: 2003-01-01
1150 !
1151 ! abstract: This routine reads the data record from a radiance
1152 ! diagnostic file
1153 !
1154 ! program history log:
1155 ! 2010-10-05 treadon - add this doc block
1156 ! 2011-02-22 kleist - changes related to memory allocation
1157 ! 2017-07-17 mccarty - rename binary-specific procedure
1158 !
1159 ! input argument list:
1160 ! ftin - unit number connected to diagnostic file
1161 ! header_fix - header information structure
1162 !
1163 ! output argument list:
1164 ! data_fix - spot header information structure
1165 ! data_chan - spot channel information structure
1166 ! data_extra - spot extra information
1167 ! iflag - error code
1168 !
1169 ! attributes:
1170 ! language: f90
1171 ! machine: ibm RS/6000 SP
1172 !
1173 !$$$
1174 
1175 
1176 ! Declare passed arguments
1177  integer(i_kind),intent(in) :: ftin
1178  type(diag_header_fix_list ),intent(in) :: header_fix
1179  logical,intent(in) :: retrieval
1180  type(diag_data_fix_list) ,intent(out):: data_fix
1181  type(diag_data_chan_list) ,allocatable :: data_chan(:)
1182  type(diag_data_extra_list) ,allocatable :: data_extra(:,:)
1183  integer(i_kind),intent(out) :: iflag
1184 
1185  integer(i_kind) :: ich,iang,i,j
1186  real(r_single),dimension(:,:),allocatable :: data_tmp
1187  real(r_single),dimension(:),allocatable :: fix_tmp
1188  real(r_single),dimension(:,:),allocatable :: extra_tmp
1189 
1190 ! Allocate arrays as needed
1191  if (allocated(data_chan)) deallocate(data_chan)
1192  allocate(data_chan(header_fix%nchan))
1193 
1194  do ich=1,header_fix%nchan
1195  if (allocated(data_chan(ich)%bifix)) deallocate(data_chan(ich)%bifix)
1196  allocate(data_chan(ich)%bifix(header_fix%angord+1))
1197  end do
1198 
1199  if (header_fix%iextra > 0) then
1200  if (allocated(data_extra)) deallocate(data_extra)
1201  allocate(data_extra(header_fix%iextra,header_fix%jextra))
1202  allocate(extra_tmp(header_fix%iextra,header_fix%jextra))
1203  end if
1204 
1205 ! Allocate arrays to hold data record
1206  allocate(data_tmp(header_fix%idiag,header_fix%nchan))
1207 
1208  if (header_fix%iversion < iversion_radiag_2) then
1209  allocate( fix_tmp( ireal_old_radiag ) )
1210  else
1211  allocate( fix_tmp( ireal_radiag ) )
1212  end if
1213 
1214 ! Read data record
1215 
1216  if (header_fix%iextra == 0) then
1217  read(ftin,iostat=iflag) fix_tmp, data_tmp
1218  else
1219  read(ftin,iostat=iflag) fix_tmp, data_tmp, extra_tmp
1220  endif
1221 
1222  if (iflag /= 0) return
1223 
1224 ! Transfer fix_tmp record to output structure
1225  data_fix%lat = fix_tmp(1)
1226  data_fix%lon = fix_tmp(2)
1227  data_fix%zsges = fix_tmp(3)
1228  data_fix%obstime = fix_tmp(4)
1229  data_fix%senscn_pos = fix_tmp(5)
1230  data_fix%satzen_ang = fix_tmp(6)
1231  data_fix%satazm_ang = fix_tmp(7)
1232  data_fix%solzen_ang = fix_tmp(8)
1233  data_fix%solazm_ang = fix_tmp(9)
1234  data_fix%sungln_ang = fix_tmp(10)
1235  data_fix%water_frac = fix_tmp(11)
1236  data_fix%land_frac = fix_tmp(12)
1237  data_fix%ice_frac = fix_tmp(13)
1238  data_fix%snow_frac = fix_tmp(14)
1239  data_fix%water_temp = fix_tmp(15)
1240  data_fix%land_temp = fix_tmp(16)
1241  data_fix%ice_temp = fix_tmp(17)
1242  data_fix%snow_temp = fix_tmp(18)
1243  data_fix%soil_temp = fix_tmp(19)
1244  data_fix%soil_mois = fix_tmp(20)
1245  data_fix%land_type = fix_tmp(21)
1246  data_fix%veg_frac = fix_tmp(22)
1247  data_fix%snow_depth = fix_tmp(23)
1248  data_fix%sfc_wndspd = fix_tmp(24)
1249  data_fix%qcdiag1 = fix_tmp(25)
1250  data_fix%qcdiag2 = fix_tmp(26)
1251 
1252  if ( header_fix%iversion <= iversion_radiag_1 ) then
1253  data_fix%tref = rmiss_radiag
1254  data_fix%dtw = rmiss_radiag
1255  data_fix%dtc = rmiss_radiag
1256  data_fix%tz_tr = rmiss_radiag
1257  else
1258  data_fix%tref = fix_tmp(27)
1259  data_fix%dtw = fix_tmp(28)
1260  data_fix%dtc = fix_tmp(29)
1261  data_fix%tz_tr = fix_tmp(30)
1262  end if
1263 
1264 
1265 ! Transfer data record to output structure
1266  do ich=1,header_fix%nchan
1267  data_chan(ich)%tbobs =data_tmp(1,ich)
1268  data_chan(ich)%omgbc =data_tmp(2,ich)
1269  data_chan(ich)%omgnbc=data_tmp(3,ich)
1270  data_chan(ich)%errinv=data_tmp(4,ich)
1271  data_chan(ich)%qcmark=data_tmp(5,ich)
1272  data_chan(ich)%emiss =data_tmp(6,ich)
1273  data_chan(ich)%tlap =data_tmp(7,ich)
1274  data_chan(ich)%tb_tz =data_tmp(8,ich)
1275  end do
1276  if (header_fix%iversion < iversion_radiag_1) then
1277  do ich=1,header_fix%nchan
1278  data_chan(ich)%bifix(1)=data_tmp(8,ich)
1279  data_chan(ich)%bilap =data_tmp(9,ich)
1280  data_chan(ich)%bilap2 =data_tmp(10,ich)
1281  data_chan(ich)%bicons =data_tmp(11,ich)
1282  data_chan(ich)%biang =data_tmp(12,ich)
1283  data_chan(ich)%biclw =data_tmp(13,ich)
1284  data_chan(ich)%bisst = rmiss_radiag
1285  if (retrieval) then
1286  data_chan(ich)%biclw =rmiss_radiag
1287  data_chan(ich)%bisst =data_tmp(13,ich)
1288  endif
1289  end do
1290  elseif ( header_fix%iversion < iversion_radiag_2 .and. header_fix%iversion >= iversion_radiag_1 ) then
1291  do ich=1,header_fix%nchan
1292  data_chan(ich)%bicons=data_tmp(8,ich)
1293  data_chan(ich)%biang =data_tmp(9,ich)
1294  data_chan(ich)%biclw =data_tmp(10,ich)
1295  data_chan(ich)%bilap2=data_tmp(11,ich)
1296  data_chan(ich)%bilap =data_tmp(12,ich)
1297  end do
1298  do ich=1,header_fix%nchan
1299  do iang=1,header_fix%angord+1
1300  data_chan(ich)%bifix(iang)=data_tmp(12+iang,ich)
1301  end do
1302  data_chan(ich)%bisst = data_tmp(12+header_fix%angord+2,ich)
1303  end do
1304  elseif ( header_fix%iversion < iversion_radiag_3 .and. header_fix%iversion >= iversion_radiag_2 ) then
1305  do ich=1,header_fix%nchan
1306  data_chan(ich)%bicons=data_tmp(9,ich)
1307  data_chan(ich)%biang =data_tmp(10,ich)
1308  data_chan(ich)%biclw =data_tmp(11,ich)
1309  data_chan(ich)%bilap2=data_tmp(12,ich)
1310  data_chan(ich)%bilap =data_tmp(13,ich)
1311  end do
1312  do ich=1,header_fix%nchan
1313  do iang=1,header_fix%angord+1
1314  data_chan(ich)%bifix(iang)=data_tmp(13+iang,ich)
1315  end do
1316  data_chan(ich)%bisst = data_tmp(13+header_fix%angord+2,ich)
1317  end do
1318  elseif ( header_fix%iversion < iversion_radiag_4 .and. header_fix%iversion >= iversion_radiag_3 ) then
1319  do ich=1,header_fix%nchan
1320  data_chan(ich)%bicons=data_tmp(9,ich)
1321  data_chan(ich)%biang =data_tmp(10,ich)
1322  data_chan(ich)%biclw =data_tmp(11,ich)
1323  data_chan(ich)%bilap2=data_tmp(12,ich)
1324  data_chan(ich)%bilap =data_tmp(13,ich)
1325  data_chan(ich)%bicos =data_tmp(14,ich) ! 1st bias correction term node*cos(lat) for SSMIS
1326  data_chan(ich)%bisin =data_tmp(15,ich) ! 2nd bias correction term sin(lat) for SSMI
1327  end do
1328  do ich=1,header_fix%nchan
1329  do iang=1,header_fix%angord+1
1330  data_chan(ich)%bifix(iang)=data_tmp(15+iang,ich)
1331  end do
1332  data_chan(ich)%bisst = data_tmp(15+header_fix%angord+2,ich)
1333  end do
1334  else
1335  do ich=1,header_fix%nchan
1336  data_chan(ich)%bicons=data_tmp(9,ich)
1337  data_chan(ich)%biang =data_tmp(10,ich)
1338  data_chan(ich)%biclw =data_tmp(11,ich)
1339  data_chan(ich)%bilap2=data_tmp(12,ich)
1340  data_chan(ich)%bilap =data_tmp(13,ich)
1341  data_chan(ich)%bicos =data_tmp(14,ich)
1342  data_chan(ich)%bisin =data_tmp(15,ich)
1343  data_chan(ich)%biemis=data_tmp(16,ich)
1344  end do
1345  do ich=1,header_fix%nchan
1346  do iang=1,header_fix%angord+1
1347  data_chan(ich)%bifix(iang)=data_tmp(16+iang,ich)
1348  end do
1349  data_chan(ich)%bisst = data_tmp(16+header_fix%angord+2,ich)
1350  end do
1351 
1352  endif
1353 
1354  if (header_fix%iextra > 0) then
1355  do j=1,header_fix%jextra
1356  do i=1,header_fix%iextra
1357  data_extra(i,j)%extra=extra_tmp(i,j)
1358  end do
1359  end do
1360  endif
1361 
1362  deallocate(data_tmp, fix_tmp)
1363  if (header_fix%iextra > 0) deallocate(extra_tmp)
1364 
1365 end subroutine read_radiag_data_bin
1366 
1367 end module read_diag
1368 
integer(i_kind), parameter iversion_radiag_2
Definition: read_diag.f90:177
integer(i_kind), save nopen_ncdiag
Definition: read_diag.f90:195
subroutine, public read_radiag_header(ftin, npred_radiag, retrieval, header_fix, header_chan, data_name, iflag, lverbose)
Definition: read_diag.f90:321
integer(i_kind), parameter, public ipchan_radiag
Definition: read_diag.f90:78
integer, parameter, public i_kind
Definition: ncd_kinds.F90:71
subroutine get_radiag_int_(what, iv, ier)
Definition: read_diag.f90:213
integer(i_kind) function find_ncdiag_id(ftin)
Definition: read_diag.f90:713
integer(i_kind), parameter iversion_radiag_3
Definition: read_diag.f90:178
subroutine read_radiag_data_nc(ftin, diag_status, header_fix, retrieval, data_fix, data_chan, data_extra, iflag)
Definition: read_diag.f90:1098
integer(i_kind), parameter max_open_ncdiag
Definition: read_diag.f90:194
subroutine, public set_netcdf_read(use_netcdf)
Definition: read_diag.f90:224
type(ncdiag_status), dimension(max_open_ncdiag), save ncdiag_open_status
Definition: read_diag.f90:197
integer(i_kind), parameter ireal_old_radiag
Definition: read_diag.f90:77
subroutine read_radiag_header_bin(ftin, npred_radiag, retrieval, header_fix, header_chan, data_name, iflag, lverbose)
Definition: read_diag.f90:467
subroutine, public read_radiag_data(ftin, header_fix, retrieval, data_fix, data_chan, data_extra, iflag)
Definition: read_diag.f90:729
integer(i_kind), dimension(max_open_ncdiag), save ncdiag_open_id
Definition: read_diag.f90:196
integer(i_kind), parameter iversion_radiag_1
Definition: read_diag.f90:176
subroutine, public close_radiag(filename, ftin)
Definition: read_diag.f90:287
subroutine set_radiag_int_(what, iv, ier)
Definition: read_diag.f90:202
subroutine read_radiag_data_nc_init(ftin, diag_status, header_fix, retrieval)
Definition: read_diag.f90:840
subroutine read_radiag_header_nc(ftin, npred_radiag, retrieval, header_fix, header_chan, data_name, iflag, lverbose)
Definition: read_diag.f90:370
logical, save netcdf
Definition: read_diag.f90:183
real(r_single), parameter rmiss_radiag
Definition: read_diag.f90:181
integer(i_kind), save iversion_radiag
Definition: read_diag.f90:175
subroutine read_radiag_data_bin(ftin, header_fix, retrieval, data_fix, data_chan, data_extra, iflag)
Definition: read_diag.f90:1147
integer(i_kind), parameter, public ireal_radiag
Definition: read_diag.f90:76
subroutine nc_diag_read_close(filename, file_ncdr_id, from_pop)
subroutine, public read_all_radiag(ftin, header_fix, retrieval, all_data_fix, all_data_chan, all_data_extra, nobs, iflag)
Definition: read_diag.f90:802
integer(i_kind), parameter iversion_radiag_4
Definition: read_diag.f90:179
subroutine, public open_radiag(filename, ftin)
Definition: read_diag.f90:248
integer, parameter, public r_single
Definition: ncd_kinds.F90:79
integer, parameter, public r_kind
Definition: ncd_kinds.F90:108
subroutine nc_diag_read_init(filename, file_ncdr_id, from_push)