ttb_restart.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """
  2. TTB restart test.
  3. """
  4. # ***
  5. def MyLogger( name ) :
  6. # external:
  7. import logging
  8. import sys
  9. # setup logger:
  10. logger = logging.getLogger( name )
  11. # no handlers yet ? then print to standard output:
  12. if len(logger.handlers) == 0 : logger.addHandler(logging.StreamHandler(sys.stdout))
  13. # no level set yet ? then set for info and higher:
  14. if logger.level == 0 : logger.setLevel(logging.INFO)
  15. # ok:
  16. return logger
  17. #enddef
  18. # ***
  19. def Test( rcf ) :
  20. # external:
  21. import os
  22. import datetime
  23. import subprocess
  24. # tools:
  25. import rc
  26. import ttb_compare
  27. # get logger:
  28. logger = MyLogger('ttb')
  29. # test name:
  30. test_name = 'restart'
  31. # info ...
  32. logger.info( ' "%s" test ...' % test_name )
  33. # name of TM5 rcfile:
  34. tm_rcfile = rcf.get( 'ttb.rcfile' )
  35. # load the tm5 rcfile:
  36. tm_rcf = rc.RcFile( tm_rcfile )
  37. # flags:
  38. no_run = rcf.get( 'ttb.no-run', 'bool' )
  39. # time range:
  40. tfmt = '%Y-%m-%d %H:%M:%S'
  41. t_start = datetime.datetime.strptime( rcf.get('ttb.restart.timerange.start'), tfmt )
  42. t_end = datetime.datetime.strptime( rcf.get('ttb.restart.timerange.end'), tfmt )
  43. # for restart:
  44. t_mid = t_start + (t_end-t_start)/2
  45. # run id's:
  46. runids = ['restart0','restart1','restart2']
  47. #
  48. # perform runs
  49. #
  50. # init lists:
  51. test_rcfs = []
  52. # loop over runs:
  53. for irun in range(len(runids)) :
  54. # skip some ..
  55. #if irun == 0 : continue
  56. #if irun == 1 : continue
  57. # current value:
  58. runid = runids[irun]
  59. # read template rcfile in raw format:
  60. test_rcf_raw = rc.RcFile( tm_rcfile, raw=True )
  61. # replace project name:
  62. my_project = 'ttb'
  63. test_rcf_raw.replace( 'my.project', my_project )
  64. # replace runid:
  65. test_rcf_raw.replace( 'runid', runid )
  66. # replace project directory:
  67. test_rcf_raw.replace( 'my.project.dir', '${my.basedir}/${my.project}/%s/${runid}' % test_name )
  68. # restart specific:
  69. if irun == 0 :
  70. test_rcf_raw.replace( 'timerange.start' , t_start.strftime(tfmt) )
  71. test_rcf_raw.replace( 'timerange.end' , t_end.strftime(tfmt) )
  72. test_rcf_raw.replace( 'istart' , '2' )
  73. test_rcf_raw.replace( 'restart.restore.dir', 'None' )
  74. test_rcf_raw.replace( 'restart.store.dir' , '${my.basedir}/${my.project}/${runid}/restart' )
  75. elif irun == 1 :
  76. test_rcf_raw.replace( 'timerange.start' , t_start.strftime(tfmt) )
  77. test_rcf_raw.replace( 'timerange.end' , t_mid.strftime(tfmt) )
  78. test_rcf_raw.replace( 'istart' , '2' )
  79. test_rcf_raw.replace( 'restart.restore.dir', 'None' )
  80. test_rcf_raw.replace( 'restart.store.dir' , '${my.basedir}/${my.project}/${runid}/restart' )
  81. elif irun == 2 :
  82. test_rcf_raw.replace( 'timerange.start' , t_mid.strftime(tfmt) )
  83. test_rcf_raw.replace( 'timerange.end' , t_end.strftime(tfmt) )
  84. test_rcf_raw.replace( 'istart' , '33' )
  85. test_rcf_raw.replace( 'restart.restore.dir', '${my.basedir}/${my.project}/%s/restart' % runids[1] )
  86. test_rcf_raw.replace( 'restart.store.dir' , '${my.basedir}/${my.project}/${runid}/restart' )
  87. else :
  88. logger.error( 'unsupported runid : %s' % runid )
  89. sys.exit(1)
  90. #endif
  91. # target filename: tmp-<origingal_basename>-<runid>.rc
  92. bname,ext = os.path.splitext(tm_rcfile)
  93. test_rcfile_test = 'tmp-ttb-%s-%s.rc' % (test_name,runid)
  94. # write:
  95. test_rcf_raw.WriteFile( test_rcfile_test )
  96. # read again:
  97. test_rcf = rc.RcFile( test_rcfile_test )
  98. # add:
  99. test_rcfs.append( test_rcf )
  100. # run:
  101. if not no_run :
  102. logger.info( '' )
  103. logger.info( '******************************************************************' )
  104. logger.info( '' )
  105. logger.info( '%s' % runid )
  106. logger.info( '' )
  107. logger.info( '******************************************************************' )
  108. logger.info( '' )
  109. # setup and submit command:
  110. command = [os.path.join(os.curdir,'setup_tm5'), test_rcfile_test, '--submit']
  111. retcode = subprocess.call( command )
  112. if retcode != 0 :
  113. logger.error( 'from call:' )
  114. logger.error( ' %s' % command )
  115. raise Exception
  116. #endif
  117. #endif
  118. #endfor # runs
  119. #
  120. # check results
  121. #
  122. # which regions ?
  123. regions = test_rcf.get( 'regions' ).split()
  124. # loop over hours:
  125. t = t_mid
  126. while t <= t_end :
  127. # flags:
  128. isfirsthour = t == t_mid
  129. islasthour = t == t_end
  130. # info ...
  131. logger.info( '' )
  132. logger.info( 'compare restart files for time %s ...' % t )
  133. # loop over regions
  134. for region in regions :
  135. # files to be compared:
  136. fnames = []
  137. for irun in range(len(runids)) :
  138. # current:
  139. runid = runids[irun]
  140. test_rcf = test_rcfs[irun]
  141. # first file ?
  142. isfirstkey = ''
  143. if (irun == 2) and isfirsthour : isfirstkey = '_initial'
  144. # directory with restart files:
  145. fdir = test_rcf.get('restart.store.dir')
  146. # target file:
  147. fnam = 'TM5_restart_%s_%s%s.nc' % (t.strftime('%Y%m%d_%H%M'),region,isfirstkey)
  148. # add:
  149. fnames.append( os.path.join(fdir,fnam) )
  150. #endfor
  151. # which files ?
  152. i1 = 0
  153. i2 = 2
  154. # compare ...
  155. ttb_compare.restart_files( fnames[i1], fnames[i2] )
  156. #endfor # regions
  157. # next hour:
  158. t = t + datetime.timedelta(0,3600) # 1 hour
  159. ## testing ...
  160. #break
  161. #endfor # hours
  162. #enddef