#!/export/ldcg_server/ldcg/solaris/LDR/python/bin/python """ Client for querying a LDRdataFindServer to find LSC data. Uses pyGlobus and the LDRdataFindClient modules. $Id: LSCdataFind,v 1.25 2005/08/23 20:03:30 duncan Exp $ """ __version__ = '$Revision: 1.25 $'[11:-2] import sys import os import getopt import re import exceptions try: from pyGlobus import security except ImportError: print >> sys.stderr, """ Error: unable to import security module from pyGlobus. Check that pyGlobus is correctly installed and in your PYTHONPATH. """ sys.exit(1) # this client should have its PYTHONPATH set for it during installation # to find LDRdataFindClient.py, but if it does not we can try to find # it by looking for LDR_LOCATION PYTHONPATH = os.getenv('PYTHONPATH', None) if not PYTHONPATH: LDR_LOCATION = os.getenv('LDR_LOCATION', None) if LDR_LOCATION: sys.path.append("%s/ldr/lib" % LDR_LOCATION) try: from glue import LDRdataFindClient from glue import gsiserverutils from glue.lal import LIGOTimeGPS except ImportError, e: print >> sys.stderr, """ Error: unable to import modules from glue. Check that glue is correctly installed and in your PYTHONPATH. %s """ % e sys.exit(1) def usage(): """ Print a usage message to stderr. """ msg = """\ NAME LSCdataFind SYNOPSIS LSCdataFind --server=NAME:PORT --observatory=NAME --type=NAME --gps-start-time=GPS --gps-end-time=GPS [ --lal-cache ] [ --url-type=SCHEME ] [ --match=EXPRESSION ] [ --names-only ] [ --limit=LIMIT ] [ --offset=OFFSET ] [ --strict-time-check ] [ --version ] LSCdataFind --server=NAME:PORT --filename LSCdataFind --server=NAME:PORT --show-observatories LSCdataFind --server=NAME:PORT --show-types LSCdataFind --server=NAME:PORT --ping LSCdataFind --server=NAME:PORT --help DESCRIPTION Query a LDRdataFindServer to obtain physical filenames or URLs for data files from a certain instrument and of a particular frame type within a GPS range. -v, --version Print version information for LSCdataFind client -o, --observatory observatory(ies) that generated frame file Use --show-observatories to see what is available -t, --type type of frame file Use --show-types to see what is available -s, --gps-start-time start of GPS time range -e, --gps-end-time end of GPS time range -r, --server hostname and optional port of server to query, in the form host:port -l, --lal-cache format output for use as a LAL cache file -m, --match return only results that match a regular expression -n, --names-only return only the names of files with particular values for instrument, type, start, and end rather than full URLs -u, --url-type return only URLs with particular scheme or head such as 'file' or 'gsiftp' -w, --show-observatories list available observatory data -y, --show-types list available types -p, --ping ping the LDRDataFind server -f, --filename return URL(s) for a particular file --limit limit the number of results returned --offset offset from which to count the limit of results returned, requires using --limit --strict-time-check return only frame files within the GPS times and with no padding on the boundaries; this may break LAL frame cache reading routines -h, --help show this usage message ENVIRONMENT LSC_DATAFIND_SERVER can be set to avoid having to use the --server option on the command line. LSC_DATAFIND_URL_TYPE can be set to avoid having to use the --url-type option on the command line. LSC_DATAFIND_MATCH can be set to avoid having to use the --match option on the command line. EXAMPLE [hydra]$ LSCdataFind --server=dataserver.phys.uwm.edu --observatory=H --type=R --gps-start-time=714024240 --gps-end-time=714024340 --url-type=file --match=localhost file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024224-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024240-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024256-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024272-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024288-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024304-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024320-16.gwf file://localhost/netdata/s001/S1/R/H/714023808-714029599/H-R-714024336-16.gwf \ """ print >>sys.stderr, msg # grab command line options shortop = "vt:s:e:wyphlr:f:u:m:o:n" longop = [ "version", "help", "show-observatories", "show-types", "observatory=", "gps-start-time=", "gps-end-time=", "type=", "ping", "server=", "lal-cache", "filename=", "url-type=", "match=", "names-only", "limit=", "offset=", "strict-time-check", ] try: opts, args = getopt.getopt(sys.argv[1:], shortop, longop) except getopt.GetoptError: print >>sys.stderr, "Error parsing command line" print >>sys.stderr, "Enter 'LSCdataFind --help' for usage" sys.exit(1) # defaults hostPortString = None port = 30010 lalcache = False clientMethodArgDict = { 'observatory': None, 'end': None, 'start': None, 'type': None, 'filename': None, 'urlType': None, 'match': None, 'limit': None, 'offset': None, 'strict': None } # default method clientMethod = 'findFrameURLs' # environment variables override defaults but not # command line options try: hostPortString = os.environ['LSC_DATAFIND_SERVER'] except: pass try: clientMethodArgDict['urlType'] = os.environ['LSC_DATAFIND_URL_TYPE'] clientMethod = 'findFrameURLsFilter' except: pass try: clientMethodArgDict['match'] = os.environ['LSC_DATAFIND_MATCH'] clientMethod = 'findFrameURLsFilter' except: pass for o, a in opts: if o in ("-h", "--help"): usage() sys.exit(0) elif o in ("-v", "--version"): print 'LSCdataFind client version', __version__ import glue.LDRdataFindClient print 'Built on top of LDRdataFindClient version', glue.LDRdataFindClient.version() sys.exit(0) elif o in ("-p", "--ping"): clientMethod = 'ping' elif o in ("-w", "--show-observatories"): clientMethod = 'showObservatories' elif o in ("-y", "--show-types"): clientMethod = 'showTypes' elif o in ("-o", "--observatory"): clientMethodArgDict['observatory'] = a elif o in ("-s", "--gps-start-time"): clientMethodArgDict['start'] = str(LIGOTimeGPS(a).seconds) elif o in ("-e", "--gps-end-time"): a = LIGOTimeGPS(a) if a.nanoseconds: clientMethodArgDict['end'] = str(a.seconds + 1) else: clientMethodArgDict['end'] = str(a.seconds) elif o in ("-t", "--type"): clientMethodArgDict['type'] = a elif o in ("-l", "--lal-cache"): lalcache = True elif o in ("-f", "--filename"): clientMethodArgDict['filename'] = a clientMethod = 'singleFrameFind' elif o in ("-r", "--server"): hostPortString = a elif o in ("-u", "--url-type"): pat = re.compile("rl-type") m = pat.match(a) if m: print >>sys.stderr, "The correct option is --url-type" print >>sys.stderr, "Enter 'LSCdataFind --help' for usage" sys.exit(1) clientMethodArgDict['urlType'] = a clientMethod = 'findFrameURLsFilter' elif o in ("-m", "--match"): clientMethodArgDict['match'] = a clientMethod = 'findFrameURLsFilter' elif o in ("-n", "--names-only"): clientMethodArgDict['namesOnly'] = True clientMethod = 'findFrameNames' elif o in ("--limit",): clientMethodArgDict['limit'] = int(a) elif o in ("--offset",): clientMethodArgDict['offset'] = int(a) elif o in ("--strict-time-check",): clientMethodArgDict['strict'] = True if not clientMethod: print >>sys.stderr, "Bad combination or missing options" print >>sys.stderr, "Enter 'LSCdataFind --help' for usage" sys.exit(1) # determine server and port if not hostPortString: print >>sys.stderr, "No LDRdataFindServer specified" print >>sys.stderr, "Enter 'LSCdataFind --help' for usage" sys.exit(1) if hostPortString.find(':') < 0: # no port specified host = hostPortString else: # server and port specified host, portString = hostPortString.split(':') port = int(portString) # open connection to LDRdataFindServer try: myClient = LDRdataFindClient.LSCdataFindClient(host, port) except Exception, e: print >>sys.stderr, "Unable to connect to LDRdataFindServer %s:%d" % (host, port) if gsiserverutils.checkCredentials(): print >>sys.stderr, "Got the following error : " + str(e) print >>sys.stderr, "Enter 'LSCdataFind --help' for usage" sys.exit(1) # execute the query and print the result try: result = eval("myClient.%s(%s)" % (clientMethod, clientMethodArgDict)) if isinstance( result, str ): print result elif isinstance( result, LDRdataFindClient.lfnlist ): if len(result) == 0: print >>sys.stderr, "No files found!" else: for r in result: print r elif isinstance( result, LDRdataFindClient.pfnlist ): if len(result) == 0: print >>sys.stderr, "No URLs found!" else: if lalcache: for pfn in result: lfn = os.path.basename(pfn) head, ext = os.path.splitext(lfn) a, b, c, d = head.split('-') print "%s %s %s %s %s" % (a, b, c, d, pfn) else: for r in result: print r elif isinstance( result, list ): for r in result: print r else: msg = "Error: unknown result format : %s" % result raise LDRdataFindClient.LDRdataFindException, msg except Exception, e: print >>sys.stderr, "Error querying LDRdataFindServer: %s" % str(e) print >>sys.stderr, "Enter 'LSCdataFind --help' for usage" sys.exit(1) sys.exit(0)