计算两点间的距离
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 destination 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')
matlab调用gfortran编译的fortran程序出现end of file问题
https: // ww2.mathworks.cn/matlabcentral/answers/44388-or-system-or-unix-input-redirection#answer_56185
Executing a GFORTRAN executable within a Apple terminal window is not equivalent to executing it within MATLAB. In particular, MATLAB will set various environment variables and this can have unexpected results which seems to be the case here.
To work around this issue you need to set your environment variables for the GFORTRAN-compiled programs correctly within MATLAB before you execute them. You can use the SETENV function for this purpose. Please execute your FORTRAN script as follows:
setenv(‘GFORTRAN_STDIN_UNIT’, ‘5’)
setenv(‘GFORTRAN_STDOUT_UNIT’, ‘6’)
setenv(‘GFORTRAN_STDERR_UNIT’, ‘0’)
!./forward <
input
setenv(‘GFORTRAN_STDIN_UNIT’, ‘-1’)
setenv(‘GFORTRAN_STDOUT_UNIT’, ‘-1’)
setenv(‘GFORTRAN_STDERR_UNIT’, ‘-1’)
Note that after executing the FORTRAN script, I set the environment variables back to their default values.
Fatal error in MPI_Init:Other MPI error, error stack
一个MPI程序,出现这样的错误,仔细看看后续的提示行,是否出现如下的错误:gethostbyname failed
出现这种错误的原因是hosts文件中没有列入当前主机的hostname先看看主机的hostname是什么,用uname -a查看,比如我的是Linux scdzj 2.6.32-696.6.3.el6.x86_64 #1 SMP Wed Jul 12 14:17:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux则第二个字段scdzj是hostname打开/etc/hosts文件,在127.0.0.1的后面加上hostname,保存退出。这个问题即可解决。
stackoverflow. com/questions/23112515/mpich2-gethostbyname-failed
正则转换大小写
大写转小写: echo ‘ABCDS’ | sed ‘s/[A-Z]/\l&/g’
小写转大写: echo ‘abcds’ | sed ‘s/[a-z]/\u&/g’
pandas挑选并改值
df.loc[df.loc[df['a'] == 1,'b'].index, 'b'] = 3
timestamp to datetime
在pandas中,我先用time.mktime得到了一些标准秒钟数,然后用datetime.datetime.fromstamp将这些秒数转化为可读的时间,注意这个时候的时间格式是timestamp,如果要得到datetime格式的,要用timestamp.to_pydatetime函数。