#*************************************************************************
#                                                                        *
# GNUmakefile.alpha:                                                     *
#                                                                        *
# Script to C-preprocess, compile, and link PE_INITIAL. It creates the   *
# executable file specified in the macro BIN.                            *
#                                                                        *
# This makefile is written for GNU Make, version 3.75                    *
#                                                                        *
#    Free Software Foundation          (617) 876-3296                    *
#    675 Mass Ave.                     gnu@prep.ai.mit.edu               *
#    Cambridge, MA  02139, USA         http://www.gnu.ai.mit.edu/        *
#                                                                        *
# It is NOT compatible with the standard UNIX Make.                      *
#                                                                        *
# Executable:                                                            *
#                                                                        *
#    pe_initial        Prepare fields for input to PE model.             *
#    make_transbdy     Extract Transport Streamfunction boundary         *
#                      conditions from previous pe_initial run.          *
#                      (for nesting)                                     *
#                                                                        *
# GNUmake targets:                                                       *
#                                                                        *
#    all               Creates all of the above executables.  (default)  *
#    clean             Remove all intermediate files.                    *
#                                                                        *
# This makefile has been designed to allow the user to compile the       *
# PE_INITIAL source codes in a separate directory from that in which the *
# source codes are located.  The makefile searches for the code segments *
# in the following alternate paths:                                      *
#                                                                        *
#    source code:  (1) the directory containing this GNUmakefile.        *
#                  (2) the directory specified by the macro SRCDIR       *
#                                                                        *
#    include files:  (1) the directory containing this GNUmakefile.      *
#                    (2) the directory specified by the macro PARAMDIR   *
#                    (3) the directory specified by the macro SRCDIR     *
#                    (4) the directory specified by the macro NCDIR      *
#                                                                        *
# This provides the user with the flexibility for the following          *
# configurations:                                                        *
#                                                                        *
#    (1) The user needs only copies of this GNUmakefile, the parameter   *
#        file "param.h" and a path to the source codes to produce a      *
#        version of PE_INITIAL with the appropriate C-preprocessing and  *
#        compilier options.                                              *
#                                                                        *
#    (2) The user who is modifying the PE_INITIAL code, can isolate      *
#        those routines actually being changed with a copy of this       *
#        GNUmakefile in a sub-directory.                                 *
#                                                                        *
#                             Patrick J. Haley Jr.           08/17/2001  *
#                             PE_INITIAL package Version     9.10        *
#                                                                        *
#*************************************************************************
#************************ USER's tunable macros **************************
#*************************************************************************
#
# BIN           name of the executable code
# BINMKTBY      name of the executable for make_transbdy
# BINDIR        directory path for executable code
# CPPFLAGS      C-preprocessing flags and options
# FFLAGS        flags for the fortran compiler
# NCDIR         directory path for NetCDF include files
# PARAMDIR      directory path for include files
# SRCDIR        directory path for source codes
#
#*************************************************************************
#
# C-preprocessing options:
#
# -Daixdate	AIX intrinsic date routine (IBM RS6000).
# -Dcraydate	CRAY's intrinsic date routine.
# -Ddecdate	DEC's VMS intrinsic date routine.
# -Dgendbg      Generic Debugging:  Preserves intermediate files
# -Dleqstate    Temporary option:   Linear Equation of State
# -Dreset_h     Temporary option:  Reset topography for new fluxes (PE 8.3)
# -Drmcomments  Remove comments when pre-processing.
# -Drmdocinc    Remove documentation in all include files
# -Drmpsimean   Temporary option:  removal of level by level dyn. ht. mean
# -Dsundate	SUN's intrinsic date routine.
# -Dsunflush    regularly flush output buffers in SUN systems.
# -Dsunfpe      enable SUN's Floating Point Exception trap.
# -Dunesco      Temporary option:  Unesco (1980) nonlinear eq. state: seawater
#
# New options
#
# -Dgridold       Use old algorithm to set vertical coordinates (back compat.).
# -Dmeantracer    Compute mean tracers for shapiro mean removal
# -Dtrytrby       Try imposing transport streamfunction BC's in open domains.
#
# -Dpepsib      Compute PSIBAR with PE model weighting (precedence over ubpsib)
# -Dubpsib      Compute PSIBAR with weighting for uniform UBARO
#
# even newer options
#
# -DbioDuse        Dusenberry biological model
# -Dtrcascii       ascii file input for biological tracers
# -Dreadbioerr     read error fields for biological tracers
# -Dmeantracer_zip Set mean tracer to initial tracer values
#
#************************************************************************

  SRCDIR = /home/projects2/AOSN2/HOPS/PE_initial/Src
