|
| 1 | +from vedastro import * # install via pip |
| 2 | +import datetime |
| 3 | +import pandas as pd # Install via pip if not already installed |
| 4 | +import json |
| 5 | + |
| 6 | +# PART 0 : Set API key |
| 7 | +Calculate.SetAPIKey('FreeAPIUser') # unlimited use API key from "vedastro.org/Account" |
| 8 | + |
| 9 | +# PART 1 : PREPARE NEEDED DATA |
| 10 | +# ----------------------------------- |
| 11 | + |
| 12 | +# Set birth location |
| 13 | +geolocation = GeoLocation("Tokyo, Japan", 139.83, 35.65) |
| 14 | + |
| 15 | +# Initial birth time string |
| 16 | +birth_time_str = "23:40 31/12/2010 +08:00" |
| 17 | + |
| 18 | +# Parse the initial birth time string into a datetime object |
| 19 | +birth_datetime = datetime.datetime.strptime(birth_time_str, "%H:%M %d/%m/%Y %z") |
| 20 | + |
| 21 | +# PART 2 : GENERATE TIME INSTANCES AND CALCULATE DATA |
| 22 | +# ----------------------------------- |
| 23 | + |
| 24 | +# List to store all results |
| 25 | +results = [] |
| 26 | + |
| 27 | +# Define the planet for which data is to be calculated |
| 28 | +planet = PlanetName.Sun # You can change this to other planets as needed |
| 29 | + |
| 30 | +# Loop to create 100 time instances, incrementing by 1 hour each |
| 31 | +for i in range(10): |
| 32 | + try: |
| 33 | + # Increment the time by 'i' hours |
| 34 | + current_datetime = birth_datetime + datetime.timedelta(hours=i) |
| 35 | + |
| 36 | + # Format the new datetime back to the string format expected by Vedastro's Time class |
| 37 | + current_time_str = current_datetime.strftime("%H:%M %d/%m/%Y %z") |
| 38 | + |
| 39 | + # Create a Time object with the new time |
| 40 | + current_time = Time(current_time_str, geolocation) |
| 41 | + |
| 42 | + # Calculate all planet data for the specified planet at the current time |
| 43 | + planet_data = Calculate.AllPlanetData(planet, current_time) |
| 44 | + |
| 45 | + # Flatten the planet_data if it's a nested dictionary |
| 46 | + # This step depends on the actual structure of planet_data |
| 47 | + # For demonstration, let's assume it's a flat dictionary |
| 48 | + flat_data = { |
| 49 | + "time": current_time_str, |
| 50 | + "planet": planet.value # Assuming PlanetName is an Enum |
| 51 | + } |
| 52 | + |
| 53 | + # Update flat_data with planet_data |
| 54 | + if isinstance(planet_data, dict): |
| 55 | + for key, value in planet_data.items(): |
| 56 | + # Handle nested dictionaries if necessary |
| 57 | + if isinstance(value, dict): |
| 58 | + for sub_key, sub_value in value.items(): |
| 59 | + flat_data[f"{key}_{sub_key}"] = sub_value |
| 60 | + else: |
| 61 | + flat_data[key] = value |
| 62 | + else: |
| 63 | + # If planet_data is not a dict, store it as a string |
| 64 | + flat_data["planet_data"] = str(planet_data) |
| 65 | + |
| 66 | + # Append the flattened data to the results list |
| 67 | + results.append(flat_data) |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + print(f"Error at iteration {i}: {e}") |
| 71 | + # Optionally, you can decide to continue or break the loop based on the error |
| 72 | + continue |
| 73 | + |
| 74 | +# PART 3 : SAVE THE RESULTS TO A CSV FILE |
| 75 | +# ----------------------------------- |
| 76 | + |
| 77 | +# Convert the results list to a pandas DataFrame |
| 78 | +df = pd.DataFrame(results) |
| 79 | + |
| 80 | +# Optional: Reorder columns if necessary |
| 81 | +# For example, place 'time' and 'planet' first |
| 82 | +cols = ['time', 'planet'] + [col for col in df.columns if col not in ['time', 'planet']] |
| 83 | +df = df[cols] |
| 84 | + |
| 85 | +# Save the DataFrame to a CSV file |
| 86 | +csv_filename = 'planet_data_results.csv' |
| 87 | +df.to_csv(csv_filename, index=False) |
| 88 | + |
| 89 | +print(f"Data successfully saved to {csv_filename}") |
0 commit comments