<commit_before>#!/usr/bin/python
import os

# -- my version of buildbot.slave.commands.Git -- #
from buildbot.slave.commands import SourceBase, ShellCommand, rmdirRecursive
from buildbot.slave.registry import registerSlaveCommand

class BitKeeperSlave(SourceBase):
    """BitKeeper specific VC operation. In addition to the arguments
    handled by SourceBase, this command reads the following keys:

    ['repourl'] (required): the BitKeeper repository string
    """

    header = "bitkeeper operation"

    def setup(self, args):
        SourceBase.setup(self, args)
        self.repourl = args['repourl']
        self.sourcedata = "%s" % (self.repourl)

    def _fullSrcdir(self):
        return os.path.join(self.builder.basedir, self.srcdir)

    def sourcedirIsUpdateable(self):
        # FIXME what the hell is this supposed to do?
        if os.path.exists(os.path.join(self._fullSrcdir(),
                                       ".buildbot-patched")):
            return False
        return os.path.isdir(os.path.join(self._fullSrcdir(), "BitKeeper"))

    def doVCUpdate(self):
        command = ['bk', '-q', 'pull']
        self.sendStatus({"header": "updating from %s\n"
            % (self.repourl)})
        c = ShellCommand(self.builder, command, self._fullSrcdir(),
            sendRC=False, timeout=self.timeout)
        self.command = c
        d = c.start()
        d.addCallback(self._abandonOnFailure)
        #d.addCallback(self._didFetch)
        return d

    def doVCFull(self):
        # Clone the repository into a temporary dir
        # FIXME the temporary dir will not be subject to deletion.
        if self.mode == "export":
            srcdir = self._fullSrcdir()
            tmpdir = os.path.join(self.builder.basedir, 'temp_clone')
            if os.path.exists(tmpdir):
                from shutil import rmtree
                rmtree(tmpdir)
            #rmdirRecursive(tmpdir)
            command = ['bk', 'clone', '-q', self.repourl, tmpdir]
            c = ShellCommand(self.builder, command, tmpdir, sendRC=False,
                timeout=self.timeout)
            self.command = c
            d = c.start()
            def _export(res):
                command = ['bk', 'export', '-Tw', tmpdir,
                    self._fullSrcdir()]
                c = ShellCommand(self.builder, command, tmpdir,
                    sendRC=False, timeout=self.timeout)
                self.command = c
                return c.start()
            d.addCallback(_export)
            return d
        else:
            srcdir = self._fullSrcdir()
            os.mkdir(srcdir)
            command = ['bk', '-q', 'clone', self.repourl, srcdir]
            c = ShellCommand(self.builder, command, srcdir, sendRC=False, timeout=self.timeout)
            self.command = c
            return c.start()

    def parseGotRevision(self):
        if self.mode == "export":
            rev_file = os.path.join(self.builder.basedir, 'temp_clone',
            'ChangeSet')
        else:
            rev_file = os.path.join(self._fullSrcdir(), 'ChangeSet')

        command = ['bk', 'log', '-r+', '-d:MD5KEY:', rev_file]
        c = ShellCommand(self.builder, command, self._fullSrcdir(),
            sendRC=False, keepStdout=True)
        c.usePTY = False
        d = c.start()
        def _parse(res):
            return c.stdout.strip()
        d.addCallback(_parse)
        return d

registerSlaveCommand("bitkeeper", BitKeeperSlave, "2.5")

## -- My version of builbot.step.source.Git --
from buildbot.steps.source import Source, LoggedRemoteCommand
from buildbot.interfaces import BuildSlaveTooOldError

class BitKeeper(Source):
    """Check out a source tree from a bitkeeper repository 'repourl'."""

    name = "bitkeeper"

    def __init__(self, repourl, **kwargs):
        """
        @type  repourl: string
        @param repourl: the URL which points at the bitkeeper repository
        """
        Source.__init__(self, **kwargs)
        self.addFactoryArguments(repourl=repourl)
        self.args.update({'repourl': repourl})

    def computeSourceRevision(self, changes):
        if not changes:
            return None
        return changes[-1].revision

    def startVC(self, branch, revision, patch):
        self.args['branch'] = branch
        self.args['revision'] = revision
        self.args['patch'] = patch
        slavever = self.slaveVersion("bitkeeper")
        if not slavever:
            raise BuildSlaveTooOldError("slave is too old, does not know "
                                        "about bitkeeper")
        cmd = LoggedRemoteCommand("bitkeeper", self.args)
        self.startCommand(cmd)

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
<commit_msg>Remove bk source step<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import csv
import json
import requests
import re

TWSE_SERVER = '220.229.103.179'
TWSE_URL = 'http://' + TWSE_SERVER + '/stock/api/getStockInfo.jsp?ex_ch='
TSE_FILE = 'tse.csv'
OTC_FILE = 'otc.csv'
GOOGLE_URL = 'http://www.google.com/finance/info?q='
INDEX_LIST = [['TPE:TAIEX', 'TAIEX'],
              ['INDEXDJX:.DJI', 'DOW'],
              ['INDEXNASDAQ:.IXIC', 'NASDAQ'],
              ['INDEXNASDAQ:SOX', 'PHLX'],
              ['INDEXDB:DAX', 'DAX'],
              ['INDEXFTSE:UKX', 'FTSE'],
              ['INDEXNIKKEI:NI225', 'N225'],
              ['KRX:KOSPI', 'KOSPI'],
              ['SHA:000001', 'SHCOMP'],
              ['INDEXHANGSENG:HSI', 'HK']]
TW_STOCK_LIST = ['2330', '2317', '3008', '00631L', '00632R'] # you can define the default stock list!!
color_print = False

def usage():
    print 'stockcmd - get stock information for TWSE'
    print ''
    print 'Usage:'
    print '    stockcmd.py [Options] [stock numbers]'
    print ''
    print 'Options:'
    print '    -a: list all stock infomation (include TSE, OTC, other stock)'
    print '    -i: list include TSE index and OTC index'
    print '    -w: list International Stock Indexes'
    print '    -q: list the stocks predefined'
    print '    -c: colorful print result'
    print '    -h: show this page'
    print ''
    print 'Example:'
    print '    stockcmd.py -i'
    print '    stockcmd.py -w'
    print '    stockcmd.py -q'
    print '    stockcmd.py 2330 2317 3008'
    print ''

def search_stock(stock_no, filename):
    ret = False

    f = open(filename,'r')
    for row in csv.reader(f):
        if row[0] == str(stock_no):
            ret = True
            break
    f.close()
    return ret

def remove_special_char(string):
    return re.sub(r'[^\x00-\x7F]','', string)

def world_index():
    url = GOOGLE_URL
    for item in INDEX_LIST:
        url = url + item[0] + ','

    result = requests.get(url)
    json_str = remove_special_char(result.content.replace('// ', '', 1))

    print_result(json_str, 'world')

def print_result(json_str, stock_type):
    red = '\033[1;31;40m'
    green = '\033[1;32;40m'
    yellow = '\033[1;33;40m'
    title_color = ''
    item_color = ''

    if color_print == True:
        title_color = yellow
    else:
        red = ''
        green = ''
        yellow = ''

    # read json data
    json_data = json.loads(json_str)

    if stock_type == 'world':
        print title_color + u'  指數            點數            漲跌          百分比'
        print '-------------------------------------------------------'
        for i in range(0, len(INDEX_LIST), 1):
            j = json_data[i]
            name = INDEX_LIST[i][1]

            ratio = j["cp"]
            if ratio.find('-'):
                ratio = '+' + ratio
                item_color = red
            else:
                item_color = green

            if float(j["c"]) == 0:
                item_color = ''

            print item_color + ' ' + '{0:13s}'.format(name) + '{0:>10s}'.format(j["l"]) + '{0:>15s}'.format(j["c"]) + '{0:>14s}%'.format(ratio)

    elif stock_type == 'tw':
        print title_color + u' 股號     股名       成交價      漲跌      百分比     成交量     資料時間'
        print '--------------------------------------------------------------------------'

        for i in range(0, len(json_data['msgArray']), 1):
            j = json_data['msgArray'][i]
            diff = float(j["z"]) - float(j["y"])

            if diff > 0:
                sign = '+'
                item_color = red
            elif diff == 0:
                sign = ''
                item_color = ''
            else:
                sign = ''
                item_color = green

            change_str = sign + str(diff)
            change_str_p = sign + '{0:.2f}'.format(diff / float(j["y"]) *100)

            stock_no = j["c"]
            name = j["n"]

            # fix too long name.....
            if stock_no == 't00':
                name = u'上市'
            elif stock_no == 'o00':
                name = u'上櫃'

            print item_color + ' ' + '{0:7s}'.format(j["c"]) + ' ' \
                  + name + '\t    {0:>7s}'.format(j["z"]) \
                  + '     {0:11s}'.format(change_str) +  change_str_p + '%'\
                  + '    {0:>7s}'.format(j["v"]) + '     ' + j["t"]
    print ''

