From def107dbba8f4da43b1c8ff601a68dd689a83789 Mon Sep 17 00:00:00 2001 From: Kevin Paul Date: Thu, 23 Jan 2020 10:18:37 -0700 Subject: [PATCH 1/4] Adding initial xy_18 example file --- Plots/XY/NCL_xy_18.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Plots/XY/NCL_xy_18.py diff --git a/Plots/XY/NCL_xy_18.py b/Plots/XY/NCL_xy_18.py new file mode 100644 index 000000000..f3a65bc5c --- /dev/null +++ b/Plots/XY/NCL_xy_18.py @@ -0,0 +1,33 @@ +""" +NCL_xy_18.py +============ +Concepts illustrated: + +- Filling the area between two curves in an XY plot +- Labeling the bottom X axis with years +- Drawing a main title on three separate lines +- Calculating a weighted average +- Changing the size/shape of an XY plot using viewport resources +- Manually creating a legend +- Overlaying XY plots on each other +- Maximizing plots after they've been created + +See the [original NCL example](https://www.ncl.ucar.edu/Applications/Scripts/xy_18.ncl) +""" + +############################################################################### +# Open files and read in monthly data + + + +############################################################################### +# Some parameters +nyrs = 110 +nlon = 128 +nlat = 64 + +############################################################################### +# Note the inline comment in the previous code cell. This was not converted to +# its own markdown cell because it was not contiguously preceeded with a +# comment line beginning with 20+ '#' characters. + From 404bfb6e1679c614637b97493fd2bd707b97e0cb Mon Sep 17 00:00:00 2001 From: Kevin Paul Date: Thu, 23 Jan 2020 10:47:05 -0700 Subject: [PATCH 2/4] Adding content --- Plots/XY/NCL_xy_18.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Plots/XY/NCL_xy_18.py b/Plots/XY/NCL_xy_18.py index f3a65bc5c..4e5debea5 100644 --- a/Plots/XY/NCL_xy_18.py +++ b/Plots/XY/NCL_xy_18.py @@ -15,9 +15,25 @@ See the [original NCL example](https://www.ncl.ucar.edu/Applications/Scripts/xy_18.ncl) """ +############################################################################### +# Basic imports +import numpy as np +import xarray as xr + ############################################################################### # Open files and read in monthly data +pass +# dir = './' +# v1 = addfile (dir+"TREFHT.B06.61.atm.1890-1999ANN.nc", "r") +# v2 = addfile (dir+"TREFHT.B06.59.atm.1890-1999ANN.nc", "r") +# v3 = addfile (dir+"TREFHT.B06.60.atm.1890-1999ANN.nc", "r") +# v4 = addfile (dir+"TREFHT.B06.57.atm.1890-1999ANN.nc", "r") +# n1 = addfile (dir+"TREFHT.B06.66.atm.1890-1999ANN.nc", "r") +# n2 = addfile (dir+"TREFHT.B06.67.atm.1890-1999ANN.nc", "r") +# n3 = addfile (dir+"TREFHT.B06.68.atm.1890-1999ANN.nc", "r") +# n4 = addfile (dir+"TREFHT.B06.69.atm.1890-1999ANN.nc", "r") +# g = addfile (dir+"gw.nc","r") ############################################################################### @@ -25,9 +41,14 @@ nyrs = 110 nlon = 128 nlat = 64 +time = np.linspace(1890, 1999, endpoint=True) ############################################################################### -# Note the inline comment in the previous code cell. This was not converted to -# its own markdown cell because it was not contiguously preceeded with a -# comment line beginning with 20+ '#' characters. +# OBS +pass +# obs = asciiread("jones_glob_ann_2002.asc",(/146/),"float") + +############################################################################### +# NATURAL +nat = From d84e5c4356f7da903505c1d3be6af840554a181e Mon Sep 17 00:00:00 2001 From: Kevin Paul Date: Thu, 23 Jan 2020 11:07:56 -0700 Subject: [PATCH 3/4] Initially filling in script --- Plots/XY/NCL_xy_18.py | 189 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 178 insertions(+), 11 deletions(-) diff --git a/Plots/XY/NCL_xy_18.py b/Plots/XY/NCL_xy_18.py index 4e5debea5..4d3b48631 100644 --- a/Plots/XY/NCL_xy_18.py +++ b/Plots/XY/NCL_xy_18.py @@ -24,16 +24,17 @@ # Open files and read in monthly data pass -# dir = './' -# v1 = addfile (dir+"TREFHT.B06.61.atm.1890-1999ANN.nc", "r") -# v2 = addfile (dir+"TREFHT.B06.59.atm.1890-1999ANN.nc", "r") -# v3 = addfile (dir+"TREFHT.B06.60.atm.1890-1999ANN.nc", "r") -# v4 = addfile (dir+"TREFHT.B06.57.atm.1890-1999ANN.nc", "r") -# n1 = addfile (dir+"TREFHT.B06.66.atm.1890-1999ANN.nc", "r") -# n2 = addfile (dir+"TREFHT.B06.67.atm.1890-1999ANN.nc", "r") -# n3 = addfile (dir+"TREFHT.B06.68.atm.1890-1999ANN.nc", "r") -# n4 = addfile (dir+"TREFHT.B06.69.atm.1890-1999ANN.nc", "r") -# g = addfile (dir+"gw.nc","r") + +# dir = './' +# v1 = addfile (dir+"TREFHT.B06.61.atm.1890-1999ANN.nc", "r") +# v2 = addfile (dir+"TREFHT.B06.59.atm.1890-1999ANN.nc", "r") +# v3 = addfile (dir+"TREFHT.B06.60.atm.1890-1999ANN.nc", "r") +# v4 = addfile (dir+"TREFHT.B06.57.atm.1890-1999ANN.nc", "r") +# n1 = addfile (dir+"TREFHT.B06.66.atm.1890-1999ANN.nc", "r") +# n2 = addfile (dir+"TREFHT.B06.67.atm.1890-1999ANN.nc", "r") +# n3 = addfile (dir+"TREFHT.B06.68.atm.1890-1999ANN.nc", "r") +# n4 = addfile (dir+"TREFHT.B06.69.atm.1890-1999ANN.nc", "r") +# g = addfile (dir+"gw.nc","r") ############################################################################### @@ -47,8 +48,174 @@ # OBS pass + # obs = asciiread("jones_glob_ann_2002.asc",(/146/),"float") ############################################################################### # NATURAL -nat = + +pass + +# nat = new ((/4,nyrs,nlat,nlon/), float) +# nat(0,:,:,:) = n1->TREFHT +# nat(1,:,:,:) = n2->TREFHT +# nat(2,:,:,:) = n3->TREFHT +# nat(3,:,:,:) = n4->TREFHT + +# ncase = 4 +# gavn = wgt_areaave(nat,g->gw,1.0,0) +# gavan = new((/ncase,nyrs/),float) + +# do c = 0, ncase-1 +# gavan(c,:) = gavn(c,:)-dim_avg(gavn(c,0:29)) +# end do +# gavan!0 = "ensembles" +# gavan!1 = "time" +# gavan&time = v1->time + +# delete(nat) + +############################################################################### +# ALL + +pass + +# volc = new ((/4,nyrs,nlat,nlon/), float) +# volc(0,:,:,:) = v1->TREFHT +# volc(1,:,:,:) = v2->TREFHT +# volc(2,:,:,:) = v3->TREFHT +# volc(3,:,:,:) = v4->TREFHT + +# ncase = 4 +# gavv = wgt_areaave(volc,g->gw,1.0,0) +# gavav = new((/ncase,nyrs/),float) + +# do c = 0, ncase-1 +# gavav(c,:) = gavv(c,:)-dim_avg(gavv(c,0:29)) +# end do + +# gavav!0 = "ensembles" +# gavav!1 = "time" +# gavav&time = v1->time + +# delete(volc) + +############################################################################### +# CALCULATE MIN & MAX + +pass + +# mnmx = new ((/7,nyrs/), float) +# mnmx(0,:) = dim_min( gavav(time|:,ensembles|:) ) +# mnmx(1,:) = dim_max( gavav(time|:,ensembles|:) ) +# mnmx(2,:) = dim_min( gavan(time|:,ensembles|:) ) +# mnmx(3,:) = dim_max( gavan(time|:,ensembles|:) ) +# mnmx(4,:) = dim_avg( gavav(time|:,ensembles|:) ) +# mnmx(5,:) = dim_avg( gavan(time|:,ensembles|:) ) +# mnmx(6,0:109) = obs(34:143)-dim_avg(obs(34:63)) + +############################################################################### +# Create plot + +pass + +# wks = gsn_open_wks("png","xy") ; send graphics to PNG file + +# res = True ; plot mods desired +# res@gsnDraw = False ; don't draw yet +# res@gsnFrame = False ; don't advance frame yet + +# res@vpHeightF = 0.4 ; change aspect ratio of plot +# res@vpWidthF = 0.7 + +# res@trYMaxF = 1.0 +# res@trXMinF = 1890 ; set x-axis minimum + +# res@xyMonoLineColor = False ; want colored lines +# res@xyLineColors = (/"Red","Blue","Black"/) ; colors chosen +# res@xyLineThicknesses = (/3.,3.,4./) ; line thicknesses +# res@xyDashPatterns = (/0.,0.,0./) ; make all lines solid + +# res@tiYAxisString = "~F35~J~F~C" ; add an axis title +# res@txFontHeightF = 0.0195 ; change title font heights + +# top_plot = gsn_csm_xy (wks,time,mnmx(4:6,:),res) ; create line plot + +# # Create a plot with the area between both curves filled in blue. +# delete(res@xyLineColors) +# res@gsnXYFillColors = "LightBlue" +# res@xyLineColor = -1 ; We don't want the line, so make it transparent. +# bot_plot = gsn_csm_xy (wks,time,mnmx(2:3,:),res) ; Create filled XY plot. + +# # Create a plot with the area between both curves filled in pink. +# res@gsnXYFillColors = "LightPink" +# res@xyLineColor = -1 ; We don't want the line, so make it transparent. +# mid_plot = gsn_csm_xy (wks,time,mnmx(0:1,:),res) ; Create another filled XY plot. + +# # +# # Overlay the top and mid plots on the bottom plot. +# # +# # Don't draw anything yet, because we still need to +# # attach a legend and some titles. +# # +# overlay(bot_plot,mid_plot) +# overlay(bot_plot,top_plot) + +############################################################################### +# Manually create and attach legend + +# res_text = True ; text mods desired +# res_text@txFontHeightF = 0.015 ; change text size +# res_text@txJust = "CenterLeft" ; text justification + +# res_lines = True ; polyline mods desired +# res_lines@gsLineDashPattern = 0. ; solid line +# res_lines@gsLineThicknessF = 5. ; line thicker +# res_lines@gsLineColor = "red" ; line color +# xx = (/1893,1907/) +# yy = (/0.705,0.705/) +# dum1 = gsn_add_polyline(wks,bot_plot,xx,yy,res_lines) ; add polyline +# dum2 = gsn_add_text(wks,bot_plot,"Anthropogenic + Natural",1910,0.705,res_text); add text + +# yy = (/0.79,0.79/) +# res_lines@gsLineColor = "blue" ; change to blue +# dum3 = gsn_add_polyline(wks,bot_plot,xx,yy,res_lines) ; add polyline +# dum4 = gsn_add_text(wks,bot_plot,"Natural",1910,0.79,res_text) ; add text + +# yy = (/0.875,0.875/) +# res_lines@gsLineColor = "black" ; change to black +# dum5 = gsn_add_polyline(wks,bot_plot,xx,yy,res_lines) ; add polyline +# dum6 = gsn_add_text(wks,bot_plot,"Observations",1910,0.875,res_text) ; add text + +############################################################################### +# Manually create and attach titles + +pass + +# # +# # Attach some titles at the top. +# # +# res_text = True +# res_text@txFontHeightF = 0.03 ; change font size +# txid_top = gsn_create_text(wks, "Parallel Climate Model Ensembles", res_text) + +# amres = True +# amres@amJust = "BottomCenter" +# amres@amParallelPosF = 0.0 ; This is the center of the plot. +# amres@amOrthogonalPosF = -0.72 ; This is above the top edge of the plot. +# annoid_top = gsn_add_annotation(bot_plot, txid_top, amres) + +# res_text@txFontHeightF = 0.02 ; change font size +# txid_mid = gsn_create_text(wks, "Global Temperature Anomalies",res_text) + +# amres@amOrthogonalPosF = -0.62 ; This is just below the previous title. +# annoid_mid = gsn_add_annotation(bot_plot, txid_mid, amres) + +# res_text@txFontHeightF = 0.015 ; change font size +# txid_bot = gsn_create_text(wks,"from 1890-1919 average",res_text) + +# amres@amOrthogonalPosF = -0.55 ; This is just below the previous title. +# annoid_bot = gsn_add_annotation(bot_plot, txid_bot, amres) + +# pres = True +# maximize_output(wks,pres) From 94ac3151f4c8865387a21b474f949e6893481903 Mon Sep 17 00:00:00 2001 From: Kevin Paul Date: Thu, 23 Jan 2020 11:31:53 -0700 Subject: [PATCH 4/4] Starting to add xarray and numpy use --- Plots/XY/NCL_xy_18.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Plots/XY/NCL_xy_18.py b/Plots/XY/NCL_xy_18.py index 4d3b48631..242cb34fd 100644 --- a/Plots/XY/NCL_xy_18.py +++ b/Plots/XY/NCL_xy_18.py @@ -23,19 +23,16 @@ ############################################################################### # Open files and read in monthly data -pass - -# dir = './' -# v1 = addfile (dir+"TREFHT.B06.61.atm.1890-1999ANN.nc", "r") -# v2 = addfile (dir+"TREFHT.B06.59.atm.1890-1999ANN.nc", "r") -# v3 = addfile (dir+"TREFHT.B06.60.atm.1890-1999ANN.nc", "r") -# v4 = addfile (dir+"TREFHT.B06.57.atm.1890-1999ANN.nc", "r") -# n1 = addfile (dir+"TREFHT.B06.66.atm.1890-1999ANN.nc", "r") -# n2 = addfile (dir+"TREFHT.B06.67.atm.1890-1999ANN.nc", "r") -# n3 = addfile (dir+"TREFHT.B06.68.atm.1890-1999ANN.nc", "r") -# n4 = addfile (dir+"TREFHT.B06.69.atm.1890-1999ANN.nc", "r") -# g = addfile (dir+"gw.nc","r") +v1 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.61.atm.1890-1999ANN.nc") +v2 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.59.atm.1890-1999ANN.nc") +v3 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.60.atm.1890-1999ANN.nc") +v4 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.57.atm.1890-1999ANN.nc") +n1 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.66.atm.1890-1999ANN.nc") +n2 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.67.atm.1890-1999ANN.nc") +n3 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.68.atm.1890-1999ANN.nc") +n4 = xr.open_dataset("../../data/netcdf_files/TREFHT.B06.69.atm.1890-1999ANN.nc") +g = xr.open_dataset("../../data/netcdf_files/gw.nc") ############################################################################### # Some parameters @@ -47,9 +44,7 @@ ############################################################################### # OBS -pass - -# obs = asciiread("jones_glob_ann_2002.asc",(/146/),"float") +obs = np.loadtxt("../../data/ascii_files/jones_glob_ann_2002.asc", dtype=float) ############################################################################### # NATURAL