PARAMDIR = /home/projects2/AOSN2/HOPS/PE_initial
   NCDIR = $(NETCDF_ROOT)/include
  BINDIR = /home/projects2/AOSN2/Bin_$(SUNTYPE)

     BIN = pe_initial
BINMKTBY = make_transbdy

CPPFLAGS = -Dsundate -Dsunflush -Dubpsib -Dunesco \
           -Drmpsimean -Dtrytrby
  FFLAGS = -u -O4

#************************************************************************
#******************** End of USER's tunable macros **********************
#************************************************************************

#------------------------------------------------------------------------
#--------------------- Internal macro definitions. ----------------------
#------------------------------------------------------------------------

RMBLKLINES = rmblklines
     SHELL = /bin/sh
        RM = rm -f
      ECHO = echo
     FALSE = false
       LIB = -lnetcdf
       CPP = cpp -P -C -I. -I$(PARAMDIR) -I$(SRCDIR) -I$(NCDIR)
        FC = f77

ifeq (Sun5,$(SUNTYPE))
       LIB := $(LIB) -lnsl
endif

#------------------------------------------------------------------------
#---------------- Look for bad C Pre-Processing Flags. ------------------
#------------------------------------------------------------------------

VALID_CPP_OPT = -Daixdate -DbioDuse -Dcraydate -Ddecdate -Dgendbg -Dgridold \
                -Dleqstate -Dmeantracer -Dmeantracer_zip \
                -Dpepsib -Dreadbioerr -Dreset_h -Drmcomments -Drmdocinc \
                -Drmpsimean -Dsundate -Dsunflush -Dsunfpe -Dtrcascii \
                -Dtrytrby -Dubpsib -Dunesco

INVALID_CPP_OPT = $(strip $(filter-out $(VALID_CPP_OPT),$(CPPFLAGS)))

#------------------------------------------------------------------------
#--------------------- Redefine compiling macros. -----------------------
#------------------------------------------------------------------------

MAKEFLAGS := -r $(MAKEFLAGS)

.SUFFIXES:

.SUFFIXES: .o .f .F .FF

%.o:	%.f
	-$(FC) -c $(FFLAGS) -o $*.o $*.f

%.o:	%.F

%.o:	%.FF

%.f:	%.F
	-$(CPP) $(CPPFLAGS) $< ./$*.cpp
	-./$(RMBLKLINES) < $*.cpp > $*.f
	-$(RM) $*.cpp

%.f:	%.FF
	-$(CPP) $(CPPFLAGS) $< ./$*.f

#------------------------------------------------------------------------
#-------------- Define alternate paths for source codes. ----------------
#------------------------------------------------------------------------

#--------------------------
#--- Define source path ---
#--------------------------

testpath = .
pathstr  =

ifeq (,$(strip $(filter $(SRCDIR),$(subst :, ,$(testpath)))))
   testpath := $(testpath):$(SRCDIR)
   pathstr  := $(SRCDIR)
endif

ifneq (,$(pathstr))
   vpath %.F   $(pathstr)
   vpath %.FF  $(pathstr)
endif

#---------------------------
#--- Define include path ---
#---------------------------

ifeq (,$(strip $(filter $(PARAMDIR),$(subst :, ,$(testpath)))))
   testpath := $(PARAMDIR):$(testpath)
   ifneq (,$(pathstr))
      pathstr  := $(PARAMDIR):$(pathstr)
     else
      pathstr  := $(PARAMDIR)
   endif
endif

ifeq (,$(strip $(filter $(NCDIR),$(subst :, ,$(testpath)))))
   testpath := $(testpath):$(NCDIR)
   ifneq (,$(pathstr))
      pathstr  := $(pathstr):$(NCDIR)
     else
      pathstr  := $(NCDIR)
   endif
endif

ifneq (,$(pathstr))
   vpath %.h   $(pathstr)
   vpath %.inc $(pathstr)
endif

#------------------------------------------------------------------------
#-------- Define string macro to parse C Pre-Processing Flags. ----------
#------------------------------------------------------------------------

find_flag = $(findstring $(flag),$(CPPFLAGS))

#------------------------------------------------------------------------
#------- Preserve intermediate source codes only when debugging. --------
#------------------------------------------------------------------------

dbg_flags = sunfpe gendbg

ifneq (,$(strip $(foreach flag,$(dbg_flags),$(find_flag))))
   .PRECIOUS: %.f
endif

