tmm_info.F90 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. !###############################################################################
  2. !
  3. ! Info on meteo field:
  4. ! o param name
  5. ! o unit
  6. ! o history
  7. ! o ...
  8. !
  9. ! use tmm_info
  10. !
  11. ! type(TMeteoInfo) :: tmi
  12. !
  13. ! ! Initialise with name and unit;
  14. ! ! history is empty or optionally filled from existing info's.
  15. ! call Init( tmi, 'name', 'unit', status, (/tmi1,tmi2,.../) )
  16. !
  17. ! ! .. or init as copy of existing info:
  18. ! call Init( tmi, tmi2, status )
  19. !
  20. ! ! Add text to history:
  21. ! call AddHistory( tmi, 'type==od', status )
  22. !
  23. ! ! Add existing history to history:
  24. ! call AddHistory_tmi( tmi, tmi2, status )
  25. !
  26. ! ! extract fields:
  27. ! call Get( tmi, status, name=name, unit=unit, history=history )
  28. !
  29. ! ! ok
  30. ! call Done( tmi, status )
  31. !
  32. !### macro's ###################################################################
  33. !
  34. #define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
  35. #define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
  36. #define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
  37. !
  38. #include "tmm.inc"
  39. !
  40. !###############################################################################
  41. module tmm_info
  42. implicit none
  43. ! --- in/out --------------------------------------
  44. private
  45. public :: TMeteoInfo
  46. public :: Init, Done, Get
  47. public :: SetHistory
  48. public :: AddHistory
  49. ! --- const ------------------------------
  50. character(len=*), parameter :: mname = 'tmm_info'
  51. ! --- types --------------------------------------
  52. type TMeteoInfo
  53. character(len=16) :: name=''
  54. character(len=16) :: unit=''
  55. character(len=4000) :: history=''
  56. end type TMeteoInfo
  57. ! --- interfaces --------------------------------
  58. interface Init
  59. module procedure tmi_Init
  60. module procedure tmi_Init_copy
  61. end interface
  62. interface Done
  63. module procedure tmi_Done
  64. end interface
  65. interface Get
  66. module procedure tmi_Get
  67. end interface
  68. interface AddHistory
  69. module procedure tmi_AddHistory
  70. module procedure tmi_AddHistory_tmi
  71. end interface
  72. interface SetHistory
  73. module procedure tmi_SetHistory_tmi
  74. end interface
  75. contains
  76. ! =============================================================
  77. subroutine tmi_Init( tmi, name, unit, status, tmis )
  78. ! --- in/out -----------------------------------
  79. type(TMeteoInfo), intent(out) :: tmi
  80. character(len=*), intent(in) :: name
  81. character(len=*), intent(in) :: unit
  82. integer, intent(out) :: status
  83. type(TMeteoInfo), intent(in), optional :: tmis(:)
  84. ! --- const --------------------------------------
  85. character(len=*), parameter :: rname = mname//'/tmi_Init'
  86. ! --- local -------------------------------------
  87. integer :: i
  88. ! --- begin ------------------------------------
  89. ! store name and unit:
  90. tmi%name = trim(name)
  91. tmi%unit = trim(unit)
  92. ! start with empty history:
  93. tmi%history = ''
  94. ! include input histories ?
  95. if ( present(tmis) ) then
  96. ! loop over input info's :
  97. do i = 1, size(tmis)
  98. call AddHistory( tmi, tmis(i), status )
  99. end do
  100. end if
  101. ! add name and unit to history:
  102. call AddHistory( tmi, 'name=='//trim(tmi%name), status )
  103. call AddHistory( tmi, 'unit=='//trim(tmi%unit), status )
  104. ! ok
  105. status = 0
  106. end subroutine tmi_Init
  107. ! ***
  108. subroutine tmi_Init_copy( tmi, tmi2, status )
  109. ! --- in/out -----------------------------------
  110. type(TMeteoInfo), intent(out) :: tmi
  111. type(TMeteoInfo), intent(in) :: tmi2
  112. integer, intent(out) :: status
  113. ! --- const --------------------------------------
  114. character(len=*), parameter :: rname = mname//'/tmi_Init_copy'
  115. ! --- begin ------------------------------------
  116. ! copy:
  117. tmi = tmi2
  118. ! ok
  119. status = 0
  120. end subroutine tmi_Init_copy
  121. ! ***
  122. subroutine tmi_Done( tmi, status )
  123. ! --- in/out -----------------------------------
  124. type(TMeteoInfo), intent(inout) :: tmi
  125. integer, intent(out) :: status
  126. ! --- const --------------------------------------
  127. character(len=*), parameter :: rname = mname//'/tmi_Done'
  128. ! --- begin ------------------------------------
  129. ! ok
  130. status = 0
  131. end subroutine tmi_Done
  132. ! ***
  133. subroutine tmi_AddHistory( tmi, history, status )
  134. ! --- in/out -----------------------------------
  135. type(TMeteoInfo), intent(inout) :: tmi
  136. character(len=*), intent(in) :: history
  137. integer, intent(out) :: status
  138. ! --- const --------------------------------------
  139. character(len=*), parameter :: rname = mname//'/tmi_AddHistory'
  140. ! --- begin ------------------------------------
  141. ! add item to history, close with ';;'
  142. if ( len_trim(tmi%history)+len_trim(history)+2 < len(tmi%history) ) then
  143. tmi%history = trim(tmi%history)//trim(history)//';;'
  144. else
  145. !write (*,'("WARNING - history buffer too small; increase size in ",a)') mname
  146. end if
  147. ! ok
  148. status = 0
  149. end subroutine tmi_AddHistory
  150. ! ***
  151. subroutine tmi_AddHistory_tmi( tmi, tmi2, status )
  152. ! --- in/out -----------------------------------
  153. type(TMeteoInfo), intent(inout) :: tmi
  154. type(TMeteoInfo), intent(in) :: tmi2
  155. integer, intent(out) :: status
  156. ! --- const --------------------------------------
  157. character(len=*), parameter :: rname = mname//'/tmi_AddHistory_tmi'
  158. ! --- begin ------------------------------------
  159. ! add extra ';;', represents a new line:
  160. call AddHistory( tmi, trim(tmi2%history)//';;', status )
  161. ! ok
  162. status = 0
  163. end subroutine tmi_AddHistory_tmi
  164. ! ***
  165. subroutine tmi_SetHistory_tmi( tmi, tmi2, status )
  166. ! --- in/out -----------------------------------
  167. type(TMeteoInfo), intent(inout) :: tmi
  168. type(TMeteoInfo), intent(in) :: tmi2
  169. integer, intent(out) :: status
  170. ! --- const --------------------------------------
  171. character(len=*), parameter :: rname = mname//'/tmi_SetHistory_tmi'
  172. ! --- begin ------------------------------------
  173. ! replace existing history:
  174. tmi%history = tmi2%history
  175. ! ok
  176. status = 0
  177. end subroutine tmi_SetHistory_tmi
  178. ! ***
  179. subroutine tmi_Get( tmi, status, name, unit, history )
  180. ! --- in/out -----------------------------------
  181. type(TMeteoInfo), intent(in) :: tmi
  182. integer, intent(out) :: status
  183. character(len=*), intent(out), optional :: name
  184. character(len=*), intent(out), optional :: unit
  185. character(len=*), intent(out), optional :: history
  186. ! --- const --------------------------------------
  187. character(len=*), parameter :: rname = mname//'/tmi_Set'
  188. ! --- begin ------------------------------------
  189. ! fill name ?
  190. if ( present(name) ) name = trim(tmi%name)
  191. ! fill unit ?
  192. if ( present(unit) ) unit = trim(tmi%unit)
  193. ! fill history ?
  194. if ( present(history) ) history = trim(tmi%history)
  195. ! ok
  196. status = 0
  197. end subroutine tmi_Get
  198. end module tmm_info