## Process CFSv2 Wind NetCDF Data
# AJMR: December 2022

import xarray as xr
import netCDF4 as nc
import numpy as np
import os
import matplotlib.pyplot as plt
from datetime import datetime

#%% Setup paths 
cdfv2Paths = os.listdir('C:/Users/arey/Downloads/wnd10m.cdas1.201104-gdas.201103grb2.nc/')

# Add path to each file
cdfv2Paths = ['C:/Users/arey/Downloads/wnd10m.cdas1.201104-gdas.201103grb2.nc/' + i for i in cdfv2Paths]

#%% Set up multiple file dataset
cfsv2_mf = nc.MFDataset(cdfv2Paths)

#%% Read in NC data
cfsv2_windU = cfsv2_mf.variables['U_GRD_L103']
cfsv2_windV = cfsv2_mf.variables['V_GRD_L103']

#%% Process time from NetCDF file
nctimes = cfsv2_mf.variables['valid_date_time'][:] # get values
# Convert numpy char array to list of strings
nctimesStr = [''.join(row) for row in nctimes.astype(str)]

ncDatetime = [datetime.strptime(date, "%Y%m%d%H") for date in nctimesStr]

#%% Plot

# Set up figure
fig, ax = plt.subplots(1, 1, figsize=(10, 10))

# Plot U and V wind speeds
# ax.plot(ncDatetime, np.sqrt(cfsv2_windU[:, 0, 0]**2 + cfsv2_windV[:, 0, 0]**2), label='CFSv2 Wind Magnitude')
ax.plot(ncDatetime)


plt.show()




