#*************************************************************************
#                                                                        *
# GNUmakefile:		                                                 *
#                                                                        *
# Script to compile and link MS-EVA source codes. 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:                                                            *
#                                                                        *
#    hflt          Horizontal filtering/local averaging of mseva terms   *
#                                                                        *
# GNUmake targets:                                                       *
#                                                                        *
#    all               Create all executables.             (default)     *
#    clean             Remove all intermediate files.                    *
#    utility	       Generate a library of utility if not existing	 *
#                                                                        *
# This makefile has been designed to allow the user to compile the       *
# MS-EVA 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 Makefile.           *
#                  (2) the directory specified by the macro SRCDIR       *
#                                                                        *
#    include files:  (1) the directory containing this Makefile.         *
#                    (2) the directory specified by the macro PARAMDIR   *
#                    (3) the directory specified by the macro SRCDIR     *
#                                                                        *
# 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 GRIDS with the appropriate C-preprocessing and       *
#        compilier options.                                              *
#                                                                        *
#    (2) The user who is modifying MS-EVA, can isolate those routines    *
#        actually being changed with a copy of this    Makefile in a     *
#        sub-directory.                                                  *
#                                                                        *
#                             X. San Liang 	    08/11/2004           *
#                             MS-EVA Version           1.1               *
#                                                                        *
#*************************************************************************
#************************ USER's tunable macros **************************
#*************************************************************************
#
# BIN           name for GRIDS executable
# BINDIR        directory path for executable code
# FFLAGS        flags for the FORTRAN compiler
# NCDIR         directory path for NetCDF include files
# PARAMDIR      alternate directory path for include files
# SRCDIR        directory path for source codes
# UTDIR		directory path for some utility files needed
#
#*************************************************************************
#************************************************************************

  SRCDIR = /data/projects2/AOSN2/NotHops/MSEVA/HFLT/Src
   UTDIR = /data/projects2/AOSN2/NotHops/MSEVA/Utility/Src
SHAREDIR = /data/projects2/AOSN2/NotHops/MSEVA/share
#  NCDIR = $(NETCDF_ROOT)/include
PARAMDIR = .
  BINDIR = /data/projects2/AOSN2/Bin_$(SUNTYPE)

  BIN = hflt
  UTLIB = libutility.a

 CPPFLAGS =

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

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

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

RMBLKLINES = rmblklines
     SHELL = /bin/sh
        RM = rm -f
      ECHO = echo
	LIB = -L. -lutility
        AR = ar
    RANLIB = ranlib
       CPP = cpp -P -C -I. -I$(PARAMDIR) -I$(SRCDIR) -I$(UTDIR) -I$(SHAREDIR)
        FC = f77

#------------------------------------------------------------------------
#--------------------- 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 $(UTDIR),$(subst :, ,$(testpath)))))
   testpath := $(testpath):$(UTDIR)
   ifneq (,$(pathstr))
      pathstr  := $(pathstr):$(UTDIR)
     else
      pathstr  := $(UTDIR)
   endif
endif

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

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

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

.PHONY: clean

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

#-------------------------------------------------------------------------
# Define the utility routines used in the local averaging.
#-------------------------------------------------------------------------

UTSRC =									\
	int2.F		m2_inv.F  	m2_power.F	phi2_shape.F	\
	str_wd1.F	int2str.F

#-------------------------------------------------------------------------
# Define the routines that locally average the MS-EVA energetics.
#-------------------------------------------------------------------------

FLTSRC = 		main.F						\
	dilat_matrix.F	fltxy.F		modify.F	para_input.F	\
	readspline.F

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

 LIBOBJS = $(UTSRC:.F=.o)
 FLTOBJS = $(FLTSRC:.F=.o)

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

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

LIBOBJSnt := $(strip $(subst $(tab),$(space),$(LIBOBJS)))
FLTOBJSnt := $(strip $(subst $(tab),$(space),$(FLTOBJS)))

#------------------------------------------------------------------------
#------------------------- Create FLT code. ---------------------------
#------------------------------------------------------------------------

$(BIN):		$(RMBLKLINES) $(UTLIB) $(FLTOBJSnt)
		@($(ECHO) "";\
		$(ECHO) "Creating FLT code:" ;\
		$(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(BIN) $(FLTOBJSnt) $(LIB)

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

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

#------------------------------------------------------------------------
#--------------------- Create the utility library. ----------------------
#------------------------------------------------------------------------

$(UTLIB):	$(LIBOBJSnt)
		@($(ECHO) "";\
		$(ECHO) "Creating utility library:" ;\
		$(ECHO) "" )
		$(AR) $(ARFLAGS) $(UTLIB) $(LIBOBJSnt)
ifneq (,$(strip $(RANLIB)))	
		$(RANLIB) $(UTLIB)	
endif 
		@($(ECHO) "" )

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

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

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

fltxy.f:	fltxy.h
main.f:		param_flt.h	dim.h
modify.f:	param_flt.h	dim.h
para_input.f:	param_flt.h	dim.h
readspline.f:	fltxy.h