def main():
    argv = sys.argv
    count = 0
    add_world = False
    add_twse = False
    add_stock_list = False
    url = TWSE_URL

    if len(argv) == 1:
        # no parameter, show usage()
        usage()
        exit()

    # remote program name
    del argv[0]

    # read parameters
    for x in argv:
        if str(x) == '-c':
            global color_print
            color_print = True
        elif str(x) == '-a':
            add_world = True;
            add_twse = True
            add_stock_list = True
        elif str(x) == '-w':
            add_world = True
        elif str(x) == '-i':
            add_twse = True
        elif str(x) == '-q':
            add_stock_list = True
        elif str(x) == '-h':
            usage()
            exit()

    if add_world:
        world_index()

    if add_twse:
        url = url + 'tse_t00.tw|otc_o00.tw|'
        count += 2

    # add user predefined stock list
    # search_stock() is use to determine stock is in tse or otc
    if add_stock_list:
        for x in TW_STOCK_LIST:
            if search_stock(str(x), OTC_FILE):
                url = url + 'otc_' + str(x) + '.tw|'
            elif search_stock(str(x), TSE_FILE):
                url = url + 'tse_' + str(x) + '.tw|'
            else:
                continue
            count += 1

    # search stock number from input
    for x in argv:
        if search_stock(str(x), OTC_FILE):
            url = url + 'otc_' + str(x) + '.tw|'
        elif search_stock(str(x), TSE_FILE):
            url = url + 'tse_' + str(x) + '.tw|'
        else:
            continue
        count += 1

    if count == 0 and add_world == False and add_twse == False and add_stock_list == False:
        usage()
        exit()

    elif count > 0:
        # access the index first and then send request for stock list
        r = requests.session()
        r.get('http://' + TWSE_SERVER + '/stock/index.jsp', headers = {'Accept-Language':'zh-TW'}, timeout=2)
        result = r.get(url)

        json_str = result.content
        print_result(json_str, 'tw')

main()
<commit_msg>fix alignment of output<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import csv
import json
import requests
import re

TWSE_SERVER = '220.229.103.179'
TWSE_URL = 'http://' + TWSE_SERVER + '/stock/api/getStockInfo.jsp?ex_ch='
TSE_FILE = 'tse.csv'
OTC_FILE = 'otc.csv'
GOOGLE_URL = 'http://www.google.com/finance/info?q='
INDEX_LIST = [['TPE:TAIEX', 'TAIEX'],
              ['INDEXDJX:.DJI', 'DOW'],
              ['INDEXNASDAQ:.IXIC', 'NASDAQ'],
              ['INDEXNASDAQ:SOX', 'PHLX'],
              ['INDEXDB:DAX', 'DAX'],
              ['INDEXFTSE:UKX', 'FTSE'],
              ['INDEXNIKKEI:NI225', 'N225'],
              ['KRX:KOSPI', 'KOSPI'],
              ['SHA:000001', 'SHCOMP'],
              ['INDEXHANGSENG:HSI', 'HK']]
TW_STOCK_LIST = ['2330', '2317', '3008', '00631L', '00632R'] # you can define the default stock list!!
color_print = False

def usage():
    print 'stockcmd - get stock information for TWSE'
    print ''
    print 'Usage:'
    print '    stockcmd.py [Options] [stock numbers]'
    print ''
    print 'Options:'
    print '    -a: list all stock infomation (include TSE, OTC, other stock)'
    print '    -i: list include TSE index and OTC index'
    print '    -w: list International Stock Indexes'
    print '    -q: list the stocks predefined'
    print '    -c: colorful print result'
    print '    -h: show this page'
    print ''
    print 'Example:'
    print '    stockcmd.py -i'
    print '    stockcmd.py -w'
    print '    stockcmd.py -q'
    print '    stockcmd.py 2330 2317 3008'
    print ''

def search_stock(stock_no, filename):
    ret = False

    f = open(filename,'r')
    for row in csv.reader(f):
        if row[0] == str(stock_no):
            ret = True
            break
    f.close()
    return ret

def remove_special_char(string):
    return re.sub(r'[^\x00-\x7F]','', string)

def world_index():
    url = GOOGLE_URL
    for item in INDEX_LIST:
        url = url + item[0] + ','

    result = requests.get(url)
    json_str = remove_special_char(result.content.replace('// ', '', 1))

    print_result(json_str, 'world')

def print_result(json_str, stock_type):
    red = '\033[1;31;40m'
    green = '\033[1;32;40m'
    yellow = '\033[1;33;40m'
    title_color = ''
    item_color = ''

    if color_print == True:
        title_color = yellow
    else:
        red = ''
        green = ''
        yellow = ''

    # read json data
    json_data = json.loads(json_str)

    if stock_type == 'world':
        print title_color + u'  指數            點數            漲跌          百分比'
        print '-------------------------------------------------------'
        for i in range(0, len(INDEX_LIST), 1):
            j = json_data[i]
            name = INDEX_LIST[i][1]

            ratio = j["cp"]
            if ratio.find('-'):
                ratio = '+' + ratio
                item_color = red
            else:
                item_color = green

            if float(j["c"]) == 0:
                item_color = ''

            print item_color + ' ' + '{0:13s}'.format(name) + '{0:>10s}'.format(j["l"]) + '{0:>15s}'.format(j["c"]) + '{0:>14s}%'.format(ratio)

    elif stock_type == 'tw':
        print title_color + u' 股號     股名       成交價      漲跌      百分比     成交量     資料時間'
        print '--------------------------------------------------------------------------'

        for i in range(0, len(json_data['msgArray']), 1):
            j = json_data['msgArray'][i]
            diff = float(j["z"]) - float(j["y"])

            if diff > 0:
                sign = '+'
                item_color = red
            elif diff == 0:
                sign = ' '
                item_color = ''
            else:
                sign = ''
                item_color = green

            change_str = sign + str(diff)
            change_str_p = sign + '{0:.2f}'.format(diff / float(j["y"]) *100)

            stock_no = j["c"]
            name = j["n"]

            # fix too long name.....
            if stock_no == 't00':
                name = u'上市'
            elif stock_no == 'o00':
                name = u'上櫃'

            print item_color + ' ' + '{0:7s}'.format(j["c"]) + ' ' \
                  + name + '\t    {0:>7s}'.format(j["z"]) \
                  + '     {0:11s}'.format(change_str) +  change_str_p + '%'\
                  + '    {0:>7s}'.format(j["v"]) + '     ' + j["t"]
    print ''

def main():
    argv = sys.argv
    count = 0
    add_world = False
    add_twse = False
    add_stock_list = False
    url = TWSE_URL

    if len(argv) == 1:
        # no parameter, show usage()
        usage()
        exit()

    # remote program name
    del argv[0]

    # read parameters
    for x in argv:
        if str(x) == '-c':
            global color_print
            color_print = True
        elif str(x) == '-a':
            add_world = True;
            add_twse = True
            add_stock_list = True
        elif str(x) == '-w':
            add_world = True
        elif str(x) == '-i':
            add_twse = True
        elif str(x) == '-q':
            add_stock_list = True
        elif str(x) == '-h':
            usage()
            exit()

    if add_world:
        world_index()

    if add_twse:
        url = url + 'tse_t00.tw|otc_o00.tw|'
        count += 2

    # add user predefined stock list
    # search_stock() is use to determine stock is in tse or otc
    if add_stock_list:
        for x in TW_STOCK_LIST:
            if search_stock(str(x), OTC_FILE):
                url = url + 'otc_' + str(x) + '.tw|'
            elif search_stock(str(x), TSE_FILE):
                url = url + 'tse_' + str(x) + '.tw|'
            else:
                continue
            count += 1

    # search stock number from input
    for x in argv:
        if search_stock(str(x), OTC_FILE):
            url = url + 'otc_' + str(x) + '.tw|'
        elif search_stock(str(x), TSE_FILE):
            url = url + 'tse_' + str(x) + '.tw|'
        else:
            continue
        count += 1

    if count == 0 and add_world == False and add_twse == False and add_stock_list == False:
        usage()
        exit()

    elif count > 0:
        # access the index first and then send request for stock list
        r = requests.session()
        r.get('http://' + TWSE_SERVER + '/stock/index.jsp', headers = {'Accept-Language':'zh-TW'}, timeout=2)
        result = r.get(url)

        json_str = result.content
        print_result(json_str, 'tw')

main()
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import csv
import json
import requests
import re

TWSE_SERVER = '220.229.103.179'
TWSE_URL = 'http://' + TWSE_SERVER + '/stock/api/getStockInfo.jsp?ex_ch='
TSE_FILE = 'tse.csv'
OTC_FILE = 'otc.csv'
GOOGLE_URL = 'http://www.google.com/finance/info?q='
INDEX_LIST = [['TPE:TAIEX', 'TAIEX'],
              ['INDEXDJX:.DJI', 'DOW'],
              ['INDEXNASDAQ:.IXIC', 'NASDAQ'],
              ['INDEXNASDAQ:SOX', 'PHLX'],
              ['INDEXDB:DAX', 'DAX'],
              ['INDEXFTSE:UKX', 'FTSE'],
              ['INDEXNIKKEI:NI225', 'N225'],
              ['KRX:KOSPI', 'KOSPI'],
              ['SHA:000001', 'SHCOMP'],
              ['INDEXHANGSENG:HSI', 'HK']]
TW_STOCK_LIST = [2330, 2317, 3008] # you can define the default stock list!!
color_print = False

def usage():
    print 'stockcmd - get stock information for TWSE'
    print ''
    print 'Usage:'
    print '    stockcmd.py [Options] [stock numbers]'
    print ''
    print 'Options:'
    print '    -a: list all stock infomation (include TSE, OTC, other stock)'
    print '    -i: list include TSE index and OTC index'
    print '    -w: list International Stock Indexes'
    print '    -q: list the stocks predefined'
    print '    -c: colorful print result'
    print '    -h: show this page'
    print ''
    print 'Example:'
    print '    stockcmd.py -i'
    print '    stockcmd.py -w'
    print '    stockcmd.py -q'
    print '    stockcmd.py 2330 2317 3008'
    print ''

def search_stock(stock_no, filename):
    ret = False

    f = open(filename,'r')
    for row in csv.reader(f):
        if row[0] == str(stock_no):
            ret = True
            break
    f.close()
    return ret

def remove_special_char(string):
    return re.sub(r'[^\x00-\x7F]','', string)

