Ejemplo n.º 1
0
def download_imgs(imgs_to_download, wikiroot, imgdir):
    for imgfile in sorted(imgs_to_download.keys()):
        ivlad.msg('Downloading: ' + imgfile)
        img_url = wikiroot + img_dir_basename + '/' + \
                  imgs_to_download[imgfile] + '/' + imgfile
        img_file = os.path.join(imgdir, imgfile)
        ivlad.exe('curl ' + img_url + '>' + img_file)
Ejemplo n.º 2
0
def download_imgs(imgs_to_download, wikiroot, imgdir):
    for imgfile in sorted(imgs_to_download.keys()):
        ivlad.msg('Downloading: ' + imgfile)
        img_url = wikiroot + img_dir_basename + '/' + \
                  imgs_to_download[imgfile] + '/' + imgfile
        img_file = os.path.join(imgdir,imgfile)
        ivlad.exe('curl ' + img_url + '>' + img_file)
Ejemplo n.º 3
0
def get_wiki_image_dict(hcp, verb):
    'Returns a dictionary with image_name:path/inside/img/url/dir'

    ivlad.msg('Downloading and parsing image list...', verb)

    pg = 'Special:ImageList?limit=500&ilsearch=&title=Special%3AImageList'
    just_before_imglist = '<th>Description</th>\n</tr></thead><tbody>'
    just_after_imglist = '''</tr>
</tbody></table>
<br />
<table class="imagelist_nav TablePager_nav" align="center" cellpadding="3">'''
    just_after_imglist += '<tr>'
    img_table = read_page_section(hcp, pg, just_before_imglist,
                                  just_after_imglist)
    list1 = img_table.split('<tr>\n<td class="TablePager_col_img_timestamp">')
    imgdict = {}
    p1 = '<td class="TablePager_col_img_name"><a href="/%s/Image:' % \
        hcp['wiki_basenm']
    p2 = '">file</a>)</td>'
    p3 = '(<a href="/%s/%s/' % (hcp['wiki_basenm'], hcp['imgdir_basenm'])

    for item in list1:
        s = after(before(after(item, p1), p2), p3)
        if s != '':
            (filepath, filename) = os.path.split(s)
            imgdict[filename] = filepath

    ivlad.msg('...done', verb)
    return imgdict
Ejemplo n.º 4
0
def inspect_docs(hcp, verb):
    'generates with docs to download (url:target)'
    ivlad.msg('Downloading and parsing document list...', verb)
    docs_to_download = {}
    inspect_doc_dir(hcp['docdir_url'], hcp['docdir_local'], docs_to_download)
    ivlad.msg('...done', verb)
    return docs_to_download
Ejemplo n.º 5
0
def get_wiki_image_dict(hcp, verb):
    'Returns a dictionary with image_name:path/inside/img/url/dir'

    ivlad.msg('Downloading and parsing image list...', verb)

    pg = 'Special:ImageList?limit=500&ilsearch=&title=Special%3AImageList'
    just_before_imglist = '<th>Description</th>\n</tr></thead><tbody>'
    just_after_imglist = '''</tr>
</tbody></table>
<br />
<table class="imagelist_nav TablePager_nav" align="center" cellpadding="3">'''
    just_after_imglist += '<tr>'
    img_table = read_page_section(hcp, 
                                  pg, 
                                  just_before_imglist, 
                                  just_after_imglist)
    list1 = img_table.split('<tr>\n<td class="TablePager_col_img_timestamp">')
    imgdict = {}
    p1 = '<td class="TablePager_col_img_name"><a href="/%s/Image:' % \
        hcp['wiki_basenm']
    p2 = '">file</a>)</td>'
    p3 = '(<a href="/%s/%s/' % (hcp['wiki_basenm'],hcp['imgdir_basenm'])

    for item in list1:
	    s = after(before(after(item,p1),p2),p3)
	    if s != '':
	        (filepath, filename) = os.path.split(s)
	        imgdict[filename] = filepath

    ivlad.msg('...done', verb)
    return imgdict
