Project

General

Profile

Masking a netcdf file by CDO

Added by Puri Am about 3 years ago

Hi,
I struggle with masking a nc file cdo. I do not know the order to to that, So I want to ask if you have any suggestions for me? Even it in python or shape file.
Thanks a lot !!


Replies (12)

RE: Masking a netcdf file by CDO - Added by Puri Am about 3 years ago

Thanks a lot! What a helpful post!

But in my case, I just want to mask a country. So, how to do this?

All the best!!

-Puri

RE: Masking a netcdf file by CDO - Added by Karin Meier-Fleischer about 3 years ago

Hi Puri,

ok, that's a little bit tricky because you need the borderline coordinates. They can be found in shapefiles and there are different ways to use them.

Amongst others, download a shapefile that contains the country data you need. Convert the shapefile content to netCDF format or extract the shapefile polygon coordinates to an ASCII file and use CDO's maskregion operator.

I have already prepared the ASCII input files for almost all countries for use with CDO. There will also be an post in the Operator News forum in the near future describing exactly how to do this (CDO and NCL are needed).

Which state do you need? Maybe I can use it to create an example for the post right away. ;)

-Karin

RE: Masking a netcdf file by CDO - Added by Puri Am about 3 years ago

Thanks a lot!!

I would like to mask Thailand :) !
And, I have a look at Operator News, such helpful information!

-Puri

RE: Masking a netcdf file by CDO - Added by Karin Meier-Fleischer about 3 years ago

I've uploaded the input file for the CDO operator maskregion for Thailand (THA_Thailand_region.txt).

To extract the data for Thailand of your data:

cdo -maskregion,THA_Thailand_region.txt infile outfile

RE: Masking a netcdf file by CDO - Added by Puri Am about 3 years ago

hi, thank you!!
But i wonder where could we download the txt file for longitude and latitude like yours?
Wish you all the best!!

RE: Masking a netcdf file by CDO - Added by Karin Meier-Fleischer about 3 years ago

Hm, I would say nowhere with the required format. I've used a shapefile to extract and reformat the lon/lat values using NCL. ;)

RE: Masking a netcdf file by CDO - Added by Puri Am about 3 years ago

Yes, i saw in another post! thank so much!

RE: Masking a netcdf file by CDO - Added by Amadou Coulibaly 11 months ago

Hi

I am having problem with cdo masking region. After creating the file.csv from the shepefile of my desired region (Mali), I wanted to create mask for ARISE global temperature data from 2035 to 2044. But I am getting the following error message:

cdo -maskregion,Mali_lonlat.csv -remapbil,r720x360 b.e21.BW.f09_g17.SSP245-TSMLT-GAUSS-DEFAULT.001.cam.h1.TREFHT.20350101-20441231.nc Mali_lonlat_tsurf.nc

cdo(1) remapbil: Process started
Warning (cdf_check_variables): Unsupported data type (char/string), skipped variable date_written!
Warning (cdf_check_variables): Unsupported data type (char/string), skipped variable time_written!

cdo maskregion (Abort): Too many different grids!
terminate called without an active exception
Aborted (core dumped)

Who can help me? See attached the shepefile of Mali

RE: Masking a netcdf file by CDO - Added by Karin Meier-Fleischer 11 months ago

Hi Amadou,

without the files b.e21.BW.f09_g17.SSP245-TSMLT-GAUSS-DEFAULT.001.cam.h1.TREFHT.20350101-20441231.nc and Mali_lonlat.csv it is not possible to help.

RE: Masking a netcdf file by CDO - Added by Amadou Coulibaly 11 months ago

Hello Karin,

Sorry, the netcdf file is too big, so let me send it like that. See attached Bamak shepefile and the ncl script I used to create Bamak_latlon.csv. But after successful running of ncl script, I can not locate the csv file. I shared the following file with you by google drive because it is to heavy.
b.e21.BW.f09_g17.SSP245-TSMLT-GAUSS-DEFAULT.001.cam.h1.TREFHT.20350101-20441231.nc

RE: Masking a netcdf file by CDO - Added by Karin Meier-Fleischer 10 months ago

I find a way without NCL to write the csv data from a shapefile with Python and geopandas. Change the shp_file variable to your shapefile path.

import os
import geopandas as gpd

shp_file = os.environ['HOME']+ '/Downloads/Bamako/Bamako.shp'   #---- capital of Mali

reader = gpd.read_file(shp_file, index_col=0)

geoms = [i for i in reader.geometry.explode(index_parts=True)]
len_geoms = len(geoms)

x, y = geoms[0].exterior.coords.xy

csv_file = ''
for np in (range(len_geoms)):
    fname = f'csvfile_{np:00d}.csv'
    with open(fname, 'w') as f:
        x0, y0 = geoms[np].exterior.coords.xy

        for i in range(len(x)-1):
            f.write(f'{x[:][i]} {y[:][i]}\n')

        if np == 0:
            csv_file = fname
        else:
            csv_file = csv_file + ',' + fname

Then you can use CDO to do the masking with the csv file (in your case just one for one polygon).

cdo -maskregion,csvfile_0.csv infile outfile
    (1-12/12)