solresol / solar-panel-astronomy

Doing lunar astronomy using domestic solar panels
The Unlicense
0 stars 0 forks source link

Sweep: Create moonpos_catchup.py #10

Closed solresol closed 8 months ago

solresol commented 8 months ago

Details

Create moonpos_catchup.py. This program queries the postgresql database and looks at the missing_moonpositions view to get dates where we don't have a moon position caclulcated. It then uses this function from moonpos.py on each of those dates...

def calculate_moon_position_and_phase(date_time, latitude, longitude):

which does return moon.alt, moon.az, moon.phase

It then populates the database table moon_position

Here is some psql output so that you know the column names:

power=# \d moon_position
                             Table "public.moon_position"
     Column      |           Type           | Collation | Nullable |      Default      
-----------------+--------------------------+-----------+----------+-------------------
 when_recorded   | timestamp with time zone |           |          | CURRENT_TIMESTAMP
 altitude        | double precision         |           |          | 
 azimuth         | double precision         |           |          | 
 phase           | double precision         |           |          | 

power=# \d missing_moonpositions 
                        View "public.missing_moonpositions"
        Column         |           Type           | Collation | Nullable | Default 
-----------------------+--------------------------+-----------+----------+---------
 when_recorded_rounded | timestamp with time zone |           |          | 

Here is the catchup_sunpos.py program that I wrote earlier. catchup_moonpos.py will be very similar:

#!/usr/bin/env python3

import argparse
import requests
import configparser
import sunpos
import psycopg2

parser = argparse.ArgumentParser()
parser.add_argument("--config", default='config.ini')
parser.add_argument("--progress", action="store_true")
parser.add_argument("--skip-refresh", action="store_true",
                    help="Skip running a time-consuming refresh materialized view for electricity production data")
args = parser.parse_args()

c = configparser.ConfigParser()
c.read(args.config)
api_key = c['openweather']['api_key']
latitude = c['location']['latitude']
longitude = c['location']['longitude']
dbname = c['database']['dbname']
user = c['database']['user']
password = c['database']['password']
host = c['database']['host']
port = c['database']['port']
conn = psycopg2.connect(f'dbname={dbname} user={user} password={password} host={host} port={port}')
write_cursor = conn.cursor()
read_cursor = conn.cursor()

if not args.skip_refresh:
    write_cursor.execute("refresh materialized view production_rounded_off")
    conn.commit()

read_cursor.execute("select when_recorded_rounded AT TIME ZONE 'gmt' as gmt_when, when_recorded_rounded from missing_sunpositions order by 1")

location = (float(latitude), float(longitude))
iterator = read_cursor
if args.progress:
    import tqdm
    iterator = tqdm.tqdm(iterator, total=read_cursor.rowcount)
for row in iterator:
    gmt_when = row[0]
    when_recorded = row[1]
    azimuth, elevation = sunpos.sunpos(
        (gmt_when.year, gmt_when.month, gmt_when.day, gmt_when.hour, gmt_when.minute, gmt_when.second, 0),
        location, refraction=True)
    write_cursor.execute("insert into sun_position (when_recorded, azimuth, elevation) values (%s, %s, %s)", [when_recorded, azimuth, elevation])
    conn.commit()
Checklist - [X] Create `moonpos_catchup.py` ✓ https://github.com/solresol/lunar-astronomy/commit/bb0b7bc64352f4010a3b507eac37763862a873c0 [Edit](https://github.com/solresol/lunar-astronomy/edit/sweep/create_moonpos_catchuppy/moonpos_catchup.py)
ifost-autodev[bot] commented 8 months ago

🚀 Here's the PR! #11

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: None)

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)

GitHub Actions failed

The sandbox appears to be unavailable or down.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/solresol/lunar-astronomy/blob/7aef02d7323b18c1b93f447794a10f8076b67634/moonpos.py#L6-L12 https://github.com/solresol/lunar-astronomy/blob/7aef02d7323b18c1b93f447794a10f8076b67634/lunar_schema.sql#L11-L14 https://github.com/solresol/lunar-astronomy/blob/7aef02d7323b18c1b93f447794a10f8076b67634/requirements.txt#L1-L0

Step 2: ⌨️ Coding


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/create_moonpos_catchuppy.


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord