[ncl-talk] Adding a Legend to Shapefile Plot

Carlos J. Valle-Diaz cj.vallediaz at gmail.com
Mon Dec 21 11:56:21 MST 2015


Hi Rick,

It made sense to me now. Thank you!

I am sending you the script that plots the colored markers and black
circles, but with no legend. When I run it in NCL, it does not give me the
mismatch error.

Carlos

On Mon, Dec 21, 2015 at 2:44 PM, Rick Brownrigg <brownrig at ucar.edu> wrote:

> Hi Carlos,
>
> I don't recall the before and after state of your script. But in general,
> NCL is what I would call "loosely typed":  you don't have to declare the
> type of a variable in advance, but once you make an assignment to a
> variable name, the variable assumes the type of the right-hand-side.  If
> the RHS is an array, the variable further assumes the shape of the array.
>
> Attempts to reassign the variable to a RHS of a different type/shape will
> lead to the "assignment type mismatch" error. The exception is if the RHS
> can be cast to a type of the variable. So for example:
>
> a = 10.5
> a = 10    ; OK -- int is cast to a float
>
> b = 10
> b = 10.5  ; error -- can't cast a float to an int
>
> If you need/want to reuse a variable name, you "delete()" it first.
>
> Make sense?
>
> Rick
>
>
> On Mon, Dec 21, 2015 at 10:58 AM, Carlos J. Valle-Diaz <
> cj.vallediaz at gmail.com> wrote:
>
>> Hi Rick,
>>
>> Related to your 3rd suggestion: when I ran the script before I
>> incorportated the codes to create the legend, I did not have a mismatch
>> error. The script generated the colored markers with the black circles as
>> you showed me in a previous conversation. Why would it present a mismatch
>> error after adding the codes creating the legend?
>>
>> Carlos
>>
>> On Mon, Dec 21, 2015 at 1:37 PM, Carlos J. Valle-Diaz <
>> cj.vallediaz at gmail.com> wrote:
>>
>>> I'll check these right now and will let you know. Thank you very much.
>>>
>>> I apologize for writing multiple emails on the same topic. Won't happen
>>> again!
>>>
>>> Carlos
>>>
>>> On Mon, Dec 21, 2015 at 12:45 PM, Rick Brownrigg <brownrig at ucar.edu>
>>> wrote:
>>>
>>>> The script you attached to your original posting has numerous bugs, and
>>>> will not even run as is. The do-loop at around line 82 is missing an "end
>>>> do".  Fixing that problem allows the script to run, but there are 3 other
>>>> errors that are likely causing your problem:
>>>>
>>>> i) the variable "R" is undefined (around line 34)
>>>>
>>>> ii) xleg (and other related arrays) contain only 8 values, whereas you
>>>> are looping over "num_distinct_makers", which is equal to 17  (around line
>>>> 90)
>>>>
>>>> iii) the "assignment type mismatch" at around line 103 arises because
>>>> in a previous loop, you set gsMarkerColor to an integer value, and then at
>>>> 103, you are setting it to a value from the colortable, which is either a 3
>>>> or 4-tuple of floats.  You'll want to "delete(gsres at gsMarkerColor)"
>>>> between the two do-loops.
>>>>
>>>> Hope that helps...
>>>> Rick
>>>>
>>>>
>>>> On Mon, Dec 21, 2015 at 8:39 AM, Carlos J. Valle-Diaz <
>>>> cj.vallediaz at gmail.com> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I'm writing to ask if somebody from NCL-talk has read my email and/or
>>>>> currently is answering it. I'm kind of in a rush to have these plots ready
>>>>> before Christmas. I apologize for my insistence.
>>>>>
>>>>> Best Regards,
>>>>>
>>>>> Carlos
>>>>>
>>>>> On Fri, Dec 18, 2015 at 4:52 PM, Carlos J. Valle-Diaz <
>>>>> cj.vallediaz at gmail.com> wrote:
>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> I've been running WRF simulations to observe 24hr precipitation in
>>>>>> Puerto Rico from (July 12, 2011 12UTC to July 13, 2011 12UTC). I want to
>>>>>> compare the model runs with the observed from the National Weather Service.
>>>>>> I have their hourly shapefiles, see link to access them:
>>>>>> http://www.srh.noaa.gov/ridge2/Precip/qpehourlyshape/2011/201107/20110712/).
>>>>>> In the shapefiles, Globvalue refers to precipitation.
>>>>>>
>>>>>> I've been trying all day to add a legend to my plot, but haven't been
>>>>>> successful. I've been using example #7 in the legends section of NCL as a
>>>>>> reference. See leg_7.ncl at
>>>>>> http://ncl.ucar.edu/Applications/legend.shtml
>>>>>>
>>>>>> I have attached the script I have at the moment. It plots fine when
>>>>>> you take out the lines that codes for the legend and text. What am I
>>>>>> missing and/or doing wrong?
>>>>>>
>>>>>> Thanks beforehand for the help,
>>>>>>
>>>>>> P.S. I set up the array(arr) data for 16 values because I'm using a
>>>>>> color map that displays 17 colors.
>>>>>> --
>>>>>> Carlos J. Valle Diaz
>>>>>> Ph.D. Chemistry Student
>>>>>> University of Puerto Rico Rio Piedras Campus
>>>>>> Department of Graduate Chemistry and Environmental Sciences
>>>>>> cj.vallediaz at gmail.com
>>>>>> Tel. 787-764-0000 x88192
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Carlos J. Valle Diaz
>>>>> Ph.D. Chemistry Student
>>>>> University of Puerto Rico Rio Piedras Campus
>>>>> Department of Graduate Chemistry and Environmental Sciences
>>>>> cj.vallediaz at gmail.com
>>>>> Tel. 787-764-0000 x88192
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> ncl-talk at ucar.edu
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>> Carlos J. Valle Diaz
>>> Ph.D. Chemistry Student
>>> University of Puerto Rico Rio Piedras Campus
>>> Department of Graduate Chemistry and Environmental Sciences
>>> cj.vallediaz at gmail.com
>>> Tel. 787-764-0000 x88192
>>>
>>
>>
>>
>> --
>> Carlos J. Valle Diaz
>> Ph.D. Chemistry Student
>> University of Puerto Rico Rio Piedras Campus
>> Department of Graduate Chemistry and Environmental Sciences
>> cj.vallediaz at gmail.com
>> Tel. 787-764-0000 x88192
>>
>
>