#------------------------------------------------------------------------
#--- Declare "phony" targets.  GNUmake can always run these targets. ----
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
.PHONY: clean
else
.PHONY: clean $(BIN)
endif

#------------------------------------------------------------------------
#---------------------------- Source Codes. -----------------------------
#------------------------------------------------------------------------

  BASESRC =		PE_initial.F					\
	all_uc.F	avg_isle.F	blank.F		blkdat.F	\
	cgpois.F	cgpois1.F	check_depths.F	cperr.F		\
	defcdf.F	errdef.F	errlim.F	exitus.F	\
	external.F	fdiagn.F	filter.F	fsave.F		\
	get_cdfdat.F	get_data.F	get_date.F	get_ewpt.F	\
	get_opn.F	get_parm.F	get_trc.F	get_unif.F	\
	header_fm.F	header_qg.F	head_unif.F	headln.F	\
	inside.F	interp.F	intrp2d.F	length.F	\
	lintrp.F	ll2xy.F		lnblk.F		op.F		\
	op1.F		opencdf.F	p2pber.F	psibar.F	\
	psibavg.F	psibdy.F	put_bry.F	put_init.F	\
	putdiag.F	read_grids.F	read1cdf.F	readcdf.F	\
	read_surfnm.F	reset_isle.F	rotparm.F	save.F		\
	scale_err.F	set_baro.F	set_land.F	set_meantrc.F	\
	set_oerr.F	set_pmask.F	set_psi.F	setbyclsd.F	\
	setbyopn.F	setclsd.F	setdepths.F	shapmean.F	\
	spline.F	splint.F	state_ini.F	swapdata.F	\
	transbdy.F	transobdy.F	trc_names.F	ts2psi.F	\
	v2perr.F	vel_err.F	velocity.F	xread.F		\
	xy2ll.F

   DAYSRC = day_code.F

   DBGSRC = my_handler.F

  VOLDSRC = vertgrid_old.F

  VNEWSRC = vertgrid.F

MKTBYSRC =		make_transbdy.F					\
	bess1d.F	bess2d.F	bess2d_pmsk.F	blkdat.F	\
	exitus.F	get_dom.F	get_ewpt.F	get_isval.F	\
	get_nbhrs.F	get_opn.F	get_pbar.F	intrp_pbar.F	\
	isincrs.F	length.F	ll2xy.F		mktby_data.F	\
	mktby_parm.F	no_digit.F	rotparm.F	set_pmask.F	\
	wrt_transbdy.F	xtr_psiby.F	xy2ll.F

#------------------------------------------------------------------------
#--------------------------- Active Objects. ----------------------------
#------------------------------------------------------------------------

     OBJS = $(BASESRC:.F=.o)
MKTBYOBJS = $(MKTBYSRC:.F=.o)

day_flags = craydate decdate
ifneq (,$(strip $(foreach flag,$(day_flags),$(find_flag))))
   OBJS := $(OBJS) $(DAYSRC:.F=.o)
endif

ifneq (,$(findstring gridold,$(CPPFLAGS)))
   OBJS := $(OBJS) $(VOLDSRC:.F=.o)
 else
   OBJS := $(OBJS) $(VNEWSRC:.F=.o)
endif

ifneq (,$(findstring sunfpe,$(CPPFLAGS)))
   OBJS := $(OBJS) $(DBGSRC:.F=.o)
   MKTBYOBJS := $(MKTBYOBJS) $(DBGSRC:.F=.o)
endif

#------------------------------------------------------------------------
#----------------------- Create all executables. ------------------------
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
all:	$(BIN)	$(BINMKTBY)
	@($(ECHO) "";\
	$(ECHO) "Made all executables:";\
	$(ECHO) "" )
else
all:
		@($(ECHO) "";\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** Invalid CPP options present ***" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "" ;\
                $(ECHO) "$(INVALID_CPP_OPT)" ;\
                $(ECHO) "" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** No compilation will be done ***" ;\
                $(ECHO) "***********************************" ;\
	        $(ECHO) "" ;\
                $(FALSE) )
endif

