I have a script that converts a part of an nc file to a csv file. The script itself works, but the problem is that I would need to specify the exact directory including the name of the file and output csv. I am interested in running the script for all nc files from folder test1 and converting it to csv's in folder test2 with the same name. I attempted modifying the script but it hasn't worked. Here is my script.
import netCDF4
from netCDF4 import num2date, date2num, date2index
import pandas as pd
import numpy as np
import netCDF4
import sys
import os
path = r"C:\\Users\\chz08006\\Documents\\test1"
for filename in os.listdir(path):
netcdf_file = r"C:\\Users\\chz08006\\Documents\\test1\\"+filename
csv_file = r"C:\\Users\\chz08006\\Documents\\test2\\"+filename
f = netCDF4.Dataset(netcdf_file)
ssha = f.variables["ssha"]
lon = f.variables['lon']
lat = f.variables['lat']
#time = f.variables['time']
timedim = ssha.dimensions[0]
times = f.variables[timedim]
dates = num2date(times[:], times.units)
dates = [date.strftime('%Y-%m-%d %H:%M:%S') for date in dates]
lon_list= list(lon)
lat_list = list(lat)
ssha_list = list(ssha)
lon_list = [x-360 if x>= 180 else x for x in lon_list]
df = pd.DataFrame({'Time':dates,'Longitude':lon_list,'Latitude':lat_list,'SSHA':ssha_list})
df.to_csv(csv_file)
My failed attempt at modifying the script was
path = r"C:\\Users\\chz08006\\Documents\\test1"
for filename in os.listdir(path):
netcdf_file = r"C:\\Users\\chz08006\\Documents\\test1\\"+filename
csv_file = r"C:\\Users\\chz08006\\Documents\\test2\\"+filename
Previously, it would have been
netcdf_file = r"C:\\Users\\chz08006\\Documents\\test1\\example1.nc"
csv_file = r"C:\\Users\\chz08006\\Documents\\test2\\exampleresult.csv"
where example1 was the nc file name and exampleresult would be the csv name.