Ejemplo n.º 6
0
def inspect_docs(hcp, verb):
    'generates with docs to download (url:target)'
    ivlad.msg('Downloading and parsing document list...', verb)
    docs_to_download = {}
    inspect_doc_dir(hcp['docdir_url'], hcp['docdir_local'], docs_to_download)
    ivlad.msg('...done', verb)
    return docs_to_download
Ejemplo n.º 7
0
def main(par):

    verb = par.bool('verb', False)
    dryrun = par.bool('dryrun', False)

    # sfbyte arguments:
    bytepars = {}
    for key in ['gainpanel', 'pclip', 'clip']:
        bytepars[key] = par.string(key, None)

    # sfgrey3 arguments:
    color = par.string('color', None)

    lbl = int(100000000 * random.random())
    suffix = '.' + str(lbl) + ivlad.ext
    prefix = sys.argv[0].rstrip('.py') + '.'

    tmp_filenm = prefix + 'inp' + suffix
    bar_filenm = prefix + 'bar' + suffix

    # Redirect the input
    if not dryrun:
        tmp_file = open(tmp_filenm, 'w')
        tmp_file.write(sys.stdin.read())
        tmp_file.close()

    sf._set_xmode('l')

    cmd_list = [
        '# Redirect input to ' + tmp_filenm,
        sf.bar(tmp_filenm, bar_filenm)
    ]
    ivlad.exe(cmd_list, verb, dryrun)

    if dryrun:
        cmd = '<' + tmp_filenm + ' sfbyte | sfgrey3 scalebar=y bar='
        cmd += bar_filenm + ' >stdout'
        ivlad.msg(cmd, verb)
    else:
        p1 = subprocess.Popen(['sfbyte'],
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE)
        tmp_file = open(tmp_filenm, 'r')
        p1.stdin.write(tmp_file.read())
        p2 = subprocess.Popen(['sfgrey3', 'scalebar=y', 'bar=' + bar_filenm],
                              stdin=p1.stdout,
                              stdout=subprocess.PIPE)
        sys.stdout.write(p2.stdout.read())
        p1.stdin.close()
        tmp_file.close()
        p2.stdout.close()

    cmd = sf.rm([tmp_filenm, bar_filenm])

    ivlad.exe(cmd, verb, dryrun)

    return ivlad.unix_success
Ejemplo n.º 8
0
def main(par):

    verb   = par.bool('verb'  , False)
    dryrun = par.bool('dryrun', False)
    
    # sfbyte arguments:
    bytepars = {}
    for key in ['gainpanel', 'pclip', 'clip']:
        bytepars[key] = par.string(key, None)

    # sfgrey3 arguments:
    color  = par.string('color', None)
    
    lbl = int(100000000*random.random())
    suffix = '.' + str(lbl) + ivlad.ext
    prefix = sys.argv[0].rstrip('.py') + '.'
    
    tmp_filenm = prefix + 'inp' + suffix
    bar_filenm = prefix + 'bar' + suffix
    
    # Redirect the input
    if not dryrun:
        tmp_file = open(tmp_filenm, 'w')
        tmp_file.write(sys.stdin.read())
        tmp_file.close()
       
    sf._set_xmode('l')
       
    cmd_list = ['# Redirect input to ' + tmp_filenm,
                sf.bar(tmp_filenm, bar_filenm)]
    ivlad.exe(cmd_list, verb, dryrun)
    
    if dryrun:
        cmd = '<' + tmp_filenm + ' sfbyte | sfgrey3 scalebar=y bar='
        cmd+= bar_filenm + ' >stdout'
        ivlad.msg(cmd, verb)
    else:  
        p1 = subprocess.Popen(['sfbyte'],
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE)
        tmp_file = open(tmp_filenm, 'r')
        p1.stdin.write(tmp_file.read())
        p2 = subprocess.Popen(['sfgrey3', 'scalebar=y', 'bar='+bar_filenm],
                              stdin=p1.stdout,
                              stdout=subprocess.PIPE)
        sys.stdout.write(p2.stdout.read())
        p1.stdin.close()
        tmp_file.close()
        p2.stdout.close()
    
    cmd = sf.rm([tmp_filenm, bar_filenm])
    
    ivlad.exe(cmd, verb, dryrun)
    
    return ivlad.unix_success