#------------------------------------------------------------------------
#-------------------- Create PE_INITIAL executable. ---------------------
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
$(BIN):		$(RMBLKLINES) $(OBJS)
		@($(ECHO) "";\
                $(ECHO) "Creating PE_INITIAL executable:" ;\
	        $(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(BIN) $(OBJS) $(LIB)
else
$(BIN):
		@($(ECHO) "";\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** Invalid CPP options present ***" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "" ;\
                $(ECHO) "$(INVALID_CPP_OPT)" ;\
                $(ECHO) "" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** No compilation will be done ***" ;\
                $(ECHO) "***********************************" ;\
	        $(ECHO) "" ;\
                $(FALSE) )
endif

#------------------------------------------------------------------------
#------------------ Create MAKE_TRANSBDY executable. --------------------
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
$(BINMKTBY):	$(RMBLKLINES) $(MKTBYOBJS)
		@($(ECHO) "";\
                $(ECHO) "Creating MAKE_TRANSBDY executable:" ;\
	        $(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(BINMKTBY) $(MKTBYOBJS) $(LIB)
else
$(BINMKTBY):
		@($(ECHO) "";\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** Invalid CPP options present ***" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "" ;\
                $(ECHO) "$(INVALID_CPP_OPT)" ;\
                $(ECHO) "" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** No compilation will be done ***" ;\
                $(ECHO) "***********************************" ;\
	        $(ECHO) "" ;\
                $(FALSE) )
endif

#------------------------------------------------------------------------
#---------------- Create remove blank lines executable. -----------------
#------------------------------------------------------------------------

$(RMBLKLINES):	rmblklines.o
		@($(ECHO) "";\
                $(ECHO) "Making post-processing code  $(RMBLKLINES):";\
		$(ECHO) "" )
		$(FC) $(FFLAGS) -o $(RMBLKLINES) rmblklines.o
		@($(ECHO) "" )

#------------------------------------------------------------------------
#--------- Target for cleaning preprocessed and object files. -----------
#------------------------------------------------------------------------

clean:
		@$(RM) *.f *.o $(RMBLKLINES)

#------------------------------------------------------------------------
#------------- Set dependencies for C-preprocessed files. ---------------
#------------------------------------------------------------------------

PE_initial.f:		cstseg.h	curflds.h	grddat.h	\
			hybrid.h	iounits.h	moddat.h	\
			ndimen.h	oldflds.h	param.h		\
			pi_netcdf.h	shapfil.h	switches.h	\
			zdat.h
bess1d.f:		pconst.h
bess2d.f:		pconst.h
bess2d_pmsk.f:		pconst.h
blkdat.f:		cstseg.h	iounits.h	param.h		\
			pi_netcdf.h	qgfldid.h	switches.h	\
			version.h	pefldid.h
cgpois1.f:		cstseg.h	param.h
check_depths.f:		param.h		hybrid.h	iounits.h	\
			meants.h	ndimen.h	zdat.h
cperr.f:		param.h		ndimen.h
defcdf.f:		param.h		baropar.h	cstseg.h	\
			hybrid.h	meants.h	moddat.h	\
			ndimen.h	netcdf.inc	pi_netcdf.h	\
			switches.h	iounits.h	version.h	\
			zdat.h
errdef.f:		param.h		ndimen.h
errlim.f:		param.h		ndimen.h
exitus.f:		iounits.h	param.h		pi_netcdf.h
external.f:		baropar.h	cstseg.h	curflds.h	\
			grddat.h	hybrid.h	iounits.h	\
			moddat.h	ndimen.h	param.h		\
			switches.h	zdat.h
fdiagn.f:		fdiag.h		iounits.h	pefldid.h
filter.f:		iounits.h	param.h
fsave.f:		cstseg.h	iounits.h	param.h
get_cdfdat.f:		curflds.h	moddat.h	ndimen.h	\
			obserr.h	param.h		pi_netcdf.h	\
			switches.h	workspa.h	zdat.h
get_data.f:		curflds.h	iounits.h	moddat.h	\
			ndimen.h	obserr.h	param.h		\
			pefldid.h	qgfldid.h	switches.h	\
			workspa.h
get_dom.f:		param.h		netcdf.inc	iounits.h
get_isval.f:		param.h		pconst.h	crs_dom.h	\
			fne_dom.h
get_nbhrs.f:		param.h		pconst.h	crs_dom.h	\
			fne_dom.h
get_parm.f:		baropar.h	cstseg.h	curflds.h	\
			hybrid.h	iounits.h	meants.h	\
			moddat.h	ndimen.h	netcdf.inc	\
			obserr.h	param.h		pi_netcdf.h	\
			shapfil.h	switches.h	zdat.h
get_pbar.f:		param.h		netcdf.inc	iounits.h
get_trc.f:		curflds.h	iounits.h	moddat.h	\
			ndimen.h	obserr.h	param.h		\
			pefldid.h	switches.h	workspa.h
get_unif.f:		param.h		curflds.h	iounits.h	\
			moddat.h	ndimen.h	zdat.h
header_fm.f:		iounits.h	moddat.h	ndimen.h	\
			param.h		zdat.h
header_qg.f:		iounits.h	moddat.h	ndimen.h	\
			param.h		zdat.h
head_unif.f:		param.h		iounits.h	zdat.h
headln.f:		version.h
interp.f:		ndimen.h	param.h
intrp_pbar.f:		param.h		crs_dom.h	fne_dom.h
intrp2d.f:		ndimen.h	param.h
make_transbdy.f:	param.h		fne_dom.h
mktby_data.f:		param.h		crs_dom.h	fne_dom.h	\
			iounits.h
mktby_parm.f:		iounits.h
op1.f:			cstseg.h	param.h
opencdf.f:		iounits.h	moddat.h	ndimen.h	\
			netcdf.inc	param.h		pi_netcdf.h	\
			switches.h	zdat.h
p2pber.f:		param.h		hybrid.h	ndimen.h
psibar.f:		curflds.h	hybrid.h	ndimen.h	\
			moddat.h	param.h		switches.h	\
			zdat.h
psibdy.f:		param.h		cstseg.h	curflds.h	\
			grddat.h	hybrid.h	iounits.h	\
			moddat.h	ndimen.h	switches.h
put_bry.f:		cstseg.h	curflds.h	grddat.h	\
			iounits.h	moddat.h	ndimen.h	\
			oldflds.h	param.h		pefldid.h	\
			pi_netcdf.h	switches.h
put_init.f:		curflds.h	iounits.h	ndimen.h	\
			obserr.h	param.h		pefldid.h	\
			pi_netcdf.h	switches.h
putdiag.f:		param.h		curflds.h	iounits.h	\
			ndimen.h	netcdf.inc	pi_netcdf.h	\
			switches.h	zdat.h
read_grids.f:		cstseg.h	grddat.h	hybrid.h	\
			iounits.h	moddat.h	ndimen.h	\
			netcdf.inc	param.h		pi_netcdf.h	\
			switches.h	zdat.h
read1cdf.f:		iounits.h
readcdf.f:		iounits.h
read_surfnm.f:		param.h		netcdf.inc	curflds.h	\
			iounits.h	ndimen.h	pi_netcdf.h
reset_isle.f:		param.h		cstseg.h
save.f:			ndimen.h	param.h		pefldid.h	\
			qgfldid.h	switches.h	workspa.h
scale_err.f:		ndimen.h	obserr.h	param.h		\
			switches.h	zdat.h
set_baro.f:		baropar.h	curflds.h	grddat.h	\
			hybrid.h	iounits.h	moddat.h	\
			ndimen.h	param.h		switches.h	\
			zdat.h
set_land.f:		cstseg.h	ndimen.h	param.h
set_meantrc.f:		param.h		cstseg.h	curflds.h	\
			ndimen.h	switches.h	zdat.h
set_oerr.f:		fdiag.h		hybrid.h	iounits.h	\
			ndimen.h	obserr.h	param.h		\
			pefldid.h	switches.h	zdat.h
set_pmask.f:		param.h
set_psi.f:		param.h		baropar.h	curflds.h	\
			iounits.h	ndimen.h	switches.h	\
			zdat.h 
setbyclsd.f:		param.h		cstseg.h	iounits.h
setbyopn.f:		param.h		cstseg.h	iounits.h
setclsd.f:		param.h		cstseg.h
setdepths.f:		param.h		hybrid.h	iounits.h	\
			ndimen.h	switches.h
spline.f:		pconst.h
splint.f:		pconst.h
swapdata.f:		curflds.h	iounits.h	ndimen.h	\
			oldflds.h	param.h
transbdy.f:		param.h		cstseg.h
transobdy.f:		param.h		cstseg.h
trc_names.f:		pi_netcdf.h
ts2psi.f:		param.h		curflds.h	iounits.h	\
			meants.h	moddat.h	ndimen.h	\
			switches.h	zdat.h
v2perr.f:		param.h		ndimen.h
vel_err.f:		param.h		cstseg.h	grddat.h	\
			hybrid.h	moddat.h
velocity.f:		curflds.h	grddat.h	hybrid.h	\
			iounits.h	moddat.h	ndimen.h	\
			param.h		switches.h	zdat.h
vertgrid.f:		param.h		hybrid.h	ndimen.h	\
			zdat.h
vertgrid_old.f:		param.h		hybrid.h	ndimen.h	\
			zdat.h
wrt_transbdy.f:		iounits.h
xtr_psiby.f:		param.h		fne_dom.h
