#*************************************************************************
#                                                                        *
# GNUmakefile (sun4 or sun5, depending upon .cshrc):                     *
#                                                                        *
# Script to C-preprocess, compile, and link PRES. 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:                                                            *
#                                                                        *
#    pres              Adds pressure to PE output.       (default)       *
#                                                                        *
# GNUmake targets:                                                       *
#                                                                        *
#    clean             Remove all intermediate files.                    *
#                                                                        *
# This makefile has been designed to allow the user to compile the       *
# PEMODEL 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 the PE model with the appropriate C-preprocessing and*
#        compilier options.                                              *
#                                                                        *
#    (2) The user who is modifying the PE model, can isolate             *
#        those routines actually being changed with a copy of this       *
#        GNUmakefile in a sub-directory.                                 *
#                                                                        *
#                                                                        *
#                             Patrick J. Haley Jr.    08/21/2001         *
#                             PE model Version        9.6                *
#                                                                        *
#*************************************************************************
#************************ USER's tunable macros **************************
#*************************************************************************
#
# BIN           name of the executable code
# 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:
#
#  -Dgendbg          Generic Debugging:  Preserves intermediate files
#  -Drmcomments      Remove all commented lines after C-preprocessing
#  -Drmdocinc        Remove documentation in all include files.
#
#  Standards that might be brought in:
#
#  -Daixdate         AIX intrinsic date routine (IBM RS6000).
#  -Ddecdate         DEC's intrisic date/time functions
#  -Dsundate         SUN's intrisic date/time function
#  -Dsunfpe          SUN's floating point exception trap
#  -Dsunflush        regularly flush output buffers in SUN systems.
#
#************************************************************************

  SRCDIR = /data/projects2/AOSN2/NotHops/MSEVA/pressure/Src
   IODIR = /data/projects2/AOSN2/NotHops/MSEVA/share
  SOLDIR = /data/projects2/AOSN2/NotHops/MSEVA/2D/Src
PARAMDIR = .
   NCDIR = $(NETCDF_ROOT)/include
#  BINDIR = /data/projects2/AOSN2/Bin_$(SUNTYPE)
  BINDIR = .

       BIN = pres
CPPFLAGS   = 

  FFLAGS = -u -ansi -fast -fsimple=1

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

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

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

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

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

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

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

VALID_CPP_OPT = -Dgendbg -Drmcomments -Drmdocinc

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

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

ifeq (,$(strip $(filter $(SOLDIR),$(subst :, ,$(testpath)))))
   testpath := $(testpath):$(SOLDIR)
   ifneq (,$(pathstr))
      pathstr  := $(pathstr):$(SOLDIR)
     else
      pathstr  := $(SOLDIR)
   endif
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

wk_flags := nest2larger nest2smaller
ifneq (,$(strip $(foreach flag,$(wk_flags),$(find_flag))))
   ifeq (,$(strip $(filter $(PVM_ROOT)/include,$(subst :, ,$(testpath)))))
      testpath := $(pathstr):$(PVM_ROOT)/include
      ifneq (,$(pathstr))
         pathstr  := $(pathstr):$(PVM_ROOT)/include
        else
         pathstr  := $(PVM_ROOT)/include
      endif
   endif
endif

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

#------------------------------------------------------------------------
#------- 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. -----------------------------
#------------------------------------------------------------------------

#------------------------------------------------------------------------
#  Standard pressure routines.
#------------------------------------------------------------------------

 PRESSRC = 		pres.F						\
	calc_pbc.F	calc_pbt.F	dena_mean.F	extrapo.F	\
	findknot.F	para_input.F	preproc.F	rho_modf.F	\
	setmask.F	vars_xtr.F	vars_modify.F	vert_intgr2.F	\
	zref.F

#------------------------------------------------------------------------
#  Input/Output routines.
#------------------------------------------------------------------------

   IOSRC = extract.F

#------------------------------------------------------------------------
#  Routines for Solver.
#------------------------------------------------------------------------

  SOLSRC = 								\
	band_pivot.ubdry.F	band2_pivot.ubdry.F

#------------------------------------------------------------------------
#  Routine for SUN's floating point exception trap.
#------------------------------------------------------------------------

   DBGSRC = my_handler.F

#------------------------------------------------------------------------
#  Auxillary routine for day of the week in date stamp.
#------------------------------------------------------------------------

   DAYSRC = day_code.F

#------------------------------------------------------------------------
#--------------------- Active Sources and Objects. ----------------------
#------------------------------------------------------------------------

    OBJS = $(PRESSRC:.F=.o) $(IOSRC:.F=.o) $(SOLSRC:.F=.o)

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

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

#------------------------------------------------------------------------
#--------------------------- Strip out tabs. ----------------------------
#------------------------------------------------------------------------

empty :=
space := $(empty) $(empty)
tab := $(empty)	$(empty)

OBJSnt := $(strip $(subst $(tab),$(space),$(OBJS)))

#------------------------------------------------------------------------
#----------------------- Create Pressure Module. ------------------------
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
$(BIN):		$(RMBLKLINES) $(OBJSnt)
		@($(ECHO) "";\
                $(ECHO) "Creating Pressure Module:" ;\
	        $(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(BIN) $(OBJSnt) $(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 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. ---------------
#------------------------------------------------------------------------

calc_pbc.f:	pres.h
calc_pbt.f:	pres.h
dena_mean.f:	pres.h
extract.f:	netcdf.inc
extrapo.f:	pres.h
para_input.f:	pres.h
preproc.f:	pres.h
pres.f:		pres.h
rho_modf.f:	pres.h
setmask.f:	pres.h
vars_modify.f:	pres.h
vars_modify.f:	pres.h
vars_xtr.f:	pres.h
