rotateUVorca.f90 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. program rotateUVorca
  2. !==============================================================================
  3. ! This program rotates U and V components from the geographical directions
  4. ! toward the spherical grid directions based on NEMO3.2 routines
  5. !
  6. ! Written on 2012/02/21
  7. ! Author : Virginie Guemas
  8. !==============================================================================
  9. !
  10. use par_kind
  11. use netcdf
  12. use geo2ocean
  13. use dom_oce
  14. use handerr
  15. implicit none
  16. include 'netcdf.inc'
  17. character (80) :: &
  18. & Ufilein, & ! filename containing the Eastward component
  19. & Uvarin, & ! name of the Eastward component
  20. & Vfilein, & ! filename containing the Northward component
  21. & Vvarin, & ! name of the Northward component
  22. & meshmask, & ! name of the meshmask
  23. & Ufileout, & ! U output file
  24. & Vfileout ! V output file
  25. integer :: nc_fileU_id, nc_fileV_id, nc_filemask_id, nc_varU_id, &
  26. & nc_varV_id, nc_time_id, nc_var_type, nc_outfile_id, ncstat, &
  27. & nc_glamt_id, nc_glamu_id, nc_glamv_id, nc_glamf_id, &
  28. & nc_gphit_id, nc_gphiu_id, nc_gphiv_id, nc_gphif_id
  29. integer, dimension(:), allocatable :: nc_dims_ids
  30. character (80) :: timename
  31. integer :: ndims, ntime, jtime
  32. real (kind=wp), dimension(:), allocatable :: &
  33. & time
  34. real (kind=wp), dimension(:,:,:), allocatable :: &
  35. & Ufield, Vfield, Ufield2, Vfield2
  36. !
  37. !==============================================================================
  38. !
  39. namelist /nam_rotUV/ Ufilein, Uvarin, Vfilein, Vvarin, &
  40. & meshmask, Ufileout, Vfileout
  41. !
  42. !==============================================================================
  43. !
  44. ! Read namelist
  45. !
  46. !==============================================================================
  47. !
  48. open(80, file='namelist_rotateUVorca', status='old', form='formatted')
  49. read(80, nml=nam_rotUV)
  50. write(*,nml=nam_rotUV)
  51. !
  52. !==============================================================================
  53. !
  54. ! Read input (U,V) components
  55. !
  56. !==============================================================================
  57. !
  58. ncstat = nf_open(Ufilein, NF90_NOWRITE, nc_fileU_id)
  59. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Open file Ufilein ")
  60. ncstat = nf_inq_varid(nc_fileU_id, Uvarin, nc_varU_id)
  61. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID varU ")
  62. ncstat = nf_inq_varndims(nc_fileU_id, nc_varU_id, ndims)
  63. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq dims varU ")
  64. if ( ndims < 2 .or. ndims > 3) then
  65. stop "Input files should have (lon, lat) ot (lon, lat, time) &
  66. & dimensions"
  67. endif
  68. if ( ndims == 3) then
  69. allocate(nc_dims_ids(ndims))
  70. ncstat = nf_inq_vardimid(nc_fileU_id, nc_varU_id, nc_dims_ids)
  71. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID dims varU ")
  72. ncstat = nf90_inquire_dimension(nc_fileU_id, nc_dims_ids(3), &
  73. & timename, ntime)
  74. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq. dims time ")
  75. allocate(time(ntime))
  76. time=0.
  77. !ncstat = nf90_get_var(nc_fileU_id, nc_time_id, time)
  78. !if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var time ")
  79. else
  80. ntime=1
  81. allocate(time(1))
  82. time=1.
  83. endif
  84. print*, jpi, jpj, ntime
  85. allocate( Ufield (jpi, jpj, ntime))
  86. ncstat = nf90_get_var(nc_fileU_id, nc_varU_id, Ufield)
  87. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var varU ")
  88. ncstat = nf_close(nc_fileU_id)
  89. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Close fileU ")
  90. ncstat = nf_open(Vfilein, NF90_NOWRITE, nc_fileV_id)
  91. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Open fileV ")
  92. ncstat = nf_inq_varid(nc_fileV_id, Vvarin, nc_varV_id)
  93. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq. ID varV ")
  94. ncstat = nf_inq_varndims(nc_fileV_id, nc_varV_id, ndims)
  95. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq. dims varV ")
  96. if ( ndims /= size(nc_dims_ids) ) then
  97. stop "Input files should have the same dimensions"
  98. endif
  99. allocate( Vfield (jpi, jpj, ntime))
  100. ncstat = nf90_get_var(nc_fileV_id, nc_varV_id, Vfield)
  101. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var varV ")
  102. ncstat = nf_close(nc_fileV_id)
  103. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Close fileV ")
  104. !==============================================================================
  105. !
  106. ! Read meshmask
  107. !
  108. !==============================================================================
  109. !
  110. ncstat = nf_open(meshmask, NF90_NOWRITE, nc_filemask_id)
  111. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Open filemask ")
  112. ncstat = nf_inq_varid(nc_filemask_id, 'glamt', nc_glamt_id)
  113. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID glamt ")
  114. ncstat = nf90_get_var(nc_filemask_id, nc_glamt_id, glamt)
  115. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var glamt ")
  116. ncstat = nf_inq_varid(nc_filemask_id, 'glamf', nc_glamf_id)
  117. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID glamf ")
  118. ncstat = nf90_get_var(nc_filemask_id, nc_glamf_id, glamf)
  119. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var glamf ")
  120. ncstat = nf_inq_varid(nc_filemask_id, 'glamu', nc_glamu_id)
  121. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID glamu ")
  122. ncstat = nf90_get_var(nc_filemask_id, nc_glamu_id, glamu)
  123. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var glamu ")
  124. ncstat = nf_inq_varid(nc_filemask_id, 'glamv', nc_glamv_id)
  125. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID glamv ")
  126. ncstat = nf90_get_var(nc_filemask_id, nc_glamv_id, glamv)
  127. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var glamv ")
  128. ncstat = nf_inq_varid(nc_filemask_id, 'gphit', nc_gphit_id)
  129. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID gphit ")
  130. ncstat = nf90_get_var(nc_filemask_id, nc_gphit_id, gphit)
  131. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var gphit ")
  132. ncstat = nf_inq_varid(nc_filemask_id, 'gphif', nc_gphif_id)
  133. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID gphif ")
  134. ncstat = nf90_get_var(nc_filemask_id, nc_gphif_id, gphif)
  135. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var gphif ")
  136. ncstat = nf_inq_varid(nc_filemask_id, 'gphiu', nc_gphiu_id)
  137. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID gphiu ")
  138. ncstat = nf90_get_var(nc_filemask_id, nc_gphiu_id, gphiu)
  139. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var gphiu ")
  140. ncstat = nf_inq_varid(nc_filemask_id, 'gphiv', nc_gphiv_id)
  141. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Inq ID gphiv ")
  142. ncstat = nf90_get_var(nc_filemask_id, nc_gphiv_id, gphiv)
  143. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Get var gphiv ")
  144. ncstat = nf_close(nc_filemask_id)
  145. if (ncstat .ne. nf_noerr) call handle_err(ncstat, "Close filemask ")
  146. !
  147. !==============================================================================
  148. !
  149. ! Perform rotation
  150. !
  151. !==============================================================================
  152. !
  153. allocate( Ufield2 (jpi, jpj, ntime))
  154. allocate( Vfield2 (jpi, jpj, ntime))
  155. do jtime = 1,ntime
  156. call rot_rep(Ufield(:,:,jtime),Vfield(:,:,jtime),'T','en->i',Ufield2(:,:,jtime))
  157. call rot_rep(Ufield(:,:,jtime),Vfield(:,:,jtime),'T','en->j',Vfield2(:,:,jtime))
  158. end do
  159. deallocate(Ufield,Vfield)
  160. !
  161. !==============================================================================
  162. !
  163. ! Create output netcdf
  164. !
  165. !==============================================================================
  166. !
  167. ncstat = nf_create (Ufileout, NF_CLOBBER, nc_outfile_id)
  168. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  169. ncstat = nf_def_dim (nc_outfile_id, 'x', jpi, nc_dims_ids(1))
  170. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  171. ncstat = nf_def_dim (nc_outfile_id, 'y', jpj, nc_dims_ids(2))
  172. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  173. if ( ndims > 2) then
  174. ncstat = nf_def_dim (nc_outfile_id, 'time', ntime, nc_dims_ids(3))
  175. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  176. ncstat = nf_def_var (nc_outfile_id, 'time', NF_DOUBLE, 1, &
  177. & nc_dims_ids(3), nc_time_id)
  178. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  179. endif
  180. ncstat = nf_def_var (nc_outfile_id, Uvarin, NF_DOUBLE, 3, &
  181. & nc_dims_ids, nc_varU_id )
  182. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  183. ncstat = nf_enddef(nc_outfile_id)
  184. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  185. ncstat = nf_put_var_double(nc_outfile_id, nc_varU_id, Ufield2)
  186. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  187. if ( ndims > 2) then
  188. ncstat = nf_put_var_double(nc_outfile_id, nc_time_id, time)
  189. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  190. endif
  191. ncstat = nf_close(nc_outfile_id)
  192. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  193. !
  194. !==============================================================================
  195. !
  196. ncstat = nf_create (Vfileout, NF_CLOBBER, nc_outfile_id)
  197. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  198. ncstat = nf_def_dim (nc_outfile_id, 'x', jpi, nc_dims_ids(1))
  199. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  200. ncstat = nf_def_dim (nc_outfile_id, 'y', jpj, nc_dims_ids(2))
  201. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  202. if ( ndims > 2) then
  203. ncstat = nf_def_dim (nc_outfile_id, 'time', ntime, nc_dims_ids(3))
  204. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  205. ncstat = nf_def_var (nc_outfile_id, 'time', NF_DOUBLE, 1, &
  206. & nc_dims_ids(3), nc_time_id)
  207. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  208. endif
  209. ncstat = nf_def_var (nc_outfile_id, Vvarin, NF_DOUBLE, 3, &
  210. & nc_dims_ids, nc_varV_id )
  211. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  212. ncstat = nf_enddef(nc_outfile_id)
  213. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  214. ncstat = nf_put_var_double(nc_outfile_id, nc_varV_id, Vfield2)
  215. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  216. if ( ndims > 2) then
  217. ncstat = nf_put_var_double(nc_outfile_id, nc_time_id, time)
  218. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  219. endif
  220. ncstat = nf_close(nc_outfile_id)
  221. if (ncstat .ne. nf_noerr) call handle_err(ncstat)
  222. deallocate(Ufield2,Vfield2,time,nc_dims_ids)
  223. end program rotateUVorca