def world_index():
    url = GOOGLE_URL
    for item in INDEX_LIST:
        url = url + item[0] + ','

    result = requests.get(url)
    json_str = remove_special_char(result.content.replace('// ', '', 1))

    print_result(json_str, 'world')

def print_result(json_str, stock_type):
    red = '\033[1;31;40m'
    green = '\033[1;32;40m'
    yellow = '\033[1;33;40m'
    title_color = ''
    item_color = ''

    if color_print == True:
        title_color = yellow
    else:
        red = ''
        green = ''
        yellow = ''

    # read json data
    json_data = json.loads(json_str)

    if stock_type == 'world':
        print title_color + u'  指數            點數            漲跌         百分比'
        for i in range(0, len(INDEX_LIST), 1):
            j = json_data[i]
            name = INDEX_LIST[i][1]

            ratio = j["cp"]
            if ratio.find('-'):
                ratio = '+' + ratio
                item_color = red
            else:
                item_color = green

            if float(j["c"]) == 0:
                item_color = ''

            print item_color + ' ' + '{0:13s}'.format(name) + '{0:>10s}'.format(j["l"]) + '{0:>15s}'.format(j["c"]) + '{0:>14s}%'.format(ratio)

    elif stock_type == 'tw':
        print title_color + u' 股號     股名      成交價       漲跌      百分比     成交量     資料時間'

        for i in range(0, len(json_data['msgArray']), 1):
            j = json_data['msgArray'][i]
            diff = float(j["z"]) - float(j["y"])

            if diff > 0:
                sign = '+'
                item_color = red
            elif diff == 0:
                sign = ''
                item_color = ''
            else:
                sign = ''
                item_color = green

            change_str = sign + str(diff)
            change_str_p = sign + '{0:.2f}'.format(diff / float(j["y"]) *100)

            stock_no = j["c"]
            name = j["n"]

            # fix too long name.....
            if stock_no == 't00':
                name = u'上市'
            elif stock_no == 'o00':
                name = u'上櫃'

            print item_color + ' ' + '{0:8s}'.format(j["c"]) \
                  + name + '\t    {0:7s}'.format(j["z"]) \
                  + '     {0:11s}'.format(change_str) +  change_str_p + '%'\
                  + '    {0:>7s}'.format(j["v"]) + '     ' + j["t"]
    print ''

def main():
    argv = sys.argv
    count = 0
    add_world = False
    add_twse = False
    add_stock_list = False
    url = TWSE_URL

    if len(argv) == 1:
        # no parameter, show usage()
        usage()
        exit()

    # remote program name
    del argv[0]

    # read parameters
    for x in argv:
        if str(x) == '-c':
            global color_print
            color_print = True
        elif str(x) == '-a':
            add_world = True;
            add_twse = True
            add_stock_list = True
        elif str(x) == '-w':
            add_world = True
        elif str(x) == '-i':
            add_twse = True
        elif str(x) == '-q':
            add_stock_list = True
        elif str(x) == '-h':
            usage()
            exit()

    if add_world:
        world_index()

    if add_twse:
        url = url + 'tse_t00.tw|otc_o00.tw|'
        count += 2

    # add user predefined stock list
    # search_stock() is use to determine stock is in tse or otc
    if add_stock_list:
        for x in TW_STOCK_LIST:
            if search_stock(str(x), OTC_FILE):
                url = url + 'otc_' + str(x) + '.tw|'
            elif search_stock(str(x), TSE_FILE):
                url = url + 'tse_' + str(x) + '.tw|'
            else:
                continue
            count += 1

    # search stock number from input
    for x in argv:
        if search_stock(str(x), OTC_FILE):
            url = url + 'otc_' + str(x) + '.tw|'
        elif search_stock(str(x), TSE_FILE):
            url = url + 'tse_' + str(x) + '.tw|'
        else:
            continue
        count += 1

    if count == 0 and add_world == False and add_twse == False and add_stock_list == False:
        usage()
        exit()

    elif count > 0:
        # access the index first and then send request for stock list
        r = requests.session()
        r.get('http://' + TWSE_SERVER + '/stock/index.jsp', headers = {'Accept-Language':'zh-TW'}, timeout=2)
        result = r.get(url)

        json_str = result.content
        print_result(json_str, 'tw')

main()
<commit_msg>adjust print format<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import csv
import json
import requests
import re

TWSE_SERVER = '220.229.103.179'
TWSE_URL = 'http://' + TWSE_SERVER + '/stock/api/getStockInfo.jsp?ex_ch='
TSE_FILE = 'tse.csv'
OTC_FILE = 'otc.csv'
GOOGLE_URL = 'http://www.google.com/finance/info?q='
INDEX_LIST = [['TPE:TAIEX', 'TAIEX'],
              ['INDEXDJX:.DJI', 'DOW'],
              ['INDEXNASDAQ:.IXIC', 'NASDAQ'],
              ['INDEXNASDAQ:SOX', 'PHLX'],
              ['INDEXDB:DAX', 'DAX'],
              ['INDEXFTSE:UKX', 'FTSE'],
              ['INDEXNIKKEI:NI225', 'N225'],
              ['KRX:KOSPI', 'KOSPI'],
              ['SHA:000001', 'SHCOMP'],
              ['INDEXHANGSENG:HSI', 'HK']]
TW_STOCK_LIST = [2330, 2317, 3008] # you can define the default stock list!!
color_print = False

def usage():
    print 'stockcmd - get stock information for TWSE'
    print ''
    print 'Usage:'
    print '    stockcmd.py [Options] [stock numbers]'
    print ''
    print 'Options:'
    print '    -a: list all stock infomation (include TSE, OTC, other stock)'
    print '    -i: list include TSE index and OTC index'
    print '    -w: list International Stock Indexes'
    print '    -q: list the stocks predefined'
    print '    -c: colorful print result'
    print '    -h: show this page'
    print ''
    print 'Example:'
    print '    stockcmd.py -i'
    print '    stockcmd.py -w'
    print '    stockcmd.py -q'
    print '    stockcmd.py 2330 2317 3008'
    print ''

def search_stock(stock_no, filename):
    ret = False

    f = open(filename,'r')
    for row in csv.reader(f):
        if row[0] == str(stock_no):
            ret = True
            break
    f.close()
    return ret

def remove_special_char(string):
    return re.sub(r'[^\x00-\x7F]','', string)

def world_index():
    url = GOOGLE_URL
    for item in INDEX_LIST:
        url = url + item[0] + ','

    result = requests.get(url)
    json_str = remove_special_char(result.content.replace('// ', '', 1))

    print_result(json_str, 'world')

def print_result(json_str, stock_type):
    red = '\033[1;31;40m'
    green = '\033[1;32;40m'
    yellow = '\033[1;33;40m'
    title_color = ''
    item_color = ''

    if color_print == True:
        title_color = yellow
    else:
        red = ''
        green = ''
        yellow = ''

    # read json data
    json_data = json.loads(json_str)

    if stock_type == 'world':
        print title_color + u'  指數            點數            漲跌          百分比'
        print '-------------------------------------------------------'
        for i in range(0, len(INDEX_LIST), 1):
            j = json_data[i]
            name = INDEX_LIST[i][1]

            ratio = j["cp"]
            if ratio.find('-'):
                ratio = '+' + ratio
                item_color = red
            else:
                item_color = green

            if float(j["c"]) == 0:
                item_color = ''

            print item_color + ' ' + '{0:13s}'.format(name) + '{0:>10s}'.format(j["l"]) + '{0:>15s}'.format(j["c"]) + '{0:>14s}%'.format(ratio)

    elif stock_type == 'tw':
        print title_color + u' 股號    股名       成交價       漲跌      百分比     成交量     資料時間'
        print '--------------------------------------------------------------------------'

        for i in range(0, len(json_data['msgArray']), 1):
            j = json_data['msgArray'][i]
            diff = float(j["z"]) - float(j["y"])

            if diff > 0:
                sign = '+'
                item_color = red
            elif diff == 0:
                sign = ''
                item_color = ''
            else:
                sign = ''
                item_color = green

            change_str = sign + str(diff)
            change_str_p = sign + '{0:.2f}'.format(diff / float(j["y"]) *100)

            stock_no = j["c"]
            name = j["n"]

            # fix too long name.....
            if stock_no == 't00':
                name = u'上市'
            elif stock_no == 'o00':
                name = u'上櫃'

            print item_color + ' ' + '{0:7s}'.format(j["c"]) + ' ' \
                  + name + '\t    {0:7s}'.format(j["z"]) \
                  + '     {0:11s}'.format(change_str) +  change_str_p + '%'\
                  + '    {0:>7s}'.format(j["v"]) + '     ' + j["t"]
    print ''

def main():
    argv = sys.argv
    count = 0
    add_world = False
    add_twse = False
    add_stock_list = False
    url = TWSE_URL

    if len(argv) == 1:
        # no parameter, show usage()
        usage()
        exit()

    # remote program name
    del argv[0]

    # read parameters
    for x in argv:
        if str(x) == '-c':
            global color_print
            color_print = True
        elif str(x) == '-a':
            add_world = True;
            add_twse = True
            add_stock_list = True
        elif str(x) == '-w':
            add_world = True
        elif str(x) == '-i':
            add_twse = True
        elif str(x) == '-q':
            add_stock_list = True
        elif str(x) == '-h':
            usage()
            exit()

    if add_world:
        world_index()

    if add_twse:
        url = url + 'tse_t00.tw|otc_o00.tw|'
        count += 2

    # add user predefined stock list
    # search_stock() is use to determine stock is in tse or otc
    if add_stock_list:
        for x in TW_STOCK_LIST:
            if search_stock(str(x), OTC_FILE):
                url = url + 'otc_' + str(x) + '.tw|'
            elif search_stock(str(x), TSE_FILE):
                url = url + 'tse_' + str(x) + '.tw|'
            else:
                continue
            count += 1

    # search stock number from input
    for x in argv:
        if search_stock(str(x), OTC_FILE):
            url = url + 'otc_' + str(x) + '.tw|'
        elif search_stock(str(x), TSE_FILE):
            url = url + 'tse_' + str(x) + '.tw|'
        else:
            continue
        count += 1

    if count == 0 and add_world == False and add_twse == False and add_stock_list == False:
        usage()
        exit()

    elif count > 0:
        # access the index first and then send request for stock list
        r = requests.session()
        r.get('http://' + TWSE_SERVER + '/stock/index.jsp', headers = {'Accept-Language':'zh-TW'}, timeout=2)
        result = r.get(url)

        json_str = result.content
        print_result(json_str, 'tw')

