fetchNoiseMap.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # A script to copy a noise channel map for specific file or time
4 
5 import subprocess, sys, os
6 from datetime import datetime,timedelta
7 
8 def get_time(time_str):
9  return datetime.strptime(time_str,"%Y-%m-%dT%H:%M:%S+00:00")
10 
11 def get_mapname(stime):
12  return stime.strftime("mask.FD.%Y%m%d_%H00*.nhm")
13 
14 def get_metadata(fname):
15  """ get the file metadata as a dict"""
16  p = subprocess.Popen(['samweb','get-metadata',fname],
17  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18  resp_s,err=p.communicate()
19  if(len(err)): print("### Error: "+err);
20 
21  resp={}
22  for line in resp_s.split('\n'):
23  pos = line.find(':') #find the delimiter
24  if(pos==-1): continue
25  key=line[:pos].strip()
26  val=line[(pos+1):].strip()
27  resp[key]=val
28  return resp
29 
30 def fetch_map(fname,tgt):
31  """ Copy a noise channel mask here"""
32  p = subprocess.call(['scp','novadaq@novadaq-far-trigger-2016.fnal.gov:/ddt/NoiseMaps/backup/'+fname,tgt])
33  return p
34 
35 
36 
37 import argparse
38 parser = argparse.ArgumentParser(description='Fetch the noise channel mask for given file/timestamp')
39 parser.add_argument('FILE', help='A filename, which timestamp will be used')
40 parser.add_argument('--output','-o', default='.', help='Where to put the mask')
41 args = parser.parse_args()
42 print(args)
43 
44 fname=args.FILE
45 target = args.output
46 
47 mdata = get_metadata(fname)
48 stime = get_time(mdata['Start Time'])
49 
50 print("Start time = {}".format(stime))
51 attempt = 0
52 while True:
53  mapname=get_mapname(stime)
54  print("Attempt #{}: fetching file {} to {}".format(attempt,mapname,target))
55  if(fetch_map(mapname, target)==0):
56  print("Success")
57  break;
58  print("Probably map doesn't exist, trying 1 hour before...")
59  attempt+=1
60  stime-=timedelta(hours=1)
61  if(attempt>3):
62  print("Failed")
63  sys.exit(1)
64 
def get_mapname(stime)
def get_metadata(fname)
def fetch_map(fname, tgt)
def get_time(time_str)
Definition: fetchNoiseMap.py:8
if(dump)
bool print
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14