p_prep_obs_ORCA1.f90 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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, errorfld, obsfld, obsfldu, obsfldv, errorfldu, errorfldv
  49. REAL :: obsmin, obsmax, errmin, errmax
  50. REAL :: latmin_NH = 40.0 !76.0 Assim only circle more or less with Svlbard latitudes
  51. REAL :: latmax_NH = 90.0 !82.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. ELSEIF ( TRIM(varname) .eq. 'ice_conc') THEN ! OSI-SAF-450
  150. WRITE(*,*) "Handling ", TRIM(varname)
  151. obsmin = 0.001
  152. obsmax = 100.0
  153. errmin = 0.001
  154. errmax = 50.0
  155. ELSEIF ( TRIM(varname) .eq. 'sic') THEN ! OSI-SAF-450 interp on eORCA1
  156. WRITE(*,*) "Handling ", TRIM(varname)
  157. obsmin = 0.001
  158. obsmax = 100.0
  159. errmin = 0.001
  160. errmax = 40.0
  161. ELSE
  162. WRITE(*,*) " (prep_obs) Currently only the variables rfb (sea ice freeboard),"
  163. WRITE(*,*) " vt_i (total sea ice volume)"
  164. WRITE(*,*) " at_i (total sea ice concentration)"
  165. WRITE(*,*) " can be processed "
  166. STOP "(prep_obs) Aborted"
  167. ENDIF
  168. ! User info
  169. WRITE(*,*) "(prep_obs) Extracting "//TRIM(varname)//" from "//TRIM(filename)
  170. ! Some preparations
  171. obsfld=0.
  172. ! open the netCDF file
  173. ierr = nf90_open(trim(filename),nf90_NoWrite,ncid) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "opening")
  174. ! Read observation data
  175. ! Find VarID of varname
  176. WRITE(*,*) "varname="//trim(varname)
  177. ierr = nf90_inq_varid(ncid, trim(varname), varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID")
  178. ! get values
  179. ierr = nf90_get_var(ncid, varID, obsfld) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable")
  180. WRITE(*,*) "OK var"//trim(varname)
  181. ! Find VarID of varname
  182. cvarerror=TRIM(varname)//'_sd'
  183. WRITE(*,*) "cvarerror="//trim(varname)//'_sd'
  184. ierr = nf90_inq_varid(ncid, cvarerror, varID) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "inquiring varID")
  185. ! get values
  186. ierr = nf90_get_var(ncid, varID, errorfld) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "getting variable")
  187. ! Close file
  188. ierr = nf90_close(ncid) ; if (ierr.ne.nf90_noerr) call handle_err(ierr, "closing")
  189. ! User info - ADAPT ACCORDINGLY
  190. WRITE (*,*) " (prep_obs) Using data >40N and <45S"
  191. ! Loop over space
  192. DO i = 1, SIZE(mask, 1)
  193. DO j = 1, SIZE(mask, 2)
  194. ! Pick out ocean points where data is existent
  195. IF ( (errorfld(i,j)) ** 2 .GT. errmin ** 2 &
  196. .AND. (errorfld(i,j)) ** 2 .LT. errmax ** 2 &
  197. .AND. obsfld(i,j) .GT. obsmin &
  198. .AND. obsfld(i,j) .LT. obsmax ) THEN
  199. ! Limit 'obs' spatially
  200. IF ( ( lats(i,j) .GE. latmin_NH &
  201. .AND. lats(i,j) .LE. latmax_NH ) &
  202. .OR.( lats(i,j) .LE. (-latmin_SH) &
  203. .AND. lats(i,j) .GE. (-latmax_SH) ) &
  204. ) THEN
  205. obscnt = obscnt + 1
  206. obs(obscnt)%d = obsfld(i,j)
  207. obs(obscnt)%lon = lons(i,j)
  208. obs(obscnt)%lat = lats(i,j)
  209. obs(obscnt)%ipiv = i ! the i-point of the grid of the model
  210. obs(obscnt)%jpiv = j ! the j-point of the grid of the model
  211. ! Put other data into obs type array
  212. obs(obscnt)%var = (errorfld(i,j))**2 ! The variance
  213. obs(obscnt)%id = TRIM(varname)
  214. obs(obscnt)%depths = 0
  215. obs(obscnt)%ns = 0
  216. obs(obscnt)%a1 = 1
  217. obs(obscnt)%a2 = 0
  218. obs(obscnt)%a3 = 0
  219. obs(obscnt)%a4 = 0
  220. obs(obscnt)%status = .TRUE.
  221. obs(obscnt)%i_orig_grid = -1
  222. obs(obscnt)%j_orig_grid = -1
  223. obs(obscnt)%h = -1.0
  224. obs(obscnt)%date = rdate
  225. obs(obscnt)%orig_id = 0
  226. END IF ! Spatial selection
  227. END IF ! if valid point
  228. END DO ! j
  229. END DO ! i
  230. WRITE(*,*) 'Nb obscnt: ', obscnt
  231. !Write data:
  232. INQUIRE(iolength=i)obs(1)
  233. OPEN (11, file='observations.uf', status='replace',form='unformatted', access='direct', recl=i)
  234. DO j = 1, obscnt
  235. WRITE(11, rec=j)obs(j)
  236. ENDDO
  237. CLOSE(11)
  238. ! Write data to textfile, for visual checking
  239. OPEN(12, file = 'observations.txt')
  240. DO j = 1, obscnt
  241. WRITE(12, FMT = 53) obs(j)
  242. 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)
  243. ENDDO
  244. CLOSE(12)
  245. ! Tell user how many obs there were
  246. WRITE(*,*) " (prep_obs) Wrote out this many obs: ", obscnt
  247. WRITE(*,*) " (prep_obs) Number of ocean points : ", sum(mask)
  248. ! Cleanup
  249. IF (allocated(obs)) deallocate(obs,STAT=ierr)
  250. WRITE(*,*) ' (prep_obs) End successfully reached'; WRITE(*,*)
  251. contains
  252. subroutine handle_err(status, infomsg)
  253. integer, intent ( in) :: status
  254. character(len = *), intent ( in), optional :: infomsg
  255. if(status /= nf90_noerr) then
  256. if (present(infomsg)) then
  257. print *, '(prep_obs) Error while '//infomsg//' - '//trim(nf90_strerror(status))
  258. else
  259. print *, trim(nf90_strerror(status))
  260. endif ! opt arg
  261. print *,'(prep_obs)'
  262. stop
  263. end if ! check error status
  264. end subroutine handle_err
  265. end program prep_obs