[ncl-talk] Using merged shapefiles to create PRISM data subset

Mary Haley haley at ucar.edu
Thu Dec 20 08:42:18 MST 2018


Hi Toni,

If you just want an average of the data masked by a particular shapefile,
it doesn't matter whether the shapefile has one features or several
features. You can call the shapefile_mask_data function (in the
shapefiles_utils.ncl
<http://www.ncl.ucar.edu/Applications/Scripts/shapefile_utils.ncl> file),
which takes your original data variable and sets values to missing if they
fall outside the shapefile area:

  data_mask = shapefile_mask_data(data,"8x_100_west.shp",opt)

Then, you can average that array with:

  data_avg(data_mask)
I've attached a script (plot_data_masked_by_shapefiles.ncl) and a couple of
images that shows how to do this with the original shapefile you gave me.

In order to mask the data by two shapefiles, what you can do is have
shapefile_mask_data return a mask array (an array of the same size but set
to 0 where the data is outside the shapefile areas, and set to 1 inside the
shapefile areas).  You can then use "where" ("mask" would also work) to
apply the mask to whatever data you want:


  shp_west_name = "8x_west.shp"
  shp_east_name = "8x_east.shp"



;---Calculate mask arrays based on shapefile data
  opt = True
  opt at return_mask = True
  data_mask_west = shapefile_mask_data(data,shp_west_name,opt)
  data_mask_east = shapefile_mask_data(data,shp_east_name,opt)

;---Keep the data at locations where mask arrays are equal to 1.
  data_mask = where(data_mask_east.eq.1.or.data_mask_west.eq.1,data,data@
_FillValue)
  copy_VarMeta(data,data_mask)


I think it's important to understand how the shapefile_mask_data is working
on my data, so I usually like to plot it and then add markers at locations
where the data was kept versus where it was masked. I included panel plots
of both original data and masked data, and then separate plots of the
masked data with the markers added.

--Mary




On Wed, Dec 19, 2018 at 11:42 PM Toni Klemm <toni-klemm at tamu.edu> wrote:

> Hi Mary,
>
> Thank you very much! Your result is almost exactly what I’m trying to get.
> What I need is the average temperature / annual precipitation for all the
> counties together, not separate for each county or shapefile feature. I
> just tried it again with a shapefile in which each county is a separate
> feature, but unfortunately I got pretty much the same result as before. I
> attached the shapefiles (there are two sets, for counties with the center
> point east and west of the 100th meridian). I also attached a screenshot of
> the data result in Panoply and my NCL code.
>
> I would greatly appreciate if you could take a look at the shapefile data
> and change it so it works correctly in NCL.
>
> Thanks again,
> Toni
>
>
> *Toni Klemm**, Ph.D.*
> Postdoctoral Research Associate
> Department of Ecosystem Science and Management
> College of Agriculture and Life Sciences
> Texas A&M University, College Station, TX
> Contributor to the Early Career Climate Forum <https://www.eccforum.org/>
> www.toni-klemm.de | @toniklemm <https://twitter.com/ToniKlemm>
>
>
>
>
>
>
> On Dec 19, 2018, at 5:06 PM, Mary Haley <haley at ucar.edu> wrote:
>
> Hi Toni,
>
> Your shapefile has just one feature and several segments for that
> feature.  Usually the shapefiles we see have several features and several
> segments for each feature. Your shapefile is a little unusual, but not
> unheard of.  Unfortunately, the examples we have all do the averaging
> across shapefile *features* and not shapefile *segments*.
>
> This doesn't mean you can't do it with NCL.  See the attached plot, where
> I contoured some dummy 2D data over the area of interest and then generated
> a second plot where the values were averaged based on whether they fell
> inside one of these segments.  Is this what you are trying to accomplish?
> If so, I'll clean up the code and send it to you.
>
> --Mary
>
> On Wed, Dec 19, 2018 at 1:26 PM Toni Klemm <toni-klemm at tamu.edu> wrote:
>
>> Hello All,
>>
>> I am trying to use a shapefile of merged counties in the western Great
>> Plains to create a spatial subset of temperature and precipitation data and
>> calculate averages and totals for those counties as a whole. However, the
>> data subset doesn't look anything like the shapefile (see the Panoply
>> screenshot). I attached the shapefile as GIS files and screenshot.
>>
>> My guess is that NCL gets confused about the disconnected-yet-combined
>> counties and doesn't know how to process a shapefile like that. Does anyone
>> know what I can do to make this work? Can I manipulate the shapefile data
>> in QGIS somehow so NCL understands it? Alternatively I could process each
>> county by itself and then average the results. But there are over 100
>> counties, which would take a very long time.
>>
>> I appreciate any help!
>>
>> Thank you very much,
>> Toni
>>
>>
>>
>> *Toni Klemm, Ph.D.*Postdoctoral Research Associate
>> Department of Ecosystem Science and Management
>> College of Agriculture and Life Sciences
>> Texas A&M University, College Station, TX
>> Contributor to the Early Career Climate Forum
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.eccforum.org&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=WsLJ5ywbW3XnTzqo_ETV62H6s1a5bcGgnrpl7xPRfic&m=S4Vpn9B9dGEDj8PfFUyL3ZZyY6XSazVZ50UULc2pwEU&s=_wmxrfPQsMF3vojIFKRAtdhltlvptw6M4HZ9fLg_3cQ&e=>
>> www.toni-klemm.de
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.toni-2Dklemm.de&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=WsLJ5ywbW3XnTzqo_ETV62H6s1a5bcGgnrpl7xPRfic&m=S4Vpn9B9dGEDj8PfFUyL3ZZyY6XSazVZ50UULc2pwEU&s=BUcEg1gjQSTNzMQTj8-c30-A9wVn4XtHJXwJ98GJFdc&e=>
>>  | @toniklemm
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__twitter.com_toniklemm&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=WsLJ5ywbW3XnTzqo_ETV62H6s1a5bcGgnrpl7xPRfic&m=S4Vpn9B9dGEDj8PfFUyL3ZZyY6XSazVZ50UULc2pwEU&s=4uj7i-TM4DYGnhfVvqE6_pSkQa5N6ZthvK0BCZWNzK8&e=>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__mailman.ucar.edu_mailman_listinfo_ncl-2Dtalk&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=WsLJ5ywbW3XnTzqo_ETV62H6s1a5bcGgnrpl7xPRfic&m=S4Vpn9B9dGEDj8PfFUyL3ZZyY6XSazVZ50UULc2pwEU&s=PmMsnjl-ihptp2YXVfcxUt782zRG1XbIMrqgS8bUQZg&e=>
>>
> <shapefile.000002.png>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plot_data_masked_by_shapefile.ncl
Type: application/octet-stream
Size: 4613 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: data_masked_by_shapefiles.000002.png
Type: image/png
Size: 496976 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: data_masked_by_shapefiles.000001.png
Type: image/png
Size: 204155 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: data_masked_by_shapefile.000001.png
Type: image/png
Size: 154946 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: data_masked_by_shapefile.000002.png
Type: image/png
Size: 432807 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plot_data_masked_by_shapefiles.ncl
Type: application/octet-stream
Size: 5351 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181220/bdf11474/attachment-0003.obj>


More information about the ncl-talk mailing list