main()
<|endoftext|>


<commit_before>"""
A collection of image utilities using the Python Imaging Library (PIL).

Note that PIL is not a dependency of SciPy and this module is not
available on systems that don't have PIL installed.

"""
from __future__ import division, print_function, absolute_import

# Functions which need the PIL

import numpy
import tempfile

from numpy import amin, amax, ravel, asarray, cast, arange, \
     ones, newaxis, transpose, mgrid, iscomplexobj, sum, zeros, uint8, \
     issubdtype, array

try:
    from PIL import Image, ImageFilter
except ImportError:
    import Image
    import ImageFilter

__all__ = ['fromimage','toimage','imsave','imread','bytescale',
           'imrotate','imresize','imshow','imfilter']

# Returns a byte-scaled image
def bytescale(data, cmin=None, cmax=None, high=255, low=0):
    """
    Byte scales an array (image).

    Byte scaling means converting the input image to uint8 dtype and scaling
    the range to ``(low, high)`` (default 0-255).
    If the input image already has dtype uint8, no scaling is done.

    Parameters
    ----------
    data : ndarray
        PIL image data array.
    cmin : scalar, optional
        Bias scaling of small values. Default is ``data.min()``.
    cmax : scalar, optional
        Bias scaling of large values. Default is ``data.max()``.
    high : scalar, optional
        Scale max value to `high`.  Default is 255.
    low : scalar, optional
        Scale min value to `low`.  Default is 0.

    Returns
    -------
    img_array : uint8 ndarray
        The byte-scaled array.

    Examples
    --------
    >>> img = array([[ 91.06794177,   3.39058326,  84.4221549 ],
                     [ 73.88003259,  80.91433048,   4.88878881],
                     [ 51.53875334,  34.45808177,  27.5873488 ]])
    >>> bytescale(img)
    array([[255,   0, 236],
           [205, 225,   4],
           [140,  90,  70]], dtype=uint8)
    >>> bytescale(img, high=200, low=100)
    array([[200, 100, 192],
           [180, 188, 102],
           [155, 135, 128]], dtype=uint8)
    >>> bytescale(img, cmin=0, cmax=255)
    array([[91,  3, 84],
           [74, 81,  5],
           [52, 34, 28]], dtype=uint8)

    """
    if data.dtype == uint8:
        return data

    if high < low:
        raise ValueError("`high` should be larger than `low`.")

    if cmin is None:
        cmin = data.min()
    if cmax is None:
        cmax = data.max()

    cscale = cmax - cmin
    if cscale < 0:
        raise ValueError("`cmax` should be larger than `cmin`.")
    elif cscale == 0:
        cscale = 1

    scale = float(high - low) / cscale
    bytedata = (data * 1.0 - cmin) * scale + 0.4999
    bytedata[bytedata > high] = high
    bytedata[bytedata < 0] = 0
    return cast[uint8](bytedata) + cast[uint8](low)

def imread(name,flatten=0):
    """
    Read an image file from a filename.

    Parameters
    ----------
    name : str
        The file name to be read.
    flatten : bool, optional
        If True, flattens the color layers into a single gray-scale layer.

    Returns
    -------
    imread : ndarray
        The array obtained by reading image from file `name`.

    Notes
    -----
    The image is flattened by calling convert('F') on
    the resulting image object.

    """

    im = Image.open(name)
    return fromimage(im,flatten=flatten)

def imsave(name, arr):
    """
    Save an array as an image.

    Parameters
    ----------
    name : str
        Output filename.
    arr : ndarray, MxN or MxNx3 or MxNx4
        Array containing image values.  If the shape is ``MxN``, the array
        represents a grey-level image.  Shape ``MxNx3`` stores the red, green
        and blue bands along the last dimension.  An alpha layer may be
        included, specified as the last colour band of an ``MxNx4`` array.

    Examples
    --------
    Construct an array of gradient intensity values and save to file:

    >>> x = np.zeros((255, 255))
    >>> x = np.zeros((255, 255), dtype=np.uint8)
    >>> x[:] = np.arange(255)
    >>> imsave('/tmp/gradient.png', x)

    Construct an array with three colour bands (R, G, B) and store to file:

    >>> rgb = np.zeros((255, 255, 3), dtype=np.uint8)
    >>> rgb[..., 0] = np.arange(255)
    >>> rgb[..., 1] = 55
    >>> rgb[..., 2] = 1 - np.arange(255)
    >>> imsave('/tmp/rgb_gradient.png', rgb)

    """
    im = toimage(arr)
    im.save(name)
    return

def fromimage(im, flatten=0):
    """
    Return a copy of a PIL image as a numpy array.

    Parameters
    ----------
    im : PIL image
        Input image.
    flatten : bool
        If true, convert the output to grey-scale.

    Returns
    -------
    fromimage : ndarray
        The different colour bands/channels are stored in the
        third dimension, such that a grey-image is MxN, an
        RGB-image MxNx3 and an RGBA-image MxNx4.

    """
    if not Image.isImageType(im):
        raise TypeError("Input is not a PIL image.")
    if flatten:
        im = im.convert('F')
    elif im.mode == '1':
        # workaround for crash in PIL, see #1613.
        im.convert('L')

    return array(im)

_errstr = "Mode is unknown or incompatible with input array shape."

def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
            mode=None, channel_axis=None):
    """Takes a numpy array and returns a PIL image.

    The mode of the PIL image depends on the array shape and the `pal` and
    `mode` keywords.

    For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode
    is given as 'F' or 'I' in which case a float and/or integer array is made.

    Notes
    -----
    For 3-D arrays, the `channel_axis` argument tells which dimension of the
    array holds the channel data.

    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
    by default or 'YCbCr' if selected.

    The numpy array must be either 2 dimensional or 3 dimensional.

    """
    data = asarray(arr)
    if iscomplexobj(data):
        raise ValueError("Cannot convert a complex-valued array.")
    shape = list(data.shape)
    valid = len(shape) == 2 or ((len(shape) == 3) and
                              ((3 in shape) or (4 in shape)))
    if not valid:
        raise ValueError("'arr' does not have a suitable array shape for any mode.")
    if len(shape) == 2:
        shape = (shape[1],shape[0])  # columns show up first
        if mode == 'F':
            data32 = data.astype(numpy.float32)
            image = Image.fromstring(mode,shape,data32.tostring())
            return image
        if mode in [None, 'L', 'P']:
            bytedata = bytescale(data,high=high,low=low,cmin=cmin,cmax=cmax)
            image = Image.fromstring('L',shape,bytedata.tostring())
            if pal is not None:
                image.putpalette(asarray(pal,dtype=uint8).tostring())
                # Becomes a mode='P' automagically.
            elif mode == 'P':  # default gray-scale
                pal = arange(0,256,1,dtype=uint8)[:,newaxis] * \
                      ones((3,),dtype=uint8)[newaxis,:]
                image.putpalette(asarray(pal,dtype=uint8).tostring())
            return image
        if mode == '1':  # high input gives threshold for 1
            bytedata = (data > high)
            image = Image.fromstring('1',shape,bytedata.tostring())
            return image
        if cmin is None:
            cmin = amin(ravel(data))
        if cmax is None:
            cmax = amax(ravel(data))
        data = (data*1.0 - cmin)*(high-low)/(cmax-cmin) + low
        if mode == 'I':
            data32 = data.astype(numpy.uint32)
            image = Image.fromstring(mode,shape,data32.tostring())
        else:
            raise ValueError(_errstr)
        return image

    # if here then 3-d array with a 3 or a 4 in the shape length.
    # Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
    if channel_axis is None:
        if (3 in shape):
            ca = numpy.flatnonzero(asarray(shape) == 3)[0]
        else:
            ca = numpy.flatnonzero(asarray(shape) == 4)
            if len(ca):
                ca = ca[0]
            else:
                raise ValueError("Could not find channel dimension.")
    else:
        ca = channel_axis

    numch = shape[ca]
    if numch not in [3,4]:
        raise ValueError("Channel axis dimension is not valid.")

    bytedata = bytescale(data,high=high,low=low,cmin=cmin,cmax=cmax)
    if ca == 2:
        strdata = bytedata.tostring()
        shape = (shape[1],shape[0])
    elif ca == 1:
        strdata = transpose(bytedata,(0,2,1)).tostring()
        shape = (shape[2],shape[0])
    elif ca == 0:
        strdata = transpose(bytedata,(1,2,0)).tostring()
        shape = (shape[2],shape[1])
    if mode is None:
        if numch == 3:
            mode = 'RGB'
        else:
            mode = 'RGBA'

    if mode not in ['RGB','RGBA','YCbCr','CMYK']:
        raise ValueError(_errstr)

    if mode in ['RGB', 'YCbCr']:
        if numch != 3:
            raise ValueError("Invalid array shape for mode.")
    if mode in ['RGBA', 'CMYK']:
        if numch != 4:
            raise ValueError("Invalid array shape for mode.")

    # Here we know data and mode is correct
    image = Image.fromstring(mode, shape, strdata)
    return image

def imrotate(arr,angle,interp='bilinear'):
    """
    Rotate an image counter-clockwise by angle degrees.

    Parameters
    ----------
    arr : ndarray
        Input array of image to be rotated.
    angle : float
        The angle of rotation.
    interp : str, optional
        Interpolation

        - 'nearest' :  for nearest neighbor
        - 'bilinear' : for bilinear
        - 'cubic' : cubic
        - 'bicubic' : for bicubic

    Returns
    -------
    imrotate : ndarray
        The rotated array of image.

    """
    arr = asarray(arr)
    func = {'nearest':0,'bilinear':2,'bicubic':3,'cubic':3}
    im = toimage(arr)
    im = im.rotate(angle,resample=func[interp])
    return fromimage(im)

def imshow(arr):
    """
    Simple showing of an image through an external viewer.

    Uses the image viewer specified by the environment variable
    SCIPY_PIL_IMAGE_VIEWER, or if that is not defined then `see`,
    to view a temporary file generated from array data.

    Parameters
    ----------
    arr : ndarray
        Array of image data to show.

    Returns
    -------
    None

    Examples
    --------
    >>> a = np.tile(np.arange(255), (255,1))
    >>> from scipy import misc
    >>> misc.pilutil.imshow(a)

    """
    im = toimage(arr)
    fnum,fname = tempfile.mkstemp('.png')
    try:
        im.save(fname)
    except:
        raise RuntimeError("Error saving temporary image data.")

    import os
    os.close(fnum)

    cmd = os.environ.get('SCIPY_PIL_IMAGE_VIEWER','see')
    status = os.system("%s %s" % (cmd,fname))

    os.unlink(fname)
    if status != 0:
        raise RuntimeError('Could not execute image viewer.')

def imresize(arr, size, interp='bilinear', mode=None):
    """
    Resize an image.

    Parameters
    ----------
    arr : ndarray
        The array of image to be resized.

    size : int, float or tuple
        * int   - Percentage of current size.
        * float - Fraction of current size.
        * tuple - Size of the output image.

    interp : str
        Interpolation to use for re-sizing ('nearest', 'bilinear', 'bicubic'
        or 'cubic').

    mode : str
        The PIL image mode ('P', 'L', etc.).

    Returns
    -------
    imresize : ndarray
        The resized array of image.

    """
    im = toimage(arr, mode=mode)
    ts = type(size)
    if issubdtype(ts,int):
        size = size / 100.0
    elif issubdtype(type(size),float):
        size = (array(im.size)*size).astype(int)
    else:
        size = (size[1],size[0])
    func = {'nearest':0,'bilinear':2,'bicubic':3,'cubic':3}
    imnew = im.resize(size, resample=func[interp])
    return fromimage(imnew)

def imfilter(arr,ftype):
    """
    Simple filtering of an image.

    Parameters
    ----------
    arr : ndarray
        The array of Image in which the filter is to be applied.
    ftype : str
        The filter that has to be applied. Legal values are:
        'blur', 'contour', 'detail', 'edge_enhance', 'edge_enhance_more',
        'emboss', 'find_edges', 'smooth', 'smooth_more', 'sharpen'.

    Returns
    -------
    imfilter : ndarray
        The array with filter applied.

    Raises
    ------
    ValueError
        *Unknown filter type.*  If the filter you are trying
        to apply is unsupported.

    """
    _tdict = {'blur':ImageFilter.BLUR,
              'contour':ImageFilter.CONTOUR,
              'detail':ImageFilter.DETAIL,
              'edge_enhance':ImageFilter.EDGE_ENHANCE,
              'edge_enhance_more':ImageFilter.EDGE_ENHANCE_MORE,
              'emboss':ImageFilter.EMBOSS,
              'find_edges':ImageFilter.FIND_EDGES,
              'smooth':ImageFilter.SMOOTH,
              'smooth_more':ImageFilter.SMOOTH_MORE,
              'sharpen':ImageFilter.SHARPEN
              }

    im = toimage(arr)
    if ftype not in _tdict:
        raise ValueError("Unknown filter type.")
    return fromimage(im.filter(_tdict[ftype]))
<commit_msg>MAINT: use Image.frombytes if available<commit_after>"""
A collection of image utilities using the Python Imaging Library (PIL).

Note that PIL is not a dependency of SciPy and this module is not
available on systems that don't have PIL installed.

"""
from __future__ import division, print_function, absolute_import

# Functions which need the PIL

import numpy
import tempfile

from numpy import amin, amax, ravel, asarray, cast, arange, \
     ones, newaxis, transpose, mgrid, iscomplexobj, sum, zeros, uint8, \
     issubdtype, array

try:
    from PIL import Image, ImageFilter
except ImportError:
    import Image
    import ImageFilter

if not hasattr(Image, 'frombytes'):
    Image.frombytes = Image.fromstring

__all__ = ['fromimage','toimage','imsave','imread','bytescale',
           'imrotate','imresize','imshow','imfilter']

# Returns a byte-scaled image
def bytescale(data, cmin=None, cmax=None, high=255, low=0):
    """
    Byte scales an array (image).

    Byte scaling means converting the input image to uint8 dtype and scaling
    the range to ``(low, high)`` (default 0-255).
    If the input image already has dtype uint8, no scaling is done.

    Parameters
    ----------
    data : ndarray
        PIL image data array.
    cmin : scalar, optional
        Bias scaling of small values. Default is ``data.min()``.
    cmax : scalar, optional
        Bias scaling of large values. Default is ``data.max()``.
    high : scalar, optional
        Scale max value to `high`.  Default is 255.
    low : scalar, optional
        Scale min value to `low`.  Default is 0.

    Returns
    -------
    img_array : uint8 ndarray
        The byte-scaled array.

    Examples
    --------
    >>> img = array([[ 91.06794177,   3.39058326,  84.4221549 ],
                     [ 73.88003259,  80.91433048,   4.88878881],
                     [ 51.53875334,  34.45808177,  27.5873488 ]])
    >>> bytescale(img)
    array([[255,   0, 236],
           [205, 225,   4],
           [140,  90,  70]], dtype=uint8)
    >>> bytescale(img, high=200, low=100)
    array([[200, 100, 192],
           [180, 188, 102],
           [155, 135, 128]], dtype=uint8)
    >>> bytescale(img, cmin=0, cmax=255)
    array([[91,  3, 84],
           [74, 81,  5],
           [52, 34, 28]], dtype=uint8)

    """
    if data.dtype == uint8:
        return data

    if high < low:
        raise ValueError("`high` should be larger than `low`.")

    if cmin is None:
        cmin = data.min()
    if cmax is None:
        cmax = data.max()

    cscale = cmax - cmin
    if cscale < 0:
        raise ValueError("`cmax` should be larger than `cmin`.")
    elif cscale == 0:
        cscale = 1

    scale = float(high - low) / cscale
    bytedata = (data * 1.0 - cmin) * scale + 0.4999
    bytedata[bytedata > high] = high
    bytedata[bytedata < 0] = 0
    return cast[uint8](bytedata) + cast[uint8](low)

def imread(name,flatten=0):
    """
    Read an image file from a filename.

    Parameters
    ----------
    name : str
        The file name to be read.
    flatten : bool, optional
        If True, flattens the color layers into a single gray-scale layer.

    Returns
    -------
    imread : ndarray
        The array obtained by reading image from file `name`.

    Notes
    -----
    The image is flattened by calling convert('F') on
    the resulting image object.

    """

    im = Image.open(name)
    return fromimage(im,flatten=flatten)

def imsave(name, arr):
    """
    Save an array as an image.

    Parameters
    ----------
    name : str
        Output filename.
    arr : ndarray, MxN or MxNx3 or MxNx4
        Array containing image values.  If the shape is ``MxN``, the array
        represents a grey-level image.  Shape ``MxNx3`` stores the red, green
        and blue bands along the last dimension.  An alpha layer may be
        included, specified as the last colour band of an ``MxNx4`` array.

    Examples
    --------
    Construct an array of gradient intensity values and save to file:

    >>> x = np.zeros((255, 255))
    >>> x = np.zeros((255, 255), dtype=np.uint8)
    >>> x[:] = np.arange(255)
    >>> imsave('/tmp/gradient.png', x)

    Construct an array with three colour bands (R, G, B) and store to file:

    >>> rgb = np.zeros((255, 255, 3), dtype=np.uint8)
    >>> rgb[..., 0] = np.arange(255)
    >>> rgb[..., 1] = 55
    >>> rgb[..., 2] = 1 - np.arange(255)
    >>> imsave('/tmp/rgb_gradient.png', rgb)

    """
    im = toimage(arr)
    im.save(name)
    return

def fromimage(im, flatten=0):
    """
    Return a copy of a PIL image as a numpy array.

    Parameters
    ----------
    im : PIL image
        Input image.
    flatten : bool
        If true, convert the output to grey-scale.

    Returns
    -------
    fromimage : ndarray
        The different colour bands/channels are stored in the
        third dimension, such that a grey-image is MxN, an
        RGB-image MxNx3 and an RGBA-image MxNx4.

    """
    if not Image.isImageType(im):
        raise TypeError("Input is not a PIL image.")
    if flatten:
        im = im.convert('F')
    elif im.mode == '1':
        # workaround for crash in PIL, see #1613.
        im.convert('L')

    return array(im)

_errstr = "Mode is unknown or incompatible with input array shape."

