Project

General

Profile

How to mask .netcdf file from shape file?

Added by jyoti lodha over 5 years ago

Hi
I have used masklotlatbox,68,97.5,7.5,37.5 ip.nc out.nc for masking,but it does not work properly as at some places we get NAN values(outside ). So now i am having india.shp file to mask my input file so how it is possible using CDO? As no command is available for shape file extracting in CDO. Please find some solution so that i can solve it, as the data is in curvilinear grid it is difficult to extract. Help us to find some solution for it.


Replies (14)

RE: How to mask .netcdf file from shape file? - Added by Karin Meier-Fleischer over 5 years ago

Hi Jyoti,

you have to convert the shapefile india.shp to a netCDF file. As CDO can't do it you can use NCL instead.
The script which converts the shapefile is:

;-- copy shapefile_utils.ncl from 
;-- https://www.ncl.ucar.edu/Applications/Scripts/shapefile_utils.ncl
load "$HOME/NCL/shapefiles/shapefile_utils.ncl" 

shpname  = "./India/india.shp" 
maskname = "./India/india_mask.nc" 

print_shapefile_info(shpname)

;-- open data file to get the grid to be used
f = addfile("monmean2007_celcius.nc","r")

;-- read variable
var                    =  f->T2(0,:,:)
var@lat2d              =  f->XLAT
var@lon2d              =  f->XLONG

;-- shapefile mask resources
opt             =  True
opt@return_mask =  True    ;-- this forces the return of a 0s and 1s mask array

;-- create the mask based on the given shapefile
mask_array             =  shapefile_mask_data(var, shpname, opt)
mask_array!0           = "y" 
mask_array!1           = "x" 
mask_array@coordinates = "XLAT XLONG" 

;-- create new netCDF file and write mask array
system("rm -f " + maskname)
fout = addfile(maskname,"c")

fout->mask_array =  mask_array
fout->XLAT       =  f->XLAT
fout->XLONG      =  f->XLONG

Then you can use the india_mask.nc (see uploaded file) file with the CDO's.

-Karin

india_mask.nc (129 KB) india_mask.nc netCDF file based on india.shp

RE: How to mask .netcdf file from shape file? - Added by jyoti lodha over 5 years ago

Hi
Karin
Thanks a Lot. It works fine. Then I used cdo div input.nc maskfile.nc outputfile.nc for masking my region. Thanks a lot.

RE: How to mask .netcdf file from shape file? - Added by kunal bali over 5 years ago

is it possible to convert the attached .shp file in .nc

RE: How to mask .netcdf file from shape file? - Added by Karin Meier-Fleischer over 5 years ago

You need all associated files of north_dehli.shp they should be in the same directory:

north_dehli.dbf
north_dehli.prj
north_dehli.shx

Then you can use NCL to read the contents and write the data to a new netCDF file.

-Karin

RE: How to mask .netcdf file from shape file? - Added by jyoti lodha almost 5 years ago

Hi
I have to mask number of shapes files like India or Pakistan together in the same code winch you have written. Can it be Possible. As I tried but I got the error as it asking for "expected (1) elements" . As I am having 2 different shape file.

ncl test1.ncl
Copyright (C) 1995-2015 - All Rights Reserved
University Corporation for Atmospheric Research
NCAR Command Language Version 6.3.0
The use of this software is governed by a License Agreement.
See http://www.ncl.ucar.edu/ for more details.
fatal:Number of elements of dimension (0) of argument (1) is (2) in function (shapefile_mask_data), expected (1) elements
fatal:["Execute.c":8575]:Execute: Error occurred at or near line 19 in file test1.ncl

fatal:Variable (mask_array) is undefined
fatal:["Execute.c":8575]:Execute: Error occurred at or near line 20 in file test1.ncl

fatal:Variable (mask_array) is undefined
fatal:["Execute.c":8575]:Execute: Error occurred at or near line 21 in file test1.ncl

fatal:Variable (mask_array) is undefined, can not assign attribute (coordinates)
fatal:["Execute.c":8575]:Execute: Error occurred at or near line 22 in file test1.ncl

fatal:Variable (mask_array) is undefined
fatal:["Execute.c":8575]:Execute: Error occurred at or near line 28 in file test1.ncl

Wait for a positive response

RE: How to mask .netcdf file from shape file? - Added by Karin Meier-Fleischer about 4 years ago

You can use only one shape file per shapefile_mask_data call. Read the shapefile_mask_data function usage from NCL.

RE: How to mask .netcdf file from shape file? - Added by C Camargo almost 4 years ago

Hi,

