NEON Hourly Averages#
Diurnal Cycle of latent heat and components#
Alternate variables at different times of year
This activity provides examples for making a diel cycle of the components of latent heat at NEON sites. Work here follows results published in Figure 6 of Lombardozzi et al. 2023.
Author : Negin Sobhani @negin513
Modified: Will Wieder @wwieder
Last updated: 2026-11-10

Mean hourly latent heat flux at the NEON Abby Road site (ABBY) in Washington
Creating Diel Cycle Plots for NEON sites#
This notebook has the scripts for:
Reading Evaluation (NEON) and model (CLM) data for all neon sites
Making diurnal cycle plots for latent heat flux and components for all neon sites
Making diurnal cycle plots for all variables without the components.
BEFORE BEGINNING THIS EXERCISE - Check that your kernel (upper right corner, above) is NPL2026a. This should be the default kernel, but if it is not, click on that button and select NPL2026a.
NOTE: Results from a series of older CLM simulations and NEON observations are provided in /glade/campaign/cgd/tss/tutorials/neon_v3/ for students to use.
This is a more advanced plotting script.
Imports:#
import os
import time
import datetime
import numpy as np
import pandas as pd
import xarray as xr
from glob import glob
from os.path import join
from matplotlib import gridspec
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import calendar
import tqdm
import cftime
import neon_utils
import warnings
warnings.filterwarnings("ignore")
print('xarray '+xr.__version__) ##-- was working with 2025.12.0
Dask Cluster#
The following will spin up a dask cluster, which only works on NCAR machines. 16 workers worked best for NEON.
#from distributed import Client
#from ncar_jobqueue import NCARCluster
#cluster = NCARCluster()
#cluster.scale(16)
#client = Client(cluster)
#client
Useful Functions and Objects#
def diel_cycle_plot(diel_df_mean, diel_df_std, var, plot_dir,color1= '#e28743',color2='#1d657e',season = "Annual", save_switch=False):
# Plot annual average diel cycle and bias
bias_var_name = var.obs_var +'_bias'
diel_df_mean [bias_var_name] = diel_df_mean[var.sim_var]- diel_df_mean[var.obs_var]
fig= plt.figure(num=None, figsize=(17, 13), facecolor='w', edgecolor='k')
# -- subplot 1 : Diurnal average + standard dev as shaded regions
plt.subplot(2, 1, 1)
ax = plt.gca()
plt.plot ( diel_df_mean.local_hour, diel_df_mean[var.obs_var], marker = 'o' , color = color1,label="NEON")
plt.plot ( diel_df_mean.local_hour, diel_df_mean[var.sim_var] , marker = 'o' , color = color2,label="CTSM", linestyle = 'dashed')
plt.fill_between(diel_df_mean.local_hour, diel_df_mean[var.obs_var]-diel_df_std[var.obs_var], diel_df_mean[var.obs_var]+diel_df_std[var.obs_var] ,alpha=0.1, color = color1)
plt.fill_between(diel_df_mean.local_hour, diel_df_mean[var.sim_var]-diel_df_std[var.sim_var], diel_df_mean[var.sim_var]+diel_df_std[var.sim_var] ,alpha=0.1, color = color2)
ax.set_ylabel(var.long_name+" ["+var.unit+"]", fontsize=15)
# -- vertical line at noon
# plt.axvline(x=12, color = 'gray',linestyle='dashed',alpha=0.75)
if season in ["DJF", "MAM","JJA","SON"]:
plt.title("Diel Cycle of "+ var.long_name +"\n " +neon_site+" "+"[2018-2021] " +season , fontweight='bold',fontsize=15)
else:
plt.title("Diel Cycle of "+ var.long_name +"\n " +neon_site+" "+"[2018-2021]", fontweight='bold',fontsize=15)
# -- adjusting tick properties.
ax.tick_params(axis='both', which='both', labelsize=14,width=1,length=6)
ax.tick_params(axis='x',direction="in")
ax.yaxis.set_ticks_position('both')
ax.tick_params(axis='y',direction="out", length = 7)
# -- x-axis range and ticks
ax.set_xlim([-1,24])
ax.xaxis.set_ticks(np.arange(0, 24, 1))
# -- y axis label location
ax.get_yaxis().set_label_coords(-0.06,0.5)
# -- add legends
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(fontsize=15)
# -- remove tick labels for x axis
labels = [item.get_text() for item in ax.get_xticklabels()]
empty_string_labels = ['']*len(labels)
ax.set_xticklabels(empty_string_labels)
# ----------------------
# -- Subplot 2: Bias Plot
plt.subplot(4, 1, 3)
ax = plt.gca()
diel_df_mean.plot ( x= 'local_hour', y = bias_var_name , marker = 'o' ,ax =ax , color = 'black',label='Bias')
plt.xlabel('Local Time', fontsize=15)
plt.ylabel('Bias'+" ["+var.unit+"]", fontsize=15)
# -- horizontal line at 0
plt.axhline(y=0, color = 'gray',linestyle='dashed',alpha=0.75)
#-- vertical line at noon
#plt.axvline(x=12, color = 'gray',linestyle='dashed',alpha=0.75)
# -- Changing tick properties:
ax.tick_params(axis='both', which='both', labelsize=14,width=1,length=7)
ax.tick_params(axis='x',which ='both',direction="out")
ax.tick_params(axis='y',direction="out", length = 7)
ax.yaxis.set_ticks_position('both')
# -- x-axis range and ticks
ax.set_xlim([-1,24])
ax.xaxis.set_ticks(np.arange(0, 24, 1))
# -- changing x axis tick labels to custom labels
labels = diel_df_mean.local_hour_dt.dt.strftime("%H:%M")
ax.set_xticklabels(labels, rotation = 45)
# -- y axis label location
ax.get_yaxis().set_label_coords(-0.06,0.5)
# -- remove legend
ax.get_legend().remove()
fig.subplots_adjust(wspace=0, hspace=0)
if save_switch:
plot_name = neon_site+'_'+var.obs_var+'_composition_diel_cyle'+'_'+season+'.png'
plot_dir1 = os.path.join(plot_dir, 'png')
if not os.path.isdir(plot_dir1):
os.makedirs(plot_dir1, exist_ok=True)
print ('Saving '+ os.path.join(plot_dir1,plot_name))
plt.savefig (os.path.join(plot_dir1,plot_name), dpi=600,bbox_inches='tight')
plot_name = neon_site+'_'+var.obs_var+'_composition_diel_cyle'+'_'+season+'.pdf'
plot_dir2= os.path.join(plot_dir, 'pdf')
if not os.path.isdir(plot_dir2):
os.makedirs(plot_dir2, exist_ok=True)
plt.savefig (os.path.join(plot_dir2,plot_name), dpi=600,bbox_inches='tight', format = 'pdf')
else:
plt.show()
plt.close('all')
def diel_cycle_lh(diel_df_mean, diel_df_std, var, plot_dir,color1= '#e28743',color2='#1d657e',season = "Annual", save_switch=False):
# Plot annual average diel cycle and bias
bias_var_name = var.obs_var +'_bias'
diel_df_mean [bias_var_name] = diel_df_mean[var.sim_var]- diel_df_mean[var.obs_var]
fig= plt.figure(num=None, figsize=(17, 15), facecolor='w', edgecolor='k')
# -- subplot 1 : Diurnal average + standard dev as shaded regions
gs = gridspec.GridSpec(3, 1, height_ratios=[3, 1, 2.25])
ax= plt.subplot(gs[0])
plt.plot ( diel_df_mean.local_hour, diel_df_mean[var.obs_var], marker = 'o' , color = color1,label="NEON")
plt.plot ( diel_df_mean.local_hour, diel_df_mean[var.sim_var] , marker = 'o' , color = color2,label="CTSM", linestyle = 'dashed')
plt.fill_between(diel_df_mean.local_hour, diel_df_mean[var.obs_var]-diel_df_std[var.obs_var], diel_df_mean[var.obs_var]+diel_df_std[var.obs_var] ,alpha=0.1, color = color1)
plt.fill_between(diel_df_mean.local_hour, diel_df_mean[var.sim_var]-diel_df_std[var.sim_var], diel_df_mean[var.sim_var]+diel_df_std[var.sim_var] ,alpha=0.1, color = color2)
ax.set_ylabel(var.long_name+" ["+var.unit+"]", fontsize=15)
# -- vertical line at noon
#plt.axvline(x=12, color = 'gray',linestyle='dashed',alpha=0.75)
if season in ["DJF", "MAM","JJA","SON"]:
plt.title("Diurnal Cycle of "+ var.long_name +"\n " +neon_site+" "+"[2018-2021] " +season , fontweight='bold',fontsize=15)
else:
plt.title("Diurnal Cycle of "+ var.long_name +"\n " +neon_site+" "+"[2018-2021]", fontweight='bold',fontsize=15)
# -- Changing tick properties:
ax.tick_params(axis='both', which='both', labelsize=14,width=1,length=7)
ax.tick_params(axis='x',which ='both',direction="in")
ax.tick_params(axis='y',direction="out", length = 7)
ax.yaxis.set_ticks_position('both')
# -- x-axis range and ticks
ax.set_xlim([-1,24])
ax.xaxis.set_ticks(np.arange(0, 24, 1))
# -- y axis label location
ax.get_yaxis().set_label_coords(-0.07,0.5)
# -- add legends
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(fontsize=15)
# -- add letters
ax.text(.01,0.935,'(a)',
horizontalalignment='left',
transform=ax.transAxes, fontweight='bold',fontsize=15)
# -- remove tick labels for x axis
labels = [item.get_text() for item in ax.get_xticklabels()]
empty_string_labels = ['']*len(labels)
ax.set_xticklabels(empty_string_labels)
# ----------------------
# -- Subplot 2: Bias Plot
ax = plt.subplot(gs[1])
diel_df_mean.plot ( x= 'local_hour', y = 'EFLX_LH_TOT_bias' , marker = 'o' ,ax =ax , color = 'black',label='Bias')
plt.ylabel('Bias'+" ["+var.unit+"]", fontsize=15)
# -- horizontal line at 0
plt.axhline(y=0, color = 'gray',linestyle='dashed',alpha=0.75)
#-- vertical line at noon
#plt.axvline(x=12, color = 'gray',linestyle='dashed',alpha=0.75)
# -- Changing tick properties:
ax.tick_params(axis='both', which='both', labelsize=14,width=1,length=7)
ax.tick_params(axis='x',which ='both',direction="in")
ax.tick_params(axis='y',direction="out", length = 7)
ax.yaxis.set_ticks_position('both')
# -- x-axis range and ticks
ax.set_xlim([-1,24])
ax.xaxis.set_ticks(np.arange(0, 24, 1))
# -- y axis label location
ax.get_yaxis().set_label_coords(-0.07,0.5)
# -- remove legend
ax.get_legend().remove()
# -- add letters
ax.text(.01,0.8,'(b)',
horizontalalignment='left',
transform=ax.transAxes, fontweight='bold',fontsize=15)
# -- remove tick labels for x axis
labels = [item.get_text() for item in ax.get_xticklabels()]
empty_string_labels = ['']*len(labels)
ax.set_xticklabels(empty_string_labels)
# ----------------------
# -- Subplot 3: Latent Heat Flux Composition Plots
ax = plt.subplot(gs[2])
diel_df_mean[['local_hour','sim_FCEV','sim_FCTR','sim_FGEV']].plot.bar (
x= 'local_hour',stacked='True',ax=ax,rot=45,
color=['tab:blue','tab:green','tab:orange'])
#-- vertical line at noon
#plt.axvline(x=12, color = 'gray',linestyle='dashed',alpha=0.75)
plt.ylabel('Latent Heat Components'+" ["+var.unit+"]", fontsize=15)
plt.xlabel('Local Time', fontsize=15)
# -- Changing tick properties:
ax.tick_params(axis='both', which='both', labelsize=14,width=1,length=7)
ax.tick_params(axis='x',which ='both',direction="out")
ax.tick_params(axis='y',direction="out", length = 7)
ax.yaxis.set_ticks_position('both')
# -- x-axis range and ticks
ax.set_xlim([-1,24])
ax.xaxis.set_ticks(np.arange(0, 24, 1))
# -- changing x axis tick labels to custom labels
labels = diel_df_mean.local_hour_dt.dt.strftime("%H:%M")
ax.set_xticklabels(labels)
# -- y axis label location
ax.get_yaxis().set_label_coords(-0.07,0.5)
# -- add legends
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, ['Canopy Evaporation','Canopy Transpiration', 'Ground Evaporation'],fontsize=15)
# -- add letters
ax.text(.01,0.9,'(c)',
horizontalalignment='left',
transform=ax.transAxes, fontweight='bold',fontsize=15)
fig.subplots_adjust(wspace=0, hspace=0)
if save_switch:
plot_name = neon_site+'_'+var.obs_var+'_composition_diel_cyle'+'_'+season+'.png'
plot_dir1 = os.path.join(plot_dir, 'png')
if not os.path.isdir(plot_dir1):
os.makedirs(plot_dir1, exist_ok=True)
print ('Saving '+ os.path.join(plot_dir1,plot_name))
plt.savefig (os.path.join(plot_dir1,plot_name), dpi=600,bbox_inches='tight')
plot_name = neon_site+'_'+var.obs_var+'_composition_diel_cyle'+'_'+season+'.pdf'
plot_dir2= os.path.join(plot_dir, 'pdf')
if not os.path.isdir(plot_dir2):
os.makedirs(plot_dir2, exist_ok=True)
plt.savefig (os.path.join(plot_dir2,plot_name), dpi=600,bbox_inches='tight', format = 'pdf')
else:
plt.show()
plt.close('all')
class PlotVariable ():
def __init__(self, short_name, long_name, unit):
self.short_name = short_name
self.long_name = long_name
self.unit = unit
self.obs_var = short_name
self.sim_var = 'sim_'+short_name
Variables to create plots#
Create a list of variable instances for the plots.
all_vars= []
plot_var = 'Rnet'
sim_var = 'sim_'+plot_var
plot_var_desc = "Net Radiation"
plot_var_unit= "Wm⁻²"
this_var = PlotVariable(plot_var, plot_var_desc, plot_var_unit)
all_vars.append(this_var)
plot_var = 'FSH'
sim_var = 'sim_'+plot_var
plot_var_desc = 'Sensible Heat Flux'
plot_var_unit= "Wm⁻²"
this_var = PlotVariable(plot_var, plot_var_desc, plot_var_unit)
all_vars.append(this_var)
plot_var = 'EFLX_LH_TOT'
sim_var = 'sim_'+plot_var
plot_var_desc = "Latent Heat Flux"
plot_var_unit= "Wm⁻²"
this_var = PlotVariable(plot_var, plot_var_desc, plot_var_unit)
all_vars.append(this_var)
plot_var = 'GPP'
sim_var = 'sim_'+plot_var
plot_var_desc = "Gross Primary Production"
plot_var_unit= "gCm⁻²day⁻¹"
this_var = PlotVariable(plot_var, plot_var_desc, plot_var_unit)
all_vars.append(this_var)
plot_var = 'NEE'
sim_var = 'sim_'+plot_var
plot_var_desc = "Net Ecosystem Exchange"
plot_var_unit= "gCm⁻²day⁻¹"
this_var = PlotVariable(plot_var, plot_var_desc, plot_var_unit)
all_vars.append(this_var)
Make Diurnal Cycle Plots#
# -- read only these variables from the whole netcdf files
def preprocess (ds):
variables = ['FCEV', 'FCTR', 'FGEV','FSH','GPP','FSA','FIRA','AR','HR','ELAI']
ds_new= ds[variables]
return ds_new
# -- fix timestamp on CTSM files so they can be matched with eval files
# Note, this is not needed with newer version of CLM (CTSM5.3 and later)
def fix_time (ds):
'''
fix time formatting with reading multiple cesm files.
'''
nsteps = len(ds.time)
yr0 = ds['time.year'][0].values
month0 = ds['time.month'][0].values
day0 = ds['time.day'][0].values
date = cftime.datetime(yr0,month0,day0).isoformat()
ds['time'] = xr.date_range(date, periods=nsteps, freq='30min', use_cftime=True)
ds['time']= ds['time'].dt.strftime("%Y-%m-%d %H:%M:%S")
return ds
plot_dir = '~/NEON_figures/diel_cycle'
plot_dir = os.path.realpath(os.path.expanduser(plot_dir))
if not os.path.isdir(plot_dir):
print ("figures directory does not exist... creating it now!")
os.makedirs(plot_dir, exist_ok=True)
else:
print("figures directory already exists")
if not os.path.exists (plot_dir):
print ("plot directory does not exist... creating it now!")
os.mkdir(plot_dir)
## -- Setting some defaults for our figures:
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"
font = {'weight' : 'bold',
'size' : 15}
matplotlib.rc('font', **font)
Going Through the list of sites & years to make plots for all of them.
If you want to save your plots change save_switch = True.
# -- read list of sites:
#neon_sites_pft = pd.read_csv('/glade/scratch/negins/preprocessed_neon_csv_data/neon_sites_dompft.csv')
#neon_sites_pft = pd.read_csv('/glade/u/home/wwieder/CTSM/tools/site_and_regional/neon_sites_dompft.csv')
#neon_sites = neon_sites_pft['Site'].to_list()
#Just do this for one site here
neon_sites = ['ABBY'] # list of the sites you want to plot
case = '.transient' # this should be the rest of your case name for each neon_site
# (e.g. "site.transient" or "site.experiment.transient")
years = ["2018","2019","2020","2021"]
save_switch = True
failed_sites = [] # -- list for saving failed sites
for neon_site in neon_sites:
try:
start_site = time.time()
print ('---------------------------')
print ("Making plots for "+neon_site)
sim_files =[]
for year in years:
sim_path = "/glade/campaign/cgd/tss/tutorials/neon_v3/hist_files/"+neon_site+case+"/lnd/hist/"
sim_files.extend(sorted(glob(join(sim_path,neon_site+".transient.clm2.h1."+year+"*.nc"))))
print("All simulation files for all years: [", len(sim_files), "files]")
print ("Start reading v3 simulation files...")
start = time.time()
ds_ctsm = xr.open_mfdataset(sim_files, decode_times=True, combine='by_coords',parallel=True,preprocess=preprocess, use_cftime=True)
ds_ctsm = fix_time (ds_ctsm)
end = time.time()
print("Reading all simulation files took:", end-start, "s.")
eval_files = []
for year in years:
eval_dir = "/glade/campaign/cgd/tss/tutorials/neon_v3/eval_files/"+neon_site
eval_files.extend(sorted(glob(join(eval_dir,neon_site+"_eval_"+year+"*.nc"))))
print ("All evaluation files for all years: [", len(eval_files), "files]")
print ("Start reading v3 evaluation files...")
start = time.time()
ds_eval = xr.open_mfdataset(eval_files, decode_times=True, combine='by_coords')
end = time.time()
print("Reading all observation files took:", end-start, "s.")
print ("Processing data...")
#Convert CTSM data to a Pandas Dataframe for easier handling:
ctsm_vars = ['FCEV', 'FCTR', 'FGEV','FSH','GPP','FSA','FIRA','AR','HR','ELAI']
df_ctsm = pd.DataFrame({'time':ds_ctsm.time})
df_ctsm['time'] = pd.to_datetime(df_ctsm['time'],format= '%Y-%m-%d %H:%M:%S' )
for var in ctsm_vars:
sim_var_name = "sim_"+var
field = np.ravel ( ds_ctsm[var] )
df_ctsm[sim_var_name]=field
#-- shift simulation data by one
df_ctsm[sim_var_name]=df_ctsm[sim_var_name].shift(-1).values
#Convert NEON data to a Pandas Dataframe for easier handling:
eval_vars = ['NEE','FSH','EFLX_LH_TOT','GPP','Rnet']
df_all = pd.DataFrame({'time':ds_eval.time})
for var in eval_vars:
field = np.ravel (ds_eval[var])
df_all[var]=field
df_all=df_all.merge(df_ctsm.set_index('time'), on='time', how='left')
clm_var = 'sim_EFLX_LH_TOT'
# -- Latent Heat Flux:
# -- EFLX_LH_TOT = FCEV + FCTR +FGEV
df_all [clm_var] = df_all['sim_FCEV']+ df_all['sim_FCTR']+ df_all['sim_FGEV']
clm_var = 'sim_Rnet'
# -- Net Radiation:
# -- Rnet = FSA-FIRA
df_all [clm_var] = df_all ['sim_FSA']-df_all['sim_FIRA']
clm_var = 'sim_NEE'
# Net Ecosystem Exchange
# NEE = GPP- (AR+HR)
# It seems like the signs are opposite so we calculated negative NEE
df_all [clm_var] = -(df_all ['sim_GPP']-(df_all['sim_AR']+df_all['sim_HR']))
# convert NEE units from umolm-2s-1 to gc/m2/s
df_all ['NEE']= df_all ['NEE']*(12.01/1000000)
df_all ['GPP']= df_all ['GPP']*(12.01/1000000)
# -- convert gc/m2/s to gc/m2/day
df_all ['NEE']= df_all['NEE']*60*60*24
df_all ['sim_NEE']= df_all['sim_NEE']*60*60*24
df_all ['GPP']= df_all['GPP']*60*60*24
df_all ['sim_GPP']= df_all['sim_GPP']*60*60*24
#-- extract year, month, day, hour information from time
df_all['year'] = df_all['time'].dt.year
df_all['month'] = df_all['time'].dt.month
df_all['day'] = df_all['time'].dt.day
df_all['hour'] = df_all['time'].dt.hour
df_all['season'] = ((df_all['month']%12+3)//3).map({1:'DJF', 2: 'MAM', 3:'JJA', 4:'SON'})
seasons = ['Annual','DJF', 'MAM', 'JJA', 'SON']
#-- convert to local time
tzone_shift = ds_eval.TimeDiffUtcLt
df_all['local_time']= df_all['time'] + datetime.timedelta(hours=int(tzone_shift))
df_all['local_hour'] = df_all['local_time'].dt.hour
print ("Making diurnal cycle plots...")
color1 = '#e28743'
#color1 = '#f7aa15'
color2 = '#1d657e'
for season in seasons:
if season=="Annual":
df_numeric = df_all.select_dtypes(exclude=['object', 'string'])
#-- Calculate annual average diel cycle and bias
diel_df_mean = df_numeric.groupby('local_hour').mean().reset_index()
diel_df_std = df_numeric.groupby('local_hour').std().reset_index()
else:
df_season = df_all[df_all['season']==season]
df_season_numeric = df_season.select_dtypes(exclude=['object', 'string'])
#-- Calculate annual average diel cycle and bias
diel_df_mean = df_season_numeric.groupby('local_hour').mean().reset_index()
diel_df_std = df_season_numeric.groupby('local_hour').std().reset_index()
diel_df_mean['local_hour_dt']= pd.to_datetime(diel_df_mean['local_hour'], format='%H')
for var in all_vars:
if var.obs_var == "EFLX_LH_TOT":
print ("making latent heat component plots")
diel_cycle_lh(diel_df_mean, diel_df_std, var, plot_dir,color1,color2, season, save_switch)
else:
diel_cycle_plot(diel_df_mean, diel_df_std, var, plot_dir,color1,color2, season, save_switch)
end_site = time.time()
print("Making these plots for "+neon_site+" took : ", end_site-start_site, "s.")
plt.close('all')
print(" Plots found in "+plot_dir)
except Exception as e:
print (e)
print ('THIS SITE FAILED:', neon_site)
failed_sites.append(neon_site)
pass
print ("Making plots for ", len(failed_sites), "failed : ")
print (*failed_sites, sep=" \n")
To look at these plots you have a few options:
Simplest may be to navigate to your NEON_figures directory using the sidebar on the left of your OnDemand window and:
Open one of the figures (directly), this typically doesn’t look very good
Right click on a figure and download it locally (see image below)
Download the whole
pngdirectory to your local machine. You can do this by:Open a new terminal window on your local machine
cd Desktopscp -r $USER@casper.hpc.ucar.edu:/glade/u/home/$USER/NEON_figures/diel_cycle/png .NOTE you’ll have to replace $USER with your actual username
Alternatively, if you have X11 forwarding enabled on your local machine you can visualize these figures on the terminal window with:
display /glade/u/home/$USER/NEON_figures/diel_cycle/png/ABBY_EFLX_LH_TOT_composition_diel_cyle_JJA.png