[ncl-talk] converting shapefile to netcdf

Amy Hendricks ashendricks at alaska.edu
Wed Nov 27 13:37:33 MST 2019


Hi David,

Yes, I found a loop was the only way to do this. Thank you for clearing
that up.  Why can't ncl_convert2nc work for this purpose?

"ncl_convert2nc converts one or more GRIB1, GRIB2, HDF 4, HDF-EOS 2,
HDF-EOS 5, netCDF, and/or shapefile files to netCDF formatted files"


  shp_file_name  = "tresh_ease23.shp"
  shape_var_name = "tresh"

;========================================================
; Region 1
;========================================================

  areas_of_interest = 1

  print("areas_of_interest = "+ areas_of_interest)

  opt             = True
  opt at debug       = True
  opt at return_mask = True
  opt at minlat = minlat
  opt at maxlat = maxlat
  opt at shape_var = shape_var_name
  opt at shape_names = areas_of_interest

  temp_mask = shapefile_mask_data(t2flip,shp_file_name,opt)
  copy_VarMeta(t2flip,temp_mask)
  printVarSummary(temp_mask)

  tresh_mask = temp_mask

  delete(temp_mask)


;========================================================
; Region 2 - 25
;========================================================


  do k = 2, 25
    areas_of_interest = k

    print("area of interest = "+k)

    opt             = True
    opt at debug       = True
    opt at return_mask = True
    opt at minlat = minlat
    opt at maxlat = maxlat
    opt at shape_var = shape_var_name
    opt at shape_names = areas_of_interest

    temp_mask = shapefile_mask_data(t2flip,shp_file_name,opt)

    do i=1, 721
      do j=1, 1440
        if(temp_mask(i-1,j-1).eq.1)
        temp_mask(i-1,j-1) = areas_of_interest
        tresh_mask(i-1,j-1) = (/temp_mask(i-1,j-1)/)
        end if
      end do
    end do


    delete(temp_mask)

  end do

On Wed, Nov 27, 2019 at 12:07 AM Hiep Duc <Hiep.Duc at environment.nsw.gov.au>
wrote:

