m_obs.f90 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. # 0 "<stdin>"
  2. # 0 "<built-in>"
  3. # 0 "<command-line>"
  4. # 1 "/usr/include/stdc-predef.h" 1 3 4
  5. # 17 "/usr/include/stdc-predef.h" 3 4
  6. # 2 "<command-line>" 2
  7. # 1 "<stdin>"
  8. # 10 "<stdin>"
  9. ! File: m_obs.F90
  10. !
  11. ! Created: 6 Feb 2008
  12. !
  13. ! Last modified: 21 Feb 2008
  14. !
  15. ! Author: Pavel Sakov*
  16. ! NERSC
  17. !
  18. ! Purpose: Generic code to deal with observations.
  19. !
  20. ! Description: This module contains the following functions and subroutines:
  21. ! - obs_setobs
  22. ! reads the observations into allocatable array obs(nobs)
  23. ! of type(measurement)
  24. ! - obs_prepareobs
  25. ! conducts state-dependent pre-processing of observations
  26. ! - obs_prepareuobs
  27. ! conducts state-dependent pre-processing of observations
  28. ! of a given type
  29. ! It also contains the following data:
  30. ! - obs
  31. ! allocatable array of type(measurement)
  32. ! - nobs
  33. ! number of observations (may differ from the size of the
  34. ! array)
  35. !
  36. ! * This file contains some modified code of unknown origin
  37. ! from EnKF package. In particular, the code here supersedes
  38. ! the code from:
  39. ! m_get_nrobs_d.F90
  40. ! m_get_obs_d.F90
  41. !
  42. ! Modifications:
  43. ! 09/11/2012 Geir Arne Waagbo:
  44. ! -- Added support for OSISAF ice drift obs
  45. ! 29/07/2010 PS:
  46. ! -- modified obs_QC(). The maximal increment now does not go to
  47. ! 0 as the innovation increases, but rather is limited by
  48. ! KMAX * sigma_ens
  49. ! 29/06/2010 PS:
  50. ! -- added obs_QC()
  51. ! 26/02/2008 PS:
  52. ! -- put "obs" and "nobs" as public data in this module
  53. module m_obs
  54. use qmpi
  55. use mod_measurement
  56. use m_uobs
  57. use m_insitu
  58. implicit none
  59. !
  60. ! public stuff
  61. !
  62. integer, public :: nobs = -1
  63. type(measurement), allocatable, dimension(:), public :: obs
  64. public obs_readobs
  65. public obs_prepareobs
  66. public obs_QC
  67. !
  68. ! private stuff
  69. !
  70. private obs_testrange
  71. integer, parameter, private :: STRLEN = 512
  72. real, parameter, private :: TEM_MIN = -2.0d0
  73. real, parameter, private :: TEM_MAX = 50.0d0
  74. real, parameter, private :: SAL_MIN = 2.0d0
  75. real, parameter, private :: SAL_MAX = 40.0d0
  76. real, parameter, private :: SSH_MIN = -3.0d0
  77. real, parameter, private :: SSH_MAX = 3.0d0
  78. real, parameter, private :: ICEC_MIN = 0.0d0
  79. real, parameter, private :: ICEC_MAX = 0.999d0 ! [FM] Changed from 0.996 to 0.999
  80. real, parameter, private :: RFB_MIN = 0.0d0 ! FM 2020
  81. real, parameter, private :: RFB_MAX = 10.0d0
  82. real, parameter, private :: VT_I_MIN = 0.0d0 ! FM 2020
  83. real, parameter, private :: VT_I_MAX = 10.0d0
  84. real, parameter, private :: UVICE_MIN = -100.0
  85. real, parameter, private :: UVICE_MAX = 100.0
  86. private obs_prepareuobs, obs_realloc
  87. contains
  88. ! Obtain observations to be used for assimilation from the file
  89. ! "observation.uf". Store the number of observations in "nobs" and the data
  90. ! in the array "obs".
  91. !
  92. subroutine obs_readobs
  93. use m_parameters
  94. logical :: exists = .false.
  95. type(measurement) :: record
  96. integer :: rsize
  97. integer :: ios
  98. integer :: o
  99. CHARACTER(LEN=*), PARAMETER :: &
  100. FMT2 = "(f8.4,X,f8.4,X,a8,X,2(f10.5,X),f4.2,X,2(I3,X),I1,X,4(f5.2,X),L,X,2(I3,X),f5.2,X,I8,X,I1)"
  101. real :: myX
  102. real :: myY
  103. !========== TEST
  104. ! inquire(iolength = rsize) record
  105. ! !open(10, file = 'test.txt', form = 'unformatted',&
  106. ! ! access = 'direct', recl = rsize, status = 'old')
  107. ! allocate(obs(2))
  108. ! open(10, file = 'observations.txt')!, form = 'unformatted',&
  109. ! !access = 'direct', recl = rsize, status = 'old')
  110. ! !read(10, *) obs(1)
  111. !
  112. ! do o = 1, 2
  113. ! read(10, *) obs(o)
  114. ! PRINT *, obs(o)
  115. ! end do
  116. ! close(10)
  117. ! stop
  118. !==========
  119. if (nobs >= 0) then
  120. return
  121. end if
  122. ! Testing existence of file
  123. inquire(file = 'observations.txt', exist = exists)
  124. !inquire(file = 'observations.uf', exist = exists)
  125. if (.not. exists) then
  126. if (master) then
  127. print *, 'ERROR: obs_getnobs(): file "observations.txt" does not exist'
  128. end if
  129. stop
  130. end if
  131. inquire(iolength = rsize) record
  132. open(10, file = 'observations.txt')!, form = 'unformatted',&
  133. ! EXPERIMENTAL
  134. !open(10, file = 'observations.uf', form = 'unformatted',&
  135. ! access = 'direct', recl = rsize, status = 'old')!, form = 'unformatted',&
  136. !access = 'direct', recl = rsize, status = 'old')
  137. ! END EXPERIMENTAL
  138. ! I guess there is no other way to work out the length other than read the
  139. ! file in fortran - PS
  140. !
  141. o = 1
  142. do while (.true.)
  143. read(10, *, iostat = ios) record
  144. if (ios /= 0) then
  145. nobs = o - 1
  146. exit
  147. end if
  148. o = o + 1
  149. enddo
  150. allocate(obs(nobs))
  151. ! PS - there were problem with using rewind(): g95 reported:
  152. ! "Cannot REWIND a file opened for DIRECT access". Therefore reopen.
  153. !
  154. close(10)
  155. open(10, file = 'observations.txt')!, form = 'unformatted',&
  156. ! BEGIN EXPERIMENTAL
  157. !open(10, file = 'observations.uf', form = 'unformatted',&
  158. ! access = 'direct', recl = rsize, status = 'old')
  159. ! -- END EXPERIMENTAL
  160. do o = 1, nobs
  161. read(10, *) obs(o)
  162. call ucase(obs(o) % id)
  163. !PRINT *, obs(o)
  164. enddo
  165. close(10)
  166. if (RFACTOR1 /= 1.0d0) then
  167. do o = 1, nobs
  168. obs(o) % var = obs(o) % var * RFACTOR1
  169. end do
  170. end if
  171. call uobs_get(obs % id, nobs, master)
  172. call obs_testrange
  173. end subroutine obs_readobs
  174. subroutine obs_testrange
  175. integer :: o, uo, nbad
  176. real :: dmin, dmax
  177. if (master) then
  178. print '(a)', ' EnKF: testing range for each type of obs '
  179. end if
  180. do uo = 1, nuobs
  181. if (trim(unique_obs(uo)) == 'SST' .or. trim(unique_obs(uo)) == 'TEM'&
  182. .or. trim(unique_obs(uo)) == 'GTEM') then
  183. dmin = TEM_MIN
  184. dmax = TEM_MAX
  185. elseif (trim(unique_obs(uo)) == 'SAL'&
  186. .or. trim(unique_obs(uo)) == 'GSAL') then
  187. dmin = SAL_MIN
  188. dmax = SAL_MAX
  189. elseif (trim(unique_obs(uo)) == 'SLA'&
  190. .or. trim(unique_obs(uo)) == 'TSLA'&
  191. .or. trim(unique_obs(uo)) == 'SSH') then
  192. dmin = SSH_MIN
  193. dmax = SSH_MAX
  194. elseif (trim(unique_obs(uo)) == 'ICEC') then
  195. dmin = ICEC_MIN
  196. dmax = ICEC_MAX
  197. elseif (trim(unique_obs(uo)) == 'AT_I') then ! [FM] Added as we assimilate total ice conc. (opposed to indiv. category
  198. dmin = ICEC_MIN
  199. dmax = ICEC_MAX
  200. elseif (trim(unique_obs(uo)) == 'RFB') then ! FM added 2020
  201. dmin = RFB_MIN
  202. dmax = RFB_MAX
  203. elseif (trim(unique_obs(uo)) == 'VT_I') then ! FM added 2021
  204. dmin = VT_I_MIN
  205. dmax = VT_I_MAX
  206. elseif (trim(unique_obs(uo)) == 'V_ICE'&
  207. .or. trim(unique_obs(uo)) == 'U_ICE') then
  208. dmin = UVICE_MIN
  209. dmax = UVICE_MAX
  210. elseif (trim(unique_obs(uo)) == 'U2D_I'& ! [FM] OSISAF 2-day sea ice drift converted to m/s and interpolated onto ORCA
  211. .OR. trim(unique_obs(uo)) == 'V2D_I') THEN
  212. dmin = UVICE_MIN
  213. dmax = UVICE_MAX
  214. elseif ((index(trim(unique_obs(uo)),'DX') .gt. 0) &
  215. .or. (index(trim(unique_obs(uo)),'DY') .gt. 0)) then
  216. ! The type can be DX1,DX2,..,DX5,DY1,..DY5
  217. dmin = UVICE_MIN
  218. dmax = UVICE_MAX
  219. else
  220. dmin = -1.0d6
  221. dmax = 1.0d6
  222. print *, 'ERROR: obs_testrange(): "', trim(unique_obs(uo)), '": unknown type'
  223. stop
  224. end if
  225. nbad = 0
  226. do o = uobs_begin(uo), uobs_end(uo)
  227. if (obs(o) % status .and.&
  228. (obs(o) % d < dmin .or. obs(o) % d > dmax)) then
  229. obs(o) % status = .false.
  230. nbad = nbad + 1
  231. end if
  232. end do
  233. if (master) then
  234. print '(a, a, a, i6, a)', ' ', trim(unique_obs(uo)), ': ', nbad, ' outliers'
  235. end if
  236. end do
  237. if (master) then
  238. print *
  239. end if
  240. end subroutine obs_testrange
  241. ! Prepare observations before allocating matrices S, D, and A in EnKF().
  242. ! This invloves mainly thinning, superobing, or sorting.
  243. !
  244. ! Note that generically this processing can not be completely outsourced
  245. ! to the preprocessing stage, at least for in-situ data, because its thinning
  246. ! involves reading ensemble members for layer depth information.
  247. !
  248. subroutine obs_prepareobs()
  249. implicit none
  250. integer :: iuobs
  251. if (master) then
  252. print '(a)', ' EnKF: preparing observations'
  253. end if
  254. do iuobs = 1, nuobs
  255. call obs_prepareuobs(trim(unique_obs(iuobs)))
  256. end do
  257. ! calculate again the number of observation of each type (that could change
  258. ! in prepare_obs)
  259. call uobs_get(obs % id, nobs, master)
  260. end subroutine obs_prepareobs
  261. ! Prepare (thin, superob) observations of type "obstag".
  262. !
  263. subroutine obs_prepareuobs(obstag)
  264. character(*), intent(in) :: obstag
  265. character(STRLEN) :: fname
  266. if (trim(obstag) == 'SAL' .or. trim(obstag) == 'TEM' .or.&
  267. trim(obstag) == 'GSAL' .or. trim(obstag) == 'GTEM') then
  268. call insitu_prepareobs(trim(obstag), nobs, obs)
  269. if (master) then
  270. write(fname, '(a, ".nc")') trim(obstag)
  271. print *, 'Writing "', trim(obstag), '" obs to be assimilated to "',&
  272. trim(fname), '"'
  273. call insitu_writeprofiles(fname, trim(obstag), nobs, obs);
  274. end if
  275. else
  276. ! do nothing for obs of other types for now
  277. end if
  278. call obs_realloc
  279. end subroutine obs_prepareuobs
  280. subroutine obs_realloc()
  281. type(measurement), allocatable :: newobs(:)
  282. if (nobs < 0 .or. nobs == size(obs)) then
  283. return
  284. end if
  285. allocate(newobs(nobs))
  286. newobs = obs(1 : nobs)
  287. deallocate(obs)
  288. allocate(obs(nobs))
  289. obs = newobs
  290. deallocate(newobs)
  291. end subroutine obs_realloc
  292. subroutine obs_QC(m, S)
  293. use m_parameters
  294. implicit none
  295. integer :: m
  296. real :: S(nobs, m)
  297. integer :: nmodified(nuobs)
  298. real :: so(m), smean, svar
  299. integer :: o, uo
  300. real :: ovar, inn, newovar
  301. if (master) then
  302. print *, 'Starting generic observation QC'
  303. end if
  304. nmodified = 0
  305. do uo = 1, nuobs
  306. do o = uobs_begin(uo), uobs_end(uo)
  307. so = S(o, :);
  308. smean = sum(so) / m ! must be 0...
  309. svar = sum((so - smean) ** 2) / real(m - 1)
  310. ovar = obs(o) % var
  311. inn = obs(o) % d - smean
  312. obs(o) % var = sqrt((svar + ovar) ** 2 +&
  313. svar * (inn / KFACTOR) ** 2) - svar
  314. if (svar > 0 .and. obs(o) % var / ovar > 2.0d0) then
  315. nmodified(uo) = nmodified(uo) + 1
  316. end if
  317. end do
  318. end do
  319. if (master) then
  320. do uo = 1, nuobs
  321. print *, ' ', trim(unique_obs(uo)), ':'
  322. print *, ' # of observations:', uobs_end(uo) - uobs_begin(uo) + 1
  323. print *, ' (of them) substantially modified:', nmodified(uo)
  324. end do
  325. end if
  326. end subroutine obs_QC
  327. end module m_obs