def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
            mode=None, channel_axis=None):
    """Takes a numpy array and returns a PIL image.

    The mode of the PIL image depends on the array shape and the `pal` and
    `mode` keywords.

    For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode
    is given as 'F' or 'I' in which case a float and/or integer array is made.

    Notes
    -----
    For 3-D arrays, the `channel_axis` argument tells which dimension of the
    array holds the channel data.

    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
    by default or 'YCbCr' if selected.

    The numpy array must be either 2 dimensional or 3 dimensional.

    """
    data = asarray(arr)
    if iscomplexobj(data):
        raise ValueError("Cannot convert a complex-valued array.")
    shape = list(data.shape)
    valid = len(shape) == 2 or ((len(shape) == 3) and
                              ((3 in shape) or (4 in shape)))
    if not valid:
        raise ValueError("'arr' does not have a suitable array shape for any mode.")
    if len(shape) == 2:
        shape = (shape[1],shape[0])  # columns show up first
        if mode == 'F':
            data32 = data.astype(numpy.float32)
            image = Image.frombytes(mode,shape,data32.tostring())
            return image
        if mode in [None, 'L', 'P']:
            bytedata = bytescale(data,high=high,low=low,cmin=cmin,cmax=cmax)
            image = Image.frombytes('L',shape,bytedata.tostring())
            if pal is not None:
                image.putpalette(asarray(pal,dtype=uint8).tostring())
                # Becomes a mode='P' automagically.
            elif mode == 'P':  # default gray-scale
                pal = arange(0,256,1,dtype=uint8)[:,newaxis] * \
                      ones((3,),dtype=uint8)[newaxis,:]
                image.putpalette(asarray(pal,dtype=uint8).tostring())
            return image
        if mode == '1':  # high input gives threshold for 1
            bytedata = (data > high)
            image = Image.frombytes('1',shape,bytedata.tostring())
            return image
        if cmin is None:
            cmin = amin(ravel(data))
        if cmax is None:
            cmax = amax(ravel(data))
        data = (data*1.0 - cmin)*(high-low)/(cmax-cmin) + low
        if mode == 'I':
            data32 = data.astype(numpy.uint32)
            image = Image.frombytes(mode,shape,data32.tostring())
        else:
            raise ValueError(_errstr)
        return image

    # if here then 3-d array with a 3 or a 4 in the shape length.
    # Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
    if channel_axis is None:
        if (3 in shape):
            ca = numpy.flatnonzero(asarray(shape) == 3)[0]
        else:
            ca = numpy.flatnonzero(asarray(shape) == 4)
            if len(ca):
                ca = ca[0]
            else:
                raise ValueError("Could not find channel dimension.")
    else:
        ca = channel_axis

    numch = shape[ca]
    if numch not in [3,4]:
        raise ValueError("Channel axis dimension is not valid.")

    bytedata = bytescale(data,high=high,low=low,cmin=cmin,cmax=cmax)
    if ca == 2:
        strdata = bytedata.tostring()
        shape = (shape[1],shape[0])
    elif ca == 1:
        strdata = transpose(bytedata,(0,2,1)).tostring()
        shape = (shape[2],shape[0])
    elif ca == 0:
        strdata = transpose(bytedata,(1,2,0)).tostring()
        shape = (shape[2],shape[1])
    if mode is None:
        if numch == 3:
            mode = 'RGB'
        else:
            mode = 'RGBA'

    if mode not in ['RGB','RGBA','YCbCr','CMYK']:
        raise ValueError(_errstr)

    if mode in ['RGB', 'YCbCr']:
        if numch != 3:
            raise ValueError("Invalid array shape for mode.")
    if mode in ['RGBA', 'CMYK']:
        if numch != 4:
            raise ValueError("Invalid array shape for mode.")

    # Here we know data and mode is correct
    image = Image.frombytes(mode, shape, strdata)
    return image

def imrotate(arr,angle,interp='bilinear'):
    """
    Rotate an image counter-clockwise by angle degrees.

    Parameters
    ----------
    arr : ndarray
        Input array of image to be rotated.
    angle : float
        The angle of rotation.
    interp : str, optional
        Interpolation

        - 'nearest' :  for nearest neighbor
        - 'bilinear' : for bilinear
        - 'cubic' : cubic
        - 'bicubic' : for bicubic

    Returns
    -------
    imrotate : ndarray
        The rotated array of image.

    """
    arr = asarray(arr)
    func = {'nearest':0,'bilinear':2,'bicubic':3,'cubic':3}
    im = toimage(arr)
    im = im.rotate(angle,resample=func[interp])
    return fromimage(im)

def imshow(arr):
    """
    Simple showing of an image through an external viewer.

    Uses the image viewer specified by the environment variable
    SCIPY_PIL_IMAGE_VIEWER, or if that is not defined then `see`,
    to view a temporary file generated from array data.

    Parameters
    ----------
    arr : ndarray
        Array of image data to show.

    Returns
    -------
    None

    Examples
    --------
    >>> a = np.tile(np.arange(255), (255,1))
    >>> from scipy import misc
    >>> misc.pilutil.imshow(a)

    """
    im = toimage(arr)
    fnum,fname = tempfile.mkstemp('.png')
    try:
        im.save(fname)
    except:
        raise RuntimeError("Error saving temporary image data.")

    import os
    os.close(fnum)

    cmd = os.environ.get('SCIPY_PIL_IMAGE_VIEWER','see')
    status = os.system("%s %s" % (cmd,fname))

    os.unlink(fname)
    if status != 0:
        raise RuntimeError('Could not execute image viewer.')

def imresize(arr, size, interp='bilinear', mode=None):
    """
    Resize an image.

    Parameters
    ----------
    arr : ndarray
        The array of image to be resized.

    size : int, float or tuple
        * int   - Percentage of current size.
        * float - Fraction of current size.
        * tuple - Size of the output image.

    interp : str
        Interpolation to use for re-sizing ('nearest', 'bilinear', 'bicubic'
        or 'cubic').

    mode : str
        The PIL image mode ('P', 'L', etc.).

    Returns
    -------
    imresize : ndarray
        The resized array of image.

    """
    im = toimage(arr, mode=mode)
    ts = type(size)
    if issubdtype(ts,int):
        size = size / 100.0
    elif issubdtype(type(size),float):
        size = (array(im.size)*size).astype(int)
    else:
        size = (size[1],size[0])
    func = {'nearest':0,'bilinear':2,'bicubic':3,'cubic':3}
    imnew = im.resize(size, resample=func[interp])
    return fromimage(imnew)

def imfilter(arr,ftype):
    """
    Simple filtering of an image.

    Parameters
    ----------
    arr : ndarray
        The array of Image in which the filter is to be applied.
    ftype : str
        The filter that has to be applied. Legal values are:
        'blur', 'contour', 'detail', 'edge_enhance', 'edge_enhance_more',
        'emboss', 'find_edges', 'smooth', 'smooth_more', 'sharpen'.

    Returns
    -------
    imfilter : ndarray
        The array with filter applied.

    Raises
    ------
    ValueError
        *Unknown filter type.*  If the filter you are trying
        to apply is unsupported.

    """
    _tdict = {'blur':ImageFilter.BLUR,
              'contour':ImageFilter.CONTOUR,
              'detail':ImageFilter.DETAIL,
              'edge_enhance':ImageFilter.EDGE_ENHANCE,
              'edge_enhance_more':ImageFilter.EDGE_ENHANCE_MORE,
              'emboss':ImageFilter.EMBOSS,
              'find_edges':ImageFilter.FIND_EDGES,
              'smooth':ImageFilter.SMOOTH,
              'smooth_more':ImageFilter.SMOOTH_MORE,
              'sharpen':ImageFilter.SHARPEN
              }

    im = toimage(arr)
    if ftype not in _tdict:
        raise ValueError("Unknown filter type.")
    return fromimage(im.filter(_tdict[ftype]))
<|endoftext|>


<commit_before>from ..Qt import QtCore, QtGui
from ..Point import Point
from .UIGraphicsItem import *
from .. import functions as fn

class TextItem(UIGraphicsItem):
    """
    GraphicsItem displaying unscaled text (the text will always appear normal even inside a scaled ViewBox).
    """
    def __init__(self, text='', color=(200,200,200), html=None, anchor=(0,0), border=None, fill=None, angle=0):
        """
        ==============  =================================================================================
        **Arguments:**
        *text*          The text to display
        *color*         The color of the text (any format accepted by pg.mkColor)
        *html*          If specified, this overrides both *text* and *color*
        *anchor*        A QPointF or (x,y) sequence indicating what region of the text box will
                        be anchored to the item's position. A value of (0,0) sets the upper-left corner
                        of the text box to be at the position specified by setPos(), while a value of (1,1)
                        sets the lower-right corner.
        *border*        A pen to use when drawing the border
        *fill*          A brush to use when filling within the border
        ==============  =================================================================================
        """

        ## not working yet
        #*angle*      Angle in degrees to rotate text (note that the rotation assigned in this item's
                     #transformation will be ignored)

        self.anchor = Point(anchor)
        #self.angle = 0
        UIGraphicsItem.__init__(self)
        self.textItem = QtGui.QGraphicsTextItem()
        self.textItem.setParentItem(self)
        self.lastTransform = None
        self._bounds = QtCore.QRectF()
        if html is None:
            self.setText(text, color)
        else:
            self.setHtml(html)
        self.fill = fn.mkBrush(fill)
        self.border = fn.mkPen(border)
        self.rotate(angle)
        #self.textItem.setFlag(self.ItemIgnoresTransformations)  ## This is required to keep the text unscaled inside the viewport

    def setText(self, text, color=(200,200,200)):
        """
        Set the text and color of this item.

        This method sets the plain text of the item; see also setHtml().
        """
        color = fn.mkColor(color)
        self.textItem.setDefaultTextColor(color)
        self.textItem.setPlainText(text)
        self.updateText()
        #html = '%s' % (color, text)
        #self.setHtml(html)

    def updateAnchor(self):
        pass
        #self.resetTransform()
        #self.translate(0, 20)

    def setPlainText(self, *args):
        """
        Set the plain text to be rendered by this item.

        See QtGui.QGraphicsTextItem.setPlainText().
        """
        self.textItem.setPlainText(*args)
        self.updateText()

    def setHtml(self, *args):
        """
        Set the HTML code to be rendered by this item.

        See QtGui.QGraphicsTextItem.setHtml().
        """
        self.textItem.setHtml(*args)
        self.updateText()

    def setTextWidth(self, *args):
        """
        Set the width of the text.

        If the text requires more space than the width limit, then it will be
        wrapped into multiple lines.

        See QtGui.QGraphicsTextItem.setTextWidth().
        """
        self.textItem.setTextWidth(*args)
        self.updateText()

    def setFont(self, *args):
        """
        Set the font for this text.

        See QtGui.QGraphicsTextItem.setFont().
        """
        self.textItem.setFont(*args)
        self.updateText()

    #def setAngle(self, angle):
        #self.angle = angle
        #self.updateText()

    def updateText(self):

        ## Needed to maintain font size when rendering to image with increased resolution
        self.textItem.resetTransform()
        #self.textItem.rotate(self.angle)
        if self._exportOpts is not False and 'resolutionScale' in self._exportOpts:
            s = self._exportOpts['resolutionScale']
            self.textItem.scale(s, s)

        self.textItem.setTransform(self.sceneTransform().inverted()[0])
        self.textItem.setPos(0,0)
        self.textItem.setPos(-self.textItem.mapToParent(Point(0,0)))

    def viewRangeChanged(self):
        self.updateText()

    def boundingRect(self):
        return self.textItem.mapToParent(self.textItem.boundingRect()).boundingRect()

    def paint(self, p, *args):
        tr = p.transform()
        if self.lastTransform is not None:
            if tr != self.lastTransform:
                self.viewRangeChanged()
        self.lastTransform = tr

        if self.border.style() != QtCore.Qt.NoPen or self.fill.style() != QtCore.Qt.NoBrush:
            p.setPen(self.border)
            p.setBrush(self.fill)
            p.setRenderHint(p.Antialiasing, True)
            p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect()))

        <commit_msg>clean up textitem, fix anchoring<commit_after>from ..Qt import QtCore, QtGui
from ..Point import Point
from .UIGraphicsItem import *
from .. import functions as fn

class TextItem(UIGraphicsItem):
    """
    GraphicsItem displaying unscaled text (the text will always appear normal even inside a scaled ViewBox).
    """
    def __init__(self, text='', color=(200,200,200), html=None, anchor=(0,0), border=None, fill=None, angle=0):
        """
        ==============  =================================================================================
        **Arguments:**
        *text*          The text to display
        *color*         The color of the text (any format accepted by pg.mkColor)
        *html*          If specified, this overrides both *text* and *color*
        *anchor*        A QPointF or (x,y) sequence indicating what region of the text box will
                        be anchored to the item's position. A value of (0,0) sets the upper-left corner
                        of the text box to be at the position specified by setPos(), while a value of (1,1)
                        sets the lower-right corner.
        *border*        A pen to use when drawing the border
        *fill*          A brush to use when filling within the border
        ==============  =================================================================================
        """

        ## not working yet
        #*angle*      Angle in degrees to rotate text (note that the rotation assigned in this item's
                     #transformation will be ignored)

        self.anchor = Point(anchor)
        #self.angle = 0
        UIGraphicsItem.__init__(self)
        self.textItem = QtGui.QGraphicsTextItem()
        self.textItem.setParentItem(self)
        self._lastTransform = None
        self._bounds = QtCore.QRectF()
        if html is None:
            self.setText(text, color)
        else:
            self.setHtml(html)
        self.fill = fn.mkBrush(fill)
        self.border = fn.mkPen(border)
        self.setAngle(angle)
        #self.textItem.setFlag(self.ItemIgnoresTransformations)  ## This is required to keep the text unscaled inside the viewport

    def setText(self, text, color=(200,200,200)):
        """
        Set the text and color of this item.

        This method sets the plain text of the item; see also setHtml().
        """
        color = fn.mkColor(color)
        self.textItem.setDefaultTextColor(color)
        self.textItem.setPlainText(text)
        self.updateText()
        #html = '%s' % (color, text)
        #self.setHtml(html)

    def updateAnchor(self):
        pass
        #self.resetTransform()
        #self.translate(0, 20)

    def setPlainText(self, *args):
        """
        Set the plain text to be rendered by this item.

        See QtGui.QGraphicsTextItem.setPlainText().
        """
        self.textItem.setPlainText(*args)
        self.updateText()

    def setHtml(self, *args):
        """
        Set the HTML code to be rendered by this item.

        See QtGui.QGraphicsTextItem.setHtml().
        """
        self.textItem.setHtml(*args)
        self.updateText()

    def setTextWidth(self, *args):
        """
        Set the width of the text.

        If the text requires more space than the width limit, then it will be
        wrapped into multiple lines.

        See QtGui.QGraphicsTextItem.setTextWidth().
        """
        self.textItem.setTextWidth(*args)
        self.updateText()

    def setFont(self, *args):
        """
        Set the font for this text.

        See QtGui.QGraphicsTextItem.setFont().
        """
        self.textItem.setFont(*args)
        self.updateText()

    def setAngle(self, angle):
        self.textItem.resetTransform()
        self.textItem.rotate(angle)
        self.updateText()

    def updateText(self):
        # update text position to obey anchor
        r = self.textItem.boundingRect()
        tl = self.textItem.mapToParent(r.topLeft())
        br = self.textItem.mapToParent(r.bottomRight())
        offset = (br - tl) * self.anchor
        self.textItem.setPos(-offset)

        ### Needed to maintain font size when rendering to image with increased resolution
        #self.textItem.resetTransform()
        ##self.textItem.rotate(self.angle)
        #if self._exportOpts is not False and 'resolutionScale' in self._exportOpts:
            #s = self._exportOpts['resolutionScale']
            #self.textItem.scale(s, s)

    def viewRangeChanged(self):
        self.updateText()

    def boundingRect(self):
        return self.textItem.mapToParent(self.textItem.boundingRect()).boundingRect()

    def viewTransformChanged(self):
        # called whenever view transform has changed.
        # Do this here to avoid double-updates when view changes.
        self.updateTransform()

    def paint(self, p, *args):
        # this is not ideal because it causes another update to be scheduled.
        # ideally, we would have a sceneTransformChanged event to react to..
        self.updateTransform()

        if self.border.style() != QtCore.Qt.NoPen or self.fill.style() != QtCore.Qt.NoBrush:
            p.setPen(self.border)
            p.setBrush(self.fill)
            p.setRenderHint(p.Antialiasing, True)
            p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect()))

    def updateTransform(self):
        # update transform such that this item has the correct orientation
        # and scaling relative to the scene, but inherits its position from its
        # parent.
        # This is similar to setting ItemIgnoresTransformations = True, but
        # does not break mouse interaction and collision detection.
        p = self.parentItem()
        if p is None:
            pt = QtGui.QTransform()
        else:
            pt = p.sceneTransform()

        if pt == self._lastTransform:
            return

        t = pt.inverted()[0]
        # reset translation
        t.setMatrix(t.m11(), t.m12(), t.m13(), t.m21(), t.m22(), t.m23(), 0, 0, t.m33())
        self.setTransform(t)

        self._lastTransform = pt
        <|endoftext|>


<commit_before># Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import time

import flask
from sqlalchemy import event

from scoreboard import main
from scoreboard import models
from scoreboard import utils

app = main.get_app()

DEFAULT_CSP_POLICY = {
        'default-src': ["'self'"],
        'script-src': [
            "'self'",
            "'unsafe-eval'",  # Needed for Charts.js
        ],
        'frame-ancestors': ["'none'"],
        'img-src': [
            "'self'",
            'data:',
        ],
        'object-src': ["'none'"],
        'reflected-xss': ['block'],
        'font-src': [
            "'self'",
            'fonts.gstatic.com',
        ],
        'style-src': [
            "'self'",
            'fonts.googleapis.com',
            "'unsafe-inline'",  # Needed for Charts.js
        ],
        }

_CSP_POLICY_STRING = None

def get_csp_policy():
    global _CSP_POLICY_STRING
    if _CSP_POLICY_STRING is not None:
        return _CSP_POLICY_STRING
    if app.config.get('CSP_POLICY'):
        policy = app.config.get('CSP_POLICY')
    elif app.config.get('EXTEND_CSP_POLICY'):
        policy = collections.defaultdict(list)
        for k, v in DEFAULT_CSP_POLICY.iteritems():
            policy[k] = v
        for k, v in app.config.get('EXTEND_CSP_POLICY').iteritems():
            policy[k].extend(v)
    else:
        policy = DEFAULT_CSP_POLICY
    components = []
    for k, v in policy.iteritems():
        sources = ' '.join(v)
        components.append(k + ' ' + sources)
    _CSP_POLICY_STRING = '; '.join(components)
    return _CSP_POLICY_STRING

# Setup flask.g
@app.before_request
def load_globals():
    """Prepopulate flask.g.* with properties."""
    try:
        del flask.g.user
    except AttributeError:
        pass
    try:
        del flask.g.team
    except AttributeError:
        pass
    if (app.config.get('SESSION_EXPIRATION_SECONDS') and
            flask.session.get('expires') > time.time()):
        flask.session.clear()
        return
    flask.g.uid = flask.session.get('user')
    flask.g.tid = flask.session.get('team')
    flask.g.admin = flask.session.get('admin') or False

# Add headers to responses
@app.after_request
def add_headers(response):
    """Add security-related headers to all outgoing responses."""
    h = response.headers
    h.add('Content-Security-Policy', get_csp_policy())
    h.add('X-Frame-Options', 'DENY')
    h.add('X-XSS-Protection', '1', mode='block')
    return response

