Last active
June 2, 2026 15:50
-
-
Save barronh/faad20a530b28a75ca9a02e83219194c to your computer and use it in GitHub Desktop.
GDAL Convert CMAQ to GeoTIFF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| INPATH=${1?Required input CMAQ File} | |
| if [[ ${INPATH} == "-h" || ${INPATH} == "--help" ]]; then | |
| echo $0 [-h] INPATH INVAR [OUTPATH] | |
| echo "Convert CMAQ IOAPI meta data to SRS and create raster using gdal_translate" | |
| echo " -h --help: print this help menu" | |
| echo " INPATH : path to CMAQ input/output file" | |
| echo " INVAR : name of variable in INPATH" | |
| echo " OUTPATH : Path for output raster (default: INPATH_INVAR.tif)" | |
| exit | |
| fi | |
| INVAR=${2?Input Variable from CMAQ File} | |
| OUTPATH=$(basename ${INPATH})_${INVAR}.tif | |
| OUTPATH=${3:-${OUTPATH}} | |
| # Grab GLOBAL VARS | |
| # awk is used to find lines for key global attributes and set value | |
| # to interal variable. Then print variables to create a declarations string | |
| INGLBL=$(gdalinfo ${INPATH} | awk -F"=" ' | |
| /^ NC_GLOBAL#GDTYP=/ { GDTYP=$2 } | |
| /^ NC_GLOBAL#P_ALP=/ { P_ALP=$2 } | |
| /^ NC_GLOBAL#P_BET=/ { P_BET=$2 } | |
| /^ NC_GLOBAL#XCELL=/ { XCELL=$2 } | |
| /^ NC_GLOBAL#XCENT=/ { XCENT=$2 } | |
| /^ NC_GLOBAL#YCENT=/ { YCENT=$2 } | |
| /^ NC_GLOBAL#XORIG=/ { XORIG=$2 } | |
| /^ NC_GLOBAL#YORIG=/ { YORIG=$2 } | |
| /^ NC_GLOBAL#NCOLS=/ { NCOLS=$2 } | |
| /^ NC_GLOBAL#NROWS=/ { NROWS=$2 } | |
| END { | |
| print "GDTYP="GDTYP | |
| print "P_ALP="P_ALP | |
| print "P_BET="P_BET | |
| print "XCELL="XCELL | |
| print "XCENT="XCENT | |
| print "YCENT="YCENT | |
| print "XORIG="XORIG | |
| print "YORIG="YORIG | |
| print "NCOLS="NCOLS | |
| print "NROWS="NROWS | |
| print "x_0="(XORIG * -1) | |
| print "y_0="(YORIG * -1) | |
| print "stere_lat_0="(P_ALP * 90) | |
| } | |
| ') | |
| declare $INGLBL | |
| # We should define at least polar stereographic and lamber conic conformal (lcc) | |
| # currently only LCC was tested. | |
| if [ ${GDTYP} == "2" ]; then | |
| CRS_PROJ4="+proj=lcc +lat_1=${P_ALP} +lat_2=${P_BET} +lat_0=${YCENT} +lon_0=${XCENT} +y_0=${y_0} +x_0=${x_0} +R=6370000 +to_meter=${XCELL} +no_defs" | |
| elif [ ${GDTYP} == "6" ]; then | |
| CRS_PROJ4="+proj=stere +lat_0=${stere_lat_0} +lat_ts=${P_BET} +lon_0=${XCENT} +x_0=${x_0} +y_0=${y_0} +R=6370000 +to_meter=${XCELL} +no_defs" | |
| elif [ ${GDTYP} == "7" ]; then | |
| CRS_PROJ4="+proj=merc +lat_ts=0 +lon_0=${XCENT} +x_0=${x_0} +y_0=${y_0} +R=6370000 +to_meter=${XCELL} +no_defs" | |
| else | |
| echo "GDTYP=${GDTYP} has not been implemented" | |
| exit 1 | |
| fi | |
| echo CRS_PROJ4=\"${CRS_PROJ4}\" | |
| gdal_translate -a_ullr 0 ${NROWS} ${NCOLS} 0 -a_srs "${CRS_PROJ4}" NETCDF:${INPATH}:${INVAR} -b 1 ${OUTPATH} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment