Appendix C: Advanced NCL object examples |
begin data_dir = ncargpath("data") + "/cdf/" ; Get directory path to data. cdf_file = addfile(data_dir + "Pstorm.cdf","r") ; Open a netCDF file. pf = cdf_file->p ; Read pressure values into variable "pf". pf@nlon = dimsizes(pf&lon) ; Store lengths of lat/lon arrays as pf@nlat = dimsizes(pf&lat) ; attributes of pf. ; ; Create an App object so that a resource file can be used with ; this example (it will look for a resource file named "example1.res" ; since the name of the App object is being called "example1"). ; No resources are set in the creation of this object. ; appid = create "example1" appClass noparent end create ; ; Create an NcgmWorkstation object. Set resources for specifying ; the name of the metafile and for changing the color map. Remember, ; the color map resource is actually a Workstation class resource, but ; since the NcgmWorkstation class is a subclass of the Workstation class, ; it inherits Workstation's resources. ; wks = create "ncgm" ncgmWorkstationClass appid "wkMetaName" : "example1.ncgm" "wkColorMap" : (/(/1.00, 1.00, 1.00/), (/0.00, 0.00, 0.00/), \ (/0.56, 0.50, 0.70/), (/0.30, 0.30, 0.70/), \ (/0.10, 0.10, 0.70/), (/0.00, 0.10, 0.70/), \ (/0.00, 0.30, 0.70/), (/0.00, 0.50, 0.50/), \ (/0.00, 0.40, 0.20/), (/0.00, 0.60, 0.00/), \ (/0.00, 1.00, 0.00/), (/0.55, 0.55, 0.00/), \ (/0.57, 0.42, 0.00/), (/0.70, 0.29, 0.00/), \ (/0.70, 0.18, 0.00/), (/0.87, 0.05, 0.00/), \ (/1.00, 0.00, 0.00/), (/0.00, 1.00, 1.00/), \ (/0.70, 0.70, 0.70/)/) end create ; ; Create a DataItem object for the ContourPlot object being created later. ; dataid = create "data" scalarFieldClass appid "sfDataArray" : pf(1,:,:)*0.01 ; Convert pressure to millibars. "sfXCStartV" : min(pf&lon) ; Define start and end values of "sfXCEndV" : max(pf&lon) ; X and Y axis in lat/lon coords, "sfYCStartV" : min(pf&lat) ; since this data field will be "sfYCEndV" : max(pf&lat) ; contoured and overlaid on a map. end create ; ; Create a MapPlot object. Set several MapPlot resources for changing ; the map projection, for zooming in on a part of the map, for filling ; certain map areas and masking others, and for drawing the lat/lon ; grid over the ocean. ; ; Also set some View resources for increasing the size of the plot ; (View resources can be set when a MapPlot is created because the ; MapPlot class is a child of the View class, and thus inherits all ; of its resources). ; ; The rest of the MapPlot resources are being set in the "example1.res" ; resource file. ; map = create "mapplot" mapPlotClass wks "mpProjection" : "LambertEqualArea" ; Change the map projection. "mpCenterLonF" : (pf&lon(pf@nlon-1) + pf&lon(0))/2 "mpCenterLatF" : (pf&lat(pf@nlat-1) + pf&lat(0))/2 "mpLimitMode" : "LatLon" ; Limit the map view. "mpMinLonF" : min(pf&lon) "mpMaxLonF" : max(pf&lon) "mpMinLatF" : min(pf&lat) "mpMaxLatF" : max(pf&lat) "vpXF" : 0.1 ; Set some View resources to change the size "vpYF" : 0.9 ; and location of the plot on the viewport. "vpWidthF" : 0.7 "vpHeightF" : 0.7 end create ; ; Create a ContourPlot object. Turn on the drawing of a label bar ; and add a title. ; ; Many of the ContourPlot resources are being set in the "example1.res" ; resource file. ; contour = create "contourplot" contourPlotClass wks "cnScalarFieldData" : dataid ; Reference data object created. "cnLevelSelectionMode" : "ExplicitLevels" ; Define own contour levels. "cnLevels" : fspan(985.,1045.,13) "tiMainString" : ":F26:January 1996 storm" ; Main title "lbTitleString" : ":F25:pressure (mb)" ; Label bar title end create ; ; Overlay the ContourPlot object on MapPlot object. The MapPlot becomes ; the "base plot" and contains the overlay plot (the ContourPlot), so if ; you draw the MapPlot, the ContourPlot will get drawn as well. ; overlay(map,contour) draw(map) ; Draw the base plot (the MapPlot object). frame(wks) ; Advance the frame. endThe resource file "example1.res":
! ! Set some MapPlot resources. ! *mpPerimOn : True *mpFillOn : True *mpFillAreaSpecifiers : (/Water,Land,USStatesWater/) *mpSpecifiedFillColors : (/17,18,17/) *mpAreaMaskingOn : True *mpMaskAreaSpecifiers : USStatesLand *mpPerimOn : True *mpGridMaskMode : MaskLand ! ! Set some contour resources. ! *cnFillOn : True *cnLineLabelsOn : False *cnInfoLabelOn : False *cnLinesOn : False *cnFillDrawOrder : Predraw ! ! Turn on the drawing of a label bar with the contour plot only. ! *contourplot.pmLabelBarDisplayMode : Always *contourplot.pmLabelBarSide : Bottom ! ! Set some label bar resources. ! *lbOrientation : Horizontal *lbPerimOn : False
begin dir = ncargpath("data") uf = addfile(dir+"/cdf/Ustorm.cdf","r") ; Open three netCDF files. vf = addfile(dir+"/cdf/Vstorm.cdf","r") pf = addfile(dir+"/cdf/Pstorm.cdf","r") ; ; Create a PSWorkstation object. Change the background to white ; and the foreground to black. Rotate the plot 90 degrees and ; reposition it to fill up most of an 8-1/2" x 11" page. ; pswks = create "ps" psWorkstationClass defaultapp "wkForegroundColor" : (/0.,0.,0./) ; Set foreground to black. "wkBackgroundColor" : (/1.,1.,1./) ; Set background to white. "wkOrientation" : "Landscape" ; Landscape mode "wkDeviceLowerX" : -90 ; Specify the position of the plot in PS "wkDeviceLowerY" : -15 ; coordinate space (see the section "wkDeviceUpperX" : 710 ; "Positioning PS output on the page" in "wkDeviceUpperY" : 785 ; the GKS User Guide). end create MinLat = 18. ; Set min and max values for MaxLat = 65. ; part of map to zoom in on. MinLon = -140. MaxLon = -58. ; ; Create ScalarField object (to provide data for the ContourPlot object). ; sfdataid = create "scalarfield" scalarFieldClass defaultapp "sfDataArray" : pf->p(0,:,:)*0.01 ; Convert pf to millibars. "sfXCEndV" : pf->lon(dimsizes(pf->lon) - 1) "sfXCStartV" : pf->lon(0) "sfYCEndV" : pf->lat(dimsizes(pf->lat) - 1) "sfYCStartV" : pf->lat(0) "sfMissingValueV" : -9999.0 end create ; ; Create VectorField object (to provide data for the VectorPlot object). ; vfdataid = create "vectorfield" vectorFieldClass defaultapp "vfUDataArray" : uf->u(0,:,:) "vfVDataArray" : vf->v(0,:,:) "vfXCEndV" : uf->lon(dimsizes(uf->lon) - 1) "vfXCStartV" : uf->lon(0) "vfYCEndV" : uf->lat(dimsizes(uf->lat) - 1) "vfYCStartV" : uf->lat(0) "vfMissingUValueV" : -9999.0 end create ; ; Create MapPlot object. ; mapid = create "mapplot" mapPlotClass pswks "mpLimitMode" : "LatLon" ; Define portion of map to zoom in on. "mpMaxLatF" : MaxLat "mpMaxLonF" : MaxLon "mpMinLatF" : MinLat "mpMinLonF" : MinLon "mpPerimOn" : True ; Turn on map perimeter. "mpGridAndLimbOn" : False ; Turn off lat/lon grid lines. "vpXF" : 0.1 ; Increase size of and reposition plot "vpYF" : 0.9 ; in the viewport. "vpWidthF" : 0.8 "vpHeightF" : 0.8 end create ; ; Create ContourPlot object. ; cnid = create "contourplot" contourPlotClass pswks "cnScalarFieldData" : sfdataid end create ; ; Create VectorPlot object. ; vcid = create "vectorplot" vectorPlotClass pswks "vcVectorFieldData" : vfdataid end create ; ; Overlay ContourPlot object on MapPlot object. ; overlay(mapid,cnid) ; ; Overlay VectorPlot object on MapPlot object (which contains the ; ContourPlot at this point). ; overlay(mapid,vcid) ; ; Draw MapPlot object. Since it is the base plot, both the ; ContourPlot and VectorPlot also get drawn. ; draw(mapid) frame(pswks) ; Advance the frame. delete(pswks) ; Clean up (not necessary, but highly recommended). delete(sfdataid) delete(vfdataid) endThe above example produces the following plot:
(Click on image to see it enlarged.)