p_prep_obs_ORCA1.f90 10 KB

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