[ncl-talk] masking the contiguous United States

Mary Haley haley at ucar.edu
Wed Jul 8 13:42:58 MDT 2015


Ming,

I think the issue is that you are using a shapefile that contains only one
"feature", which is "United States".  In order to properly mask against
areas like "Alaska", you need a shapefile which contains features that
include state names, like "Alaska", "Hawaii", etc.

The USA_adm/USA_adm1.shp file from gadm.org has this type of features, so I
used that to create an example with dummy data.

The masking is done with, "shapefile_mask_data", which is in the
"shapefile_mask_data.ncl" script attached to this email (it's also linked
to from the shapefiles examples page).

  opt             = True
  opt at debug       = True
  opt at shape_var   = "NAME_1"
  opt at shape_names = "Alaska"
  opt at keep        = False   ; This tells the function to *throw
                            ; away* points inside of Alaska.
                            ; The default is to keep them.

  data_mask  = shapefile_mask_data(data,"USA_adm/USA_adm1.shp",opt)

I've attached a sample script and image.

--Mary

On Wed, Jul 8, 2015 at 1:00 PM, Ming Chen <chen1718 at umn.edu> wrote:

> Mary,
>
> Thank you for the information. I tried -127, the result is the same. I
> guess this is a bug in the shapefile_mask_data.ncl script.
>
> Anyway, I used ArcGIS to subset the shapefile to conterminous US and it
> worked.
>
> Thanks!
> Ming
>
> On Wed, Jul 8, 2015 at 12:21 PM, Mary Haley <haley at ucar.edu> wrote:
>
>> Hi Ming,
>>
>> Sorry for the delay in response.  I think you might need to use something
>> larger than -130, like -127.
>>
>> I'm in a conference this week. If you continue to have problems, you
>> might try the masking using shapefile names, rather than lat/lon boundaries.
>>
>> --Mary
>>
>>
>> On Sun, Jul 5, 2015 at 12:34 PM, Ming Chen <chen1718 at umn.edu> wrote:
>>
>>> Thanks a lot Mary!
>>>
>>> The lonFlip function worked and I was able to mask the data.
>>> However, I found that Alaska is also masked in, while I just need the
>>> contiguous US.
>>> I have specified the max/min of longitude and latitude, is it not
>>> working?
>>>
>>> Here is the debug message:
>>> (0)    ==================================================
>>> (0)    Shapefile:         cb_2014_us_nation_20m.shp   *###NOTE that I
>>> changed a shape file because the previous one did not work well and only
>>> masked in the Alaska area. ###* This one could be download at
>>> https://www.census.gov/geo/maps-data/data/cbf/cbf_nation.html
>>> (0)    Areas of interest: the whole shapefile
>>> (0)    min_lat_chk:       20
>>> (0)    max_lat_chk:       50
>>> (0)    min_lon_chk:       -130
>>> (0)    max_lon_chk:       -65
>>> (0)    min_lat_data:      -90
>>> (0)    max_lat_data:      90
>>> (0)    min_lon_data:      -180
>>> (0)    max_lon_data:      177.5
>>> (0)    13824 data values originally
>>> (0)    Will keep data values inside given shapefile areas
>>> (0)    ==================================================
>>> (0)    229 data values kept
>>> (0)    shapefile_mask_data: elapsed time: 0.377945 CPU seconds.
>>> (0)    ==================================================
>>>
>>> Thanks,
>>> Ming
>>>
>>> On Sat, Jul 4, 2015 at 11:45 PM, Mary Haley <haley at ucar.edu> wrote:
>>>
>>>> If you have lat/lon coordinate arrays attached to your data, then can
>>>> try the "lonFlip" function to convert your data to be -180 to 180 longitude:
>>>>
>>>> http://www.ncl.ucar.edu/Document/Functions/Contributed/lonFlip.shtml
>>>>
>>>> If you don't have coordinate arrays, but instead are using the special
>>>> lat2d/lon2d or lat1d/lon1d attributes, then you can try the "where"
>>>> function:
>>>>
>>>> NEE_AVG at lon2d = where(lon2d.gt.180, lon2d - 360, lon2d)
>>>>
>>>> Note: I don't know what your longitude array is called.  I'm just using
>>>> "lon2d" as an example here.  You can operate on the attribute directly:
>>>>
>>>> NEE_AVG at lon2d = where(NEE_AVG @lon2d.gt.180, NEE_AVG @lon2d - 360,
>>>>  NEE_AVG at lon2d)
>>>>
>>>> You will also need to update your minlon/maxlon attributes to be in
>>>> -180 to 180
>>>>
>>>>   opt at minlon = -130   ; 230 - 360
>>>>   opt at maxlon = -65   ; 295 - 360
>>>>
>>>>
>>>> --Mary
>>>>
>>>>
>>>>
>>>> On Sat, Jul 4, 2015 at 9:34 PM, Ming Chen <chen1718 at umn.edu> wrote:
>>>>
>>>>> I think I know why it is happening... The data I'm trying to mask has
>>>>> the longitude from 0 to 357.5, while the shapefile has the longitude from
>>>>> -179.1 to 179.8...
>>>>>
>>>>> Does anyone has suggestion how I can fix that? Thanks!
>>>>>
>>>>> Ming
>>>>>
>>>>>
>>>>>
>>>>> On Sat, Jul 4, 2015 at 10:19 PM, Ming Chen <chen1718 at umn.edu> wrote:
>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I was trying to mask the contiguous United States using a shape file
>>>>>> and the script "shapefile_mask_data.ncl", but got all missing values.
>>>>>>
>>>>>> I used ncl 6.2.1. The script I used is here:
>>>>>>
>>>>>> ;Begins to calculate US averaged NEE
>>>>>>   shp_filename = "coastl_usa.shp"
>>>>>>   opt = True
>>>>>>   opt at minlat = 20
>>>>>>   opt at maxlat = 50
>>>>>>   opt at minlon = 230
>>>>>>   opt at maxlon = 295
>>>>>>   opt at debug  = True
>>>>>>   opt at keep   = True
>>>>>>   opt at loop_check   = True
>>>>>>   data_mask    = shapefile_mask_data(NEE_AVG,shp_filename,opt)
>>>>>>   NEE_scalar   = wgt_areaave(data_mask,1.,1.,0) ;averaged NEE over
>>>>>> the United States
>>>>>>
>>>>>>
>>>>>> And here is the message I got:
>>>>>> (0)    ==================================================
>>>>>> (0)    Shapefile:         coastl_usa.shp
>>>>>> (0)    Areas of interest: the whole shapefile
>>>>>> (0)    min_lat_chk:       20
>>>>>> (0)    max_lat_chk:       50
>>>>>> (0)    min_lon_chk:       230
>>>>>> (0)    max_lon_chk:       295
>>>>>> (0)    min_lat_data:      -90
>>>>>> (0)    max_lat_data:      90
>>>>>> (0)    min_lon_data:      0
>>>>>> (0)    max_lon_data:      357.5
>>>>>> (0)    13824 data values originally
>>>>>> (0)    Will keep data values inside given shapefile areas
>>>>>> (0)    ==================================================
>>>>>> (0)    0 data values kept
>>>>>> (0)    shapefile_mask_data: elapsed time: 0.930859 CPU seconds.
>>>>>> (0)    ==================================================
>>>>>>
>>>>>> I can see there are 13824 data points in my NEE_AVG data. And I have
>>>>>> plot that without problem. Why there was no data masked?
>>>>>>
>>>>>> Thanks,
>>>>>> Ming
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Department of Soil, Water and Climate
>>>>>> 439 Borlaug Hall 1991 Upper Buford Cir.
>>>>>> St. Paul 55108
>>>>>> MN USA
>>>>>> Office: 612-624-1645
>>>>>> http://www.biometeorology.umn.edu/people_ming.php
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Department of Soil, Water and Climate
>>>>> 439 Borlaug Hall 1991 Upper Buford Cir.
>>>>> St. Paul 55108
>>>>> MN USA
>>>>> Office: 612-624-1645
>>>>> http://www.biometeorology.umn.edu/people_ming.php
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> ncl-talk at ucar.edu
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>> Department of Soil, Water and Climate
>>> 439 Borlaug Hall 1991 Upper Buford Cir.
>>> St. Paul 55108
>>> MN USA
>>> Office: 612-624-1645
>>> http://www.biometeorology.umn.edu/people_ming.php
>>>
>>
>>
>
>
> --
> Department of Soil, Water and Climate
> 439 Borlaug Hall 1991 Upper Buford Cir.
> St. Paul 55108
> MN USA
> Office: 612-624-1645
> http://www.biometeorology.umn.edu/people_ming.php
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150708/a4eb7b92/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shapefiles.png
Type: image/png
Size: 162770 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150708/a4eb7b92/attachment.png 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shapefiles_mask_alaska.ncl
Type: application/octet-stream
Size: 3548 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150708/a4eb7b92/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shapefile_mask_data.ncl
Type: application/octet-stream
Size: 13478 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150708/a4eb7b92/attachment-0001.obj 


More information about the ncl-talk mailing list