Combining shapefiles and netCDF files
Added by Miles Sowden over 2 years ago
The post https://code.mpimet.mpg.de/boards/2/topics/9139 raises an interesting observation. Their problem is related to one shapefile and cropping the netcdf accordingly.
If the shapefile contained countries, with a unique country ID and the netCDF file contained global weather data what would the easiest way be to extract the weather data by country?
The obvious answer would be a big for loop of code generating a file per country.
But is it possible to take the shapefile and use the histogram function to define levels (one per country)
Then multiply the multi-level country mask with the 2D weather file
then summarize by level.
My problem for next week :-)
Replies (5)
RE: Combining shapefiles and netCDF files - Added by Karin Meier-Fleischer over 2 years ago
Hi Miles,
you can use NCL to convert the shapefile to netCDF or you can extract the weather data using the shapefile polygons with Python.
RE: Combining shapefiles and netCDF files - Added by Miles Sowden over 2 years ago
Hi Karen,
Sorry if I wasn't clear. If I have a shapefile of countries (for instance) and I converted that to a netCDF where each country is a different ID what is the easiest way to mask by each country and extract and summarise the weather data by country.
RE: Combining shapefiles and netCDF files - Added by Karin Meier-Fleischer over 2 years ago
Ok, let's say your netCDF shapefile shp_file.nc has a variable NAME_ID containing integers 0 to 195 (one unique id for each country).
Create the mask file for country NAME_ID == 2 (set ones for selected country area and missing value for the other areas:
cdo -expr,'NAME_ID = ((NAME_ID == 2)) ? 1.0 : (NAME_ID/0.0)' shp_file.nc mask_id_2.nc
Then use the mask file to mask your weather data:
cdo -mul mask_id_2.nc weather_data.nc masked_weather_data.nc
See also the article https://code.mpimet.mpg.de/boards/53/topics/10933
RE: Combining shapefiles and netCDF files - Added by Miles Sowden over 2 years ago
Karin,
This will get me the mask for country 2 but I want a mask for each of the 195 countries. I then want for each country to get some weather statistics.
So is the easiest a huge big for loop?
RE: Combining shapefiles and netCDF files - Added by Karin Meier-Fleischer over 2 years ago
I would say - yes.