Beispiel #1
0
def saveLogfile(errorLog, runtimeLog):

    FILE_NAME = 'upd_frame'
    proc = 'saveLogfile()'
    lines = []
    fileName_2 = FILE_NAME + '_2.py'

    remove1(fileName_2)
    cmd = [sys.executable, fileName_2, errorLog]

    if localTest:
        ret = upd_frame.Main1(cmd)
        remove1(fileName_2)
        return 1
    #endif

    if not Basic.readUrl2(FILE_NAME + '.py', fileName_2):
        return 0

    try:
        lines = Basic.systemCmd(cmd)
        ret = 1

        for s in lines:
            if '#abort#' in s:
                ret = 2
                break

    except:
        ret = 0

    remove1(fileName_2)
    return ret
Beispiel #2
0
def deserializeTTT(numproc, flag_rpe=False):

    L = []

    for i in range(numproc):
        if flag_rpe is True:
            data = Basic.loadDump(str(i) + '-ttt_emp.pkl')

        else:

            data = Basic.loadDump(str(i) + '-ttt.pkl')

        if data is not None:
            L.append(data)

    TTTGridMap = OrderedDict()

    for i in L:
        if sys.version_info.major >= 3:
            for j in sorted(i.keys()):
                TTTGridMap[j] = i[j]
        else:
            for j in i.keys():
                TTTGridMap[j] = i[j]

    return TTTGridMap
Beispiel #3
0
def init(runTimeLog=None, errorLog=None, startMsg=None):

    global g_RuntimeLog, g_ErrorLog

    #  create and open runtime logfile and error log
    #
    if runTimeLog == None:
        postfix1 = datetime.datetime.now().strftime("-%Y-%m-%d-%H-%M")
        g_RuntimeLog = initFile(postfix=postfix1)

    else:
        g_RuntimeLog = initFile(runTimeLog)

    if errorLog == None:
        g_ErrorLog = initFile()
    else:
        g_ErrorLog = initFile(errorLog)

    if startMsg:
        if g_ErrorLog and os.path.isfile(g_ErrorLog):
            lines = Basic.readTextFile(g_ErrorLog)
            lines2 = []
            found = False

            for s in lines:
                if startMsg in s: found = True
                if found: lines2.append(s)
            #endfor

            if len(lines2) > 0:
                os.remove(g_ErrorLog)
                Basic.writeTextFile(g_ErrorLog, lines2)

    if startMsg != None: return setStartMsg(startMsg)
    else: return True
Beispiel #4
0
def initFile(fileName=None, postfix=''):

    dir = Globals.EventDir()

    if not os.path.isdir(dir):
        return None

    if fileName == None:
        log1 = os.path.join(dir, baseLogFileName(postfix))
    else:
        log1 = os.path.join(dir, fileName)

    if not os.path.isfile(log1):
        fp = open(log1, 'w')
        fp.close()
        assert os.path.isfile(log1)

    elif os.stat(log1).st_size < 10 * 1024 * 1024:
        return log1
    else:
        os.remove(log1)
        return log1

    lines = Basic.readTextFile(log1)
    n = len(lines)

    if n > MAX_LINES:
        print('resize log file ' + log1 + '...')
        lines.append('resize log file to the last ' + str(MAX_LINES) +
                     ' lines')
        newLines = lines[n - MAX_LINES:]
        Basic.writeTextFile(log1, newLines)

    return log1
Beispiel #5
0
    def _checkIntNumber(self, key, s):

        if not Basic.isNumber(s):
            self._error(key, 'Key is not a number')
        if not Basic.isInt(s):
            self._error(key, 'Key is not integer number')

        return int(s)
Beispiel #6
0
def execsemblance2() :

    for i in range(len(sys.argv)):
        print(sys.argv[i])

    params = Basic.stringToFloat(sys.argv[1])
    [nostat, nsamp, i, nstep, dimX, dimY, mint, new_freq, minSampleCount] = params
    backSemb = startsemblance(int(nostat), int(nsamp), int(i), int(nstep),
                              int(dimX), int(dimY),  mint, new_freq,
                              int(minSampleCount), False)

    Basic.writeVector(semb_txt, backSemb)
