p_prep_obs_ORCA1.F90 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. program prep_obs
  2. ! This takes a model restart file, extracts the desired variable
  3. ! and brings into a format that the EnKF V2 can read & treat ('observations.uf').
  4. !
  5. ! !!! AGAIN: THIS USES EnKF VERSION 2 !!!
  6. !
  7. ! Two command line arguments are expected:
  8. ! 1. Path to the nc file of which the data is to be extracted.
  9. ! 2. Variable name that can be found in there.
  10. !
  11. ! Output is written to 'observations.uf'. If lots of files are to be treated,
  12. ! use a shell script to call me and rename output.
  13. !
  14. ! Warning: Currently only the data from the first time step are being read.
  15. ! (No problem with restart files, normally)
  16. !
  17. !
  18. ! TO DO: Add possibility of treating several obs types.
  19. !
  20. !
  21. ! (c) September 2008, Christof König Beatty, Christof.KonigBeatty@uclouvain.be
  22. ! (c) May 2009, generalized by same.
  23. ! (c) Jun 2011, simplified by F. Massonnet
  24. ! (c) April 2016, cleaned by F. Massonnet
  25. use mod_measurement
  26. use netcdf
  27. implicit none
  28. ! NetCDF vars
  29. character(len=99) :: filename, cvarerror, cvarerroru, cvarerrorv
  30. integer :: ncid, ierr, nvar, narg
  31. logical :: ex
  32. character(len=16) :: varname, varnameu, varnamev ! Type of measurement ('a_i_htcX', 'u_ice', 'v_ice', maybe 'v_i_htcX')
  33. character(len=80), parameter :: maskfile = 'mask-ORCA1.nc' !hc!
  34. ! Data vars
  35. integer, parameter :: nx=362, ny=292 ! Unfortunately, still hard coded.
  36. real, dimension(nx,ny) :: lons, lats, obsfld, errorfld, obsfldu, obsfldv, errorfldu, errorfldv
  37. REAL :: obsmin, obsmax, errmin, errmax
  38. REAL :: latmin_NH = 40.0
  39. REAL :: latmax_NH = 90.0
  40. REAL :: latmin_SH = 40.0 ! Same for SH (sign will be added)
  41. REAL :: latmax_SH = 90.0
  42. integer, dimension(nx,ny) :: mask
  43. integer :: obscnt ! Counts nr of obs's.
  44. ! Other vars
  45. character(len=99) dummy ! To read cmd line args
  46. ! for loops (haha)
  47. integer :: i, j, varID, icomp
  48. ! Ice thickness category stuff
  49. integer, parameter :: nhtc=5 !hc! nr of ice thickn. cat's
  50. real(KIND=8) :: rdate
  51. ! Obs stuff
  52. type (measurement), allocatable :: obs(:)
  53. ! Need to fill:
  54. ! d (val), var (error var), id (iiceconc etc.), lon, lat, depths,
  55. ! ipic, jpic (i/j-pivot point in grid), ns (support, 0: point meas.),
  56. ! a1-4 (bilin. coeff), status (not used)
  57. narg= iargc()
  58. PRINT *, narg
  59. if (narg<=1) then
  60. ! Write info
  61. write(*,*)
  62. write(*,*) " (prep_obs) takes a real obs, extracts the desired variable and outputs"
  63. write(*,*) " it in a format that the EnKF can read & treat ('observations.uf')."
  64. write(*,*)
  65. write(*,*) " A file named mask.nc containing the variables tmaskutil, nav_lon and nav_lat"
  66. write(*,*) " is expected to be in the current directory (ORCA-file)"
  67. write(*,*)
  68. write(*,*) " Three command line arguments are expected:"
  69. write(*,*) " 1. Path to the nc file of which the data is to be extracted."
  70. write(*,*) " 2. Variable name that can be found in there, 'h_i_htc1' or"
  71. write(*,*) " 'sic'. or dxdy_ice"
  72. write(*,*) " 3. A tag with the date, e.g. 19790520"
  73. write(*,*)
  74. write(*,*) " Hope to see you again soon."
  75. write(*,*)
  76. stop "(prep_obs): Stopped."
  77. end if
  78. ! Command line arguments
  79. nvar=narg-1
  80. ! Get them
  81. call getarg(1, dummy)
  82. read(dummy,'(A)') filename
  83. ! Some parameter checking
  84. inquire(file=trim(filename), exist=ex)
  85. if (.not.ex) then
  86. print *, '(p_prep_obs): file does not exist: '// trim(filename)
  87. stop
  88. end if
  89. ! Get mask, lons & lats
  90. ! open the maskfile
  91. ierr = nf90_open(trim(maskfile),nf90_NoWrite,ncid); if (ierr.ne.nf90_noerr) call handle_err(ierr, "opening mask file")
  92. ! Find VarID of tmask & get values
  93. ierr = nf90_inq_varid(ncid, 'tmask', varID); if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID tmask")
  94. ierr = nf90_get_var(ncid, varID, mask) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable tmaks")
  95. ! Find VarID of longitude & get vals
  96. ierr = nf90_inq_varid(ncid, 'nav_lon', varID); if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID nav_lon")
  97. ierr = nf90_get_var(ncid, varID, lons) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable nav_lon")
  98. ! Find VarID of latitude & get vals
  99. ierr = nf90_inq_varid(ncid, 'nav_lat', varID); if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID nav_lat")
  100. ierr = nf90_get_var(ncid, varID, lats) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable nav_lat")
  101. ! Close maskfile
  102. ierr = nf90_close(ncid)
  103. if (ierr.ne.nf90_noerr) call handle_err(ierr, "closing")
  104. allocate( obs(nvar*sum(mask)), STAT=ierr )
  105. if (ierr.ne.0) call handle_err(ierr, "allocating obs") !no netcdf-error! ohwell.
  106. obscnt = 0 ! Keeps track of nr of added obs's.
  107. call getarg(2, dummy)
  108. read(dummy,'(A)') varname
  109. call getarg(3, dummy)
  110. read(dummy,*) rdate
  111. IF ( TRIM(varname) .eq. 'rfb' ) THEN
  112. WRITE(*,*) "Handling ", TRIM(varname)
  113. ! Min and max values for error used to screen the data (any data with
  114. ! standard deviation in between those values will be selected
  115. obsmin = 0.01
  116. obsmax = 10.0
  117. errmin = 0.01
  118. errmax = 1.0
  119. ELSEIF ( TRIM(varname) .eq. 'vt_i') THEN
  120. WRITE(*,*) "Handling ", TRIM(varname)
  121. obsmin = 0.01
  122. obsmax = 50.0
  123. errmin = 0.01
  124. errmax = 1.0
  125. ELSEIF ( TRIM(varname) .eq. 'at_i') THEN
  126. WRITE(*,*) "Handling ", TRIM(varname)
  127. obsmin = 0.001
  128. obsmax = 1.0
  129. errmin = 0.001
  130. errmax = 0.5
  131. ELSE
  132. WRITE(*,*) " (prep_obs) Currently only the variables rfb (sea ice freeboard),"
  133. WRITE(*,*) " at_i (total sea ice volume)"
  134. WRITE(*,*) " at_i (total sea ice concentration)"
  135. WRITE(*,*) " can be processed "
  136. STOP "(prep_obs) Aborted"
  137. ENDIF
  138. ! User info
  139. WRITE(*,*) "(prep_obs) Extracting "//TRIM(varname)//" from "//TRIM(filename)
  140. ! Some preparations
  141. obsfld=0.
  142. ! open the netCDF file
  143. ierr = nf90_open(trim(filename),nf90_NoWrite,ncid) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "opening")
  144. ! Read observation data
  145. ! Find VarID of varname
  146. ierr = nf90_inq_varid(ncid, trim(varname), varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID")
  147. ! get values
  148. ierr = nf90_get_var(ncid, varID, obsfld) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable")
  149. ! Find VarID of varname
  150. cvarerror=TRIM(varname)//'_sd'
  151. ierr = nf90_inq_varid(ncid, cvarerror, varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID")
  152. ! get values
  153. ierr = nf90_get_var(ncid, varID, errorfld) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable")
  154. ! Close file
  155. ierr = nf90_close(ncid) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "closing")
  156. ! User info - ADAPT ACCORDINGLY
  157. WRITE (*,*) " (prep_obs) Using data >40N and <45S"
  158. ! Loop over space
  159. DO i = 1, SIZE(mask, 1)
  160. DO j = 1, SIZE(mask, 2)
  161. ! Pick out ocean points where data is existent
  162. IF ( (errorfld(i,j)) ** 2 .GT. errmin ** 2 &
  163. .AND. (errorfld(i,j)) ** 2 .LT. errmax ** 2 &
  164. .AND. obsfld(i,j) .GT. obsmin &
  165. .AND. obsfld(i,j) .LT. obsmax ) THEN
  166. ! Limit 'obs' spatially
  167. IF ( ( lats(i,j) .GE. latmin_NH &
  168. .AND. lats(i,j) .LE. latmax_NH ) &
  169. .OR.( lats(i,j) .LE. (-latmin_SH) &
  170. .AND. lats(i,j) .GE. (-latmax_SH) ) &
  171. ) THEN
  172. obscnt = obscnt + 1
  173. obs(obscnt)%d = obsfld(i,j)
  174. obs(obscnt)%lon = lons(i,j)
  175. obs(obscnt)%lat = lats(i,j)
  176. obs(obscnt)%ipiv = i ! the i-point of the grid of the model
  177. obs(obscnt)%jpiv = j ! the j-point of the grid of the model
  178. ! Put other data into obs type array
  179. obs(obscnt)%var = (errorfld(i,j))**2 ! The variance
  180. obs(obscnt)%id = TRIM(varname)
  181. obs(obscnt)%depths = 0
  182. obs(obscnt)%ns = 0
  183. obs(obscnt)%a1 = 1
  184. obs(obscnt)%a2 = 0
  185. obs(obscnt)%a3 = 0
  186. obs(obscnt)%a4 = 0
  187. obs(obscnt)%status = .TRUE.
  188. obs(obscnt)%i_orig_grid = -1
  189. obs(obscnt)%j_orig_grid = -1
  190. obs(obscnt)%h = -1.0
  191. obs(obscnt)%date = rdate
  192. obs(obscnt)%orig_id = 0
  193. END IF ! Spatial selection
  194. END IF ! if valid point
  195. END DO ! j
  196. END DO ! i
  197. !Write data:
  198. INQUIRE(iolength=i)obs(1)
  199. OPEN (11, file='observations.uf', status='replace',form='unformatted', access='direct', recl=i)
  200. DO j = 1, obscnt
  201. WRITE(11, rec=j)obs(j)
  202. ENDDO
  203. CLOSE(11)
  204. ! Write data to textfile, for visual checking
  205. OPEN(12, file = 'observations.txt')
  206. DO j = 1, obscnt
  207. WRITE(12, FMT = 53) obs(j)
  208. 53 FORMAT(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)
  209. ENDDO
  210. CLOSE(12)
  211. ! Tell user how many obs there were
  212. WRITE(*,*) " (prep_obs) Wrote out this many obs: ", obscnt
  213. WRITE(*,*) " (prep_obs) Number of ocean points : ", sum(mask)
  214. ! Cleanup
  215. IF (allocated(obs)) deallocate(obs,STAT=ierr)
  216. WRITE(*,*) ' (prep_obs) End successfully reached'; WRITE(*,*)
  217. contains
  218. subroutine handle_err(status, infomsg)
  219. integer, intent ( in) :: status
  220. character(len = *), intent ( in), optional :: infomsg
  221. if(status /= nf90_noerr) then
  222. if (present(infomsg)) then
  223. print *, '(prep_obs) Error while '//infomsg//' - '//trim(nf90_strerror(status))
  224. else
  225. print *, trim(nf90_strerror(status))
  226. endif ! opt arg
  227. print *,'(prep_obs)'
  228. stop
  229. end if ! check error status
  230. end subroutine handle_err
  231. end program prep_obs