m_get_mod_xyz.f90 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. error = nf90_inq_dimid(ncid, 'z', dimID)
  58. if (error.ne.nf90_noerr) call handle_err(error, "inquiring dimID z")
  59. ! Get size of dimension
  60. error = nf90_inquire_dimension(ncid, dimID, len = z)
  61. if (error.ne.nf90_noerr) call handle_err(error, "getting dimension z")
  62. ! Close file
  63. error = nf90_close(ncid) ! close netCDF dataset
  64. if (error.ne.nf90_noerr) call handle_err(error, "closing")
  65. contains
  66. subroutine handle_err(status, infomsg)
  67. integer, intent ( in) :: status
  68. character(len = *), intent ( in), optional :: infomsg
  69. if(status /= nf90_noerr) then
  70. if (present(infomsg)) then
  71. print *, 'Error while '//infomsg//' - '//trim(nf90_strerror(status))
  72. else
  73. print *, trim(nf90_strerror(status))
  74. endif
  75. stop " Stopped"
  76. end if
  77. end subroutine handle_err
  78. end subroutine get_mod_xyz
  79. end module m_get_mod_xyz