Commit e2aefc8c authored by Akash Koppa's avatar Akash Koppa

added a folder with a bash script for preprocessing irregular netcdf DEM for EDK

parent 18f64581
# Bash script to preprocess DEM for EDK
# Author: Akash Koppa
# Description: a single bash script to prepare the target DEM and the meteorological forcings for EDK in netcdf format
# Workflow: 1) Target DEM: Upscale source DEM to target resolution ---> subset required domain ---> change variable names ---> assign missing value ---> add easting and northing ---> Ready for EDK
# 2) Meteorological Forcing: ---> subset to target domain ---> add easting and northing ---> add DEM ---> Read for EDK
#------------------------USER DEFINED SETTINGS-------------------------#
## input and output paths
# absolute path to target DEM
# absolute path to meteorological files (currently supports mHM inputs)
path_rr="/data/hicam/EOBS/raw/latlon/" # precipitation
path_tg="/data/hicam/EOBS/raw/latlon/" # average temperature
path_tn="/data/hicam/EOBS/raw/latlon/" # minimum temperature
path_tx="/data/hicam/EOBS/raw/latlon/" # maximum temperature
# output directory
# domain specifications
ymax="55.5" # domain extent, format xmin xmax, ymin ymax
target_res="0.015625" # target resolution
crs_reqd="+init=epsg:3035" # provide the epsg of the m-m coordinate system
# meteorological variable details (name of the variable in the netcdf file)
# --------------------PROCESSING STEPS---------------------------#
# load required modules (currently using eve modules).
# Note: Change appropriately
# load netcdf and R modules
module load GCC/7.3.0-2.30 OpenMPI/3.1.1 NCO/4.7.8 CDO/1.9.5 GDAL/2.2.3-Python-3.6.6 ncview/2.1.7 R/3.6.0
## process target DEM
# convert the ascii file to .tif
gdal_translate ${source_dem} "${output_dir}/dem_target.tif"
# upscale the DEM to the required resolution. Also convert to netcdf
gdalwarp -tr ${target_res} ${target_res} -r "average" -of netCDF "${output_dir}/dem_target.tif" "${output_dir}/"
# use ncks to cut domain
ncks -d lon,${xmin},${xmax} -d lat,${ymin},${ymax} -O "${output_dir}/" "${output_dir}/"
# change the names of primary variable, lat and lon
ncrename -v Band1,dem -d lat,latitude -d lon,longitude -O "${output_dir}/" "${output_dir}/"
ncrename -v lat,latitude -O "${output_dir}/" "${output_dir}/"
ncrename -v lon,longitude -O "${output_dir}/" "${output_dir}/"
# set missing value to -9999
cdo setmissval,-9999 "${output_dir}/" "${output_dir}/"
# add northing and easting to the target variable using the R script
Rscript latlon2northeast.R "${output_dir}/" "dem" ${crs_reqd}
## process input meteorological forcing
# subset the meteorological forcing
ncks -d longitude,${xmin},${xmax} -d latitude,${ymin},${ymax} "${path_rr}" "${output_dir}/"
ncks -d longitude,${xmin},${xmax} -d latitude,${ymin},${ymax} "${path_tg}" "${output_dir}/"
ncks -d longitude,${xmin},${xmax} -d latitude,${ymin},${ymax} "${path_tn}" "${output_dir}/"
ncks -d longitude,${xmin},${xmax} -d latitude,${ymin},${ymax} "${path_tx}" "${output_dir}/"
# set missing value to -9999
cdo setmissval,-9999 "${output_dir}/" "${output_dir}/"
cdo setmissval,-9999 "${output_dir}/" "${output_dir}/"
cdo setmissval,-9999 "${output_dir}/" "${output_dir}/"
cdo setmissval,-9999 "${output_dir}/" "${output_dir}/"
# remove all temporary files
rm "${output_dir}/" "${output_dir}/" "${output_dir}/" "${output_dir}/"
# add northing and easting information
Rscript latlon2northeast.R "${output_dir}/" ${var_rr} ${crs_reqd}
Rscript latlon2northeast.R "${output_dir}/" ${var_tg} ${crs_reqd}
Rscript latlon2northeast.R "${output_dir}/" ${var_tn} ${crs_reqd}
Rscript latlon2northeast.R "${output_dir}/" ${var_tx} ${crs_reqd}
# remap dem to meteorological forcing
gdalwarp -tr ${met_res} ${met_res} -r "average" -of netCDF "${output_dir}/dem_target.tif" "${output_dir}/"
ncrename -v Band1,dem "${output_dir}/" "${output_dir}/"
cdo setmissval,-9999 "${output_dir}/" "${output_dir}/"
rm "${output_dir}/"
# add dem to the meteorological forcing
Rscript add_dem2met.R "${output_dir}/" "${output_dir}/"
Rscript add_dem2met.R "${output_dir}/" "${output_dir}/"
Rscript add_dem2met.R "${output_dir}/" "${output_dir}/"
Rscript add_dem2met.R "${output_dir}/" "${output_dir}/"
# Script to add DEM data to the EOBS netcdf file
# Author - Akash Koppa
# Date - 03/04/2020
# clear workspace
# read command line arguments
args = commandArgs(trailingOnly=TRUE)
met_file = args[1]
dem_file = args[2]
# load required libraries
# read in the 10km dem for Europe (E-OBS grid)
id = nc_open(dem_file)
dem = ncvar_get(id, varid="dem")
# write the DEM variable into the netcdf files
id = nc_open(met_file,write=TRUE)
xdim = id$dim[['longitude']]
ydim = id$dim[['latitude']]
# create the dimension information for the netcdf files
var_dem = ncvar_def(name = "dem",units="meter",dim=list(xdim, ydim),missval=-9999,longname="Elevation")
# put variables
id = ncvar_add(id, var_dem)
ncvar_put(id, var_dem, dem)
#Script to convert lat lon coordinates and add extra "northing" and "easting" variables to the EOBS netcdf files
# Author - Akash Koppa
# Date - 2/10/2020
# clear workspace
# read in arguments from the command line
args = commandArgs(trailingOnly=TRUE)
file_reqd = args[1]
varid_reqd = args[2]
crs_reqd = args[3]
# load required libraries
# read in the lat and lon coordinates
id = nc_open(file_reqd)
data = ncvar_get(id, varid = varid_reqd)
northing = ncvar_get(id, varid = "latitude")
easting = ncvar_get(id, varid = "longitude")
# create a two dimensional matrix of latitudes and longitudes
north2d = NULL
for(i in 1:length(easting)){
north2d = rbind(north2d, northing)
east2d = NULL
for (i in 1:length(northing)){
east2d = cbind(east2d, easting)
# convert latitude and longitude into northing and easting
coord_latlon = data.frame(Longitude = c(east2d),
Latitude = c(north2d))
coord_latlon = coordinates(coord_latlon)
coord_latlon = SpatialPoints(coord_latlon, CRS("+init=epsg:4326"))
coord_proj = spTransform(coord_latlon, CRS(crs_reqd))
coord_reqd = coordinates(coord_proj)
north2d_reqd = matrix(data = coord_reqd[,2],nrow=nrow(data))
east2d_reqd = matrix(data = coord_reqd[,1],nrow=nrow(data))
# write these variables into the netcdf files
id = nc_open(file_reqd,write=TRUE)
xdim = id$dim[['longitude']]
ydim = id$dim[['latitude']]
var_north2d = ncvar_def(name = "northing",units = "meter",dim = list(xdim, ydim), missval = -9999,longname = paste0("Northing in ", crs_reqd))
var_east2d = ncvar_def(name = "easting",units = "meter",dim = list(xdim, ydim), missval = -9999,longname = paste0("Easting in ", crs_reqd))
# put variables
id = ncvar_add(id, var_north2d)
id = ncvar_add(id, var_east2d)
ncvar_put(id, var_north2d, north2d_reqd)
ncvar_put(id, var_east2d, east2d_reqd)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment