Project

General

Profile

failed masking ncfile by shpfile

Added by Qiyun Ma about 4 years ago

Hi,
I am trying to mask ncfiles by shpfile followed by the answer: https://code.mpimet.mpg.de/boards/2/topics/9139.
But I failed. I guess the reason is that the created masked file is lack of georeference.

I have tried many times. Could someone please help me?
The script I used was covert.ncl.

Thanks,
Qiyun

covert.ncl (1.09 KB) covert.ncl the script
jra55_JJA_1993m.nc (167 KB) jra55_JJA_1993m.nc the reanalysis ncfile which needs to be masked
US_west_shpfile.rar (703 KB) US_west_shpfile.rar the shpfile

Replies (5)

RE: failed masking ncfile by shpfile - Added by kunal bali about 4 years ago

Hi Qiyun,

Steps:

1.) I have downloaded the one fine resolution file. e.g (0.01 degree) then created the Shapefiles according to that resolution using NCL .

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

shpname  = "./NW.shp" 
maskname = "./mask_nw.nc" 

print_shapefile_info(shpname)

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

;-- read variable
var                    =  f->PM25(:,:)
var@lat2d              =  f->LAT
var@lon2d              =  f->LON

;-- 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 = "LAT LON" 

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

fout->mask_array =  mask_array
fout->LAT       =  f->LAT
fout->LON     =  f->LON

Step:2

remap the your original JRA file according to Shapefiles

cod remapbil,mask_nw JRA.nc regrid.nc

Step:3

mask your file

cdo ifthen mask_nw regrid.nc JRA_mask.nc

Kunal Bali

RE: failed masking ncfile by shpfile - Added by Qiyun Ma about 4 years ago

Hi Kunal,

I see. Thank you very much!
BTW, would you mind to share me your pm.nc file?
Or maybe kindly share with me the link where you download the data.

Regards,
Qiyun

RE: failed masking ncfile by shpfile - Added by Qiyun Ma about 4 years ago

Thank you so much.:D

Qiyun

RE: failed masking ncfile by shpfile - Added by Ryan Murphy almost 4 years ago

Thanks for the help!
I reposted it to Roms Planet

    (1-5/5)