Exemplo n.º 1
0
def update_ifdb(ifb):
    ''' Updates ifdb files, Here we just write out the files for the
    previous ndays days'''

    files_out = []
    #Get days for each ifb entry
    ifdays = fdb.fdb_list_fileid(ifb)
    # 'FDByyyymmddhhmmss' extract subscript range 3 to 11 to get the date
    # 'IFDByyyymmddhhmmss' extract subscript range 4 to 12 to get the date
    for j in range(len(ifdays)):
        y = str.find(ifdays[j], 'DB')
        x0 = y + 2
        x1 = y + 10
        ifdays[j] = ifdays[j][x0:x1]
    #endfor
    #Get the filenames for the files,
    days = fdb.fdb_uniq_day(ifb)
    ndays_out = len(days)
    if ndays_out == 0:
        print 'UPDATE_IFDB Error: No days to process'
        sys.stdout.flush()
    else:
        #need an array of days for extract commands
        idyarr = array(ifdays)
        for j in range(ndays_out):
            #Find files with dates matching each day here, build a
            #list to output
            ifbj = extract(idyarr == days[j], ifb)
            ifbj_out = ifbj.tolist()
            if len(ifbj_out) == 0:
                print 'UPDATE_IFDB Error: No output for: ' + days[j]
                sys.stdout.flush()
            else:
                #filename
                yyyy = days[j]
                yyyy = yyyy[:4]
                filenamej = ifdbdir + '/' + yyyy + '/' + 'IFDB' + days[
                    j] + '.txt'
                #check for yyyy directory
                if os.path.isdir(ifdbdir + yyyy) != True:
                    os.mkdir(ifdbdir + yyyy)
                fdb.pfdb_write(ifbj_out, filenamej)
                print 'Wrote: ' + filenamej
                sys.stdout.flush()
                files_out = files_out + [filenamej]
            #endif
        #endfor
    #endif
    return files_out
Exemplo n.º 2
0
def update_ufdb(ufb, ndays=5, day0=None):
    '''Updates ufdb files, need to read the last ndays files, check to
    see if you're overwriting an entry, if not then append the new
    scan to the list and output. If so, replace the old entry, then
    output. Note that ufb is a 1-element list'''

    if len(ufb) == 0:
        print 'UPDATE_UFDB ERROR: No UFB input'
        sys.stdout.flush()

    files_out = []
    #get filenames
    ufdb_files = udb_init_fdbfiles(ndays=ndays, fdbtype='UFDB', day0=day0)

    ufb_in = []
    if len(ufdb_files) == 0:
        #maybe there are none
        print 'UPDATE_UFDB: No UFDB files in range:'
        sys.stdout.flush()
    else:
        for j in range(len(ufdb_files)):
            ufbj = fdb.pfdb_read(ufdb_files[j])
            if len(ufbj) > 0:
                ufb_in = ufb_in + ufbj
            #endif
        #endfor
    #endif
    # If there is no ufb_in, then it's easy
    if len(ufb_in) == 0:
        ufb_out = ufb
    else:
        # Check to see if there is an entry for the current ufb
        uflist_in = fdb.fdb_list_fileid(ufb_in)
        ufarray = array(uflist_in)
        # return the subscript
        ss_ufb_in = extract(ufarray == ufb[0].fileid,
                            array(range(len(ufb_in))))
        #I think ss_ufb_in should be a scalar..
        if len(ss_ufb_in) == 0:
            ufb_out = ufb_in + ufb
        else:
            # Get all of the entries that don't have this fileid
            ss_ufb_not = extract(ufarray != ufb[0].fileid,
                                 array(range(len(ufb_in))))
            if len(ss_ufb_not) == 0:
                ufb_out = ufb
            else:
                ufb_out = []
                for j in range(len(ss_ufb_not)):
                    ufb_out = ufb_out + [ufb_in[ss_ufb_not[j]]]
                #endfor
                ufb_out = ufb_out + ufb
            #endif
        #endif
    #endif
    #Here, now output the ufb_out entries
    files_out = []
    #Get days for each ufb entry
    ufdays = fdb.fdb_list_fileid(ufb_out)
    for j in range(len(ufdays)):
        y = str.find(ufdays[j], 'DB')
        x0 = y + 2
        x1 = y + 10
        ufdays[j] = ufdays[j][x0:x1]
    #endfor

    #Get the filenames for the files,
    days_out = fdb.fdb_uniq_day(ufb_out)
    #Adjustment due to lack
    ndays_out = len(days_out)
    if ndays_out == 0:
        print 'UPDATE_UFDB Error: No days to process'
        sys.stdout.flush()
    else:
        #need an array of days for extract commands
        udyarr = array(ufdays)
        for j in range(ndays_out):
            #Find files with dates matching each day here, build a
            #list to output
            ufbj = extract(udyarr == days_out[j], ufb_out)
            ufbj_out = ufbj.tolist()
            if len(ufbj_out) == 0:
                print 'UPDATE_UFDB Error: No output for: ' + days[j]
                sys.stdout.flush()
            else:
                #filename
                yyyy = days_out[j]
                yyyy = yyyy[:4]
                filenamej = ufdbdir + '/' + yyyy + '/' + 'UFDB' + days_out[
                    j] + '.txt'
                #check for yyyy
                if os.path.isdir(ufdbdir + yyyy) != True:
                    os.mkdir(ufdbdir + yyyy)
                fdb.pfdb_write(ufbj_out, filenamej)
                print 'Wrote: ' + filenamej
                sys.stdout.flush()
                files_out = files_out + [filenamej]
            #endif
        #endfor
    #endif

    return files_out