> Hi all,
>
>
>
> Is it possible to convert from netcdf file back to shapefile ?.
>
> Or is this transformation one way only ?
>
> Thanks
>
> Hiep
>
>
>
> *From:* ncl-talk <ncl-talk-bounces at ucar.edu> *On Behalf Of *Dave Allured
> - NOAA Affiliate via ncl-talk
> *Sent:* Wednesday, 27 November 2019 6:27 PM
> *To:* Amy Hendricks <ashendricks at alaska.edu>
> *Cc:* ncl-talk at ucar.edu
> *Subject:* Re: [ncl-talk] converting shapefile to netcdf
>
>
>
> Correction, the name of this example is shapefiles_14_mask, not just
> shapefiles_14.
>
>
>
>
>
> On Tue, Nov 26, 2019 at 8:57 PM Dave Allured - NOAA Affiliate <
> dave.allured at noaa.gov> wrote:
>
> Example shapefiles_14 is close to what you want.  Please notice these two
> lines.  Their function is to select a particular subset of all the state
> outlines in the example shapefile, then apply a binary mask to the base
> grid "tc_lev0" so that only grid points over the listed states are
> retained.  All grid points outside this list of states are set to missing.
> This demonstrates how to access a subset of a shape file, instead of all
> regions together,
>
>    opt at shape_names = (/"Texas","New
> Mexico","Colorado","Kansas","Oklahoma"/)
>    tc_mask  = shapefile_mask_data(tc_lev0,shp_filename1,opt)
>
> For your application, use this masking function to select one region at a
> time.  Start with a master grid with ERA5 coordinates, set to all zero or
> missing values.  Loop over each region value or region number.  Call the
> function shapefile_mask_data inside the loop, selecting the grid points for
> only one region each time. Use the resulting binary mask to add the
> current region number to the master grid.  When the loop is complete, write
> the master grid with all region numbers to a Netcdf file.
>
>
>
>
>
> On Tue, Nov 26, 2019 at 3:51 PM Amy Hendricks via ncl-talk <
> ncl-talk at ucar.edu> wrote:
>
> Hello NCL geniuses,
>
>
>
> I am trying to write a shapefile to netcdf. The shapefile consists of
> circumpolar Treshnikov regions, and I would like to create a netCDF file
> that has the region values like tresh(lon,lat) = [region# from shapefile]
> on the ERA5 grid. All the examples I'm finding are for creating binary
> masks, and the ncl_convert2nc does not seem to work, at least it doesn't
> return anything useable.
>
>
>
> Attached are the ERA5 netCDF file, Treshnikov shapefile, as well as the
> result of ncl_convert2nc.
>
>
>
> Thank you in advance,
>
>
>
> Amy
>
>
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
> This email is intended for the addressee(s) named and may contain
> confidential and/or privileged information.
> If you are not the intended recipient, please notify the sender and then
> delete it immediately.
> Any views expressed in this email are those of the individual sender
> except where the sender expressly and with authority states them to be the
> views of the NSW Office of Environment and Heritage.
>
> PLEASE CONSIDER THE ENVIRONMENT BEFORE PRINTING THIS EMAIL
>


-- 
Amy Hendricks
Graduate Research Assistant
Department of Atmospheric Sciences
University of Alaska Fairbanks
IARC 343-E
907-388-9893
ashendricks at alaska.edu

On Tue, Nov 26, 2019 at 10:26 PM Dave Allured - NOAA Affiliate <
dave.allured at noaa.gov> wrote:

> Correction, the name of this example is shapefiles_14_mask, not just
> shapefiles_14.
>
>
> On Tue, Nov 26, 2019 at 8:57 PM Dave Allured - NOAA Affiliate <
> dave.allured at noaa.gov> wrote:
>
>> Example shapefiles_14 is close to what you want.  Please notice these two
>> lines.  Their function is to select a particular subset of all the state
>> outlines in the example shapefile, then apply a binary mask to the base
>> grid "tc_lev0" so that only grid points over the listed states are
>> retained.  All grid points outside this list of states are set to missing.
>> This demonstrates how to access a subset of a shape file, instead of all
>> regions together,
>>
>>    opt at shape_names = (/"Texas","New
>> Mexico","Colorado","Kansas","Oklahoma"/)
>>    tc_mask  = shapefile_mask_data(tc_lev0,shp_filename1,opt)
>>
>> For your application, use this masking function to select one region at a
>> time.  Start with a master grid with ERA5 coordinates, set to all zero or
>> missing values.  Loop over each region value or region number.  Call the
>> function shapefile_mask_data inside the loop, selecting the grid points for
>> only one region each time. Use the resulting binary mask to add the
>> current region number to the master grid.  When the loop is complete, write
>> the master grid with all region numbers to a Netcdf file.
>>
>>
>> On Tue, Nov 26, 2019 at 3:51 PM Amy Hendricks via ncl-talk <
>> ncl-talk at ucar.edu> wrote:
>>
>>> Hello NCL geniuses,
>>>
>>> I am trying to write a shapefile to netcdf. The shapefile consists of
>>> circumpolar Treshnikov regions, and I would like to create a netCDF file
>>> that has the region values like tresh(lon,lat) = [region# from shapefile]
>>> on the ERA5 grid. All the examples I'm finding are for creating binary
>>> masks, and the ncl_convert2nc does not seem to work, at least it doesn't
>>> return anything useable.
>>>
>>> Attached are the ERA5 netCDF file, Treshnikov shapefile, as well as the
>>> result of ncl_convert2nc.
>>>
>>> Thank you in advance,
>>>
>>> Amy
>>>
>>

-- 
Amy Hendricks
Graduate Research Assistant
Department of Atmospheric Sciences
University of Alaska Fairbanks
IARC 343-E
907-388-9893
ashendricks at alaska.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20191127/c251701f/attachment.html>


More information about the ncl-talk mailing list