@app.context_processor
def util_contexts():
    return dict(gametime=utils.GameTime)

_query_count = 0

if app.config.get('COUNT_QUERIES'):
    @event.listens_for(models.db.engine, 'before_cursor_execute')
    def receive_before_cursor_execute(
            conn, cursor, statement, parameters, context, executemany):
        global _query_count
        _query_count += 1

    @app.after_request
    def count_queries(response):
        global _query_count
        if _query_count > 0:
            app.logger.info('Request issued %d queries.', _query_count)
            _query_count = 0
        return response

def ensure_setup():
    if not app:
        raise RuntimeError('Invalid app setup.')
<commit_msg>Fix logic in testing cookie expiration. (#80)<commit_after># Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import time

import flask
from sqlalchemy import event

from scoreboard import main
from scoreboard import models
from scoreboard import utils

app = main.get_app()

DEFAULT_CSP_POLICY = {
        'default-src': ["'self'"],
        'script-src': [
            "'self'",
            "'unsafe-eval'",  # Needed for Charts.js
        ],
        'frame-ancestors': ["'none'"],
        'img-src': [
            "'self'",
            'data:',
        ],
        'object-src': ["'none'"],
        'reflected-xss': ['block'],
        'font-src': [
            "'self'",
            'fonts.gstatic.com',
        ],
        'style-src': [
            "'self'",
            'fonts.googleapis.com',
            "'unsafe-inline'",  # Needed for Charts.js
        ],
        }

_CSP_POLICY_STRING = None

def get_csp_policy():
    global _CSP_POLICY_STRING
    if _CSP_POLICY_STRING is not None:
        return _CSP_POLICY_STRING
    if app.config.get('CSP_POLICY'):
        policy = app.config.get('CSP_POLICY')
    elif app.config.get('EXTEND_CSP_POLICY'):
        policy = collections.defaultdict(list)
        for k, v in DEFAULT_CSP_POLICY.iteritems():
            policy[k] = v
        for k, v in app.config.get('EXTEND_CSP_POLICY').iteritems():
            policy[k].extend(v)
    else:
        policy = DEFAULT_CSP_POLICY
    components = []
    for k, v in policy.iteritems():
        sources = ' '.join(v)
        components.append(k + ' ' + sources)
    _CSP_POLICY_STRING = '; '.join(components)
    return _CSP_POLICY_STRING

# Setup flask.g
@app.before_request
def load_globals():
    """Prepopulate flask.g.* with properties."""
    try:
        del flask.g.user
    except AttributeError:
        pass
    try:
        del flask.g.team
    except AttributeError:
        pass
    if (app.config.get('SESSION_EXPIRATION_SECONDS') and
            flask.session.get('expires') < time.time()):
        flask.session.clear()
        return
    flask.g.uid = flask.session.get('user')
    flask.g.tid = flask.session.get('team')
    flask.g.admin = flask.session.get('admin') or False

# Add headers to responses
@app.after_request
def add_headers(response):
    """Add security-related headers to all outgoing responses."""
    h = response.headers
    h.add('Content-Security-Policy', get_csp_policy())
    h.add('X-Frame-Options', 'DENY')
    h.add('X-XSS-Protection', '1', mode='block')
    return response

@app.context_processor
def util_contexts():
    return dict(gametime=utils.GameTime)

_query_count = 0

if app.config.get('COUNT_QUERIES'):
    @event.listens_for(models.db.engine, 'before_cursor_execute')
    def receive_before_cursor_execute(
            conn, cursor, statement, parameters, context, executemany):
        global _query_count
        _query_count += 1

    @app.after_request
    def count_queries(response):
        global _query_count
        if _query_count > 0:
            app.logger.info('Request issued %d queries.', _query_count)
            _query_count = 0
        return response

def ensure_setup():
    if not app:
        raise RuntimeError('Invalid app setup.')
<|endoftext|>


<commit_before>#!/usr/bin/python2.7
# encoding: utf-8

from __future__ import division
import numpy as np
import h5py

class _load_adcp:
    """
'Variables' subset in FVCOM class contains the following numpy arrays:
    """
    def __init__(self,cls, debug=False):
        if debug:
            print 'Loading variables...'
        #Test if load with h5py or scipy
        if not type(cls.Data)==h5py._hl.files.File:
            self.lat = cls.Data['lat']
            self.lon = cls.Data['lon']
            self.bins = cls.Data['data'].bins[:].flatten()
            self.north_vel = cls.Data['data'].north_vel[:]
            self.east_vel = cls.Data['data'].east_vel[:]
            self.vert_vel = cls.Data['data'].vert_vel[:]
            self.dir_vel = cls.Data['data'].dir_vel[:]
            self.mag_signed_vel = cls.Data['data'].mag_signed_vel[:]
            self.pressure = cls.Data['pres']
            self.surf = self.pressure.surf[:].flatten()
            self.time = cls.Data['time']
            self.mtime = self.time.mtime[:].flatten()
            try:
                self.ucross = cls.Data['data'].ucross[:]
                self.ualong = cls.Data['data'].ualong[:]
            except AttributeError:
                pass

        else:
            self.lat = cls.Data['lat']
            self.lon = cls.Data['lon']
            self.bins = cls.Data['data'].bins[:].flatten()
            self.north_vel = cls.Data['data'].north_vel[:].T
            self.east_vel = cls.Data['data'].east_vel[:].T
            self.vert_vel = cls.Data['data'].vert_vel[:].T
            self.dir_vel = cls.Data['data'].dir_vel[:].T
            self.mag_signed_vel = cls.Data['data'].mag_signed_vel[:].T
            self.pressure = cls.Data['pres']
            self.surf = self.pressure.surf[:].flatten()
            self.time = cls.Data['time']
            self.mtime = self.time.mtime[:].flatten()
            try:
                self.ucross = cls.Data['data'].ucross[:].T
                self.ualong = cls.Data['data'].ualong[:].T
            except AttributeError:
                pass

        #Find the depth average of a variable based on percent_of_depth
        #choosen by the user. Currently only working for east_vel (u) and
        #north_vel (v)
        #TR: alaternative with percent of the depth
        #ind = np.argwhere(self.bins < self.percent_of_depth * self.surf[:,None])
        ind = np.argwhere(self.bins < self.surf[:,None])
        index = ind[np.r_[ind[1:,0] != ind[:-1,0], True]]
        data_ma_u = np.ma.array(self.east_vel, mask=np.arange(self.east_vel.shape[1]) > index[:, 1, None])
        data_ma_v = np.ma.array(self.north_vel, mask=np.arange(self.north_vel.shape[1]) > index[:, 1, None])
        self.ua = np.array(data_ma_u.mean(axis=1))
        self.va = np.array(data_ma_v.mean(axis=1))

        if debug:
            print '...Passed'
<commit_msg>Changed ADCP class<commit_after>#!/usr/bin/python2.7
# encoding: utf-8

from __future__ import division
import numpy as np
import h5py

class _load_adcp:
    """
'Variables' subset in FVCOM class contains the following numpy arrays:
    """
    def __init__(self,cls, debug=False):
        if debug:
            print 'Loading variables...'
        #Test if load with h5py or scipy
        if not type(cls.Data)==h5py._hl.files.File:
            self.lat = cls.Data['lat']
            self.lon = cls.Data['lon']
            self.bins = cls.Data['data'].bins[:].flatten()
            self.north_vel = cls.Data['data'].north_vel[:]
            self.east_vel = cls.Data['data'].east_vel[:]
            self.vert_vel = cls.Data['data'].vert_vel[:]
            self.dir_vel = cls.Data['data'].dir_vel[:]
            self.mag_signed_vel = cls.Data['data'].mag_signed_vel[:]
            self.pressure = cls.Data['pres']
            self.surf = self.pressure.surf[:].flatten()
            self.time = cls.Data['time']
            self.mtime = self.time.mtime[:].flatten()
            try:
                self.ucross = cls.Data['data'].ucross[:]
                self.ualong = cls.Data['data'].ualong[:]
            except AttributeError:
                pass

        else:
            self.lat = cls.Data['lat']
            self.lon = cls.Data['lon']
            self.bins = cls.Data['data']['bins'][:].flatten()
            self.north_vel = cls.Data['data']['north_vel'][:].T
            self.east_vel = cls.Data['data']['east_vel'][:].T
            self.vert_vel = cls.Data['data']['vert_vel'][:].T
            self.dir_vel = cls.Data['data']['dir_vel'][:].T
            self.mag_signed_vel = cls.Data['data']['mag_signed_vel'][:].T
            self.pressure = cls.Data['pres']
            self.surf = self.pressure['surf'][:].flatten()
            self.time = cls.Data['time']
            self.mtime = self.time['mtime'][:].flatten()
            try:
                self.ucross = cls.Data['data']['ucross'][:].T
                self.ualong = cls.Data['data']['ualong'][:].T
            except AttributeError:
                pass

        #Find the depth average of a variable based on percent_of_depth
        #choosen by the user. Currently only working for east_vel (u) and
        #north_vel (v)
        #TR: alaternative with percent of the depth
        #ind = np.argwhere(self.bins < self.percent_of_depth * self.surf[:,None])
        ind = np.argwhere(self.bins < self.surf[:,None])
        index = ind[np.r_[ind[1:,0] != ind[:-1,0], True]]
        data_ma_u = np.ma.array(self.east_vel, mask=np.arange(self.east_vel.shape[1]) > index[:, 1, None])
        data_ma_v = np.ma.array(self.north_vel, mask=np.arange(self.north_vel.shape[1]) > index[:, 1, None])
        self.ua = np.array(data_ma_u.mean(axis=1))
        self.va = np.array(data_ma_v.mean(axis=1))

        if debug:
            print '...Passed'
<|endoftext|>