#*************************************************************************
#                                                                        *
# GNUmakefile (sun4 or sun5, depending upon .cshrc):                     *
#                                                                        *
# Script to C-preprocess, compile, and link various tools for use with   *
# PE model.                                                              *
#                                                                        *
# 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.                      *
#                                                                        *
# Executables:                                                           *
#                                                                        *
# MAKEFLUX2  Reads FNOC files and Fluxes files.                          *
# MAKEFLUX3  Reads FNMOC files and diurnally interpolates.               *
#                                                                        *
# GNUmake targets:                                                       *
#                                                                        *
#    clean             Remove all intermediate files.                    *
#                                                                        *
# This makefile has been designed to allow the user to compile the       *
# 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     *
#                                                                        *
# 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 tools with the appropriate C-preprocessing and   *
#        compilier options.                                              *
#                                                                        *
#    (2) The user who is modifying the tools, can isolate those routines *
#        actually being changed with a copy of this GNUmakefile in a     *
#        sub-directory.                                                  *
#                                                                        *
#                                                                        *
#                             Patrick J. Haley Jr.    01/22/98           *
#                             PE Tools Version        2.1                *
#                                                                        *
#*************************************************************************
#************************ USER's tunable macros **************************
#*************************************************************************
#
# BINDIR        directory path for executable code
# CPPFLAGS      C-preprocessing flags and options
# FLUXBIN       Name for executable for FAKENEST
# FFLAGS        flags for the FORTRAN compiler
# PARAMDIR      directory path for include files
# SRCDIR        directory path for source codes
#
#*************************************************************************
#
# C-preprocessing options:
#
#  -Ddailyavg        produce daily average in new.
#  -Dgendbg          Generic Debugging:  Preserves intermediate files
#  -Dguesscloud      Guess at proper scaling for cloud cover.
#  -Dmakeclimo       Make "climatological" mean fields from the data.
#  -Dsunfpe          SUN's floating point exception trap
#
#  -Dpjhstress       probable correction to wind stress.
#  -Dfixlims         impose max/min values on certain fields.  Generally
#                    NOT a good idea until you're certain of the reading.
#
#  -Dnormcr          Assume cloud cover & relative humidity are normalized.
#
#************************************************************************

  SRCDIR = ../Lib
PARAMDIR = .
  BINDIR = ..

FLUXBIN    = makeflux2_climo

CPPFLAGS   = -Dpjhstress -Dsunfpe
FLUXNEWBIN = aladwind

  FFLAGS = -u -fast -fsimple=1 -ansi -xvector=no
#  FFLAGS = -u -g -C -ansi

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

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

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

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

#------------------------------------------------------------------------
#--------------------- 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 $(findstring $(SRCDIR),$(testpath))))
   testpath := $(testpath):$(SRCDIR)
   pathstr  := $(SRCDIR)
endif

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

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

ifeq (,$(strip $(findstring $(PARAMDIR),$(testpath))))
   testpath := $(PARAMDIR):$(testpath)
   ifneq (,$(pathstr))
      pathstr  := $(PARAMDIR):$(pathstr)
     else
      pathstr  := $(PARAMDIR)
   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. ----
#------------------------------------------------------------------------

.PHONY: clean

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

#------------------------------------------------------------------------
#  Basic source codes for MAKEFLUX
#------------------------------------------------------------------------

FLUXSRC =								\
	makeflux2.F gregorian.F readutil.F

#------------------------------------------------------------------------
#  Basic source codes for new MAKEFLUX
#------------------------------------------------------------------------

FLUXNEWSRC =		aladwind.F					\
	cloud_scale.F	evap_precip.F	get_data.F	gregorian.F	\
	get_parm.F	intatm.F	length.F	maxabs.F	\
	net_heat.F	push_clm.F	read_fnmoc.F	read_frc.F	\
	run_avg.F	set_out.F	scale_fld.F	setfmt.F	\
	shift_fld.F	shortwave.F	windstress.F	wrt_frc.F

#------------------------------------------------------------------------
#  Routine for fixing the limits on certain fields.
#------------------------------------------------------------------------

MMSRC = impmxmn.F

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

DBGSRC = my_handler.F

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

FLUXOBJS = $(FLUXSRC:.F=.o)
FLUXNEWOBJS = $(FLUXNEWSRC:.F=.o)

ifneq (,$(findstring fixlims,$(CPPFLAGS)))
   FLUXNEWOBJS := $(FLUXNEWOBJS) $(MMSRC:.F=.o)
endif

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

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

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

FLUXOBJSnt := $(strip $(subst $(tab),$(space),$(FLUXOBJS)))
FLUXNEWOBJSnt := $(strip $(subst $(tab),$(space),$(FLUXNEWOBJS)))

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

all:	$(FLUXBIN)	$(FLUXNEWBIN)	
	@($(ECHO) "";\
	$(ECHO) "Made all executables:";\
	$(ECHO) "" )

#------------------------------------------------------------------------
#-------------------------- Create MAKEFLUX2. ----------------------------
#------------------------------------------------------------------------

$(FLUXBIN):	$(RMBLKLINES) $(FLUXOBJSnt)
		@($(ECHO) "";\
                $(ECHO) "Creating MAKEFLUX2:" ;\
	        $(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(FLUXBIN) $(FLUXOBJSnt) $(LIB)

#------------------------------------------------------------------------
#-------------------------- Create MAKEFLUX3. ----------------------------
#------------------------------------------------------------------------

$(FLUXNEWBIN):	$(RMBLKLINES) $(FLUXNEWOBJSnt)
		@($(ECHO) "";\
                $(ECHO) "Creating MAKEFLUX3:" ;\
	        $(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(FLUXNEWBIN) $(FLUXNEWOBJSnt) \
			$(LIB)

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

makeflux2.f:	param.h

evap_precip.f:	param.h

aladwind.f:	param.h

net_heat.f:	param.h

shortwave.f:	param.h

windstress.f:	param.h