-- 
Carlos J. Valle Diaz
Ph.D. Chemistry Student
University of Puerto Rico Rio Piedras Campus
Department of Graduate Chemistry and Environmental Sciences
cj.vallediaz at gmail.com
Tel. 787-764-0000 x88192
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151221/5fcc4f36/attachment.html 
-------------- next part --------------
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"

begin
  wks  = gsn_open_wks("X11","NWS-Precip-2011071215")

  f = addfile("nws_precip_2011071215.shp","r")

  minLat = 17.30
  maxLat = 19
  latRange = maxLat - minLat
  minLon = 292
  maxLon = 295
  lonRange = maxLon - minLon

  globMin = min(f->Globvalue)
  globRange = max(f->Globvalue) - min(f->Globvalue)

  mres                       = True
  mres at mpDataBaseVersion     = "MediumRes"
  mres at mpLimitMode           = "Corners"     ; corner method of zoom
  mres at mpLeftCornerLatF      = minLat - (latRange * .01)
  mres at mpLeftCornerLonF      = minLon - (lonRange * .01)
  mres at mpRightCornerLatF     = maxLat + (latRange * .01)
  mres at mpRightCornerLonF     = maxLon + (lonRange * .01)

  mres at gsnDraw               = True          ; don't draw yet
  mres at gsnFrame              = False         ; don't advance frame yet
  mres at gsnMaximize           = False
  mres at pmTickMarkDisplayMode = "Always"      ; turn on tickmarks
  mres at tiMainString          = "NWS Precipitation July 12 2011 at 15 UTC"

  plot = gsn_csm_map(wks,mres)

  cmap = read_colormap_file("prcp_1")
  numColors = dimsizes(cmap(:,0)) - 1        ; skip the first color in the table

  print(globMin + " " + globRange)

  txres = True
  txres at txFontHeightF = 0.015

  gsres = True

   do i=0, dimsizes(f->Globvalue) - 1
      indx = doubletoint( (f->Globvalue(i)-globMin) / globRange * numColors) + 1  ; skip first color in table
      if (indx.ge.numColors) then
          indx = numColors - 1
      end if
      
      gsres at gsMarkerIndex = 16
      gsres at gsMarkerSizeF = 0.005
      gsres at gsMarkerColor = cmap(indx, :)
      gsn_polymarker(wks, plot, f->Lon(i), f->Lat(i), gsres)
      gsres at gsMarkerIndex = 4
      gsres at gsMarkerColor = (/ 0., 0., 0., 1. /)   ; opaque-black
      gsn_polymarker(wks, plot, f->Lon(i), f->Lat(i), gsres)
;       gsn_polymarker_ndc(wks, x, y, gsres)
;       gsn_text_ndc(wks, text, x, y, txres)

   end do

 draw(plot)
 frame(wks)

end


More information about the ncl-talk mailing list