I have a for loop that downloads climate data and crops it to the extent of my study area. Technically it works, but I'm wondering if there is a more efficient approach than having the loop repeatedly try to make the directories.
I was thinking about nesting this loop inside one that calls a variable from an array and makes a directory for it, then runs gdal... but I'm not sure how that would work because the gdal lines are slightly different.
## INITIAL LOOP
for MM in 01 02 03 04 05 06 07 08 09 10 11 12 ; do
#download tmin data
mkdir tmin
cd tmin
gdal_translate -projwin $ulx $uly $lrx $lry -co COMPRESS=DEFLATE -co ZLEVEL=9 /vsicurl/https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V1/climatologies/tmin/CHELSA_tmin10_${MM}_1979-2013_V1.2_land.tif CHELSA_tmin10_${MM}_1979-2013_V1.2_land_crop.tif
cd ..
#download precpitation data
mkdir prec
cd prec
gdal_translate -projwin $ulx $uly $lrx $lry -co COMPRESS=DEFLATE -co ZLEVEL=9 /vsicurl/https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V1/climatologies/prec/CHELSA_prec_${MM}_V1.2_land.tif CHELSA_prec_${MM}_V1.2_land_crop.tif
cd ..
#download tmax data
mkdir tmax
cd tmax
gdal_translate -projwin $ulx $uly $lrx $lry -co COMPRESS=DEFLATE -co ZLEVEL=9 /vsicurl/https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V1/climatologies/tmax/CHELSA_tmax10_${MM}_1979-2013_V1.2_land.tif CHELSA_tmax10_${MM}_1979-2013_V1.2_land_crop.tif
cd ..
done
#THOUGHTS
var_list=(tmin tmax prec)
for var in ${var_list[@]} ; do
mkdir $var
cd $var
for MM in 01 02 03 04 05 06 07 08 09 10 11 12 ; do
#download tmin data
mkdir tmin
cd tmin
gdal_translate -projwin $ulx $uly $lrx $lry -co COMPRESS=DEFLATE -co ZLEVEL=9 /vsicurl/https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V1/climatologies/tmin/CHELSA_tmin10_${MM}_1979-2013_V1.2_land.tif CHELSA_tmin10_${MM}_1979-2013_V1.2_land_crop.tif
cd ..
#download precpitation data
mkdir prec
cd prec
gdal_translate -projwin $ulx $uly $lrx $lry -co COMPRESS=DEFLATE -co ZLEVEL=9 /vsicurl/https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V1/climatologies/prec/CHELSA_prec_${MM}_V1.2_land.tif CHELSA_prec_${MM}_V1.2_land_crop.tif
cd ..
#download tmax data
mkdir tmax
cd tmax
gdal_translate -projwin $ulx $uly $lrx $lry -co COMPRESS=DEFLATE -co ZLEVEL=9 /vsicurl/https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V1/climatologies/tmax/CHELSA_tmax10_${MM}_1979-2013_V1.2_land.tif CHELSA_tmax10_${MM}_1979-2013_V1.2_land_crop.tif
cd ..
done
done
Assuming the last arg for the gdal_translate
calls is a file you wish to create, and assuming this argument can include a relative path to said file (to be created), how about:
mkdir -p tmin tmax prec # '-p' suppresses the 'directory already exists' message
for MM in {01..12}
do
# no ned for 'cd' calls if gdal_translate can write to a relative path/file, eg:
gdal_translate ... tmin/CHELSA_tmin10_${MM}_1979-2013_V1.2_land_crop.tif
gdal_translate ... prec/CHELSA_prec_${MM}_V1.2_land_crop.tif
gdal_translate ... tmax/CHELSA_tmax10_${MM}_1979-2013_V1.2_land_crop.tif
done