m_get_mod_xyz.f90 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. # 10 "<stdin>"
  9. module m_get_mod_xyz
  10. ! Gets model dimensions in file './mask.nc'
  11. ! (unless another netcdf file is submitted) and returns them.
  12. ! Added by F. Massonnet to the NERSC-ENKF routines, May 2013.
  13. ! (Presumably) Coded by C. König Beatty, in 2009
  14. ! Goal is to quickly retrieve model dimensions without using parseblk
  15. use netcdf
  16. use qmpi
  17. private handle_err
  18. contains
  19. subroutine get_mod_xyz(x, y, z, moddimfilein)
  20. implicit none
  21. ! In/out
  22. integer, intent(out) :: x, y, z
  23. character(len=*), intent(in), optional :: moddimfilein
  24. ! NetCDF vars
  25. integer :: ncid, dimID, error
  26. character(len=120) :: moddimfile
  27. logical ex
  28. if (present(moddimfilein)) then
  29. moddimfile=moddimfilein
  30. else
  31. moddimfile='./mask.nc'
  32. end if
  33. ! check the netCDF file exists
  34. inquire(file=moddimfile, exist=ex)
  35. if (.not.ex) then
  36. if (master) then
  37. print *, '(get_mod_xyz): file does not exist: '//trim(moddimfile)
  38. end if
  39. call stop_mpi()
  40. end if
  41. ! open the netCDF file
  42. error = nf90_open(trim(moddimfile),nf90_NoWrite,ncid)
  43. if (error.ne.nf90_noerr) call handle_err(error, "opening")
  44. ! Find DimID of x
  45. error = nf90_inq_dimid(ncid, 'x', dimID)
  46. if (error.ne.nf90_noerr) call handle_err(error, "inquiring dimID x")
  47. ! Get size of dimension
  48. error = nf90_inquire_dimension(ncid, dimID, len = x)
  49. if (error.ne.nf90_noerr) call handle_err(error, "getting dimension x")
  50. ! Find DimID of y
  51. error = nf90_inq_dimid(ncid, 'y', dimID)
  52. if (error.ne.nf90_noerr) call handle_err(error, "inquiring dimID y")
  53. ! Get size of dimension
  54. error = nf90_inquire_dimension(ncid, dimID, len = y)
  55. if (error.ne.nf90_noerr) call handle_err(error, "getting dimension y")
  56. ! Find DimID of z
  57. ! in mesh file from nemo z var = nav_lev
  58. !error = nf90_inq_dimid(ncid, 'z', dimID)
  59. error = nf90_inq_dimid(ncid, 'nav_lev', dimID)
  60. if (error.ne.nf90_noerr) call handle_err(error, "inquiring dimID z")
  61. ! Get size of dimension
  62. error = nf90_inquire_dimension(ncid, dimID, len = z)
  63. if (error.ne.nf90_noerr) call handle_err(error, "getting dimension z")
  64. ! Close file
  65. error = nf90_close(ncid) ! close netCDF dataset
  66. if (error.ne.nf90_noerr) call handle_err(error, "closing")
  67. contains
  68. subroutine handle_err(status, infomsg)
  69. integer, intent ( in) :: status
  70. character(len = *), intent ( in), optional :: infomsg
  71. if(status /= nf90_noerr) then
  72. if (present(infomsg)) then
  73. print *, 'Error while '//infomsg//' - '//trim(nf90_strerror(status))
  74. else
  75. print *, trim(nf90_strerror(status))
  76. endif
  77. stop " Stopped"
  78. end if
  79. end subroutine handle_err
  80. end subroutine get_mod_xyz
  81. end module m_get_mod_xyz