m_parse_blkdat.f90 3.5 KB

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