Ejemplo n.º 9
0
def download_file(url, filename, verb, whatisfile, textmode=False):
    'Downloads a file from the web'
    format = 'w'
    if not textmode:
        format += 'b'
    ivlad.msg('Downloading %s: %s' % (whatisfile, url), verb)
    urlhandle = urlopen(url)
    contents = urlhandle.read()
    outhandle = open(filename, format)
    outhandle.write(contents)
    outhandle.close()
Ejemplo n.º 10
0
def download_file(url, filename, verb, whatisfile, textmode=False):
    'Downloads a file from the web'
    format = 'w'
    if not textmode:
        format += 'b'
    ivlad.msg('Downloading %s: %s' % (whatisfile, url), verb)
    urlhandle = urlopen(url)
    contents  = urlhandle.read()
    outhandle = open(filename, format)
    outhandle.write(contents)
    outhandle.close()
Ejemplo n.º 11
0
def download_pages(hcp, pagelist, wiki_img_repl_dict):
    for page in pagelist:
        ivlad.msg('Downloading and parsing: ' + page)
        page_url    = hcp['wiki_url'] + page + hcp['printable']
        if page == 'Main_Page':
            local_basename = 'index'
        else:
            local_basename = page
        if '/' in local_basename:
            # Temporary fix -- this will break some links...
            local_basename = string.replace(local_basename, '/', '')
        page_html = os.path.join(hcp['wiki_local'], local_basename+hcp['ext'])
        page_handle = urlopen(page_url)
        s = page_handle.read()
        page_handle.close()
        s = filter_page(s,hcp,pagelist,wiki_img_repl_dict)
        f = open(page_html, 'w')
        f.write(s)
        f.close()
Ejemplo n.º 12
0
def download_pages(hcp, pagelist, wiki_img_repl_dict):
    for page in pagelist:
        ivlad.msg('Downloading and parsing: ' + page)
        page_url = hcp['wiki_url'] + page + hcp['printable']
        if page == 'Main_Page':
            local_basename = 'index'
        else:
            local_basename = page
        if '/' in local_basename:
            # Temporary fix -- this will break some links...
            local_basename = string.replace(local_basename, '/', '')
        page_html = os.path.join(hcp['wiki_local'],
                                 local_basename + hcp['ext'])
        page_handle = urlopen(page_url)
        s = page_handle.read()
        page_handle.close()
        s = filter_page(s, hcp, pagelist, wiki_img_repl_dict)
        f = open(page_html, 'w')
        f.write(s)
        f.close()
Ejemplo n.º 13
0
def main(par):

    verb = par.bool('verb',
                    False)  # Display headers and binaries being deleted
    mydir = par.string('dir')  # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively

    # Clean up headers with existing binaries

    valid_files_list = []

    if recursive:
        for root, dirs, files in mydir:
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = [x for x in os.listdir(mydir) if os.path.isfile(x)]
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan=False)

    for f in valid_files_list:
        ivlad.msg(f + ': ' + ivlad.getout('sfin', ['info=n', f]), verb)
        sf.rm(f, verb)

    # Clean up headers with no binaries

    if recursive:
        hdr_str = ivlad.getout('find',
                               [mydir, '-type', 'f', '-name', '"*.rsf"'])
        hdr_list = hdr_str.split('\n')
    else:
        hdr_list = [
            x for x in glob.glob(os.path.join(mydir, '*.rsf'))
            if os.path.isfile(x)
        ]

    for f in hdr_list:
        ivlad.msg(f)
        os.remove(f)

    return ivlad.unix_success
Ejemplo n.º 14
0
def get_page_list(hcp, verb):
    'Returns a list with pages to download from the wiki'

    ivlad.msg('Downloading and parsing page list...', verb)

    just_before_pagelist = '<hr />'
    just_before_pagelist += \
    '<table style="background: inherit;" border="0" width="100%">'
    just_before_pagelist += '<tr><td width="33%">'

    just_after_pagelist = '</table><div class="printfooter">'

    s = read_page_section(hcp, 'Special:AllPages', just_before_pagelist,
                          just_after_pagelist)
    list1 = s.split('<td width="33%">')
    p0 = '<a href="'
    p1 = '" title="'
    p2 = '/%s/' % hcp['wiki_basenm']
    list2 = map(lambda x: after(before(x.lstrip(p0), p1), p2), list1)
    ivlad.msg('...done', verb)
    return sorted(list2)
Ejemplo n.º 15
0
def get_page_list(hcp, verb):
    'Returns a list with pages to download from the wiki'

    ivlad.msg('Downloading and parsing page list...', verb)

    just_before_pagelist = '<hr />'
    just_before_pagelist += \
    '<table style="background: inherit;" border="0" width="100%">'
    just_before_pagelist += '<tr><td width="33%">'

    just_after_pagelist='</table><div class="printfooter">'

    s = read_page_section(hcp, 
                          'Special:AllPages', 
                          just_before_pagelist, 
                          just_after_pagelist)
    list1 = s.split('<td width="33%">')
    p0 = '<a href="'
    p1 = '" title="'
    p2 = '/%s/' % hcp['wiki_basenm']
    list2 = map(lambda x: after(before(x.lstrip(p0),p1),p2), list1)
    ivlad.msg('...done', verb)
    return sorted(list2)
Ejemplo n.º 16
0
def main(par):

    verb = par.bool('verb', False) # Display headers and binaries being deleted
    mydir = par.string('dir')           # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively

    # Clean up headers with existing binaries

    valid_files_list = []

    if recursive:
        for root, dirs, files in mydir:
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = filter(lambda x:os.path.isfile(x),os.listdir(mydir))
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan=False)

    for f in valid_files_list:
        ivlad.msg(f + ': ' + ivlad.getout('sfin',['info=n',f]), verb)
        sf.rm(f, verb)

    # Clean up headers with no binaries

    if recursive:
        hdr_str = ivlad.getout('find',[mydir, '-type', 'f', '-name', '"*.rsf"'])
        hdr_list = hdr_str.split('\n')
    else:
        hdr_list = filter(lambda x:os.path.isfile(x),
                          glob.glob(os.path.join(mydir,'*.rsf')))

    for f in hdr_list:
        ivlad.msg(f)
        os.remove(f)

    return ivlad.unix_success
Ejemplo n.º 17
0
def main(par):

    verb = par.bool('verb', False)      # Display what is wrong with the dataset
    mydir = par.string('dir','.')       # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively
    chk4nan = par.bool('chk4nan',False) # Check for NaN values. Expensive!!

    valid_files_list = []

    if recursive:
        for root, dirs, files in os.walk(mydir):
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = [x for x in os.listdir(mydir) if os.path.isfile(x)]
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan)

    for f in valid_files_list:
        bfile = ivlad.getout('sfin',['info=n',f])
        ivlad.msg(f + ': ' + bfile, verb)
        ivlad.exe('touch -c ' + bfile)

    return ivlad.unix_success
Ejemplo n.º 18
0
def main(par):

    verb = par.bool('verb', False)      # Display what is wrong with the dataset
    mydir = par.string('dir','.')       # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively
    chk4nan = par.bool('chk4nan',False) # Check for NaN values. Expensive!!

    valid_files_list = []

    if recursive:
        for root, dirs, files in os.walk(mydir):
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = filter(lambda x:os.path.isfile(x),os.listdir(mydir))
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan)

    for f in valid_files_list:
        bfile = ivlad.getout('sfin',['info=n',f])
        ivlad.msg(f + ': ' + bfile, verb)
        ivlad.exe('touch -c ' + bfile)

    return ivlad.unix_success
Ejemplo n.º 19
0
Archivo: ooio.py Proyecto: jcapriot/src
 def print_self(self, varname):
     attribs = self.__dict__
     akeys = list(attribs.keys())
     akeys.sort()
     max_nm_len = max(list(map(len, akeys)))
     ivlad.msg(ivlad.hr)
     ivlad.msg(varname + ': instance of ' + self.__class__.__name__)
     indent = '  '
     spc = ' '
     for key in akeys:
         dots_to_print = max_nm_len - len(key) + 2
         line_to_print = indent + key + spc + dots_to_print * '.' + spc
         if key in self.bny_attribs:
             line_to_print += '<' + str(len(attribs[key])) + \
                             ' binary bytes, not printed>'
         else:
             val = attribs[key]
             if type(val) == list and len(val) == 1:
                 val = val[0]  # Better readability without brackets
         line_to_print += str(val)
         ivlad.msg(line_to_print)
Ejemplo n.º 20
0
 def print_self(self, varname):
     attribs = self.__dict__
     akeys = attribs.keys()
     akeys.sort()
     max_nm_len = max(map(len,akeys))
     ivlad.msg(ivlad.hr)
     ivlad.msg(varname + ': instance of ' + self.__class__.__name__)
     indent = '  '
     spc = ' '
     for key in akeys:
         dots_to_print = max_nm_len - len(key) + 2
         line_to_print = indent + key + spc + dots_to_print * '.' + spc
         if key in self.bny_attribs:
             line_to_print += '<' + str(len(attribs[key])) + \
     	                    ' binary bytes, not printed>'
 	    else:
             val = attribs[key]
             if type(val) == list and len(val) == 1:
     	        val=val[0] # Better readability without brackets
         line_to_print += str(val)
         ivlad.msg(line_to_print)
Ejemplo n.º 21
0
def main(par):

    inp = par.string('inp') # input file
    out = par.string('out') # output file

    verb = par.bool('verb', False) # if y, print system commands, outputs

    ivlad.chk_file_dims(inp, 2)

    n1 = ivlad.getout('sfget',['parform=n','n1'], inp, verb)
    n2 = ivlad.getout('sfget',['parform=n','n2'], inp, verb)
    n1 = int(n1)
    n2 = int(n2)

    h = par.int('h', 768)  # output height
    w = par.int('w', 1024) # output width
    has_h = par.string('h')
    has_w = par.string('w')

    if has_h and has_w: # both w and h were read. Check for sanity:
        ivlad.chk_param_limit(w, 'w')
        ivlad.chk_param_limit(h, 'h')
        if (h,w) == (n1,n2):
            ivlad.msg('Change h or w if you want out != inp')
            sf.cp(inp, out, verb)
            return ivlad.unix_success

    # Transform h and w to pixels, if they are not
    # No default value for par.string -- Quirk of rsf, replicated in rsfbak
    unit = par.string('unit', 'px') # unit of h and w. Can be: px, mm, cm, in
    ivlad.chk_par_in_list(unit,['mm','cm','in','px'])

    if unit != 'px':
        ppi = par.int('ppi') # outp. resolution (px/in). Necessary when unit!=px
        ivlad.chk_param_limit(ppi, 'ppi')
        # Transform w and h to px
        if unit == 'in':
            scale = 1
        elif unit == 'mm':
            scale = 254
        elif unit == 'cm':
            scale = 25.4
        w *= ppi / float(scale)
        h *= ppi / float(scale)
        # Don't worry, we'll convert to int after prar block
        del scale

    # Now h and w are in pixels

    # If prar=y, then h and/or w define a bounding box.
    # Find the dimensions of the image inside this box
    prar = par.bool('prar', True) # if y, PReserve Aspect Ratio of input
    if prar: # preserve aspect ratio
        if has_h and not has_w:
            w = n2 * float(h) / n1
        elif has_w and not has_h:
            h = n1 * float(w) / n2
        else: # Full bounding box specified
            hscale = float(h) / n1
            wscale = float(w) / n2
            if hscale < wscale:
                w = n2 * hscale
            else:
                h = n1 * wscale

    h = int(h)
    w = int(w)

    assert h > 1
    assert w > 1

    h = ivlad.valswitch(h, n1, None)
    w = ivlad.valswitch(w, n2, None)
    tmp = tempfile.mktemp(dir=rsf.path.datapath())

    # Interpolation and, if needed, bandpass 
    if h != None:
        d1 = ivlad.getout('sfget', ['parform=n','d1'], inp, verb)
        d1 = float(d1) * (n1-1)/float(h-1)
        if h < n1:
            ready_for_remap_1 = tmp + '1'
            sf.bandpass(inp, ready_for_remap_1, fhi=0.5/d1, verb=verb)
            rem2del_junk1 = True
        else:
            ready_for_remap_1 = inp
            rem2del_junk1 = False
        if w:
            out_remap1 = tmp + '2'
            rem2del_junk2 = True
        else:
            out_remap1 = out
            rem2del_junk2 = False
        sf.remap1(ready_for_remap_1, out_remap1, n1=h, d1=d1, verb=verb)
        if rem2del_junk1:
            sf.rm(ready_for_remap_1, verb)
    else: # no action on axis 1
        out_remap1 = inp
        rem2del_junk2 = False

    if w != None:
        d2 = ivlad.getout('sfget', ['parform=n','d2'], inp, verb)
        d2 = float(d2) * (n2-1)/float(w-1)
        out_transp1 = tmp + '3'
        sf.transp(out_remap1, out_transp1, verb=verb)
        if rem2del_junk2:
            sf.rm(out_remap1, verb)
        if w < n2:
            ready_for_remap_2 = tmp + '4'
            sf.bandpass(out_transp1, ready_for_remap_2, fhi=0.5/d2, verb=verb)
            rem2del_junk4 = True
        else:
            ready_for_remap_2 = out_transp1
            rem2del_junk4 = False
        ready_for_transp2 = tmp + '5'
        sf.remap1(ready_for_remap_2,ready_for_transp2,n1=w,d1=d2,verb=verb)
        sf.rm(out_transp1, verb)
        if rem2del_junk4:
            sf.rm(ready_for_remap_2, verb)
        sf.transp(ready_for_transp2, out, verb=verb)
        sf.rm(ready_for_transp2, verb)

    return ivlad.unix_success
Ejemplo n.º 22
0
def main(par):

    # Input parameters

    delim = par.string('delimiter',',') # Separator between values in input file
    numtype = par.string('dtype', 'float') # Input type    

    ivlad.chk_par_in_list(numtype, ooio.dtype_avl)

    # Process parameters

    verb = par.bool('verb', False) # Whether to echo n1, n2, infill/truncation
    debug = par.bool('debug', False) # Extra verbosity for debugging
    truncate = par.bool('trunc', False) 
    # Truncate or add zeros if nr elems in rows differs

    header = par.bool('header',False) # If the first line is a header

    # Output parameters
    
    o = [
    par.float('o1', 0.), # Origin of axis 1 in output (rows in input)
    par.float('o2', 0.)] # Origin of axis 2 in output (columns in input)

    d = [
    par.float('d1', 1.), # Axis 1 sampling
    par.float('d2', 1.)] # Axis 2 sampling
    
    unit = [
    par.string('unit1', 'unknown'),
    par.string('unit2', 'unknown')]

    lbl = [
    par.string('label1', 'unknown'),
    par.string('label2', 'unknown')]

    ##### End reading parameters #####

    stdin = csv.reader(sys.stdin, delimiter=delim)
 
    # Copy stdin so we can go back through it
    lines = []
    i = 0
    nr_cols_cst = True # Whether the nr of values in rows is constant

    if header: # the first line contains header keys
        line = stdin.next()
        k = 0
        for name in line:
            k += 1
            print 'key%d=%s' % (k,name)
        
    # Find max nr of elements in a row
    for line in stdin:
        if line == []: # throw away blank lines
            continue
        curline = [float(x) for x in map(lambda x: x or '0', line)]
        if numtype == 'int':
            curline = [int(x) for x in curline]
        lines.append(curline)
        llen = len(curline)
        i+=1 # We have successfully read line i
        if i==1:
            n2 = llen
        elif llen != n2:
            nr_cols_cst = False
            if (llen < n2 and truncate) or (llen > n2 and not truncate):
                n2 = llen

    n1 = len(lines)

    if not nr_cols_cst: # Truncate or append as necessary
        for i in range(n1):
            line = lines[i]
            lines[i] = ivlad.trunc_or_append(n2, line, 0, verb)

    # Avoiding to add a second dimension of length 1
    if n1 == 1:
        ndim_out = 1
        n = [n2]
        o = [o[0]]
        d = [d[0]]
        unit = [unit[0]]
        lbl = [lbl[0]]
    else:
        ndim_out = 2
        n = [n2, n1]

    out = ooio.RSFfile(ooio.stdout,par,ndim=ndim_out,intent='out',dtype=numtype) 
    out.set_hdr_info(n, o, d, unit, lbl)
    
    if debug:
        out.print_self('out')
        out.hdr.print_self('out.hdr')
        out.dat.print_self('out.dat')
        ivlad.msg(ivlad.hr)

    for line in lines:
        for val in line:
            out.write(val)

    return ivlad.unix_success