I am trying to obtain a mask file from the Antarctica drainage basins (http://imbie.org/imbie-2016/drainage-basins/). I have the shapefile, which is composed of 19 polygons (basins), and I would like to transform it in a netcdf, so that I can combine with my data (which is in a 1 degree resolution world map).
I've tried following this script (copied below, with adaptations for my data), but the final mask is all 0. Could you tell me what I'm doing wrong?
I am guessing it's something related to the coordinate system (the Antartica shapefile is EPSG 3031), but don't know how to solve it.

Thank you

;-- copy shapefile_utils.ncl from
;-- https://www.ncl.ucar.edu/Applications/Scripts/shapefile_utils.ncl
;load "$HOME/NCL/shapefiles/shapefile_utils.ncl" 
load "/Users/ccamargo/Documents/ncl/shapefile_utils.ncl" 

shpname  = "./rignot/ANT_Basins_IMBIE2_v1.6/ANT_Basins_IMBIE2_v1.6.shp" 
maskname = "./rignot/ANT_Basins_IMBIE2_v1.6/ANT_Basins_mask.nc" 

print_shapefile_info(shpname)

;-- open data file to get the grid to be used
f = addfile("./rignot/landmask2.nc","r")

;-- read variable
var                    =  f->data(:,:)
var@lat2d              =  f->ylat
var@lon2d              =  f->xlon

;-- shapefile mask resources
opt             =  True
opt@return_mask =  True    ;-- this forces the return of a 0s and 1s mask array

;-- create the mask based on the given shapefile
mask_array             =  shapefile_mask_data(var, shpname, opt)
mask_array!0           = "y" 
mask_array!1           = "x" 
mask_array@coordinates = "ylat xlon" 

;-- create new netCDF file and write mask array
system("rm -f " + maskname)
fout = addfile(maskname,"c")

fout->mask_array =  mask_array
fout->ylat      =  f->ylat
fout->xlon      =  f->xlon

]]

ANT_Basins_IMBIE2_v1.6.zip (860 KB) ANT_Basins_IMBIE2_v1.6.zip shapefile folder
landmask2.nc (1020 KB) landmask2.nc nc file used to get the grid
ANT_Basins_mask.nc (760 KB) ANT_Basins_mask.nc final nc file (all 0)

RE: How to mask .netcdf file from shape file? - Added by Karin Meier-Fleischer almost 4 years ago

The shapefile has swapped longitude and latitude values! So NCL cannot work with it.
The following small NCL script checks the content of the shapefile in two ways.

load "$HOME/NCL/shapefiles/shapefile_utils.ncl" 

shpf    = addfile("ANT_Basins_IMBIE2_v1.6.shp", "r")
shp_lon = shpf->x
shp_lat = shpf->y

print_shapefile_info(shpname)

print("Lon: "+min(shp_lon)+" - "+max(shp_lon))
print("Lat: "+min(shp_lat)+" - "+max(shp_lat))

Output:

 Copyright (C) 1995-2019 - All Rights Reserved
 University Corporation for Atmospheric Research
 NCAR Command Language Version 6.6.2
 The use of this software is governed by a License Agreement.
 See http://www.ncl.ucar.edu/ for more details.
(0)    ======================================================================
(0)    Filename: "$HOME/Downloads/ANT_Basins_IMBIE2_v1.6.shp" 
(0)       Geometry type: polygon
(0)       # of features: 19
(0)       Min/max lat:   -180.00/ 179.82
(0)       Min/max lon:    -88.48/ -63.21
(0)       Variable names and their types:
(0)           geometry : integer
(0)           segments : integer
(0)           x : double
(0)           y : double
(0)           Regions : string
(0)           Subregion : string
(0)    ======================================================================
(0)    Lon: -88.47603217818096 - -63.21291351318195
(0)    Lat: -179.9998931884766 - 179.8186240405629

-Karin

RE: How to mask .netcdf file from shape file? - Added by sara Abdelaziz over 2 years ago

Kindly can you assisst in masking the attached netcdf file? i successfully converted the shapefile eez.shp to eez22.nc but when trying to multiply it to the wsgmax10m_rcp85_land-cpm_uk_2.2km_01_1hr_19801201-19801230.nc (avilable inhttps://drive.google.com/file/d/1Voj6CzmPgWvFs5OKjbHwGVdmDZm1NEPJ/view?usp=sharing) I get errors because of dimensions.

eez22.nc (23 MB) eez22.nc
eez2.cpg (5 Bytes) eez2.cpg
eez2.prj (149 Bytes) eez2.prj
eez2.dbf (4.81 KB) eez2.dbf
eez2.shx (108 Bytes) eez2.shx
eez2.shp (5.31 MB) eez2.shp

RE: How to mask .netcdf file from shape file? - Added by Simran Suresh 9 months ago

jyoti lodha wrote in RE: How to mask .netcdf file from shape file?:

Hi
Karin
Thanks a Lot. It works fine. Then I used cdo div input.nc maskfile.nc outputfile.nc for masking my region. Thanks a lot.

I have used the cdo div to mask out (mask.nc) my input file(2003_01_EU_3km.nc), but the values are divided(see result 2003_01_SWE_WS.nc). I only need the input to be filtered based on the mask index with no values modified. Kindly help.

RE: How to mask .netcdf file from shape file? - Added by Karin Meier-Fleischer 9 months ago

Hi Simran,

can you tell me more about your problem. What do you want to do with which file of the two given netcdf files?

RE: How to mask .netcdf file from shape file? - Added by Simran Suresh 9 months ago

I had a shapefile which I converted to nc file (mask.nc) using regionmask mask_geopandas. I want to mask out the input file (2003_01_EU_3km.nc) using this file. I tried using cdo div 2003_01_EU_3km.nc mask.nc masked_2003_01_EU_3km.nc.
The result masked file is as expected but the scale is such that the values are divided by the indices of mask regions. I want to retain the values as it is, while only masking out the region based on lat lon.

RE: How to mask .netcdf file from shape file? - Added by Karin Meier-Fleischer 9 months ago

The cdo -div does not make sense with your kind of 'mask' because it needs a mask file containing only zeros and ones.

You have to set all values in the mask.nc file to 1 except missing values.

RE: How to mask .netcdf file from shape file? - Added by Simran Suresh 9 months ago

Dear Karin,

This way works for me! Thanks a lot.

Best,
Simran

    (1-14/14)