def readURL(url, tmpFile=None): lines = [] try: datasource = urlopen(url) while True: line = datasource.readline() if line == '': break lines.append(line) if len(lines) == 0: Logfile.error('Cannot read from URL:', url) lines = [] Logfile.add(str(len(lines)) + ' lines read from URL:', url) if tmpFile: writeTextFile(tmpFile, lines) except: Logfile.exception('readURL') return lines
def existsHTML_Page(url, text=None, withComment=False, isAbort=False): h = httplib2.Http() try: resp = h.request(url, 'HEAD') status = int(resp[0]['status']) if status < 400: return True # page exists except: status = 104 # error: [Errno 104] Connection reset by peer ??? if withComment: s = 'HTML page not found, error = ' + str(status) if text is None: Logfile.error(s, url) else: Logfile.error(text, s, url) if isAbort: Logfile.abort() return False
def checkExistsDir(dirName, isAbort=False): if os.path.isdir(dirName): return True Logfile.error('Cannot find directory', dirName) if isAbort: Logfile.abort() return False
def checkKeys(conf, keyList, optional=False): if type(keyList) is str: list1 = list(keyList) else: list1 = keyList if not optional: Basic.checkExistsKeys(conf, list1, isAbort=True) eventDir = Globals.EventDir() isOk = True for key in list1: val = conf[key] msg = None if key == duration: msg = Basic.checkGreaterZero(val) elif key in [mindist, maxdist]: msg = Basic.checkNotNegative(val) elif key in [keyfilefolder, metaCatalog]: Basic.checkExistsDir(os.path.join(eventDir, val), isAbort=True) elif key in [blacklist, mail, pwd]: continue if msg is not None: isOk = Logfile.error('Key <' + key + '> in config file: ' + msg) if not isOk: Logfile.abort() return True
def createDirectory(dirName, optional=False): if os.path.isdir(dirName): return True os.makedirs(dirName) if os.path.isdir(dirName): return True else: Logfile.error('Cannot open directory', dirName) if not optional: Logfile.abort() return False
def readWaveforms(stationList, tw, EventPath, Origin): t2 = UTCDateTime(Origin.time) sdspath = os.path.join(EventPath, 'data', str(t2.year)) Wdict = OrderedDict() for i in stationList: streamData = i.getName() + '.D.' + str(t2.year)\ + '.' + str("%03d" % t2.julday) entry = os.path.join(sdspath, i.net, i.sta, i.comp + '.D', streamData) tdiff = tw['end'] - tw['start'] try: st = read(entry, format="MSEED", starttime=tw['start'], endtime=tw['end'], nearest_sample=True) except Exception: Logfile.error('readWaveforms: File not found', entry) pass if len(st.get_gaps()) > 0: st.merge(method=0, fill_value='interpolate', interpolation_samples=0) if len(st) > 0: trdiff = st[0].stats.endtime - st[0].stats.starttime totaldiff = abs(trdiff - tdiff) if totaldiff < 1: Wdict[i.getName()] = st Logfile.add(i.getName() + ' added to StreamList ') else: print(' OUT ', streamData) Logfile.red('%d Streams added with available Data' % len(Wdict)) return Wdict
def checkExistsKeys(dict, keyList, isAbort=False): isOk = True for key in keyList: if not key in dict: isOk = Logfile.error('Key <' + str(key) + '> missing in config file') if isOk: return True elif isAbort: Logfile.abort() return False
def _error0(self, msg): Logfile.error(msg) Logfile.abort()