Project

General

Profile

how to mask or extract data from nc files by one certain region? thanks

Added by Wi Du almost 7 years ago

Hi everyone,

I have many nc files, which are global climate data (50 years). Now I am want to focus one country region (I have the shapefile) to do statistic analysis (such as region mean,trend, and so on). So now I tried:
First to turn the shapefile into nc file using ARCGIS.
Second, I use the cdo command:
du@ubuntu:~/model/CC$ cdo div country.nc precipitation_2051-2110.nc out.nc
But I got errors:
cdo div: Open failed on >cdd_yr_HFB_4K_CC_m102_2051-2110.nc<
Unsupported file type (library support not compiled in)
It seems the cdo can not open the nc files. And I checked the nc files using ncdump
du@ubuntu:~/CC$ ncdump -h precipitation_2051-2110.nc
netcdf precipitation_2051-2110 {
dimensions:
bnds = 2 ;
time = UNLIMITED ; // (60 currently)
lon = 116 ;
lat = 71 ;
variables:
double time(time) ;
time:units = "days since 2051-1-1" ;
time:standard_name = "time" ;
time:long_name = "Time" ;
time:calendar = "standard" ;
time:axis = "T" ;
double time_bnds(time, bnds) ;
double lon(lon) ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:axis = "X" ;
double lat(lat) ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:axis = "Y" ;
float precipi(time, lat, lon) ;
cddETCCDI:units = "days" ;
cddETCCDI:_FillValue = 1.e+20f ;
cddETCCDI:long_name = "Precipitation" ;
cddETCCDI:grid_type = "gaussian" ;
// global attributes:
Anyone can help me? 
Thanks a lot!

Replies (20)

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Thanks.
I checked the data, and the information is as follows.
Climate Data Operators version 1.6.2 (http://code.zmaw.de/projects/cdo)
Compiler: gcc -std=gnu99 -g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wall -pedantic -fPIC -pthread
version: gcc (Ubuntu/Linaro 4.8.2-4ubuntu1) 4.8.2
Compiled: by buildd on allspice (x86_64-unknown-linux-gnu) Nov 15 2013 17:10:54
Features: PTHREADS Z JASPER PROJ.4 MAGICS
Libraries: proj/4.7
Filetypes: srv ext ieg grb grb2 nc
CDI library version : 1.6.2 of Nov 15 2013 17:10:44
GRIB_API library version : 1.10.4
netCDF library version : 4.1.3 of Feb 24 2014 21:05:37 $
HDF5 library version : 1.8.11
SERVICE library version : 1.3.1 of Nov 15 2013 17:10:28
EXTRA library version : 1.3.1 of Nov 15 2013 17:10:20
IEG library version : 1.3.1 of Nov 15 2013 17:10:24
FILE library version : 1.8.2 of Nov 15 2013 17:10:20

Use commandline option -h for help.
Error (operatorInqModID) : operator missing! cdd_yr_HFB_4K_CC_m102_2051-2110.nc is a file on disk!

So how can I modify these data and make them can be used in CDO?
Thanks a lot!

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Ralf Mueller almost 7 years ago

cdo mul -selname,altcddETCCDI altcdd_yr_HFB_4K_CC_m101_2051-2110.nc China.nc masked.nc
works with cdo-1.8.2. Can you update your CDO version?

I uploaded the results.

hth
ralf

masked.nc (1.89 MB) masked.nc

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Hi,Dr.Ralf,
Thanks a lot. I updated the cdo and it can be run.
But I checked the results using grads and I found the results are headstand.
Please check the attached figure.
So how to modify it. I tried several times, but failed.

Thanks!
Best regards,

Du

111.PNG (36.2 KB) 111.PNG

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Ralf Mueller almost 7 years ago

the grids in China.nc and the other file a quite different although they have the same dimension size. longitudes are a little different abd latitudes are in reversed order.

1. remap data to the China.nc grid

cdo remapnn,China.nc -selname,altcddETCCDI altcdd_yr_HFB_4K_CC_m101_2051-2110.nc alt.nc

2. multiply mask and data

cdo mul alt.nc China.nc alt_masked.nc

3. results look like this

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Hello, Dr.Ralf,

Thank you very much!
It is ok now.
Best regards,

Du

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Hi, Dr.Ralf,
Sorry to disturb you again.
I found the value are changed after the mask. Them are much bigger than the original value
:(
Thanks again!

Regards,

Du

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Ralf Mueller almost 7 years ago

Wi Du wrote:

Hi, Dr.Ralf,
Sorry to disturb you again.
I found the value are changed after the mask. Them are much bigger than the original value
:(
Thanks again!

Regards,

Du

That's because the China variable is not a 0-1 mask, it has values from 0 to ca. 950. You can create such a field by dividing China.nc by itself and do the multiplication afterwards - or as a one line

cdo mul alt.nc -div China.nc China.nc alt_realMaskedOut.nc

hth
ralf

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Hello, Dr. Ralf,

Thanks a lot.
But I faced another problem for some dataset. Of them, the mask value is very strange. Like the zero value could not be deleted.
Would you please check it for me?
Thanks!

Du

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Ralf Mueller almost 7 years ago

no problem, you can add 1.0 before doing the devision like this

f=mask_r99p_yr_HFB_4K_CC_m101_2051-2110.nc
cdo -L -div  -addc,1 -abs $f -addc,1 -abs $f newMask.nc

newMask.nc has no new missing values (introduced by devision by zero)

hth
ralf

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Sorry, Dr.Ralf,

I just want to ask how to put this step, because I tried several times and got the constant(1) value for the final results.
Thanks a lot!
Best regards,

Du

1.PNG (20.4 KB) 1.PNG

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Ralf Mueller almost 7 years ago

Do I get it right, that you want the data from r99p_yr_HFB_4K_CC_m101_2051-2110.nc masked out to the china region???

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Ralf Mueller almost 7 years ago

cdo selname,r99pETCCDI r99p_yr_HFB_4K_CC_m101_2051-2110.nc r99Mask.nc
f=China.nc
cdo -L -div  -addc,1 -abs $f -addc,1 -abs $f chinaMask.nc
cdo remapnn,r99Mask.nc chinaMask.nc r99ChinaMask.nc
cdo div -selname,r99pETCCDI r99p_yr_HFB_4K_CC_m101_2051-2110.nc r99ChinaMask.nc r99Masked2China.nc

looks like

is that what you had in mind?

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Wi Du almost 7 years ago

Hi, Dr.Ralf,

Thanks a lot!
Best regards,

Du

how to extract DJF only from .nc file using cdo? thanks - Added by SANJUKTA RANI PADHI almost 6 years ago

HELLO ALL,
I want to extrct djf from a .nc file (starting fro december of 1st year to february of last year) using cdo. I used cdo -selmon,12,1,2 -shifttime,-1mo infile.nc outfile.nc. but it show wrong in values......2nd year january data show as 1st year december ... Please help me in this regards. Here I attach the infile (sst_dec.nc) and outfile (124.nc) Thank you

Anyone can help me? 
Thanks a lot!
124.nc (2.58 MB) 124.nc Outfile.nc
sst_dec.nc (10.4 MB) sst_dec.nc Infile.nc

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Karin Meier-Fleischer almost 6 years ago

Hi Sanjukta,

please, create always a new forum question unless your question depends of an old one.

Why do you do a shifttime one month backwards? This is what the wrong month are coming from.
It should be

cdo -selmon,12,1,2 infile.nc outfile.nc

Another way to select DJF is to use the select operator

cdo select,season=DJF infile.nc outfile.nc
But at the beginning it will select only JF and for the last year only D because of missing years.

-Karin

RE: how to mask or extract data from nc files by one certain region? thanks - Added by SANJUKTA RANI PADHI almost 6 years ago

hi Karin sir,

I am so sorry. Actually I am new here and i dont know how to start a new forum...And my question was little similar to this forum so i was query here. Thanks for replr me. when i used cdo select,season=DJF infile.nc outfile.nc
i got this
cdo select : Enter parameter list >

after this i dont know what to do.
kindly help
Thank You

RE: how to mask or extract data from nc files by one certain region? thanks - Added by Karin Meier-Fleischer almost 6 years ago

Do you really enter the cdo command above or do you have a blank between select, and season? That's where I would get the 'cdo select : Enter parameter list >' request.

You have to enter

cdo select,season=DJF infile.nc outfile.nc

If this won't work which cdo version are you using(cdo -V)? The current version is 1.9.5.

-Karin

    (1-20/20)