-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc08_environmLayers.sh
More file actions
104 lines (70 loc) · 3.52 KB
/
sc08_environmLayers.sh
File metadata and controls
104 lines (70 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#SBATCH -p day
#SBATCH -n 1 -c 1 -N 1
####SBATCH -t 00:10:00
#SBATCH -o /gpfs/scratch60/fas/sbsc/jg2657/stdout/sc08_environmLayers.sh.%A_%a.out
#SBATCH -e /gpfs/scratch60/fas/sbsc/jg2657/stderr/sc08_environmLayers.sh.%A_%a.err
#SBATCH --mem-per-cpu=10000M
####SBATCH --array=1-314 ### different macro basins
# scp -i ~/.ssh/JG_PrivateKeyOPENSSH /home/jaime/Code/environmental-data-extraction/sc08_environmLayers.sh jg2657@grace1.hpc.yale.edu:/home/jg2657/project/code/environmental-data-extraction
# sbatch /home/jg2657/project/BenthicEU/scripts/sc08_environmLayers.sh
# sacct -j 18657950 --format=JobID,State,Elapsed
# srun --pty -t 6:00:00 --mem=20G --x11 -p interactive bash
module purge
source ~/bin/gdal3
source ~/bin/pktools
source ~/bin/grass78m
###############################################################
#export DIR=/home/jg2657/project/BenthicEU
export DIR=$1
#export TMP=/home/jg2657/scratch60
export TMP=$2
export BASIN=$TMP/basins
export basinID=$(awk -F, 'FNR > 1 {print $4}' $DIR/BasisDataBasinsIDs.csv | sort | uniq)
[ ! -d $TMP/r_univar ] && mkdir $TMP/r_univar
export OUTDIR=$TMP/r_univar
# Macro-basin
export ID=$(echo $basinID | awk -v id=$SLURM_ARRAY_TASK_ID '{print $id}')
#export ID=1096615
# Name of column for unique ID
export SITE=$( awk -F, 'NR==1 {print $1}' $DIR/BasisDataBasinsIDs.csv )
#rm -r $OUTDIR/*
mkdir $OUTDIR/basin_$ID
#echo "***** RUN : $SLURM_ARRAY_TASK_ID --- Processing BASIN : $ID *****" >> $DIR/script08_out.txt
#### slope, accumulation, order, elevation, upstream distance
##################################
###### START GRASS
grass78 -f -text --tmp-location -c $BASIN/basin_${ID}/bid_${ID}_mask.tif <<'EOF'
## Points in microbasin
v.in.ogr --o input=$DIR/BasisDataBasinsIDs.gpkg layer=orig_points output=benthicEU \
type=point where="MacrobasinID = ${ID}" key=$SITE
## Read in microbasin raster
r.in.gdal --o input=$BASIN/basin_${ID}/bid_${ID}_micb.tif output=micb
## Procedure to create a microbasin raster but only with those where points overlap
micro=$(v.db.select benthicEU | awk -F'|' 'NR > 1 {print $5}' ORS=' ' | xargs -n1 | sort | uniq)
for i in $micro; do
echo $i = 1 >> $TMP/reclass_${ID}.txt
done
echo '* = NULL' >> $TMP/reclass_${ID}.txt
r.reclass --o input=micb output=micb_recl rules=$TMP/reclass_${ID}.txt
r.mapcalc --o "micbonly = if(micb_recl==1,micb,null())"
## Calculate the statistics for each microbasin for each variable and save in csv file
for VAR in slope elev accu; do
r.external input=$BASIN/basin_${ID}/bid_${ID}_${VAR}.tif output=$VAR --overwrite
v.what.rast -p map=benthicEU raster=$VAR > $OUTDIR/basin_$ID/stats_atpoint_${ID}_${VAR}.csv
r.univar -t --o map=$VAR zones=micbonly separator=comma | \
awk -F, 'BEGIN{OFS=",";} {print $1, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 }' \
> $OUTDIR/basin_$ID/stats_${ID}_${VAR}.csv
done
## Procedure to extract the information on stream order and distance downstream to outlet
## read the cleaned stream network generated by r.stream.order (but only the
## reaches where the points are located)
v.in.ogr --o input=$BASIN/basin_${ID}/bid_${ID}_stre_order.gpkg layer=orderV_bid${ID} \
output=stre_cl type=line key=stream where="stream IN ($(v.db.select benthicEU | \
awk -F'|' 'NR > 1 {print $5}' ORS=',' | sed 's/,*$//g'))"
v.db.select stre_cl | awk -F'|' 'NR==1 { for (i=1; i<=NF; i++) {f[$i] = i}} \
BEGIN{OFS=",";} { print $(f["stream"]), $(f["strahler"]), $(f["out_dist"]),\
$(f["elev_drop"]) }' > $OUTDIR/basin_$ID/stats_${ID}_order.csv
rm $TMP/*${ID}*
EOF
exit