This Python script calculates civil twilight, sunrise, sunset, and solar noon (meridian) times for a list of geographic locations provided in a CSV file. The output includes local timezone information and UTC offsets for each location, ensuring precise timing based on coordinates.
- Calculates civil dawn, sunrise, meridian (solar noon), sunset, and civil dusk.
- Adjusts times to the local timezone of each location.
- Outputs the timezone name and UTC offset for clarity.
- Processes multiple dates between a specified start and end date.
The script expects the input CSV file to have the following columns:
Column Name | Description |
---|---|
X |
Longitude of the location (decimal degrees). |
Y |
Latitude of the location (decimal degrees). |
name |
Name of the location (e.g., "San Francisco"). |
@type |
Type of the place (e.g., city, village, hamlet). |
Additional Columns | Other metadata, not used in calculations. |
X,Y,id,@id,@type,capital,ele,name,place,population,short_name,website,wikidata,wikipedia
-122.4193286,37.7792588,node/26819236,"26819236",node,"6",,San Francisco,city,"873965",SF,,Q62,en:San Francisco
-120.01296,38.8560185,node/141045122,"141045122",node,,"1936",Meyers,village,"2163",,,Q6826467,"en:Meyers, California"
-110.1497981,37.6351618,node/150934572,"150934572",node,,"1638",Fry Canyon,hamlet,,,,,
X
(Longitude): Geographic longitude of the location.Y
(Latitude): Geographic latitude of the location.name
: Location name (for reference in output).
Other columns are optional and can contain metadata.
The script generates a CSV file with the following columns:
Column Name | Description |
---|---|
date |
Date of the calculation (YYYY-MM-DD ). |
civil_dawn |
Time of civil dawn (local timezone, ISO 8601 format). |
sunrise |
Time of sunrise (local timezone, ISO 8601 format). |
meridian |
Time of solar noon/meridian (local timezone, ISO 8601 format). |
sunset |
Time of sunset (local timezone, ISO 8601 format). |
civil_dusk |
Time of civil dusk (local timezone, ISO 8601 format). |
timezone |
Local timezone identifier (e.g., America/Los_Angeles ). |
offset |
UTC offset in hours (e.g., -7.0 for PDT). |
name |
Name of the location (as provided in input). |
-
Install Python Libraries:
pip install pandas astral timezonefinder pytz
-
Prepare Input File:
- Ensure your CSV file follows the described format.
- Update the script with the correct file path for the input and output files.
-
Run the Script: Execute the script in your Python environment:
python solar_event_calculator.py
-
Review Output: The results will be saved as a CSV file containing calculated solar event times.
Input data example (CSV):
X,Y,name
-122.4193286,37.7792588,San Francisco
-120.01296,38.8560185,Meyers
-110.1497981,37.6351618,Fry Canyon
Output example (CSV):
date,civil_dawn,sunrise,meridian,sunset,civil_dusk,timezone,offset,name
2025-05-15,2025-05-15T05:34:00-07:00,2025-05-15T06:04:00-07:00,2025-05-15T12:45:00-07:00,2025-05-15T19:26:00-07:00,2025-05-15T19:56:00-07:00,America/Los_Angeles,-7.0,San Francisco
- The script handles timezones dynamically using the
timezonefinder
library. - Locations with invalid or missing timezones are skipped with a warning in the console.
- Civil twilight is calculated based on astronomical definitions, as described in the requirements.
This project is open-source and available under the MIT License.