import numpy as np
import datetime
import re
def parse_ionex(file_path):
with open(file_path, ‘r’) as file:
lines = file.readlines()
header_end = 0
for i, line in enumerate(lines):
if “END OF HEADER” in line:
header_end = i + 1
break
ionex_data = lines[header_end:]
tec_maps = []
current_map = []
current_time = None
lat_lon_grid = None
lat_lon_start = False
tec_values = []
for line_number, line in enumerate(ionex_data):
line = line.strip()
if not line:
continue
if “START OF TEC MAP” in line:
current_map = []
tec_values = []
elif “END OF TEC MAP” in line:
if current_time and tec_values:
try:
tec_map = np.array(tec_values).reshape(grid_lat_count, grid_lon_count)
tec_maps.append((current_time, tec_map))
except ValueError as e:
print(f"Error converting tec_values to array: {e}“)
elif “EPOCH OF CURRENT MAP” in line:
parts = re.split(r’\s+', line)
if len(parts) >= 6:
try:
year, month, day, hour, minute, second = map(int, parts[:6])
current_time = datetime.datetime(year, month, day, hour, minute, second)
except ValueError as e:
print(f"Error parsing date: {e}”)
continue
elif “LAT/LON1/LON2/DLON/H” in line:
parts = re.split(r’\s+‘, line)
if len(parts) >= 5:
try:
lat_start = float(parts[0])
lon_start = float(parts[1])
lon_end = float(parts[2])
dlon = float(parts[3])
H = int(parts[4]) # not used, could be vertical height step
grid_lon_count = int((lon_end - lon_start) / dlon) + 1
lat_lon_grid = (lat_start, lon_start, lon_end, dlon)
lat_lon_start = True
except ValueError as e:
print(f"Skipping line {line_number + header_end} due to error: {e}")
elif “END OF FILE” in line:
break
elif lat_lon_start:
# Extract TEC values
tec_values.extend(list(map(float, re.findall(r’[-+]?\d*.\d+|\d+‘, line))))
return tec_maps, lat_lon_grid
def interpolate_tec(tec_maps, lat_lon_grid, lat, lon, height, elevation_cutoff):
if not lat_lon_grid:
raise ValueError(“Latitude/Longitude grid information is missing.”)
lat_start, lon_start, lon_end, dlon = lat_lon_grid
lat_idx = int((lat - lat_start) / 2.5)
lon_idx = int((lon - lon_start) / dlon)
closest_map = min(tec_maps, key=lambda x: abs(x[0] - datetime.datetime.utcnow()))
tec_value = closest_map[1][lat_idx, lon_idx]
if tec_value >= elevation_cutoff:
return tec_value
else:
return None
Define the coordinates and parameters
latitude = 39.764214
longitude = 30.529289
height = 300 # meters
elevation_cutoff = 10 # degrees
Load and parse the IONEX file
ionex_file = ‘c1pg1580.24i’
tec_maps, lat_lon_grid = parse_ionex(ionex_file)
if lat_lon_grid:
# Interpolate the TEC value at the specified coordinates
tec_value = interpolate_tec(tec_maps, lat_lon_grid, latitude, longitude, height, elevation_cutoff)
if tec_value is not None:
print(f’TEC Value at ({latitude}, {longitude}): {tec_value}’)
else:
print(f’TEC Value at ({latitude}, {longitude}) is below the elevation cutoff.')
else:
print(“Latitude/Longitude grid information could not be determined.”)
def interpolate_tec(tec_maps, lat_lon_grid, lat, lon, height, elevation_cutoff):
if not lat_lon_grid:
raise ValueError("Latitude/Longitude grid information is missing.")
lat_start, lon_start, lon_end, dlon = lat_lon_grid
lat_idx = int((lat - lat_start) / 2.5)
lon_idx = int((lon - lon_start) / dlon)
closest_map = min(tec_maps, key=lambda x: abs(x[0] - datetime.datetime.utcnow()))
tec_value = closest_map[1][lat_idx, lon_idx]
if tec_value >= elevation_cutoff:
return tec_value
else:
return None
Define the coordinates and parameters
latitude = 39.764214
longitude = 30.529289
height = 300 # meters
elevation_cutoff = 10 # degrees
Load and parse the IONEX file
ionex_file = 'c1pg1580.24i'
tec_maps, lat_lon_grid = parse_ionex(ionex_file)
if lat_lon_grid:
# Interpolate the TEC value at the specified coordinates
tec_value = interpolate_tec(tec_maps, lat_lon_grid, latitude, longitude, height, elevation_cutoff)
if tec_value is not None:
print(f'TEC Value at ({latitude}, {longitude}): {tec_value}')
else:
print(f'TEC Value at ({latitude}, {longitude}) is below the elevation cutoff.')
else:
print("Latitude/Longitude grid information could not be determined.")
error :
File "C:\Users\pc\Desktop\ionexflask\deneme.py", line 6
with open(file_path, ‘r’) as file:
^
SyntaxError: invalid character '‘' (U+2018)