p_prep_obs_ORCA1.F90 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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', 'siconc' if model forced by itself)
  33. !!!!!TAG-DEV-AD : change mask-ORCA1.nc into mask-eORCA1.nc
  34. ! character(len=80), parameter :: maskfile = 'mask-ORCA1.nc' !hc!
  35. character(len=80), parameter :: maskfile = 'mask-eORCA1.nc' !hc!
  36. ! Data vars
  37. !!!!!TAG-DEV-AD : adapted coordinate for eORCA1, mlon mlat mx my for model dim
  38. integer, parameter :: mx=360, my=331 ! Unfortunately, still hard coded.
  39. real, dimension(mx,my,1) :: errorfld, obsfld, obsfldu, obsfldv, errorfldu, errorfldv
  40. real, dimension(mx,my) :: mlons, mlats
  41. !!!!!TAG-DEV-AD : add OSI-SAF dim = obs dim = ox,oy
  42. ! integer, parameter :: ox=432, oy=432 ! Unfortunately, still hard coded.
  43. ! real, dimension(ox,oy,1) :: errorfld, obsfld, obsfldu, obsfldv, errorfldu, errorfldv
  44. ! real, dimension(ox,oy) :: olons, olats
  45. REAL :: obsmin, obsmax, errmin, errmax
  46. REAL :: latmin_NH = 40.0 !76.0 Assim only circle more or less with Svlbard latitudes
  47. REAL :: latmax_NH = 90.0 !82.0
  48. REAL :: latmin_SH = 40.0 ! Same for SH (sign will be added)
  49. REAL :: latmax_SH = 90.0
  50. integer, dimension(mx,my) :: mask
  51. integer :: obscnt, obscnt2, cpt, cptemin, cptemax, cptomin, cptomax ! Counts nr of obs's.
  52. ! Other vars
  53. character(len=99) dummy ! To read cmd line args
  54. ! for loops (haha)
  55. integer :: i, j, varID, varID_lon, varID_lat, varID_err, varID_var, icomp
  56. ! Ice thickness category stuff
  57. integer, parameter :: nhtc=5 !hc! nr of ice thickn. cat's
  58. real(KIND=8) :: rdate
  59. ! Obs stuff
  60. type (measurement), allocatable :: obs(:)
  61. ! Need to fill:
  62. ! d (val), var (error var), id (iiceconc etc.), lon, lat, depths,
  63. ! ipic, jpic (i/j-pivot point in grid), ns (support, 0: point meas.),
  64. ! a1-4 (bilin. coeff), status (not used)
  65. narg= iargc()
  66. PRINT *, narg
  67. if (narg<=1) then
  68. ! Write info
  69. write(*,*)
  70. write(*,*) " (prep_obs) takes a real obs, extracts the desired variable and outputs"
  71. write(*,*) " it in a format that the EnKF can read & treat ('observations.uf')."
  72. write(*,*)
  73. write(*,*) " A file named mask.nc containing the variables tmaskutil, nav_lon and nav_lat"
  74. write(*,*) " is expected to be in the current directory (ORCA-file)"
  75. write(*,*)
  76. write(*,*) " Three command line arguments are expected:"
  77. write(*,*) " 1. Path to the nc file of which the data is to be extracted."
  78. write(*,*) " 2. Variable name that can be found in there, 'h_i_htc1' or"
  79. write(*,*) " 'sic'. or dxdy_ice"
  80. write(*,*) " 3. A tag with the date, e.g. 19790520"
  81. write(*,*)
  82. write(*,*) " Hope to see you again soon."
  83. write(*,*)
  84. stop "(prep_obs): Stopped."
  85. end if
  86. ! Command line arguments
  87. nvar=narg-1
  88. ! Get them
  89. call getarg(1, dummy)
  90. read(dummy,'(A)') filename
  91. ! Some parameter checking
  92. inquire(file=trim(filename), exist=ex)
  93. if (.not.ex) then
  94. print *, '(p_prep_obs): file does not exist: '// trim(filename)
  95. stop
  96. end if
  97. ! Get mask, lons & lats
  98. ! open the maskfile
  99. ierr = nf90_open(trim(maskfile),nf90_NoWrite,ncid); if (ierr.ne.nf90_noerr) call handle_err(ierr, "opening mask file")
  100. ! Find VarID of tmask & get values
  101. ierr = nf90_inq_varid(ncid, 'tmask', varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID tmask")
  102. WRITE(*,*)'tmask varID:',varID
  103. ierr = nf90_get_var(ncid, varID, mask) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable tmaks")
  104. ! Find VarID of longitude & get vals
  105. ierr = nf90_inq_varid(ncid, 'nav_lon', varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID nav_lon")
  106. WRITE(*,*)'nav_lon varID:',varID
  107. ierr = nf90_get_var(ncid, varID, mlons) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable nav_lon")
  108. ! Find VarID of latitude & get vals
  109. ierr = nf90_inq_varid(ncid, 'nav_lat', varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID nav_lat")
  110. ierr = nf90_get_var(ncid, varID, mlats) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable nav_lat")
  111. ! Close maskfile
  112. ierr = nf90_close(ncid)
  113. if (ierr.ne.nf90_noerr) call handle_err(ierr, "closing")
  114. allocate( obs(nvar*sum(mask)), STAT=ierr )
  115. if (ierr.ne.0) call handle_err(ierr, "allocating obs") !no netcdf-error! ohwell.
  116. obscnt = 0 ! Keeps track of nr of added obs's.
  117. call getarg(2, dummy)
  118. read(dummy,'(A)') varname
  119. call getarg(3, dummy)
  120. read(dummy,*) rdate
  121. IF ( TRIM(varname) .eq. 'rfb' ) THEN
  122. WRITE(*,*) "Handling ", TRIM(varname)
  123. ! Min and max values for error used to screen the data (any data with
  124. ! standard deviation in between those values will be selected
  125. obsmin = 0.01
  126. obsmax = 10.0
  127. errmin = 0.01
  128. errmax = 1.0
  129. ELSEIF ( TRIM(varname) .eq. 'vt_i') THEN
  130. WRITE(*,*) "Handling ", TRIM(varname)
  131. obsmin = 0.01
  132. obsmax = 50.0
  133. errmin = 0.01
  134. errmax = 1.0
  135. ELSEIF ( TRIM(varname) .eq. 'at_i') THEN
  136. WRITE(*,*) "Handling ", TRIM(varname)
  137. obsmin = 0.001
  138. obsmax = 1.0
  139. errmin = 0.001
  140. errmax = 0.5
  141. ELSEIF ( TRIM(varname) .eq. 'siconc') THEN
  142. WRITE(*,*) "Handling ", TRIM(varname)
  143. obsmin = 0.001
  144. obsmax = 1.0
  145. errmin = 0.001
  146. errmax = 0.5
  147. ELSEIF ( TRIM(varname) .eq. 'ice_conc') THEN ! OSI-SAF-450
  148. WRITE(*,*) "Handling ", TRIM(varname)
  149. obsmin = 0.0
  150. obsmax = 100.0
  151. errmin = 0.001
  152. errmax = 40.0
  153. ELSE
  154. WRITE(*,*) " (prep_obs) Currently only the variables rfb (sea ice freeboard),"
  155. WRITE(*,*) " vt_i (total sea ice volume)"
  156. WRITE(*,*) " at_i (total sea ice concentration)"
  157. WRITE(*,*) " can be processed "
  158. STOP "(prep_obs) Aborted"
  159. ENDIF
  160. ! User info
  161. WRITE(*,*) "(prep_obs) Extracting "//TRIM(varname)//" from "//TRIM(filename)
  162. ! Some preparations
  163. obsfld=0.
  164. errorfld=0.
  165. ! open the netCDF file
  166. ierr = nf90_open(trim(filename),nf90_NoWrite,ncid) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "opening")
  167. ! Read observation data
  168. ! Find VarID of longitude & get vals from obs
  169. ierr = nf90_inq_varid(ncid, 'lon', varID_lon) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inq varID lon obs")
  170. WRITE(*,*)'lon obs varID:',varID_lon
  171. ierr = nf90_get_var(ncid, varID_lon, olons) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting var lon obs")
  172. ! Find VarID of latitude & get vals
  173. ierr = nf90_inq_varid(ncid, 'lat', varID_lat) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inq varID lat obs")
  174. ierr = nf90_get_var(ncid, varID_lat, olats) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting var lat obs")
  175. ! Find VarID of varname (variable to assim)
  176. WRITE(*,*) "varname="//trim(varname)
  177. ierr = nf90_inq_varid(ncid, trim(varname), varID_var) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inq varID obs")
  178. ! get values
  179. ierr = nf90_get_var(ncid, varID_var, obsfld) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting var obs")
  180. obsfld=obsfld/100.
  181. WRITE(*,*) "OK var"//trim(varname)
  182. ! Find VarID of varname (error)
  183. cvarerror=TRIM(varname)//'_sd'
  184. WRITE(*,*) "cvarerror="//trim(varname)//"_sd"
  185. ierr = nf90_inq_varid(ncid, cvarerror, varID_err) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inq varID obs")
  186. !! get values
  187. ierr = nf90_get_var(ncid, varID_err, errorfld) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting error var obs")
  188. errorfld=errorfld/100.
  189. ! Close file
  190. ierr = nf90_close(ncid) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "closing")
  191. ! User info - ADAPT ACCORDINGLY
  192. WRITE (*,*) " (prep_obs) Using data >40N and <45S"
  193. obscnt2=0
  194. WRITE(*,*) 'obs min:',obsmin,'obs max:',obsmax,'err min:',errmin,'err max:',errmax
  195. ! Loop over space
  196. DO i = 1, SIZE(olats, 1)
  197. DO j = 1, SIZE(olats, 2)
  198. ! Pick out ocean points where data is existent
  199. IF ( obsfld(i,j,1) .LT. obsmax &
  200. .AND. obsfld(i,j,1) .GT. obsmin &
  201. .AND. ( (errorfld(i,j,1)) ** 2 ) .GT. ( errmin ** 2 ) &
  202. .AND. ( (errorfld(i,j,1)) ** 2 ) .LT. ( errmax ** 2 ) &
  203. ) THEN
  204. obscnt2 = obscnt2 + 1
  205. ! Limit 'obs' spatially
  206. IF ( ( olats(i,j) .GE. latmin_NH &
  207. .AND. olats(i,j) .LE. latmax_NH ) &
  208. .OR.( olats(i,j) .LE. (-latmin_SH) &
  209. .AND. olats(i,j) .GE. (-latmax_SH) ) &
  210. ) THEN
  211. obscnt = obscnt + 1
  212. obs(obscnt)%d = obsfld(i,j,1)
  213. obs(obscnt)%lon = olons(i,j)
  214. obs(obscnt)%lat = olats(i,j)
  215. obs(obscnt)%ipiv = i ! the i-point of the grid of the model
  216. obs(obscnt)%jpiv = j ! the j-point of the grid of the model
  217. ! Put other data into obs type array
  218. obs(obscnt)%var = (errorfld(i,j,1))**2 ! The variance
  219. obs(obscnt)%id = TRIM(varname)
  220. obs(obscnt)%depths = 0
  221. obs(obscnt)%ns = 0
  222. obs(obscnt)%a1 = 1
  223. obs(obscnt)%a2 = 0
  224. obs(obscnt)%a3 = 0
  225. obs(obscnt)%a4 = 0
  226. obs(obscnt)%status = .TRUE.
  227. obs(obscnt)%i_orig_grid = -1
  228. obs(obscnt)%j_orig_grid = -1
  229. obs(obscnt)%h = -1.0
  230. obs(obscnt)%date = rdate
  231. obs(obscnt)%orig_id = 0
  232. END IF ! Spatial selection
  233. END IF ! if valid point
  234. END DO ! j
  235. END DO ! i
  236. WRITE(*,*) 'Nb obscnt: ', obscnt
  237. WRITE(*,*) 'Nb obscnt2: ', obscnt2
  238. !Write data:
  239. INQUIRE(iolength=i)obs(1)
  240. OPEN (11, file='observations.uf', status='replace',form='unformatted', access='direct', recl=i)
  241. DO j = 1, obscnt
  242. WRITE(11, rec=j)obs(j)
  243. ENDDO
  244. CLOSE(11)
  245. ! Write data to textfile, for visual checking
  246. OPEN(12, file = 'observations.txt')
  247. DO j = 1, obscnt
  248. WRITE(12, FMT = 53) obs(j)
  249. 53 FORMAT(f8.4,X,f9.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)
  250. ENDDO
  251. CLOSE(12)
  252. ! Tell user how many obs there were
  253. WRITE(*,*) " (prep_obs) Wrote out this many obs: ", obscnt
  254. WRITE(*,*) " (prep_obs) Number of ocean points : ", sum(mask)
  255. ! Cleanup
  256. IF (allocated(obs)) deallocate(obs,STAT=ierr)
  257. WRITE(*,*) ' (prep_obs) End successfully reached'; WRITE(*,*)
  258. contains
  259. subroutine handle_err(status, infomsg)
  260. integer, intent ( in) :: status
  261. character(len = *), intent ( in), optional :: infomsg
  262. if(status /= nf90_noerr) then
  263. if (present(infomsg)) then
  264. print *, '(prep_obs) Error while '//infomsg//' - '//trim(nf90_strerror(status))
  265. else
  266. print *, trim(nf90_strerror(status))
  267. endif ! opt arg
  268. print *,'(prep_obs)'
  269. stop
  270. end if ! check error status
  271. end subroutine handle_err
  272. end program prep_obs