m_read_FFI_glider.F90 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ! File: m_read_FFI_glider.F90
  2. !
  3. ! Created: November 2009
  4. !
  5. ! Author: Pavel Sakov
  6. ! NERSC
  7. !
  8. ! Purpose: Read glider data from text files by FFI into TOPAZ system
  9. !
  10. ! Description: Data file(s) are defined by the string in the 4th line of
  11. ! "infile.data". It should have the following format:
  12. ! <BEGIN>
  13. ! FFI
  14. ! GSAL | GTEM
  15. ! <obs. error variance>
  16. ! <File name>
  17. ! <END>
  18. !
  19. ! This is a very beta code, just to make an initial assessment.
  20. !
  21. ! Modifications: none
  22. module m_read_FFI_glider
  23. implicit none
  24. integer, parameter, private :: STRLEN = 512
  25. public read_ffi_glider
  26. private grid_readxyz
  27. contains
  28. subroutine read_ffi_glider(fname, obstype, variance, nx, ny, data)
  29. use mod_measurement
  30. use m_confmap
  31. use m_oldtonew
  32. use m_pivotp
  33. use m_bilincoeff
  34. character(*), intent(in) :: fname
  35. character(*), intent(in) :: obstype
  36. real, intent(in) :: variance
  37. integer, intent(in) :: nx, ny
  38. type(measurement), allocatable, intent(out) :: data(:)
  39. real, dimension(nx, ny) :: modlat, modlon, depths
  40. real :: latnew, lonnew
  41. character(STRLEN) :: record
  42. integer :: ios
  43. integer :: r, nr, o, nobs, oo
  44. real :: tmp
  45. type(measurement) :: obs
  46. type(measurement), allocatable :: tmpdata(:)
  47. ! count number of records
  48. !
  49. open(10, file = trim(fname), access = 'sequential', status = 'old', iostat = ios)
  50. if (ios /= 0) then
  51. print *, 'ERROR: read_FFI_glider(): could not open "', fname, '"'
  52. end if
  53. nr = 1
  54. do while(.true.)
  55. read(10, *, iostat = ios) record
  56. if (ios /= 0) then
  57. exit
  58. end if
  59. nr = nr + 1
  60. end do
  61. print *, trim(fname), ': ', nr, ' lines'
  62. if (nr == 0) then
  63. print *, 'ERROR: read_FFI_glider(): "', fname, '": empty file?'
  64. stop
  65. end if
  66. allocate(data(nr))
  67. close(10)
  68. open(10, file = trim(fname), access = 'sequential', status = 'old')
  69. nobs = 0
  70. do r = 1, nr
  71. if (trim(obstype) == 'GSAL' .or. trim(obstype) == 'SAL') then
  72. read(10, *, iostat = ios) obs % date, obs % lat, obs % lon, obs % depth, tmp, tmp, obs % d
  73. elseif (trim(obstype) == 'GTEM' .or. trim(obstype) == 'TEM') then
  74. read(10, *, iostat = ios) obs % date, obs % lat, obs % lon, obs % depth, tmp, obs % d, tmp
  75. else
  76. print *, trim(fname), ': unknown data type "', trim(obstype), '"'
  77. stop
  78. end if
  79. if (obs % date <= 0) then
  80. cycle
  81. end if
  82. nobs = nobs + 1
  83. data(nobs) = obs
  84. end do
  85. close(10)
  86. allocate(tmpdata(1 : nobs))
  87. tmpdata = data(1 : nobs)
  88. deallocate(data)
  89. allocate(data(nobs))
  90. data = tmpdata
  91. deallocate(tmpdata)
  92. if (nobs == 0) then
  93. print *, 'ERROR: read_FFI_glider(): "', trim(fname),&
  94. '": no meaningful data for ', trim(obstype), ' found'
  95. stop
  96. end if
  97. print *, trim(fname), ': ', nobs, ' records for ', trim(obstype)
  98. data % id = obstype
  99. data % var = variance
  100. data % status = .true.
  101. data % ns = 0
  102. data % i_orig_grid = 0
  103. ! convert seconds since 1/1/1970 to days since 1/1/1950
  104. !
  105. ! data(1 : nobs) % date = data(1 : nobs) % date / 86400 + 7305
  106. call confmap_init(nx, ny)
  107. call grid_readxyz(nx, ny, modlat, modlon, depths)
  108. do o = 1, nobs
  109. call oldtonew(data(o) % lat, data(o) % lon, latnew, lonnew)
  110. call pivotp(lonnew, latnew, data(o) % ipiv, data(o) % jpiv)
  111. if (data(o) % ipiv < 1 .or. data(o) % jpiv < 1&
  112. .or. data(o) % ipiv > nx - 1 .or. data(o) % jpiv > ny - 1) then
  113. data(o) % status = .false.
  114. else
  115. call bilincoeff(modlon, modlat, nx, ny, data(o) % lon, data(o) % lat,&
  116. data(o) % ipiv, data(o) % jpiv, data(o) % a1, data(o) % a2,&
  117. data(o) % a3, data(o) % a4)
  118. end if
  119. end do
  120. ! some basic QC
  121. where (data % depth < 0.0d0 .or. data % depth > 6000.0d0)
  122. data % status = .false.
  123. end where
  124. if (trim(obstype) == 'TEM') then
  125. where (data % d < -3.0d0 .or. data % d > 40.0d0)
  126. data % status = .false.
  127. end where
  128. elseif (trim(obstype) == 'SAL') then
  129. where (data % d < 30.0d0 .or. data % d > 40.0d0)
  130. data % status = .false.
  131. end where
  132. end if
  133. allocate(tmpdata(1 : count(data % status)))
  134. oo = 0
  135. do o = 1, nobs
  136. if (data(o) % status) then
  137. oo = oo + 1
  138. tmpdata(oo) = data(o)
  139. end if
  140. end do
  141. nobs = oo
  142. deallocate(data)
  143. allocate(data(nobs))
  144. data = tmpdata
  145. deallocate(tmpdata)
  146. end subroutine read_ffi_glider
  147. ! Copied from m_read_ifremer_argo.
  148. !
  149. subroutine grid_readxyz(nx, ny, lat, lon, depth)
  150. integer, intent(in) :: nx, ny
  151. real(8), dimension(nx, ny), intent(inout) :: lat, lon, depth
  152. logical :: exists
  153. character(len = 128) :: fname
  154. fname = 'newpos.uf'
  155. inquire(file = fname, exist = exists)
  156. if (.not. exists) then
  157. print *, 'grid_readxyz(): ERROR: "', trim(fname), '" does not exist'
  158. stop
  159. end if
  160. open(10, file = fname, form = 'unformatted', status = 'old')
  161. print *, ' grid_readxyz(): reading "', trim(fname), '"...'
  162. read(10) lat, lon
  163. close(10)
  164. write(fname, '(a, i3.3, a, i3.3, a)') 'depths', nx, 'x', ny, '.uf'
  165. inquire(file = fname, exist = exists)
  166. if (.not. exists) then
  167. print*, 'grid_readxyz(): ERROR: "', trim(fname), '" does not exist'
  168. stop
  169. end if
  170. open (unit = 10, file = fname, status = 'old', form = 'unformatted')
  171. print *, ' grid_readxyz(): reading "', trim(fname), '"...'
  172. read(10) depth
  173. close(10)
  174. end subroutine grid_readxyz
  175. end module m_read_FFI_glider