Commit dcbd99e2 authored by Jonathan  Minz's avatar Jonathan Minz
Browse files

Add new file

parent d52c2c10
Loading
Loading
Loading
Loading
+95 −0
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Doppler LiDAR HPL to NetCDF Conversion

This notebook demonstrates the full pipeline for converting `.hpl` files from Halo Photonics Doppler LiDAR systems into CF-compliant NetCDF format.

%% Cell type:code id: tags:

``` python
# Install required packages (uncomment and run if needed)
# !pip install xarray netCDF4 numpy
```

%% Cell type:markdown id: tags:

## Step 1: Load and Parse HPL Header

%% Cell type:code id: tags:

``` python
def parse_hpl_header(lines):
    header = {}
    for line in lines:
        if line.strip() == "****":
            break
        if ":" in line:
            key, value = line.split(":", 1)
            header[key.strip()] = value.strip()
    return header

with open("Stare_115_20240423_00.hpl", "r") as file:
    lines = file.readlines()
header = parse_hpl_header(lines)
header
```

%% Cell type:markdown id: tags:

## Step 2: Convert Decimal Time to Epoch Seconds

%% Cell type:code id: tags:

``` python
import datetime
import numpy as np

base_date = datetime.datetime(2024, 4, 23)
decimal_time = 1.5  # Example: 1.5 hours
measured_time = base_date + datetime.timedelta(hours=decimal_time)
epoch_seconds = (measured_time - datetime.datetime(1970, 1, 1)).total_seconds()
epoch_seconds
```

%% Cell type:markdown id: tags:

## Step 3: Create and Populate xarray Dataset

%% Cell type:code id: tags:

``` python
import xarray as xr

# Dummy data
time = np.array([epoch_seconds])
range_gate = np.arange(10) * 30.0 + 15.0
doppler = np.random.randn(1, 10)

ds = xr.Dataset(
    data_vars={
        "doppler": ("time", "range", doppler)
    },
    coords={
        "time": time,
        "range": range_gate
    },
    attrs={"title": "Sample Doppler LiDAR Data"}
)

ds["doppler"].attrs = {
    "units": "m s-1",
    "long_name": "radial_velocity_of_scatterers_away_from_instrument",
    "standard_name": "radial_velocity_of_scatterers_away_from_instrument"
}
ds
```

%% Cell type:markdown id: tags:

## Step 4: Save as CF-Compliant NetCDF

%% Cell type:code id: tags:

``` python
ds.to_netcdf("sample_lidar_output.nc", format="NETCDF4", engine="netcdf4")
```