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