fetch_tb_beamline_files.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 from __future__ import print_function
4 import os, sys, shlex, subprocess
5 import samweb_client
6 
7 samweb = samweb_client.SAMWebClient( experiment = "nova" )
8 
9 fname = os.path.basename( os.getenv( "fname" ) ) # Input raw detector file name from art_sam_wrap.sh
10 
11 try :
12  md = samweb.getMetadata( fname )
13 except :
14  print("Could not retrieve metadata for \"%s\"" % fname, file=sys.stderr)
15  sys.exit(1)
16 
17 try :
18  end_time = md[ "Online.SubRunEndTime" ]
19  start_time = md[ "Online.SubRunStartTime" ]
20 except :
21  print("Could not retrieve subrun start/end time metadata for \"%s\"" % fname, file=sys.stderr)
22  sys.exit(2)
23 
24 # Search for beamline files that overlap in time with the detector file
25 dims = "file_type importedDetector and data_tier raw"
26 dims += " and data_stream beamline and online.detector testbeam-beamline"
27 dims += " and Beamline.FirstTriggerTime < %d and Beamline.LastTriggerTime > %d" % ( end_time, start_time )
28 
29 beamline_files = samweb.listFiles( dimensions = dims )
30 
31 if len( beamline_files ) > 0 :
32  print("\nBeamline files matched to detector file \"%s\":" % fname)
33  for f in beamline_files :
34  print(f)
35  print("\nFetching beamline files...")
36 
37 else :
38  print("\nNo matching beamline files found for detector file \"%s\"" % fname)
39  #sys.exit(3)
40 
41 
42 for f in beamline_files :
43 
44  if os.path.isfile( os.path.join( os.getcwd(), f ) ) :
45  print("\nBeamline file \"%s\" already exists in working directory" % f)
46  continue
47 
48  else :
49  print("\nFetching beamline file \"%s\"..." % f)
50  cmd = "ifdh_fetch -e nova %s" % f
51  p = subprocess.Popen( shlex.split( cmd ) )
52  returncode = p.wait()
53 
54  if returncode != 0 :
55  print("ifdh_fetch failed for beamline file \"%s\"" % f, file=sys.stderr)
56  sys.exit(4)
57 
bool print