Ejemplo n.º 23
0
def main(par):

    inp = par.string('inp')  # input file
    out = par.string('out')  # output file

    verb = par.bool('verb', False)  # if y, print system commands, outputs

    ivlad.chk_file_dims(inp, 2)

    n1 = ivlad.getout('sfget', ['parform=n', 'n1'], inp, verb)
    n2 = ivlad.getout('sfget', ['parform=n', 'n2'], inp, verb)
    n1 = int(n1)
    n2 = int(n2)

    h = par.int('h', 768)  # output height
    w = par.int('w', 1024)  # output width
    has_h = par.string('h')
    has_w = par.string('w')

    if has_h and has_w:  # both w and h were read. Check for sanity:
        ivlad.chk_param_limit(w, 'w')
        ivlad.chk_param_limit(h, 'h')
        if (h, w) == (n1, n2):
            ivlad.msg('Change h or w if you want out != inp')
            sf.cp(inp, out, verb)
            return ivlad.unix_success

    # Transform h and w to pixels, if they are not
    # No default value for par.string -- Quirk of rsf, replicated in rsfbak
    unit = par.string('unit', 'px')  # unit of h and w. Can be: px, mm, cm, in
    ivlad.chk_par_in_list(unit, ['mm', 'cm', 'in', 'px'])

    if unit != 'px':
        ppi = par.int(
            'ppi')  # outp. resolution (px/in). Necessary when unit!=px
        ivlad.chk_param_limit(ppi, 'ppi')
        # Transform w and h to px
        if unit == 'in':
            scale = 1
        elif unit == 'mm':
            scale = 254
        elif unit == 'cm':
            scale = 25.4
        w *= ppi / float(scale)
        h *= ppi / float(scale)
        # Don't worry, we'll convert to int after prar block
        del scale

    # Now h and w are in pixels

    # If prar=y, then h and/or w define a bounding box.
    # Find the dimensions of the image inside this box
    prar = par.bool('prar', True)  # if y, PReserve Aspect Ratio of input
    if prar:  # preserve aspect ratio
        if has_h and not has_w:
            w = n2 * float(h) / n1
        elif has_w and not has_h:
            h = n1 * float(w) / n2
        else:  # Full bounding box specified
            hscale = float(h) / n1
            wscale = float(w) / n2
            if hscale < wscale:
                w = n2 * hscale
            else:
                h = n1 * wscale

    h = int(h)
    w = int(w)

    assert h > 1
    assert w > 1

    h = ivlad.valswitch(h, n1, None)
    w = ivlad.valswitch(w, n2, None)
    tmp = tempfile.mktemp(dir=rsf.path.datapath())

    # Interpolation and, if needed, bandpass
    if h != None:
        d1 = ivlad.getout('sfget', ['parform=n', 'd1'], inp, verb)
        d1 = float(d1) * (n1 - 1) / float(h - 1)
        if h < n1:
            ready_for_remap_1 = tmp + '1'
            sf.bandpass(inp, ready_for_remap_1, fhi=0.5 / d1, verb=verb)
            rem2del_junk1 = True
        else:
            ready_for_remap_1 = inp
            rem2del_junk1 = False
        if w:
            out_remap1 = tmp + '2'
            rem2del_junk2 = True
        else:
            out_remap1 = out
            rem2del_junk2 = False
        sf.remap1(ready_for_remap_1, out_remap1, n1=h, d1=d1, verb=verb)
        if rem2del_junk1:
            sf.rm(ready_for_remap_1, verb)
    else:  # no action on axis 1
        out_remap1 = inp
        rem2del_junk2 = False

    if w != None:
        d2 = ivlad.getout('sfget', ['parform=n', 'd2'], inp, verb)
        d2 = float(d2) * (n2 - 1) / float(w - 1)
        out_transp1 = tmp + '3'
        sf.transp(out_remap1, out_transp1, verb=verb)
        if rem2del_junk2:
            sf.rm(out_remap1, verb)
        if w < n2:
            ready_for_remap_2 = tmp + '4'
            sf.bandpass(out_transp1,
                        ready_for_remap_2,
                        fhi=0.5 / d2,
                        verb=verb)
            rem2del_junk4 = True
        else:
            ready_for_remap_2 = out_transp1
            rem2del_junk4 = False
        ready_for_transp2 = tmp + '5'
        sf.remap1(ready_for_remap_2, ready_for_transp2, n1=w, d1=d2, verb=verb)
        sf.rm(out_transp1, verb)
        if rem2del_junk4:
            sf.rm(ready_for_remap_2, verb)
        sf.transp(ready_for_transp2, out, verb=verb)
        sf.rm(ready_for_transp2, verb)

    return ivlad.unix_success
Ejemplo n.º 24
0
def main(par):

    # Input parameters

    delim = par.string('delimiter',',') # Separator between values in input file
    numtype = par.string('dtype', 'float') # Input type    

    ivlad.chk_par_in_list(numtype, ooio.dtype_avl)

    # Process parameters

    verb = par.bool('verb', False) # Whether to echo n1, n2, infill/truncation
    debug = par.bool('debug', False) # Extra verbosity for debugging
    truncate = par.bool('trunc', False) 
    # Truncate or add zeros if nr elems in rows differs

    header = par.bool('header',False) # If the first line is a header

    # Output parameters
    
    o = [
    par.float('o1', 0.), # Origin of axis 1 in output (rows in input)
    par.float('o2', 0.)] # Origin of axis 2 in output (columns in input)

    d = [
    par.float('d1', 1.), # Axis 1 sampling
    par.float('d2', 1.)] # Axis 2 sampling
    
    unit = [
    par.string('unit1', 'unknown'),
    par.string('unit2', 'unknown')]

    lbl = [
    par.string('label1', 'unknown'),
    par.string('label2', 'unknown')]

    ##### End reading parameters #####

    stdin = csv.reader(sys.stdin, delimiter=delim)
 
    # Copy stdin so we can go back through it
    lines = []
    i = 0
    nr_cols_cst = True # Whether the nr of values in rows is constant

    if header: # the first line contains header keys
        line = stdin.next()
        k = 0
        for name in line:
            k += 1
            print('key%d=%s' % (k,name))
        
    # Find max nr of elements in a row
    for line in stdin:
        if line == []: # throw away blank lines
            continue
        curline = [float(x) for x in [x or '0' for x in line]]
        if numtype == 'int':
            curline = [int(x) for x in curline]
        lines.append(curline)
        llen = len(curline)
        i+=1 # We have successfully read line i
        if i==1:
            n2 = llen
        elif llen != n2:
            nr_cols_cst = False
            if (llen < n2 and truncate) or (llen > n2 and not truncate):
                n2 = llen

    n1 = len(lines)

    if not nr_cols_cst: # Truncate or append as necessary
        for i in range(n1):
            line = lines[i]
            lines[i] = ivlad.trunc_or_append(n2, line, 0, verb)

    # Avoiding to add a second dimension of length 1
    if n1 == 1:
        ndim_out = 1
        n = [n2]
        o = [o[0]]
        d = [d[0]]
        unit = [unit[0]]
        lbl = [lbl[0]]
    else:
        ndim_out = 2
        n = [n2, n1]

    out = ooio.RSFfile(ooio.stdout,par,ndim=ndim_out,intent='out',dtype=numtype) 
    out.set_hdr_info(n, o, d, unit, lbl)
    
    if debug:
        out.print_self('out')
        out.hdr.print_self('out.hdr')
        out.dat.print_self('out.dat')
        ivlad.msg(ivlad.hr)

    for line in lines:
        for val in line:
            out.write(val)

    return ivlad.unix_success