-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessier_object_data_handler.py
297 lines (250 loc) · 12.1 KB
/
messier_object_data_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# messier_object_data_handler.py
from astropy.coordinates import SkyCoord
import astropy.units as u
import pandas as pd
import numpy as np
from messier_catalog import messier_catalog, star_cluster_catalog, get_nebulae, get_star_clusters
from star_notes import unique_notes
class MessierObjectHandler:
"""Handles all Messier object related operations."""
def __init__(self):
"""Initialize with both nebulae and star cluster catalogs."""
self.nebulae = get_nebulae()
self.clusters = get_star_clusters()
def get_visible_objects(self, mag_limit, object_type=None):
"""
Get Messier objects visible up to specified magnitude limit.
Parameters:
mag_limit (float): Maximum apparent magnitude to include
object_type (str, optional): Filter by object type
('Nebula', 'Cluster', 'Planetary', 'Emission', etc.)
Returns:
list: List of visible objects matching criteria
"""
visible_objects = []
print(f"\nFiltering Messier objects to magnitude {mag_limit}")
# Process both catalogs
for catalog in [self.nebulae, self.clusters]:
for messier_id, data in catalog.items():
if data['vmag'] <= mag_limit:
if object_type is None or object_type.lower() in data['type'].lower():
obj_data = {
'messier_id': messier_id,
'name': data['name'],
'type': data['type'],
'vmag': data['vmag'],
'distance_ly': data['distance_ly'],
'ra': data['ra'],
'dec': data['dec'],
'notes': data.get('notes', ''),
'size': data.get('size', None),
'age': data.get('age', None),
'parent_constellation': data.get('constellation', None)
}
visible_objects.append(obj_data)
print(f"Found {len(visible_objects)} visible Messier objects")
if object_type:
print(f"(Filtered to type: {object_type})")
return visible_objects
def calculate_3d_coordinates(self, objects):
"""
Calculate x, y, z coordinates for Messier objects.
Parameters:
objects (list): List of Messier objects with ra, dec, and distance
Returns:
list: Objects with added x, y, z coordinates in light-years
"""
for obj in objects:
try:
# Parse coordinates
coords = SkyCoord(obj['ra'], obj['dec'], unit=(u.hourangle, u.deg))
distance_pc = obj['distance_ly'] / 3.26156
# Create 3D coordinates
coord_with_dist = SkyCoord(
ra=coords.ra,
dec=coords.dec,
distance=distance_pc * u.pc
)
# Convert to light-years
obj['x'] = coord_with_dist.cartesian.x.value * 3.26156
obj['y'] = coord_with_dist.cartesian.y.value * 3.26156
obj['z'] = coord_with_dist.cartesian.z.value * 3.26156
except Exception as e:
print(f"Error calculating coordinates for {obj['messier_id']}: {e}")
obj['x'] = obj['y'] = obj['z'] = np.nan
return objects
def create_dataframe(self, objects):
"""Convert Messier objects to DataFrame format compatible with stellar data."""
if not objects:
return pd.DataFrame()
# Calculate coordinates for each object
print("\nCalculating coordinates for Messier objects...")
for obj in objects:
try:
# Parse coordinates
coords = SkyCoord(obj['ra'], obj['dec'], unit=(u.hourangle, u.deg))
distance_pc = obj['distance_ly'] / 3.26156
# Create 3D coordinates
coord_with_dist = SkyCoord(
ra=coords.ra,
dec=coords.dec,
distance=distance_pc * u.pc
)
# Convert to light-years
obj['x'] = coord_with_dist.cartesian.x.value * 3.26156
obj['y'] = coord_with_dist.cartesian.y.value * 3.26156
obj['z'] = coord_with_dist.cartesian.z.value * 3.26156
print(f" {obj['messier_id']}: Calculated coordinates ({obj['x']:.1f}, {obj['y']:.1f}, {obj['z']:.1f}) ly")
except Exception as e:
print(f"Error calculating coordinates for {obj['messier_id']}: {e}")
obj['x'] = obj['y'] = obj['z'] = np.nan
# Create DataFrame
df = pd.DataFrame(objects)
# Add required columns to match stellar data format
df['Star_Name'] = df.apply(lambda row: f"{row['messier_id']}: {row['name']}", axis=1)
df['Source_Catalog'] = 'Messier'
df['Apparent_Magnitude'] = df['vmag']
df['Distance_pc'] = df['distance_ly'] / 3.26156
df['Distance_ly'] = df['distance_ly']
df['Object_Type'] = df['type']
df['Object_Type_Desc'] = df.apply(self._create_type_description, axis=1)
# Add null values for stellar-specific columns
df['Temperature'] = np.nan
df['Luminosity'] = np.nan
df['B_V'] = np.nan
df['Spectral_Type'] = None
df['Temperature_Normalized'] = 0.5 # Middle value for color scale
# Add visualization properties
df['Marker_Size'] = 20 # Fixed larger size for Messier objects
# Create hover texts
df['Hover_Text'] = df.apply(
lambda row: (
f"<b>{row['messier_id']}: {row['name']}</b><br>"
f"Type: {row['type']}<br>"
f"Apparent Magnitude: {row['Apparent_Magnitude']:.1f}<br>"
f"Distance: {row['Distance_ly']:.1f} ly<br>"
f"Position: ({row['x']:.1f}, {row['y']:.1f}, {row['z']:.1f}) ly<br>"
f"{unique_notes.get(row['messier_id'], 'None')}<br>" # Use unique_notes with messier_id
),
axis=1
)
df['Min_Hover_Text'] = df.apply(lambda row: f"<b>{row['messier_id']}</b>", axis=1)
print(f"\nProcessed {len(df)} Messier objects with columns:")
print(df.columns.tolist())
return df
def _calculate_marker_size(self, vmag):
"""Calculate marker sizes based on apparent magnitude."""
def calc_size(mag):
if pd.isna(mag):
return 20 # Default size for Messier objects
mag_min, mag_max = -1.44, 9.0
size_min, size_max = 2, 24
mag_clipped = np.clip(mag, mag_min, mag_max)
log_brightness = -0.4 * mag_clipped
log_brightness_min = -0.4 * mag_max
log_brightness_max = -0.4 * mag_min
normalized_brightness = (log_brightness - log_brightness_min) / (log_brightness_max - log_brightness_min)
return np.clip(size_min + (size_max - size_min) * normalized_brightness, size_min, size_max)
return vmag.apply(calc_size)
def _create_hover_text(self, row):
"""Create hover text for a Messier object."""
text = [
f"<b>{row['messier_id']}: {row['name']}</b>",
f"Type: {row['type']}",
f"Apparent Magnitude: {row['vmag']:.1f}",
f"Distance: {row['distance_ly']:.1f} light-years",
f"Position: ({row['x']:.1f}, {row['y']:.1f}, {row['z']:.1f}) ly"
f"{unique_notes.get(row['messier_id'], 'None')}<br>" # Use unique_notes with messier_id
]
if 'size' in row and pd.notna(row['size']):
text.append(f"Size: {row['size']}")
if 'age' in row and pd.notna(row['age']):
text.append(f"Age: {row['age']}")
if 'notes' in row and pd.notna(row['notes']):
text.append(f"Notes: {row['notes']}")
return '<br>'.join(text)
def _create_type_description(self, row):
"""Create detailed type description."""
desc = row['type']
if row.get('age'):
desc += f", Age: {row['age']}"
if row.get('size'):
desc += f", Size: {row['size']}"
return desc
def _get_marker_symbol(self, obj_type):
"""
Get appropriate marker symbol based on object type.
Uses only symbols available in Plotly's Scatter3d:
- 'circle'
- 'square'
- 'diamond'
- 'cross'
- 'x'
- 'triangle-up'
- 'triangle-down'
"""
if 'Nebula' in obj_type:
return 'diamond' # Compatible with Scatter3d
elif 'HII Region' in obj_type:
return 'square' # Changed from 'diamond' for distinction
elif 'Cluster' in obj_type:
return 'triangle-up' # Changed from 'cross' for better visibility
elif 'Planetary' in obj_type:
return 'circle' # Specific symbol for planetary nebulae
elif 'Supernova' in obj_type:
return 'cross' # Specific symbol for supernova remnants
return 'diamond' # Default symbol
def _get_marker_color(self, obj_type):
"""Get appropriate color based on object type."""
if 'Emission' in obj_type or 'HII Region' in obj_type:
return 'red'
elif 'Planetary' in obj_type:
return 'green'
elif 'Reflection' in obj_type:
return 'blue'
elif 'Cluster' in obj_type:
return 'yellow'
return 'white'
def get_object_info(self, messier_id):
"""Get detailed information for a specific Messier object."""
obj = self.nebulae.get(messier_id) or self.clusters.get(messier_id)
if not obj:
return "Object not found in catalog"
info = [
f"{messier_id}",
f"Name: {obj['name']}",
f"Type: {obj['type']}",
f"Apparent Magnitude: {obj['vmag']}",
f"Distance: {obj['distance_ly']} light-years"
]
if 'size' in obj:
info.append(f"Size: {obj['size']}")
if 'age' in obj:
info.append(f"Age: {obj['age']}")
if 'constellation' in obj:
info.append(f"Constellation: {obj['constellation']}")
if 'notes' in obj:
info.append(f"Notes: {obj['notes']}")
return '\n'.join(info)
def analyze_catalog(self):
"""Analyze the catalog contents."""
total_objects = len(self.nebulae) + len(self.clusters)
type_counts = {}
mag_distribution = []
for obj in list(self.nebulae.values()) + list(self.clusters.values()):
# Count object types
obj_type = obj['type']
type_counts[obj_type] = type_counts.get(obj_type, 0) + 1
# Collect magnitudes
mag_distribution.append(obj['vmag'])
print("\nMessier Catalog Analysis")
print("=" * 50)
print(f"Total Objects: {total_objects}")
print("\nObject Types:")
for obj_type, count in sorted(type_counts.items()):
print(f" {obj_type}: {count}")
if mag_distribution:
print("\nMagnitude Statistics:")
print(f" Brightest: {min(mag_distribution):.1f}")
print(f" Faintest: {max(mag_distribution):.1f}")
print(f" Average: {np.mean(mag_distribution):.1f}")