#This short program converts PACE/OCI data from Netcdf to Envi in BSQ format.
#Once converted, go to ENVI and open it as "Image" file, and then provide the 
#information on #samples, scanlines, and bands (you can use ncinfo to find out 
#from the orginal Netcdf file.
#You will also need to change the input and output file names in this file.

#This program was generated by ChatGPT (Provided by C. Cao on 4/26/2024)


import netCDF4 as nc

# Open the netCDF file
with nc.Dataset('PACE_OCI.20240417T134339.L1B.nc', 'r') as nc_file:
    # Read the variables from the 'observation_data' group
    rhot_blue = nc_file['observation_data']['rhot_blue'][:]
    rhot_red = nc_file['observation_data']['rhot_red'][:]

# Combine both variables into a single array
combined_data = {
    'rhot_blue': rhot_blue,
    'rhot_red': rhot_red
}

# Write the combined data into a binary file
with open('pace2envi.bin', 'wb') as bin_file:
    for var_name, data in combined_data.items():
        # Convert the data to bytes and write to the binary file
        data_bytes = data.astype('float32').tobytes()
        bin_file.write(data_bytes)

print("Data has been written to pace2envi.bin")

# Close the netCDF file
nc_file.close()

