Skip to content

Commit ccc6ac5

Browse files
committed
Widespread changes, bugfixes; now supporting backfilling of missing datetime values
1 parent 180de48 commit ccc6ac5

File tree

5 files changed

+499
-203
lines changed

5 files changed

+499
-203
lines changed

README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,38 @@ Your input file should have a format similiar to those under `test_data`. To hav
66

77
If working with a database, the database access information/credentials must be setup in `db_creds.py`.
88

9+
The `cycle_start` `talker`+`sentence_type`, e.g. `GNRMC`, passed to `datetime_stamp_sentences()` and `assign_cycle_ids()`, must appear once and only once in each cycle, and it must be at the beginning of each cycle. For sentences to be datetime stamped, `cycle_start` sentences must contain date and time information. The `--backfill_datetimes` flag can be used to back fill datetimes for cycles where that information was not avaiable.
10+
911

1012
## Usage
1113
```
12-
$ cd ~/Downloads/nmea_parser/
13-
$ pip install -r requirements.txt
14+
$ cd nmea_data_convert/
15+
$ pip install -r requirements.txt
1416
...
15-
$ python nmea_parser.py --help
16-
usage: nmea_parser.py [-h] [--drop_previous_db_tables] filepath {csv,db,both}
17+
$ python nmea_data_convert.py --help
18+
usage: nmea_data_convert.py [-h] [--drop_previous_db_tables]
19+
[--backfill_datetimes]
20+
filepath {csv,db,both}
1721
1822
positional arguments:
1923
filepath file system path to file containing NMEA data
2024
{csv,db,both} where to output data: CSV files, database, or both
2125
2226
optional arguments:
2327
-h, --help show this help message and exit
24-
--drop_previous_db_tables
28+
--drop_previous_db_tables, --dropt
2529
drop previous DB tables before importing new data;
2630
only applies when output_method is 'db' or 'both'
31+
--backfill_datetimes, --bfdt
32+
backfill datetimes where missing by extrapolating from
33+
messages with datetime information
2734
```
2835
## Examples
2936
### Example 1
3037
```
3138
$ ls -l *.csv
3239
ls: *.csv: No such file or directory
33-
$ python nmea_parser.py test_data/test_data_all.nmea csv
40+
$ python nmea_data_convert.py test_data/test_data_all.nmea csv
3441
3542
Reading in data... done.
3643
@@ -61,7 +68,7 @@ $ ls -l *.csv
6168

6269
### Example 2
6370
```
64-
$ python nmea_parser.py test_data/test_data_all.nmea db
71+
$ python nmea_data_convert.py test_data/test_data_all.nmea db
6572
6673
Reading in data... done.
6774
@@ -82,7 +89,7 @@ All done. Exiting.
8289

8390
### Example 3
8491
```
85-
$ python nmea_parser.py test_data/test_data_all.nmea both
92+
$ python nmea_data_convert.py test_data/test_data_all.nmea both --bfdt --dropt
8693
8794
Reading in data... done.
8895
@@ -98,6 +105,14 @@ Writing data to CSVs... data from logfile 'test_data/test_data_all.nmea' written
98105
test_data_all_GNGLL.csv
99106
done.
100107
108+
Dropping database table nmea_gl_gsv (and any dependent objects) if it exists.
109+
Dropping database table nmea_gn_gga (and any dependent objects) if it exists.
110+
Dropping database table nmea_gn_gll (and any dependent objects) if it exists.
111+
Dropping database table nmea_gn_gsa (and any dependent objects) if it exists.
112+
Dropping database table nmea_gn_rmc (and any dependent objects) if it exists.
113+
Dropping database table nmea_gn_vtg (and any dependent objects) if it exists.
114+
Dropping database table nmea_gp_gsv (and any dependent objects) if it exists.
115+
101116
Writing data to database... data from logfile 'test_data/test_data_all.nmea' written to:
102117
'nmea_gn_rmc' table in 'nmea_data' database
103118
'nmea_gn_vtg' table in 'nmea_data' database
@@ -119,4 +134,14 @@ https://www.trimble.com/OEM_ReceiverHelp/V4.44/en/NMEA-0183messages_MessageOverv
119134

120135
https://www.u-blox.com/sites/default/files/products/documents/u-blox8-M8_ReceiverDescrProtSpec_%28UBX-13003221%29.pdf (section 31 'NMEA Protocol')
121136

122-
https://www.sparkfun.com/datasheets/GPS/NMEA%20Reference%20Manual1.pdf
137+
https://www.sparkfun.com/datasheets/GPS/NMEA%20Reference%20Manual1.pdf
138+
139+
https://www.nmea.org/Assets/20190303%20nmea%200183%20talker%20identifier%20mnemonics.pdf
140+
141+
142+
## Support
143+
If you find this tool useful, please consider supporting development of this tool and other tools like it. You can do so using the `Sponsor` button at the top of the [GitHub page](https://github.com/Petrichor-Labs/nmea_data_convert).
144+
145+
146+
## Discussion
147+
For any questions, feedback, or other discussion items, please feel free to post in the [`Discussions` tab on the GitHub page](https://github.com/Petrichor-Labs/nmea_data_convert/discussions).

db_data_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def send_data_to_db(log_file_path, dfs, table_name_base, table_name_suffixes=Non
2929
table_name = table_name + '_' + table_name_suffixes[df_idx]
3030

3131
try:
32-
df.to_sql(table_name, engine, method='multi', if_exists=if_exists_opt_loc)
32+
df.to_sql(table_name, engine, method='multi', if_exists=if_exists_opt_loc, index=False)
3333
except (sqlalchemy.exc.OperationalError, psycopg2.OperationalError) as e:
3434
sys.exit(f"\n\n\033[1m\033[91mERROR writing to database:\n {e}\033[0m\n\nExiting.\n\n") # Print error text bold and red
3535

db_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import sys
12
import psycopg2
23
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
4+
import sqlalchemy
35
import functools
46
print = functools.partial(print, flush=True) # Prevent print statements from buffering till end of execution
57

@@ -61,8 +63,12 @@ def setup_db_connection():
6163

6264
db_access_str = f'postgresql://{db_creds.DB_USER}:{db_creds.DB_PASSWORD}@{db_creds.DB_HOST}:{db_creds.DB_PORT}/{db_creds.DB_NAME}'
6365

64-
# Start a PostgreSQL database session
65-
psqlCon = psycopg2.connect(db_access_str);
66+
# Start a PostgreSQL database session
67+
try:
68+
psqlCon = psycopg2.connect(db_access_str);
69+
except (sqlalchemy.exc.OperationalError, psycopg2.OperationalError) as e:
70+
sys.exit(f"\n\033[1m\033[91mERROR connecting to database:\n {e}\033[0m\n\nExiting.\n\n") # Print error text bold and red
71+
6672
psqlCon.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT);
6773

6874
# Open a database cursor

0 commit comments

Comments
 (0)