m_FileResolv.F90 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. !-------------------------------------------------------------------------
  2. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  3. !-------------------------------------------------------------------------
  4. ! CVS m_FileResolv.F90,v 1.5 2012-04-30 01:02:53 jacob Exp
  5. ! CVS MCT_2_8_0
  6. !-----------------------------------------------------------------------
  7. !BOP
  8. !
  9. ! !MODULE: m_FileResolv --- Resolve file name templates
  10. !
  11. ! !INTERFACE:
  12. !
  13. MODULE m_FileResolv
  14. ! !USES:
  15. use m_StrTemplate ! grads style templates
  16. use m_die
  17. Implicit NONE
  18. !
  19. ! !PUBLIC MEMBER FUNCTIONS:
  20. !
  21. PRIVATE
  22. PUBLIC FileResolv
  23. PUBLIC remote_cp
  24. PUBLIC gunzip
  25. !
  26. ! !DESCRIPTION: This module provides routines for resolving GrADS like
  27. ! file name templates.
  28. !
  29. ! !REVISION HISTORY:
  30. !
  31. ! 10Jan2000 da Silva Initial code.
  32. !
  33. !EOP
  34. !-------------------------------------------------------------------------
  35. character(len=255) :: remote_cp = 'rcp'
  36. character(len=255) :: gunzip = 'gunzip'
  37. CONTAINS
  38. !-------------------------------------------------------------------------
  39. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  40. !-------------------------------------------------------------------------
  41. !BOP
  42. !
  43. ! !IROUTINE: FileResolv -- Resolve file name templates (single file)
  44. !
  45. ! !INTERFACE:
  46. !
  47. subroutine FileResolv ( expid, nymd, nhms, templ, fname, &
  48. stat, cache )
  49. ! !USES:
  50. IMPLICIT NONE
  51. !
  52. ! !INPUT PARAMETERS:
  53. !
  54. character(len=*), intent(in) :: expid ! Experiment id
  55. integer, intent(in) :: nymd ! Year-month-day
  56. integer, intent(in) :: nhms ! Hour-min-sec
  57. character(len=*), intent(in) :: templ ! file name template
  58. !
  59. ! !OUTPUT PARAMETERS:
  60. !
  61. character(len=*), intent(out) :: fname ! resolved file name
  62. integer, OPTIONAL, intent(out) :: stat ! Status
  63. ! 0 - file exists
  64. ! 1 - file does not exist
  65. logical, OPTIONAL, intent(in) :: cache ! skips rcp/gunzip if
  66. ! file exists locally
  67. ! !DESCRIPTION: Resolve file name templates, rcp'ing files from remote and
  68. ! performing gunzip'ing as necessary.
  69. !
  70. ! !TO DO:
  71. ! 1. Expand environment variables in templates
  72. !
  73. ! !REVISION HISTORY:
  74. !
  75. ! 10Jan2000 da Silva Initial code,
  76. ! 23Jul2002 J. Larson <larson@mcs.anl.gov> - fixed bug detected by the
  77. ! Fujitsu frt compiler (on the VPP).
  78. !
  79. !EOP
  80. !--------------------------------------------------------------------------
  81. character(len=*), parameter :: myname = 'MCT(MPEU)::FileResolv'
  82. #if SYSUNICOS || CPRCRAY
  83. integer, external :: ishell
  84. #elif (!defined __GFORTRAN__)
  85. integer, external :: system
  86. #endif
  87. character(len=255) :: path, host, dirn, basen, head, tail, cmd, filen
  88. integer i, rc
  89. logical :: fexists, caching
  90. ! Default is cache = .true.
  91. ! -------------------------
  92. if ( present(cache) ) then
  93. caching = cache
  94. else
  95. caching = .TRUE.
  96. end if
  97. ! Start by expanding template
  98. ! ---------------------------
  99. call strTemplate ( path, templ, 'GRADS', trim(expid), nymd, nhms, rc )
  100. if ( rc .ne. 0 ) then
  101. if ( present(stat) ) then
  102. stat = 1
  103. return
  104. else
  105. call die ( myname, 'cannot expand template '//trim(templ) )
  106. end if
  107. end if
  108. ! Parse file name
  109. ! ---------------
  110. i = index ( trim(path), ':' )
  111. if ( i .gt. 0 ) then
  112. host = path(1:i-1)
  113. fname = path(i+1:)
  114. else
  115. host = ''
  116. fname = path
  117. end if
  118. i = index ( trim(fname), '/', back=.true. )
  119. if ( i .gt. 1 ) then
  120. dirn = fname(1:i-1)
  121. basen = fname(i+1:)
  122. else if ( i .gt. 0 ) then
  123. dirn = fname(1:i)
  124. basen = fname(i+1:)
  125. else
  126. dirn = ''
  127. basen = fname
  128. end if
  129. i = index ( basen, '.', back=.true. )
  130. if ( i .gt. 0 ) then
  131. head = basen(1:i-1)
  132. tail = basen(i+1:)
  133. else
  134. head = basen
  135. tail = ''
  136. end if
  137. ! print *, 'Template = |'//trim(templ)//'|'
  138. ! print *, ' path = |'//trim(path)//'|'
  139. ! print *, ' host = |'//trim(host)//'|'
  140. ! print *, ' dirn = |'//trim(dirn)//'|'
  141. ! print *, ' basen = |'//trim(basen)//'|'
  142. ! print *, ' head = |'//trim(head)//'|'
  143. ! print *, ' tail = |'//trim(tail)//'|'
  144. ! print *, ' fname = |'//trim(fname)//'|'
  145. ! If file is remote, bring it here
  146. ! --------------------------------
  147. if ( len_trim(host) .gt. 0 ) then
  148. if ( trim(tail) .eq. 'gz' ) then
  149. inquire ( file=trim(head), exist=fexists )
  150. filen = head
  151. else
  152. inquire ( file=trim(basen), exist=fexists )
  153. filen = basen
  154. end if
  155. if ( .not. ( fexists .and. caching ) ) then
  156. cmd = trim(remote_cp) // ' ' // &
  157. trim(host) // ':' // trim(fname) // ' . '
  158. #if SYSUNICOS || CPRCRAY
  159. rc = ishell ( cmd )
  160. #else
  161. rc = system ( cmd )
  162. #endif
  163. if ( rc .eq. 0 ) then
  164. fname = basen
  165. else
  166. if ( present(stat) ) then ! return an error code
  167. stat = 2
  168. return
  169. else ! shut down
  170. fname = basen
  171. call die ( myname, 'cannot execute: '//trim(cmd) )
  172. end if
  173. end if
  174. else
  175. fname = filen
  176. call warn(myname,'using cached version of '//trim(filen) )
  177. end if
  178. ! If not, make sure file exists locally
  179. ! -------------------------------------
  180. else
  181. inquire ( file=trim(fname), exist=fexists )
  182. if ( .not. fexists ) then
  183. if ( present(stat) ) then
  184. stat = 3
  185. else
  186. call die(myname,'cannot find '//trim(fname) )
  187. end if
  188. end if
  189. end if
  190. ! If file is gzip'ed, leave original alone and create uncompressed
  191. ! version in the local directory
  192. ! ----------------------------------------------------------------
  193. if ( trim(tail) .eq. 'gz' ) then
  194. inquire ( file=trim(head), exist=fexists ) ! do we have a local copy?
  195. if ( .not. ( fexists .and. caching ) ) then
  196. if ( len_trim(host) .gt. 0 ) then ! remove file.gz
  197. cmd = trim(gunzip) // ' -f ' // trim(fname)
  198. else ! keep file.gz
  199. cmd = trim(gunzip) // ' -c ' // trim(fname) // ' > ' // trim(head)
  200. end if
  201. #if SYSUNICOS || CPRCRAY
  202. rc = ishell ( cmd )
  203. #else
  204. rc = system ( cmd )
  205. #endif
  206. if ( rc .eq. 0 ) then
  207. fname = head
  208. else
  209. if ( present(stat) ) then
  210. stat = 4
  211. return
  212. else
  213. call die ( myname, 'cannot execute: '//trim(cmd) )
  214. end if
  215. end if
  216. else
  217. fname = head
  218. call warn(myname,'using cached version of '//trim(head) )
  219. end if
  220. end if
  221. ! Once more, make sure file exists
  222. ! --------------------------------
  223. inquire ( file=trim(fname), exist=fexists )
  224. if ( .not. fexists ) then
  225. if ( present(stat) ) then
  226. stat = 3
  227. else
  228. call die(myname,'cannot find '//trim(fname) )
  229. end if
  230. end if
  231. ! All done
  232. ! --------
  233. if ( present(stat) ) stat = 0
  234. end subroutine FileResolv
  235. end MODULE m_FileResolv