Benutzerspezifische Werkzeuge

##### Sektionen
Sie sind hier: NCL / DKRZ PyNGL example plot curvilinear data
Info
Alle Inhalte des Nutzerportal sind nur auf Englisch verfügbar.

# DKRZ PyNGL example plot curvilinear data

In this example we use xarray to read the curvilinear data and PyNGL to create a plot. The local function add_title shows how to add annotations to the plot like a title string and strings placed like NCL's gsnLeftString, gsnCenterString and gsnRightString do.

NOTE: We will use xarray in the future exclusively for reading and writing data files because at the beginning of 2019 NCAR has decided to freeze PyNIO until further notice.

Example script:

`'''Script:    curvilinear_data_CORDEX_EUR-11.pyDescription:    Example of how to plot curvilinear data.Effects illustrated:    - Plotting curvilinear data    - Filled contour plot'''import osimport numpy as npimport xarray as xrimport Ngl#-----------------------------------------------------------------------#-- Function:   add_titles(wks,plot,resources,title,left,center,right)#-----------------------------------------------------------------------def add_titles(wks,plot,title="",left="",center="",right=""):   vpx = Ngl.get_float(plot,"vpXF")             #-- retrieve value of res.vpXF from plot   vpy = Ngl.get_float(plot,"vpYF")             #-- retrieve value of res.vpYF from plot   vpw = Ngl.get_float(plot,"vpWidthF")         #-- retrieve value of res.vpWidthF from plot   vph = Ngl.get_float(plot,"vpHeightF")        #-- retrieve value of res.vpHeightF from plot      ymax = vpy+0.08                              #-- we need space for the title and strings      if(ymax > 0.98):      print("--> if you can't see the title use res.nglMaximize = False and/or set res.vpYF")#-- add title   if(title != ""):      tires = Ngl.Resources()      tires.txFontHeightF =  0.018      tires.txJust        = "CenterCenter"      tires.txFont        =  22                     #-- Font 22: Helvetica bold      if(left != "" or center != "" or right != ""):         y = vpy + 0.06      else:         y = vpy + 0.04            vpxc = vpx + (vpw/2)      Ngl.text_ndc(wks, title, vpxc, y, tires)#-- add left, center and/or right string   txres = Ngl.Resources()   txres.txFontHeightF = 0.014                  #-- font size for left, center and right string   y = vpy + 0.02                               #-- y-position   if(left != ""):      txres.txJust = "CenterLeft"               #-- text justification      x = vpx                                   #-- x-position      Ngl.text_ndc(wks, left, x, y, txres)      #-- add text to wks         if(center != ""):      txres.txJust = "CenterCenter"             #-- text justification      Ngl.text_ndc(wks, center, 0.5, y, txres)  #-- add text to wks      if(right != ""):      txres.txJust = "CenterRight"              #-- text justification      x = vpx+vpw                               #-- x-position      Ngl.text_ndc(wks, right, x, y, txres)     #-- add text to wks#-----------------------------------------------------------------------#         MAIN#-----------------------------------------------------------------------#-- set file namehome  =  os.environ.get('HOME')diri  = 'data/CORDEX/EUR-11'fname = 'tas_EUR-11_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_CLMcom-CCLM4-8-17_v1_mon_200601-201012.nc'file_name = os.path.join(home,diri,fname)#-- variable namevarname = 'tas'#-- open fileds = xr.open_dataset(file_name)#-- read the variablesvar   = ds[varname][0,:,:]lat2d = ds['lat']                           #-- 2D coordinatelon2d = ds['lon']                           #-- 2D coordinateprint('lat2d.shape: {}'.format(lat2d.shape))print('lon2d.shape: {}'.format(lon2d.shape))#-- open a workstationwks_type              = "png"               #-- output typewks                   =  Ngl.open_wks(wks_type,'plot_curvilinear_data_CORDEX_EUR-11')#-- set resourcesres                   =  Ngl.Resources()    #-- generate an resources object for plotres.nglFrame          =  Falseres.nglMaximize       =  Trueres.cnFillOn          =  True               #-- turn on contour fillres.cnLinesOn         =  False              #-- don't draw contour linesres.cnLineLabelsOn    =  False              #-- don't draw line labelsres.cnFillPalette     = "BlueYellowRed"     #-- set color mapres.cnLevelSelectionMode  = "ManualLevels"res.cnMinLevelValF    =  250res.cnMaxLevelValF    =  290res.cnLevelSpacingF   =  2res.lbOrientation     = "Horizontal"        #-- labelbar orientationres.sfXArray          =  lon2d.values       #-- longitude grid cell centerres.sfYArray          =  lat2d.values       #-- latitude grid cell centerres.mpFillOn          =  False              #-- don't draw filled mapres.mpGridLatSpacingF =  10.                #-- grid lat spacingres.mpGridLonSpacingF =  10.                #-- grid lon spacingres.mpDataBaseVersion = "MediumRes"         #-- map databaseres.mpLimitMode       = "LatLon"            #-- must be set using minLatF/maxLatF/minLonF/maxLonFres.mpMinLatF         =  15.                #-- sub-region minimum latituderes.mpMaxLatF         =  80.                #-- sub-region maximum latituderes.mpMinLonF         = -50.                #-- sub-region minimum longituderes.mpMaxLonF         =  70.                #-- sub-region maximum longitude#-- create the plotplot = Ngl.contour_map(wks,var,res)         #-- create the contour plot#-- add the title and left, center and/or right stringtitle = "Curvilinear grid:  tas (CORDEX EUR-11)"   #-- main title stringif var.attrs['units']:                      #-- retrieve units attribute   units = var.attrs['units']add_titles(wks,plot,title=title,left=varname,right=units)#-- advance the frameNgl.frame(wks)#-- endNgl.end()`

Result:

Artikelaktionen