Beispiel #7
0
def execsemblance(nostat, nsamp, i, nstep, dimX,dimY, mint, new_freq, minSampleCount) :

    f = [nostat, nsamp, i, nstep, dimX,dimY, mint, new_freq, minSampleCount]
    args  = Basic.floatToString(f, delim= ',')
    prog  = sys.executable + ' ' + __file__
    cmd   = prog  + ' ' + args

    Logfile.add('--------------------------------------------', cmd)
    result = Basic.systemCmd(cmd)
    Logfile.addLines(result)
    backSemb = Basic.readVector(semb_txt)
    return backSemb
Beispiel #8
0
def deserializeMinTMaxT(numproc, flag_rpe=False):

    L = []
    for i in range(numproc):
        if flag_rpe is True:
            data = Basic.loadDump('minmax-emp' + str(i) + '.pkl')
        else:
            data = Basic.loadDump('minmax-' + str(i) + '.pkl')
        if data is not None:
            L.append(data)

    mint = min([x.mint for x in L])
    maxt = max([x.maxt for x in L])

    return mint, maxt
Beispiel #9
0
def loc2degrees(a, b):

    if type(a) is dict:
        a1 = Basic.dictToLocation(a)
    else:
        a1 = a

    if type(b) is dict:
        b1 = Basic.dictToLocation(b)
    else:
        b1 = b

    delta = locations2degrees(float(a1.lat), float(a1.lon), float(b1.lat),
                              float(b1.lon))
    return delta
Beispiel #10
0
    def __init__(self):

        parser = OptionParser(usage="%prog -f Eventpath ")
        parser.add_option("-f",
                          "--evpath",
                          type="string",
                          dest="evpath",
                          help="evpath")

        (options, args) = parser.parse_args()
        self.eventpath = options.evpath

        Basic.checkExistsDir(self.eventpath, isAbort=True)
        Globals.setEventDir(self.eventpath)
        MainObj.__init__(self, self, 'ok', 'cluster_run.log', 'cluster.log')
Beispiel #11
0
def readConf(fileName):

    if not Basic.checkFileExists(fileName):
        return None

    cDict = {}
    parser = SafeConfigParser()
    parser.read(fileName)

    isClient = Globals.isClient

    if not isClient:
        Logfile.setVisible(False)
        Logfile.add(' ', fileName, ': ')

    Logfile.setErrorLog(True)

    for section_name in parser.sections():
        for name, value in parser.items(section_name):
            cDict[name] = value

            if not isClient:
                if name != 'mail' and name != 'pwd':
                    Logfile.add(name + ' = ' + value)

    if not isClient:
        Logfile.add(' ')
        Logfile.setVisible(True)

    Logfile.setErrorLog(False)
    return cDict
Beispiel #12
0
def startsemblance(nostat,
                   nsamp,
                   i,
                   nstep,
                   dimX,
                   dimY,
                   mint,
                   new_freq,
                   minSampleCount,
                   isParent=True):

    backSemb = []

    if isParent:
        backSemb = execsemblance(nostat, nsamp, i, nstep, dimX, dimY, mint,
                                 new_freq, minSampleCount)

    else:
        trace = Basic.readMatrix(trace_txt, nostat, minSampleCount, '%e')
        traveltime = Basic.readMatrix(travel_txt, nostat, dimX * dimY, '%e')
        latv = Basic.readVector(latv_txt, '%e')
        lonv = Basic.readVector(lonv_txt, '%e')

    for j in range(dimX * dimY):
        semb = 0
        nomin = 0
        denom = 0

        for l in range(int(nsamp)):
            sum = 0

            for k in range(nostat):
                relstart_samples = int((traveltime[k][j] - mint) * new_freq +
                                       0.5) + i * nstep

                val = trace[k][relstart_samples + l]
                sum += val
                denom += (val * val)

            nomin += sum * sum
            semb = nomin / (float(nostat) * denom)

        backSemb.append(semb)

    return backSemb
Beispiel #13
0
def start(config):

    intern = Intern()

    if sys.argv[1] == 'cluster':
        intern.checkProgramParameter(3, 4)

        workDir = [Globals.EventDir(), 'tmp2', 'cluster']
        workDir = ['cluster']
        cmd = "palantiri_cluster" + ' -f ' + Globals.EventDir()

    else:
        return False

    Basic.changeDirectory(workDir)

    os.system(cmd)
    return True
