m_parse_blkdat.F90 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. module m_parse_blkdat
  2. private :: blkini, blkinr, blkinvoid
  3. contains
  4. subroutine parse_blkdat(cvar,vtype,realvar,intvar,blkfilein,imatch)
  5. implicit none
  6. character(len=6), intent(in) :: cvar
  7. character(len=*), intent(in) :: vtype
  8. integer, intent(out) :: intvar
  9. real , intent(out) :: realvar
  10. character(len=*), intent(in), optional :: blkfilein
  11. integer , intent(in), optional :: imatch
  12. character(len=80) :: blkfile
  13. logical :: found,ex
  14. integer :: nmatch,imatch2
  15. if (present(blkfilein)) then
  16. blkfile=blkfilein
  17. else
  18. blkfile='blkdat.input'
  19. end if
  20. if (present(imatch)) then
  21. imatch2=imatch
  22. else
  23. imatch2=1
  24. end if
  25. inquire(exist=ex,file=trim(blkfile))
  26. nmatch=0
  27. if (ex) then
  28. open(99,file=trim(blkfile),status='old')
  29. ! Skip header
  30. read(99,*)
  31. read(99,*)
  32. read(99,*)
  33. read(99,*)
  34. found=.false.
  35. do while (.not.found)
  36. found = blkinvoid(cvar)
  37. if (found) then
  38. nmatch=nmatch+1
  39. !print *,found,nmatch,imatch2
  40. found=found.and.nmatch==imatch2
  41. !print *,found
  42. end if
  43. end do
  44. ! if found, read..
  45. if (found) then
  46. backspace(99)
  47. if (trim(vtype)=='integer') then
  48. call blkini(intvar,cvar)
  49. elseif (trim(vtype)=='real') then
  50. call blkinr(realvar,cvar,'(a6," =",f10.4," m")')
  51. else
  52. print *,'Dont know how to handle variable type '//trim(vtype)
  53. stop '(parse_blkdat)'
  54. end if
  55. else
  56. print *,'Cant find varable'
  57. stop '(parse_blkdat)'
  58. end if
  59. close(99)
  60. else
  61. print *,'Cant find '//trim(blkfile)
  62. stop '(parse_blkdat)'
  63. end if
  64. end subroutine parse_blkdat
  65. subroutine blkinr(rvar,cvar,cfmt)
  66. !use mod_xc ! HYCOM communication interface
  67. implicit none
  68. real rvar
  69. character cvar*6,cfmt*(*)
  70. ! read in one real value
  71. character*6 cvarin
  72. read(99,*) rvar,cvarin
  73. write(6,cfmt) cvarin,rvar
  74. !call flush(6)
  75. if (cvar.ne.cvarin) then
  76. write(6,*)
  77. write(6,*) 'error in blkinr - input ',cvarin, &
  78. ' but should be ',cvar
  79. write(6,*)
  80. !call flush(6)
  81. stop '(blkinr)'
  82. endif
  83. return
  84. end subroutine
  85. subroutine blkini(ivar,cvar)
  86. implicit none
  87. integer ivar
  88. character*6 cvar
  89. ! read in one integer value
  90. character*6 cvarin
  91. read(99,*) ivar,cvarin
  92. if (cvar.ne.cvarin) then
  93. write(6,*)
  94. write(6,*) 'error in blkini - input ',cvarin, &
  95. ' but should be ',cvar
  96. write(6,*)
  97. !call flush(6)
  98. stop '(blkini)'
  99. endif
  100. end subroutine blkini
  101. logical function blkinvoid(cvar)
  102. implicit none
  103. real :: rvar
  104. character :: cvar*6
  105. character*6 :: cvarin
  106. read(99,*) rvar, cvarin
  107. blkinvoid = trim(cvar) == trim(cvarin)
  108. end function blkinvoid
  109. end module m_parse_blkdat