Skip to content

Commit

Permalink
updated README with quick start; made a script to do all calendars fo…
Browse files Browse the repository at this point in the history
…r 2024
  • Loading branch information
matt-the-ogre committed Jun 8, 2024
1 parent 66e3478 commit deaac4e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ venv/
ENV/
env.bak/
venv.bak/
tide-env/

# Spyder project settings
.spyderproject
Expand Down
22 changes: 22 additions & 0 deletions 2024-tides.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# print all months for 2024

# note: pcal and ps2pdf must be installed before running

python3 -m venv tide-env
source tide-env/bin/activate
pip install requests
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 1
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 2
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 3
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 4
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 5
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 6
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 7
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 8
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 9
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 10
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 11
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 12
deactivate
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,52 @@

A python program to retrieve tide data for a station, year, and month then create a calendar page (or pages) using `pcal`.

## Quick start

Install `pcal` (example uses brew on MacOS):

```bash
brew install pcal
```

Run this:

```bash
python3 -m venv tide-env
source tide-env/bin/activate
pip install requests
```

Then this, replacing the tide station, year, and month with your choices:

```bash
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 6
```

You'll see a new PDF file created in the same directory with the tide calendar.

After you're finished making calendars, run this to clean up:

```bash
deactivate
```

If you want tide calendars for June through December 2024, run this complete script:

```bash
python3 -m venv tide-env
source tide-env/bin/activate
pip install requests
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 6
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 7
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 8
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 9
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 10
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 11
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 12
deactivate
```

## Virtual environment

`python3 -m venv tide-env`
Expand Down Expand Up @@ -79,7 +125,7 @@ python get_tide_data.py
And if you're using command line arguments:

```bash
python get_tide_data.py --station_id 9449639 --year 2024 --month 6
python3 get_tide_data.py --station_id 9449639 --year 2024 --month 6
```

### Step 6: Deactivate the Virtual Environment
Expand Down
28 changes: 19 additions & 9 deletions get_tide_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
import calendar
import subprocess
import logging

# sample call: python get_tide_data.py --station_id 9449639 --year 2024 --month 6

Expand Down Expand Up @@ -35,8 +36,9 @@ def convert_tide_data_to_pcal(tide_data_filename, pcal_filename):
# Format the date for pcal (mm/dd)
pcal_date = f"{int(month)}/{int(day)}"

if prediction < 0.3:
# add an asterisk to the pcal_date if the tide is less than 1.0
if prediction < 1.0:
# add an asterisk to the pcal_date if the tide is less than 1.0 meter
# this indicates the day is special to pcal and it will be colour-coded
pcal_date += "*"

# Write the event to the pcal file
Expand Down Expand Up @@ -71,11 +73,14 @@ def download_tide_data(station_id, year, month):
filename = f"{station_id}_{year}_{month:02d}.csv"
with open(filename, 'wb') as file:
file.write(response.content)
print(f"Data successfully saved to {filename}")
logging.debug(f"Data successfully saved to {filename}")
else:
print(f"Failed to download data: {response.status_code}")
logging.error(f"Failed to download data: {response.status_code}")

if __name__ == "__main__":
# Set up logging
logging.basicConfig(level=logging.INFO)

parser = argparse.ArgumentParser(description="Download tide data as CSV.")
parser.add_argument('--station_id', type=str, default='9449639', help='Station ID (default: 9449639)')
parser.add_argument('--year', type=int, default=datetime.now().year, help='Year (default: current year)')
Expand All @@ -85,7 +90,7 @@ def download_tide_data(station_id, year, month):

# Ensure month is in the correct format
if args.month < 1 or args.month > 12:
print("Month must be between 1 and 12")
logging.error("Month must be between 1 and 12")
else:
download_tide_data(args.station_id, args.year, args.month)

Expand All @@ -96,7 +101,7 @@ def download_tide_data(station_id, year, month):

convert_tide_data_to_pcal(downloaded_filename, pcal_filename)

print(f"PCAL file created: {pcal_filename}")
logging.debug(f"PCAL file created: {pcal_filename}")

# now make a calendar page using `pcal` and the pcal file with the tide events for that month and year
# print("To create a calendar page with the tide events, run the following command:")
Expand All @@ -115,8 +120,13 @@ def download_tide_data(station_id, year, month):
# Convert the PostScript file to PDF
subprocess.run(["ps2pdf", pcal_filename.replace('.txt', '.ps'), pcal_filename.replace('.txt', '.pdf')])

# delete the PostScript file
subprocess.run(["rm", pcal_filename.replace('.txt', '.ps')])
# delete the CSV file
subprocess.run(["rm", downloaded_filename])
# delete the pcal file
subprocess.run(["rm", pcal_filename])

# Print a message indicating the PDF file creation
print(f"PDF file created: {pcal_filename.replace('.txt', '.pdf')}")
logging.info(f"PDF file created: {pcal_filename.replace('.txt', '.pdf')}")
# call the shell command to create the calendar page


0 comments on commit deaac4e

Please sign in to comment.