Skip to content

Instantly share code, notes, and snippets.

@hcarter333
Last active August 4, 2025 12:45
Show Gist options
  • Select an option

  • Save hcarter333/bd6363847baa570ef342dbd505e72336 to your computer and use it in GitHub Desktop.

Select an option

Save hcarter333/bd6363847baa570ef342dbd505e72336 to your computer and use it in GitHub Desktop.
import requests
from unlzw3 import unlzw
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
import os
def download_and_decompress(station, day=211, year=2025):
"""
Download a .25d.Z file from NCEDC and decompress using unlzw3.
"""
base_url = f"https://ncedc.org/gps/highrate/rinex/{year}/{year}.{day:03d}/"
filename = f"{station.lower()}{day:03d}0.25d.Z"
url = base_url + filename
compressed_path = filename
decompressed_path = compressed_path[:-2] # Remove .Z extension
print(f"Downloading {url} ...")
response = requests.get(url)
if response.status_code != 200:
raise RuntimeError(f"Download failed with status code {response.status_code}")
with open(compressed_path, 'wb') as f:
f.write(response.content)
# Decompress with unlzw3
with open(compressed_path, 'rb') as f_in:
compressed_data = f_in.read()
decompressed_data = unlzw(compressed_data)
with open(decompressed_path, 'wb') as f_out:
f_out.write(decompressed_data)
print(f"Decompressed to {decompressed_path}")
return decompressed_path
def parse_displacement_file(filepath):
"""
Parse the decompressed .25d file to extract time and ENU displacements.
"""
data = []
with open(filepath, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) >= 9:
try:
time_str = ' '.join(parts[:6])
dt = datetime.strptime(time_str, "%Y %m %d %H %M %S.%f")
east, north, up = map(float, parts[6:9])
data.append((dt, east, north, up))
except ValueError:
continue # skip malformed lines
df = pd.DataFrame(data, columns=['time', 'east', 'north', 'up'])
return df
def plot_displacements(df, station):
"""
Plot East, North, and Up displacement time series.
"""
plt.figure(figsize=(12, 8))
plt.subplot(3, 1, 1)
plt.plot(df['time'], df['east'], label='East', color='blue')
plt.ylabel("East (mm)")
plt.grid()
plt.subplot(3, 1, 2)
plt.plot(df['time'], df['north'], label='North', color='orange')
plt.ylabel("North (mm)")
plt.grid()
plt.subplot(3, 1, 3)
plt.plot(df['time'], df['up'], label='Up', color='green')
plt.ylabel("Up (mm)")
plt.xlabel("Time (UTC)")
plt.grid()
plt.suptitle(f"Displacement at {station.upper()} on 2025-07-30")
plt.tight_layout()
plt.show()
# === Main execution ===
if __name__ == "__main__":
station_id = "cmbb" # Change this to another station if desired
filepath = download_and_decompress(station_id)
df = parse_displacement_file(filepath)
plot_displacements(df, station_id)
# Optionally clean up
os.remove(filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment