import csv import netCDF4 import os import sys import datetime import numpy as np # Check if the input filename is provided as a command-line argument if len(sys.argv) < 2: print("Please provide the input NetCDF filename as a command-line argument.") exit() # Get the input filename from the command-line argument input_filename = sys.argv[1] # Check if the input file exists if not os.path.isfile(input_filename): print("Input file does not exist:", input_filename) exit() # Create the output filename by changing the extension from .nc to .csv output_filename = os.path.splitext(input_filename)[0] + ".csv" # Open the NetCDF file dataset = netCDF4.Dataset(input_filename) # Get the variables of interest file_variable = dataset.variables["file"] lat_variable = dataset.variables["Latitude"] lon_variable = dataset.variables["Longitude"] optical_depth_variable = dataset.variables["Optical_Depth_Land_And_Ocean"] scan_time_variable = dataset.variables["Scan_Start_Time"] aer_type_variable = dataset.variables["Aerosol_Type_Land"] deepb_AOD_variable = dataset.variables["Deep_Blue_Aerosol_Optical_Depth_550_Land"] deepb_Angstrom_variable = dataset.variables["Deep_Blue_Angstrom_Exponent_Land"] deepb_AOD_variable = dataset.variables["AOD_550_Dark_Target_Deep_Blue_Combined"] # Get the dimensions of the file variable dim_file = file_variable.shape[0] # Dimension size of the file dimension # Create and open the CSV file in write mode with open(output_filename, "w", newline="") as csv_file: writer = csv.writer(csv_file) # Write the header row writer.writerow(["File", "Date", "Scan_Start_Time", "Latitude", "Longitude", "Optical_Depth_Land_And_Ocean", "Aerosol_type", "DeepBlue_AOD", "DeepBlue_angstrom", "AOD_Combined"]) # Loop over the file dimension indices for i in range(dim_file): # Extract the values at the given index file_value = file_variable[i] scan_time_value = scan_time_variable[i] lat_value = lat_variable[i] lon_value = lon_variable[i] optical_depth_value = optical_depth_variable[i] aer_type_value = aer_type_variable[i] deepb_AOD_value = deepb_AOD_variable[i] ddeepb_Angstrom_value = deepb_Angstrom_variable[i] deepb_AOD_value = deepb_AOD_variable[i] # Convert TAI Time to datetime tai_datetime = datetime.datetime(1993, 1, 1) + datetime.timedelta(seconds=int(scan_time_value)) # Extract year, month, day, hours, and minutes #year = tai_datetime.year #month = tai_datetime.month #ay = tai_datetime.day #ours = tai_datetime.hour #inutes = tai_datetime.minute # Write the values to the CSV file writer.writerow([file_value, tai_datetime, scan_time_value, lat_value, lon_value, optical_depth_value,aer_type_value,deepb_AOD_value,ddeepb_Angstrom_value,deepb_AOD_value]) # Close the NetCDF file dataset.close() print("Data has been saved to:", output_filename)