# Last edited on 2004-09-26 21:44:15 by stolfi
# Creates the range histograms

MAKEFILE := range-hists.make

# Histogram bucket size:
HSTEP := 2000

######################################################################
# Recipe for all plots and stacks
# Caller must define BOOKS (list of three-letter book codes), 
# FMTS (list of "eps" and/or "png"), KMIN (integer), KMAX (integer)

BOOKS := BOOKS.IS.UNDEFINED
ifneq "/${BOOKS}" "/BOOKS.IS.UNDEFINED"
FMTS := FMTS.IS.UNDEFINED
ifneq "/${FMTS}" "/FMTS.IS.UNDEFINED"
KMIN := KMIN.IS.UNDEFINED
ifneq "/${KMIN}" "/KMIN.IS.UNDEFINED"
KMAX := KMAX.IS.UNDEFINED
ifneq "/${KMAX}" "/KMAX.IS.UNDEFINED"


all:
	@echo "BOOKS = ${BOOKS}  FMTS = ${FMTS}  KMAX = ${KMAX}"
	@echo "HSTEP = ${HSTEP}"
	@for fmt in ${FMTS}; do \
	  for g in ${BOOKS}; do \
            ${MAKE} -f ${MAKEFILE} \
                FMT=$$fmt BOOK=$$g \
                KMIN=${KMIN} KMAX=${KMAX} \
                stack ; \
	  done ; \
	done
        
endif
endif
endif
endif
# end of BOOKS, FMTS, KMIN, KMAX
######################################################################

######################################################################
# Recipe for a single stack
# Caller must define BOOK (three-letter book code), 
# FMT ("eps" or "png"), KMIN (integer), KMAX (integer)

BOOK := BOOK.IS.UNDEFINED
ifneq "/${BOOK}" "/BOOK.IS.UNDEFINED"
FMT := FMT.IS.UNDEFINED
ifneq "/${FMT}" "/FMT.IS.UNDEFINED"
KMIN := KMIN.IS.UNDEFINED
ifneq "/${KMIN}" "/KMIN.IS.UNDEFINED"
KMAX := KMAX.IS.UNDEFINED
ifneq "/${KMAX}" "/KMAX.IS.UNDEFINED"

# Stack of histograms:
STKFILE := ${BOOK}-rh.${FMT}

# List of K values, with spaces and commas:
KLIST := ${shell stolfi-count ${KMIN} ${KMAX}}
BLANK := ${EMPTY} ${EMPTY}
COMMA := ,
KLISTCM := ${subst ${BLANK},${COMMA},${KLIST}}

stack: 
	@echo "BOOK = ${BOOK}  FMT = ${FMT}  KMAX = ${KMAX}"
	@echo "KLIST = ${KLIST}"
	@echo "KLISTCM = ${KLISTCM}"
	for k in ${KLIST}; do \
	  ${MAKE} -f ${MAKEFILE} \
              FMT=${FMT} BOOK=${BOOK} \
              KMIN=$$k KMAX=$$k TAG=$$k XLABS=0 \
              single; \
	done
	${MAKE} -f ${MAKEFILE} \
            FMT=${FMT} BOOK=${BOOK} \
            KMIN=${KMIN} KMAX=${KMAX} TAG=tt XLABS=1 \
            single;
	if [ ".${FMT}" == ".png" ]; then \
	  convert ${BOOK}-{${KLISTCM},tt}-rh.${FMT} -append "${STKFILE}"; \
	  display "${STKFILE}"; \
	fi
        
endif
endif
endif
endif
# end of BOOK, FMT, KMIN, KMAX
######################################################################

######################################################################
# Recipe for a single histogram pair
# Caller must define BOOK (three-letter book code), 
# FMT ("eps" or "png"), KMIN (integer), KMAX (integer),
# TAG (chars for file name), XLABS (bool: use labels in H axis)

BOOK := BOOK.IS.UNDEFINED
ifneq "/${BOOK}" "/BOOK.IS.UNDEFINED"
FMT := FMT.IS.UNDEFINED
ifneq "/${FMT}" "/FMT.IS.UNDEFINED"
KMIN := KMIN.IS.UNDEFINED
ifneq "/${KMIN}" "/KMIN.IS.UNDEFINED"
KMAX := KMAX.IS.UNDEFINED
ifneq "/${KMAX}" "/KMAX.IS.UNDEFINED"
TAG := TAG.IS.UNDEFINED
ifneq "/${TAG}" "/TAG.IS.UNDEFINED"
XLABS := XLABS.IS.UNDEFINED
ifneq "/${XLABS}" "/XLABS.IS.UNDEFINED"

# Files with word counts and ranges for ${BOOK}, in original
# and randomized order.
NFILE := ${BOOK}-n.flr
RFILE := ${BOOK}-r.flr

# Histogram image
PLTFILE := ${BOOK}-${TAG}-rh.${FMT}

single: ${PLTFILE}

${PLTFILE}: ${NFILE} ${RFILE} ${MAKEFILE} plot-range-width-hist
	join \
	    -j1 5 -j2 5 -o 1.1,1.4,2.4,0 \
	    ${NFILE} ${RFILE} \
	  | gawk -v KMIN=${KMIN} -v KMAX=${KMAX} \
              '(($$1 >= KMIN) && ($$1 <= KMAX)) {print;}' \
	  | plot-range-width-hist \
              ${HSTEP} ${FMT} ${XLABS} "${TAG}" \
	  > ${PLTFILE}
	if [ ".${FMT}" == ".png" ]; then display -title '%d%f' ${PLTFILE}; fi
	if [ ".${FMT}" == ".eps" ]; then gv ${PLTFILE}; fi
        
endif
endif
endif
endif
endif
endif
# end of BOOK, FMT, KMIN, KMAX, TAG, XLABS
######################################################################