bbe-更改二进制数据流

某个程序输出的文本结果中出现了特殊字符^ @,用grep匹配字符串时说是二进制文本,只好想办法去掉。

方法一:在vim下打开文件,可以看出本身就是个正常的文本文件,仅只是多了那几个特殊字符,输入:%s/\x00//g回车后即可删除。本来想照猫画虎用sed来处理,毕竟几百个文件都用vim操作是不现实的,结果发现用sed删除字符不成功,https://unix.stackexchange.com/questions/346291/editing-binary-streams-containing-x00-bytes

这里表示sed只能用来处理text文件。这个网站提供了另一种处理工具:

方法二:bbe,是一种类似于sed的流编辑工具,能处理二进制字符,可以这么操作bb -e ‘s/\x00//g’ output

另,在gawk中输出单引号,可以用\47

计算两点间的距离

from great_circle_calculator.__conversion import _point_to_radians, _point_to_degrees, _radians_to_degrees, \
    _degrees_to_radians
from great_circle_calculator.__error_checking import _error_check_point
from great_circle_calculator._constants import *


def distance_between_points(p1, p2, unit='meters', haversine=True):
    """ This function computes the distance between two points in the unit given in the unit parameter.  It will
    calculate the distance using the haversine unless the user specifies haversine to be False.  Then law of cosines
    will be used
    :param p1: tuple point of (lon, lat)
    :param p2: tuple point of (lon, lat)
    :param unit: unit of measurement. List can be found in constants.eligible_units
    :param haversine: True (default) uses haversine distance, False uses law of cosines
    :return: Distance between p1 and p2 in the units specified.
    """
    lon1, lat1 = _point_to_radians(_error_check_point(p1))
    lon2, lat2 = _point_to_radians(_error_check_point(p2))
    r_earth = getattr(radius_earth, unit, 'meters')
    if haversine:
        # Haversine
        d_lat, d_lon = lat2 - lat1, lon2 - lon1
        a = sin(d_lat / 2) * sin(d_lat / 2) + cos(lat1) * cos(lat2) * sin(d_lon / 2) * sin(d_lon / 2)
        c = 2 * atan2(sqrt(a), sqrt((1 - a)))
        dist = r_earth * c
        return dist
    # Spherical Law Of Cosines
    dist = acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)) * r_earth
    return dist


def bearing_at_p1(p1, p2):
    """ This function computes the bearing (i.e. course) at p1 given a destination of p2.  Use in conjunction with
    midpoint(*) and intermediate_point(*) to find the course along the route.  Use bearing_at_p2(*) to find the bearing
    at the endpoint
    :param p1: tuple point of (lon, lat)
    :param p2: tuple point of (lon, lat)
    :return: Course, in degrees
    """
    lon1, lat1 = _point_to_radians(_error_check_point(p1))
    lon2, lat2 = _point_to_radians(_error_check_point(p2))
    x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lon2 - lon1)
    y = sin(lon2 - lon1) * cos(lat2)
    course = atan2(y, x)
    return _radians_to_degrees(course)


def bearing_at_p2(p1, p2):
    """ This function computes the bearing (i.e. course) at p2 given a starting point of p1.  Use in conjunction with
    midpoint(*) and intermediate_point(*) to find the course along the route.  Use bearing_at_p1(*) to find the bearing
    at the endpoint
    :param p1: tuple point of (lon, lat)
    :param p2: tuple point of (lon, lat)
    :return: Course, in degrees
    """
    return (bearing_at_p1(p2, p1) + 180) % 360


def midpoint(p1, p2):
    """ This is the half-way point along a great circle path between the two points.
    :param p1: tuple point of (lon, lat)
    :param p2: tuple point of (lon, lat)
    :return: point (lon, lat)
    """
    lon1, lat1 = _point_to_radians(_error_check_point(p1))
    lon2, lat2 = _point_to_radians(_error_check_point(p2))
    b_x = cos(lat2) * cos(lon2 - lon1)
    b_y = cos(lat2) * sin(lon2 - lon1)
    lat3 = atan2(sin(lat1) + sin(lat2), sqrt((cos(lat1) + b_x) * (cos(lat1) + b_x) + b_y * b_y))
    lon3 = lon1 + atan2(b_y, cos(lat1) + b_x)
    lat3 = _radians_to_degrees(lat3)
    lon3 = (_radians_to_degrees(lon3) + 540) % 360 - 180
    p3 = (lon3, lat3)
    return p3


def intermediate_point(p1, p2, fraction=0.5):
    """ This function calculates the intermediate point along the course laid out by p1 to p2.  fraction is the fraction
    of the distance between p1 and p2, where 0 is p1, 0.5 is equivalent to midpoint(*), and 1 is p2.
    :param p1: tuple point of (lon, lat)
    :param p2: tuple point of (lon, lat)
    :param fraction: the fraction of the distance along the path.
    :return: point (lon, lat)
    """
    lon1, lat1 = _point_to_radians(_error_check_point(p1))
    lon2, lat2 = _point_to_radians(_error_check_point(p2))
    delta = distance_between_points(p1, p2) / radius_earth.meters
    a = sin((1 - fraction) * delta) / sin(delta)
    b = sin(fraction * delta) / sin(delta)
    x = a * cos(lat1) * cos(lon1) + b * cos(lat2) * cos(lon2)
    y = a * cos(lat1) * sin(lon1) + b * cos(lat2) * sin(lon2)
    z = a * sin(lat1) + b * sin(lat2)
    lat3 = atan2(z, sqrt(x * x + y * y))
    lon3 = atan2(y, x)
    return _point_to_degrees((lon3, lat3))


def point_given_start_and_bearing(p1, course, distance, unit='meters'):
    """ Given a start point, initial bearing, and distance, this will calculate the destina­tion point and final
    bearing travelling along a (shortest distance) great circle arc.
    :param p1: tuple point of (lon, lat)
    :param course: Course, in degrees
    :param distance: a length in unit
    :param unit: unit of measurement. List can be found in constants.eligible_units
    :return: point (lon, lat)
    """
    lon1, lat1 = _point_to_radians(_error_check_point(p1))
    brng = _degrees_to_radians(course)
    r_earth = getattr(radius_earth, unit, 'meters')
    delta = distance / r_earth
    lat2 = asin(sin(lat1) * cos(delta) + cos(lat1) * sin(delta) * cos(brng))
    lon2 = lon1 + atan2(sin(brng) * sin(delta) * cos(lat1), cos(delta) - sin(lat1) * sin(lat2))
    lon2 = (_radians_to_degrees(lon2) + 540) % 360 - 180
    p2 = (lon2, _radians_to_degrees(lat2))
    return p2

matplotlib同x轴作图,y轴反转

matplotlib同x轴作图,y轴反转
编辑删除转载2018-10-08 15:08:09
标签:linux
stackoverflow.com/questions/37737538/merge-matplotlib-subplots-with-shared-x-axis 

这里给了个例子,用了gridspec,其实只用subplot就可以了。


ax1=plt.subplot(211)

plt.vlines(tm1,0,mg1,color='r')

plt.xlim((1700,2020))

plt.ylim((0,8))

plt.setp(ax1.get_xticklabels(),visible=False)  ###隐藏第一个图的x轴标注



ax2=plt.subplot(212,sharex=ax1)

plt.vlines(tm2,0,mg2,color='b')

plt.xlim((1700,2020))

plt.ylim((8,0))   ##### 这里反着写,让y轴倒过来

yticks=ax2.yaxis.get_major_ticks()

yticks[0].label1.set_visible(False)  #### 两个图相交的y轴第一个标注会重叠,让第二个坐标的标注隐藏

plt.subplots_adjust(hspace=.0)    ##### 让两个图的间隔为0

matplotlib画条状时间序列图

想画一个台站的运行状态图像,如果某天台站正常运行,则在该天用方框标识为红色,否则就空在那里,因此该图像是个时间序列的条状图。

在这里找到了解决方案

stackoverflow.com/questions/44951911/plot-a-binary-timeline-in-maplotlib

用的是broken_barh函数

要注意的问题有:

这个函数貌似不认识日期类型的数据,因此要将日期转为数值,这里用到了

matplotlib.dates.date2num

但如果直接出图,会发现x轴坐标是数值而不是日期,因此还要对x轴的属性进行修改,类似于这样的语句

ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())

ax.xaxis.set_minor_locator(matplotlib.dates.YearLocator())



broken_barh函数主要包含x和y两个参数,其中x的构成是

(start,offset)

也可以是由这种tuple构成的list。画图的时候方框在x轴的起点是start,终点是start+offset

参数y类似,但如果高度固定,给定一个tuple就够了,可以如下处理

inxval=matplotlib.dates.date2num(test_ser)

times=list(zip(inxval,np.ones(len(test_ser))))

plt.broken_barh(times,(-1,1),color='red')