#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Sep 8 07:35:08 2023 @author: mohamedazizbhouri """ from matplotlib import pyplot as plt import numpy as np from mpl_toolkits.basemap import Basemap from moviepy.editor import VideoClip from moviepy.video.io.bindings import mplfig_to_npimage plt.rcParams.update(plt.rcParamsDefault) plt.rc('font', family='serif') plt.rcParams.update({'font.size': 16, 'lines.linewidth': 2, 'axes.labelsize': 20, 'axes.titlesize': 20, 'xtick.labelsize': 16, 'ytick.labelsize': 16, 'legend.fontsize': 20, 'axes.linewidth': 2, "pgf.texsystem": "pdflatex" }) lat = 96 lon = 144 x = np.linspace(0, 360-360/144, lon) y = np.linspace(-90, 90, lat) X, Y = np.meshgrid(x, y) dim_heat = 26 dim_moist = 22 mu_error_out = np.concatenate((np.zeros((1,dim_heat),dtype=np.float32), np.zeros((1,dim_moist),dtype=np.float32)),axis=1) mu_error_out = mu_error_out.T[:,:,None,None] sigma_error_out = np.concatenate((1/1004.6*np.ones((1,dim_heat),dtype=np.float32), 1/2.26e6*np.ones((1,dim_moist),dtype=np.float32)),axis=1) sigma_error_out = sigma_error_out.T[:,:,None,None] y_var = 43 # 14, 18, 21, 36, 40, 43 if y_var == 14: ttl = 'Heat tendency at 259 hPa' ttl_s = 'videos/instant_heat_tend_259.mp4' elif y_var == 18: ttl = 'Heat tendency at 494 hPa' ttl_s = 'videos/instant_heat_tend_494.mp4' elif y_var == 21: ttl = 'Heat tendency at 761 hPa' ttl_s = 'videos/instant_heat_tend_761.mp4' elif y_var == 36: ttl = 'Moisture tendency at 259 hPa' ttl_s = 'videos/instant_moist_tend_259.mp4' elif y_var == 40: ttl = 'Moisture tendency at 494 hPa' ttl_s = 'videos/instant_moist_tend_494.mp4' elif y_var == 43: ttl = 'Moisture tendency at 761 hPa' ttl_s = 'videos/instant_moist_tend_761.mp4' test = (np.load('data_SPCAM5_4K/all_outputs_reshaped.npy')[y_var:y_var+1,:,:,:] - mu_error_out) / sigma_error_out test = np.array(test,dtype=np.float64) months = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan'] days = np.array([28, 28+31, 28+31+30, 28+31+30+31, 28+31+30+31+30, 28+31+30+31+30+31, 28+31+30+31+30+31+31, 28+31+30+31+30+31+31+30, 28+31+30+31+30+31+31+30+31, 28+31+30+31+30+31+31+30+31+30, 28+31+30+31+30+31+31+30+31+30+31, 28+31+30+31+30+31+31+30+31+30+31+31]) hours = 24*days std_rpn_MF = np.load('MF_param/mean_RPN_MF_reshaped.npy')[y_var:y_var+1,:,:,:]/ sigma_error_out mean_rpn_MF = (np.load('MF_param/std_RPN_MF_reshaped.npy')[y_var:y_var+1,:,:,:] - mu_error_out) / sigma_error_out mean_rpn_MF = np.array(mean_rpn_MF,dtype=np.float64) std_rpn_MF = np.array(std_rpn_MF,dtype=np.float64) std_rpn_LF = np.load('MF_param/mean_RPN_LF_reshaped.npy')[y_var:y_var+1,:,:,:]/ sigma_error_out mean_rpn_LF = (np.load('MF_param/std_RPN_LF_reshaped.npy')[y_var:y_var+1,:,:,:] - mu_error_out) / sigma_error_out mean_rpn_LF = np.array(mean_rpn_LF,dtype=np.float64) std_rpn_LF = np.array(std_rpn_LF,dtype=np.float64) std_rpn_SF = np.load('SF_param/mean_RPN_SF_reshaped.npy')[y_var:y_var+1,:,:,:]/ sigma_error_out mean_rpn_SF = (np.load('SF_param/std_RPN_SF_reshaped.npy')[y_var:y_var+1,:,:,:] - mu_error_out) / sigma_error_out std_rpn_SF = np.array(std_rpn_SF,dtype=np.float64) mean_rpn_SF = np.array(mean_rpn_SF,dtype=np.float64) MAE_MF = np.abs( test-mean_rpn_MF ) MAE_LF = np.abs( test-mean_rpn_LF ) MAE_SF = np.abs( test-mean_rpn_SF ) N_frame = std_rpn_MF.shape[1] fps = 5 duration = N_frame / fps fig = plt.figure(figsize=(31.5, 14)) def create_fr(t): print(t,int(t*fps)) fig.clf() ax1 = fig.add_subplot(231) ax1.set_title("MF-RPN - MAE") m = Basemap(projection='robin',lon_0=-180) contour_plot = m.pcolormesh(X, Y, MAE_MF[0,int(t*fps),:,:], latlon = True,cmap='Blues_r') m.drawcoastlines(linewidth=2.0, color='0.25') ax2 = fig.add_subplot(234) ax2.set_title("MF-RPN - Uncertainty "+r'$\sigma_M$') m = Basemap(projection='robin',lon_0=-180) contour_plot = m.pcolormesh(X, Y, std_rpn_MF[0,int(t*fps),:,:], latlon = True,cmap='Blues_r') m.drawcoastlines(linewidth=2.0, color='0.25') ############################################# ax1 = fig.add_subplot(232) ax1.set_title("SF-RPN - MAE") m = Basemap(projection='robin',lon_0=-180) contour_plot = m.pcolormesh(X, Y, MAE_SF[0,int(t*fps),:,:], latlon = True,cmap='Blues_r') m.drawcoastlines(linewidth=2.0, color='0.25') ax2 = fig.add_subplot(235) ax2.set_title("SF-RPN - Uncertainty "+r'$\sigma_M$') m = Basemap(projection='robin',lon_0=-180) contour_plot = m.pcolormesh(X, Y, std_rpn_SF[0,int(t*fps),:,:], latlon = True,cmap='Blues_r') m.drawcoastlines(linewidth=2.0, color='0.25') ############################################# ax1 = fig.add_subplot(233) ax1.set_title("LF-RPN - MAE") m = Basemap(projection='robin',lon_0=-180) contour_plot = m.pcolormesh(X, Y, MAE_LF[0,int(t*fps),:,:], latlon = True,cmap='Blues_r') m.drawcoastlines(linewidth=2.0, color='0.25') ax2 = fig.add_subplot(236) ax2.set_title("LF-RPN - Uncertainty "+r'$\sigma_M$') m = Basemap(projection='robin',lon_0=-180) contour_plot = m.pcolormesh(X, Y, std_rpn_LF[0,int(t*fps),:,:], latlon = True,cmap='Blues_r') m.drawcoastlines(linewidth=2.0, color='0.25') imonths = np.argmax(hours>int(t*fps)) nhour = int(t*fps) % 24 if nhour<10: nhour = '0'+str(nhour) else: nhour = str(nhour) if imonths == 0: nday = 1+int(t*fps)//24 else: nday = int(t*fps)//24-days[imonths-1]+1 if nday<10: nday = '0'+str(nday) else: nday = str(nday) if imonths == 11: ty = '2004' else: ty = '2003' fig.suptitle(ttl + ' ; '+nday+' '+months[imonths]+' '+ty+' ; '+nhour+':00') plt.margins(x=0) plt.margins(y=0) plt.subplots_adjust(hspace=-0.3) fig.subplots_adjust(top=0.85) plt.tight_layout() fig.canvas.draw() cbar_ax = fig.add_axes([0.05, 0.02, 0.9, 0.05]) cbar = fig.colorbar(contour_plot, orientation='horizontal', cax=cbar_ax) cbar.set_ticks([]) return mplfig_to_npimage(fig) animation = VideoClip(create_fr, duration = duration) animation.write_videofile(ttl_s,fps=fps,threads=16, logger=None,codec="mpeg4",preset="slow",ffmpeg_params=['-b:v','10000k'])