Skip to content

Commit

Permalink
Batch scripts updated and gridding script completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
sthancock committed Mar 23, 2019
1 parent 7b38a11 commit 46ccee5
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
Binary file added .batchBiomass.bash.swp
Binary file not shown.
69 changes: 69 additions & 0 deletions batchBiomass.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash -f


#################
# Batch process #
# GEDI data #
#################


# defaults
output="allBiomass.txt"
bin="/geos/netdata/avtrain/data/3d/active_sensing/week10/active_sensing"

# Read the command line
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-inDir)
inDir="$2"
shift;shift
;;
-output)
output="$2"
shift;shift
;;
-help)
echo " "
echo "Batch processing of GEDI biomass"
echo " "
echo "-inDir name; directory name to process all HDF5 files within"
echo "-output name; output filename"
echo " "
exit
;;
*)
echo $"Unrecognised option: $key"
exit 1
esac
done # command line reader


# if a directory specified, read directory
list="/tmp/gedoToProcess.$$.txt"
pushd $inDir/
ls -l|grep ".metric.txt"|gawk '{printf("%s/%s\n",dir,$NF)}' dir="$inDir" > $list
popd

# clear old output
if [ -e $output ];then
rm $output
fi

# loop over input files
temp="/tmp/workSpace.biomass.$$.txt"
while read inName; do

python3 $bin/predictBiomass.py --input $inName --outpout $temp
cat $temp >> $output

if [ -e $temp ];then
rm $temp
fi
done < $list

if [ -e $list ];then
rm $list
fi

2 changes: 1 addition & 1 deletion batchProcess.bash
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ key="$1"
echo "-inDir name; directory name to process all HDF5 files within"
echo "-input name; input filename for single file"
echo "-outRoot name; output filename root"
echo "-bound minX minY maxX maxY; define bounds"
echo "-bounds minX minY maxX maxY; define bounds"
echo " "
echo "### Denoising options ###"
echo "-varScale nStdev; number of standard deviations to use for noise threshold"
Expand Down
94 changes: 94 additions & 0 deletions gridBiomass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

'''
Grids biomass estimates
'''

#############################################
import numpy as np
import argparse
import scipy.stats as stats


#############################################

class gediBiomass(object):
'''Class to hold GEDI biomass estimates'''

#################################

def __init__(self,filename):
'''Read footprint level file'''
self.lon,self.lat,self.agbd=np.loadtxt=np.loadtxt(filename,usecols=(0,1,2),unpack=True, dtype=float,comments='#')


#################################

def gridAGBDmean(self,res,outName):
'''Grid AGBD estimates by simple mean'''
# determine number of cells
minX=np.min(self.lon)
maxX=np.max(self.lon)
minY=np.min(self.lat)
maxY=np.max(self.lat)
nX=int((maxX-minX)/res+1)
nY=int((maxY-minY)/res+1)
# grid up
meanAGBD,x0,y0,binnumber=stats.binned_statistic_2d(self.lon,self.lat,values=self.agbd,statistic='mean',bins=[np.arange(nX), np.arange(nY)])
# write header
f=open(outName,'w')
noData=-9999
header="NCOLS"+str(nX)+"\n"
header=header+"NROWS"+str(nY)+"\n"
header=header+"XLLCENTER"+str(minX)+"\n"
header=header+"YLLCENTER"+str(minY)+"\n"
header=header+"CELLSIZE"+str(res)+"\n"
header=header+"NODATA_VALUE"+str(noData)+"\n"
f.write(header)
# remove nan
meanAGBD[np.isnan(meanAGBD)]=noData
# write data
for j in range(nY-1,-1,-1):
line=""
for x in meanAGBD[j]:
line=line+str(x)+" "
line=line+"\n"
f.write(line)
f.close()
print("Written to",outName)


# end of gediBiomass
#############################################


###########################################
# read the command line

def gediCommands():
'''
Read commandline arguments
'''
p = argparse.ArgumentParser(description=("Writes out properties of GEDI waveform files"))
p.add_argument("--input",dest="inName",type=str,help=("Input GEDI metric filename"))
p.add_argument("--output",dest="output",type=str,default='test.asc',help=("Output graph filename"))
p.add_argument("--res",dest="res",type=float,default=100,help=("Output resolution"))
cmdargs = p.parse_args()
return cmdargs


###########################################
# the main block

if __name__ == '__main__':
# read the command line
cmdargs=gediCommands()
inName=cmdargs.inName
output=cmdargs.output
res=cmdargs.res

# read data
agbd=gediBiomass(inName)

# grid data and write output
agbd.gridAGBDmean(res,output)

0 comments on commit 46ccee5

Please sign in to comment.