Beispiel #14
0
def deserializeSembMaxFile(numproc, flag_rpe=False):

    L = []

    for i in range(numproc):
        if flag_rpe is True:
            data = Basic.loadDump('sembMAX-emp' + str(i) + '.pkl')
        else:
            data = Basic.loadDump('sembMAX-' + str(i) + '.pkl')
        if data is not None:
            L.append(data)

    sembMax = OrderedDict()

    for i in L:
        for j in i.keys():
            sembMax[j] = i[j]

    return sembMax
Beispiel #15
0
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
Beispiel #16
0
def deserializeSembDict(numproc, flag_rpe=False):

    L = []

    for i in range(numproc):
        if flag_rpe is True:
            data = Basic.loadDump('sembDict-emp' + str(i) + '.pkl')
        else:
            data = Basic.loadDump('sembDict-' + str(i) + '.pkl')
        if data is not None:
            L.append(data)

    sembDict = OrderedDict()

    for i in L:
        if sys.version_info.major >= 3:
            for j in sorted(i.keys()):
                sembDict[j] = i[j]
        else:
            for j in i.keys():
                sembDict[j] = i[j]

    return sembDict
Beispiel #17
0
def TempFileName(name):

    assert ProtFileDir != None
    Basic.createDirectory(ProtFileDir)
    return os.path.join(ProtFileDir,name)
Beispiel #18
0
def baseLogFileName(postfix):

    s = sys.argv[0]

    return Basic.baseFileName(s) + postfix + '.log'
Beispiel #19
0
def calcTTTAdvTauP(Config,
                   station,
                   Origin,
                   flag,
                   Xcorrshift=None,
                   Refshift=None,
                   flag_rpe=False):

    cfg = ConfigObj(dict=Config)
    if flag_rpe is False:
        dimX = cfg.Int('dimx')
        dimY = cfg.Int('dimy')
    else:
        dimX = cfg.Int('dimx_emp')
        dimY = cfg.Int('dimy_emp')
    gridspacing = cfg.config_geometry.gridspacing
    print('done this')
    o_lat = float(Origin['lat'])
    o_lon = float(Origin['lon'])
    o_depth = float(Origin['depth'])

    oLator = o_lat + dimX / 2
    oLonor = o_lon + dimY / 2
    oLatul = 0
    oLonul = 0

    TTTGridMap = {}
    LMINMAX = []
    GridArray = {}
    locStation = Location(station.lat, station.lon)

    sdelta = loc2degrees(Location(o_lat, o_lon), locStation)
    Logfile.add('TTT PROCESS %d STATION: %s --> DELTA: %f' %
                (flag, station.getName(), sdelta))

    inputpath = str(flag) + '-' + station.getName() + ".input"
    outputpath = str(flag) + '-' + station.getName() + ".output"
    errorpath = str(flag) + '-' + station.getName() + '.error'

    fobjinput = open(inputpath, 'w')

    fobjinput.write('s\n')
    fobjinput.write(('%s %s\n') % (station.lat, station.lon))
    fobjinput.write('h\n')
    fobjinput.write(('%s\n') % (o_depth))

    for i in xrange(dimX):
        oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

        for j in xrange(dimY):
            oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing

            fobjinput.write('e\n')
            fobjinput.write(('%s %s\n') % (oLatul, oLonul))
    # endfor

    fobjinput.close()

    cmd = ('taup_time -ph P -mod ak135 -time -o %s < %s > %s') % (
        outputpath, inputpath, errorpath)
    p = subprocess.Popen(cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    p.wait()

    L = []
    output = open(outputpath, 'r')
    'OUTPUT: ', outputpath

    for k in output:
        k = k.split()

        if len(k) == 1:
            tt = k[0].replace('\n', '')
            tt = float(tt) - float(Xcorrshift[station.getName()].shift)
            L.append(tt)

    output.close()

    z = 0

    for i in xrange(dimX):
        oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

        if z == 0 and i == 0:
            Latul = oLatul
        o = 0

        for j in xrange(dimY):
            oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing

            if o == 0 and j == 0:
                Lonul = oLonul

            de = loc2degrees(Location(oLatul, oLonul), locStation)
            time = L[i * dimX + j]

            GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, time, de)
            LMINMAX.append(time)

    mint = float(min(LMINMAX))
    maxt = float(max(LMINMAX))
    k = MinTMaxT(mint, maxt)

    TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul,
                                            oLator, oLonor, GridArray)

    #tttname = str(flag)+'-ttt.pkl'
    #Basic.dumpToFile(tttname, TTTGridMap)
    #Basic.dumpToFile('minmax-'+str(flag)+'.pkl', k)
    if flag_rpe is True:
        Basic.dumpToFile(str(flag) + '-ttt_emp.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-emp' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-emp' + str(flag) + '.pkl', station)
    else:
        Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-' + str(flag) + '.pkl', station)
    try:
        os.remove(inputpath)
        os.remove(outputpath)
        os.remove(errorpath)

    except:
        Logfile.exception('cannot delete files')
Beispiel #20
0
def calcTTTAdv_cube(Config,
                    station,
                    Origin,
                    flag,
                    arrayname,
                    Xcorrshift,
                    Refshift,
                    phase,
                    flag_rpe=False):

    cfg = ConfigObj(dict=Config)
    if flag_rpe is True:
        dimX = cfg.Int('dimx_emp')
        dimY = cfg.Int('dimy_emp')
        dimZ = cfg.Int('dimz_emp')
    else:
        dimX = cfg.Int('dimx')
        dimY = cfg.Int('dimy')
        dimZ = cfg.Int('dimz')

    orig_depth = float(Origin['depth'])

    start, stop, step = cfg.String('depths').split(',')
    start = orig_depth + float(start)
    stop = orig_depth + float(stop)
    depths = np.linspace(start, stop, num=dimZ)

    gridspacing = cfg.config_geometry.gridspacing
    traveltime_model = cfg_yaml.config.traveltime_model

    o_lat = float(Origin['lat'])
    o_lon = float(Origin['lon'])

    oLator = o_lat + dimX / 2
    oLonor = o_lon + dimY / 2
    oLatul = 0
    oLonul = 0
    o_dip = 80.
    plane = False

    TTTGridMap = {}
    LMINMAX = []
    GridArray = {}
    locStation = Location(station.lat, station.lon)
    sdelta = loc2degrees(Location(o_lat, o_lon), locStation)
    Phase = cake.PhaseDef(phase)
    path = palantiri.__path__
    model = cake.load_model(path[0] + '/data/' + traveltime_model)
    for depth in depths:
        o_depth = depth
        for i in xrange(dimX):
            oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

            if i == 0:
                Latul = oLatul
            o = 0
            for j in xrange(dimY):
                oLonul = o_lon - (
                    (dimY - 1) / 2) * gridspacing + j * gridspacing

                if o == 0 and j == 0:
                    Lonul = oLonul
                de = loc2degrees(Location(oLatul, oLonul), locStation)
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=o_depth * km)
                try:
                    ttime = arrivals[0].t
                except Exception:
                    try:
                        arrivals = model.arrivals([de, de],
                                                  phases=Phase,
                                                  zstart=o_depth * km,
                                                  zstop=o_depth * km,
                                                  refine=True)
                        ttime = arrivals[0].t
                    except Exception:
                        arrivals = model.arrivals([de, de],
                                                  phases=Phase,
                                                  zstart=o_depth * km - 2.5,
                                                  zstop=o_depth * km + 2.5,
                                                  refine=True)
                        ttime = arrivals[0].t

                GridArray[(i, j, depth)] = GridElem(oLatul, oLonul, o_depth,
                                                    ttime, de)
                LMINMAX.append(ttime)

                if ttime == 0:
                    raise Exception("\033[31mILLEGAL: phase definition\033[0m")

    mint = min(LMINMAX)
    maxt = max(LMINMAX)
    TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul,
                                            oLator, oLonor, GridArray)
    k = MinTMaxT(mint, maxt)

    if flag_rpe is True:
        Basic.dumpToFile(str(flag) + '-ttt_emp.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-emp' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-emp' + str(flag) + '.pkl', station)
    else:
        Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-' + str(flag) + '.pkl', station)
Beispiel #21
0
def calcTTTAdv(cfg,
               station,
               Origin,
               flag,
               arrayname,
               Xcorrshift,
               Refshift,
               phase,
               flag_rpe=False):

    if flag_rpe is True:
        dimX = cfg.config_geometry.dimx_emp
        dimY = cfg.config_geometry.dimy_emp
    else:
        dimX = cfg.config_geometry.dimx
        dimY = cfg.config_geometry.dimy

    gridspacing = cfg.config_geometry.gridspacing
    o_lat = float(Origin['lat'])
    o_lon = float(Origin['lon'])
    o_depth = float(Origin['depth'])

    oLator = o_lat + dimX / 2
    oLonor = o_lon + dimY / 2
    oLatul = 0
    oLonul = 0
    o_dip = 80.
    plane = False

    TTTGridMap = {}
    LMINMAX = []
    GridArray = {}
    locStation = Location(station.lat, station.lon)
    sdelta = loc2degrees(Location(o_lat, o_lon), locStation)
    Phase = cake.PhaseDef(phase)
    path = palantiri.__path__
    traveltime_model = cfg.config.traveltime_model
    model = cake.load_model(path[0] + '/data/' + traveltime_model)
    z = 0
    if plane is True:
        depth = np.linspace(0., 40., num=dimY)
        for i in xrange(70):
            oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing
            if z == 0 and i == 0:
                Latul = oLatul
            o = 0
            start_time = time.clock()

            for j in xrange(40):
                oLonul = o_lon - (
                    (dimY - 1) /
                    2) * gridspacing + j * gridspacing / np.cos(o_dip)
                if o == 0 and j == 0:
                    Lonul = oLonul
                de = loc2degrees(Location(oLatul, oLonul), locStation)
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=depth[j] * km,
                                          zstop=0.)
            try:
                ttime = arrivals[0].t
            except Exception:
                try:
                    arrivals = model.arrivals([de, de],
                                              phases=Phase,
                                              zstart=depth[j] * km - 2.5,
                                              zstop=depth[j] * km + 2.5,
                                              refine=True)
                    ttime = arrivals[0].t
                except Exception:
                    tt = obs_TravelTimes(de, o_depth)
                    for k in tt:
                        if k['phase_name'] == 'P' or k['phase_name'] == (
                                '%sdiff') % (Config[phasename]):
                            ttime = k['time']
                        print("Something wrong with phase arrival, too large\
                             distances choosen?")

                GridArray[(i, j)] = GridElem(oLatul, oLonul, depth[j], ttime,
                                             de)
                LMINMAX.append(ttime)

                if ttime == 0:
                    raise Exception("\033[31mILLEGAL: phase definition\033[0m")
    else:
        for i in xrange(dimX):
            oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

            if z == 0 and i == 0:
                Latul = oLatul
            o = 0
            for j in xrange(dimY):
                oLonul = o_lon - (
                    (dimY - 1) / 2) * gridspacing + j * gridspacing

                if o == 0 and j == 0:
                    Lonul = oLonul
                de = loc2degrees(Location(oLatul, oLonul), locStation)
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=o_depth * km)
                try:
                    ttime = arrivals[0].t
                except:
                    try:
                        arrivals = model.arrivals([de, de],
                                                  phases=Phase,
                                                  zstart=o_depth * km,
                                                  zstop=o_depth * km,
                                                  refine=True)
                        ttime = arrivals[0].t
                    except:
                        arrivals = model.arrivals([de, de],
                                                  phases=Phase,
                                                  zstart=o_depth * km - 2.5,
                                                  zstop=o_depth * km + 2.5,
                                                  refine=True)
                        ttime = arrivals[0].t

                GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime,
                                             de)
                LMINMAX.append(ttime)
                if ttime == 0:
                    raise Exception("\033[31mILLEGAL: phase definition\033[0m")

    mint = min(LMINMAX)
    maxt = max(LMINMAX)
    TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul,
                                            oLator, oLonor, GridArray)
    k = MinTMaxT(mint, maxt)

    if flag_rpe is True:
        Basic.dumpToFile(str(flag) + '-ttt_emp.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-emp' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-emp' + str(flag) + '.pkl', station)
    else:
        Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-' + str(flag) + '.pkl', station)
Beispiel #22
0
    def _checkFloatNumber(self, key, s):

        if not Basic.isNumber(s):
            self._error(key, 'Key is not a number')
        return float(s)