Skip to content

Instantly share code, notes, and snippets.

@rutj3
Last active March 9, 2018 09:42
Show Gist options
  • Save rutj3/594b5a322f623680684ca76385b8b3f3 to your computer and use it in GitHub Desktop.
Save rutj3/594b5a322f623680684ca76385b8b3f3 to your computer and use it in GitHub Desktop.
Orbit Example Shape output
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pyorbital.orbital import Orbital\n",
"\n",
"from datetime import datetime, timedelta\n",
"from collections import namedtuple\n",
"import numpy as np\n",
"\n",
"import fiona\n",
"import osr\n",
"from shapely import geometry\n",
"from fiona.crs import from_string, from_epsg\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def parse_tle(line1):\n",
" \"\"\"Function to get the dt from line1 of a TLE file\n",
" \n",
" returns epoch_year, epoch_day, dt\"\"\"\n",
" \n",
" \n",
" epoch_year = int('20' + line1[18:20])\n",
" epoch_day = float(line1[20:32])\n",
"\n",
" dt = datetime(epoch_year-1, 12, 31) + timedelta(days=epoch_day)\n",
" \n",
" return epoch_year, epoch_day, dt\n",
" \n",
"def parse_archived_tle(txt_file): \n",
" \"\"\"Function to parse all TLE's in a textfile\n",
" \n",
" returns a list of namedtuples\"\"\"\n",
" \n",
" tle = namedtuple('tle', 'sat_name line1 line2 epoch_year epoch_day dt')\n",
" \n",
" results = []\n",
" \n",
" with open(txt_file) as f:\n",
"\n",
" while True:\n",
" sat_name = f.readline()\n",
" line1 = f.readline()\n",
" line2 = f.readline() \n",
" \n",
" if not line2:\n",
" break\n",
" \n",
" epoch_year, epoch_day, dt = parse_tle(line1) \n",
" results.append(tle(sat_name=sat_name, line1=line1, line2=line2, \n",
" epoch_year=epoch_year, epoch_day=epoch_day, \n",
" dt=dt))\n",
" \n",
" return results\n",
"\n",
"def find_closest_tle(all_tle, target_dt):\n",
" \"\"\"Function to find the closest TLE to a given target date\n",
" \n",
" returns a namedtuple of the tle\"\"\"\n",
" \n",
" sorted_tle = sorted(all_tle, key=lambda x: abs((x.dt-target_dt).total_seconds()))\n",
" \n",
" return sorted_tle[0]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"tle_file_archive = r'C:/Users/Rutger/Downloads/3le-tiangongKOMPLETT-formatted.txt'\n",
"all_tle = parse_archived_tle(r'C:/Users/Rutger/Downloads/3le-tiangongKOMPLETT-formatted.txt')\n",
"\n",
"now = datetime.utcnow()\n",
"last_days = 1\n",
"\n",
"max_day_difference = 10 # raise a warning if the difference is bigger\n",
"\n",
"# print('Year Day Lon Lat Alt TLE date Target date')\n",
"\n",
"positions = []\n",
"\n",
"for i in np.arange(0, last_days, 0.001):\n",
"\n",
" target_dt = now - timedelta(days=i)\n",
" \n",
" tle = find_closest_tle(all_tle, target_dt)\n",
"\n",
" if abs((tle.dt - target_dt).days) > max_day_difference:\n",
" raise ValueError('Difference between TLE and target date to large')\n",
"\n",
" orb = Orbital(\"TIANGONG 1\", line1=tle.line1, line2=tle.line2)\n",
" lon, lat, alt = orb.get_lonlatalt(target_dt)\n",
" \n",
" positions.append((target_dt, tle, lon, lat, alt))\n",
" \n",
"# print(orb.tle.epoch_year, \n",
"# *[f'{x:12.3f}' for x in [orb.tle.epoch_day, lon, lat, alt]], \n",
"# tle.dt.strftime('\\t%Y-%m-%d %H:%M:%S'), \n",
"# target_dt.strftime('\\t%Y-%m-%d %H:%M:%S'))\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create point shape with the date"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# the shapefile attributes\n",
"props = {'timestamp': 'str'}\n",
"\n",
"write_every = 10 # write only every x-th point\n",
"\n",
"# setup the shapefile\n",
"with fiona.open('date_points.shp',\n",
" mode='w',\n",
" driver='ESRI Shapefile',\n",
" crs=from_epsg(4326),\n",
" schema={'geometry': 'Point', 'properties': props}) as c:\n",
" \n",
" # loop over the positions\n",
" for target_dt, tle, lon, lat, alt in positions[::write_every]:\n",
" \n",
" # create the polygon\n",
" geom = geometry.Point(lon, lat, alt) \n",
" \n",
" # set the attributes\n",
" properties = {'timestamp': target_dt.strftime('%Y-%m-%d %H:%M')}\n",
" \n",
" # write the record to the shapefile\n",
" c.write({'geometry': geometry.mapping(geom),\n",
" 'properties': properties})\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create a line shape with the Orbit"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# the shapefile attributes\n",
"props = {}\n",
"\n",
"# setup the shapefile\n",
"with fiona.open('orbit_line.shp',\n",
" mode='w',\n",
" driver='ESRI Shapefile',\n",
" crs=from_epsg(4326),\n",
" schema={'geometry': 'LineString', 'properties': props}) as c:\n",
"\n",
" # create the line \n",
" geom = geometry.LineString(list(zip(*list(zip(*positions))[2:5])))\n",
" \n",
" # set the attributes\n",
" properties = {}\n",
" \n",
" # write the record to the shapefile\n",
" c.write({'geometry': geometry.mapping(geom),\n",
" 'properties': properties})\n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment