# Makefile to compile the entire multitaper library, 
# including multitaper routines, plotting routines, 
# and interactive programs. 
#
# Last Modified:
# 	March 26, 2008
# 	German A. Prieto
#

#---------------------------------------------------------------------
# Compiler
#--------------------------------------------------------------------

# Absoft, Sun/Solaris
#FC =      f90
# G95 Free compiler
#FC = g95
# Intel Compiler
#FC = ifort
# Gfortran
FC = gfortran

#--------------------------------------------------------------------
# Compiler flags
#--------------------------------------------------------------------

#   none
#FFLAGS = 

#   Optimize
#FFLAGS = -O 
 
#   debug
FFLAGS = -g
 
# Large memory needed (ifort)
#FFLAGS = -O -heap-arrays 

# Large memory needed (ifort)
#FFLAGS = -O  


#--------------------------------------------------------------------
# Location of files
#--------------------------------------------------------------------

DIR   =    ./
MTSRC =    ./src/
PLSRC =    ./plot/src/
PLXY  =    ./plot/plotprogram/

LIB =      ./libraries/

MOD =      ./modules/

MTPROG =   ./programs/

PLPROG =   ./plot/programs/

PROG =     ./bin/

FIGPROG =  ./examples/src/

EXAMPLE =  ./examples/


#--------------------------------------------------------------------
# Module flag
#--------------------------------------------------------------------

# Sun Compiler
#MFLAG = -M

# Nag Compiler
#MFLAG = -i
#MFLAG = -I

# Absoft Compiler
#MFLAG = -p

# Intel or g95 compiler
MFLAG = -I

MODULE = $(MFLAG)$(MOD)

#--------------------------------------------------------------------
# Fortran Files
#--------------------------------------------------------------------

MTFILES = spectra.f90      mtspec.f90      adaptspec.f90    \
          dpss.f90         dpss_ev.f90     eigenft.f90      \
          fft.f90          ifft.f90        pythag.f90       \
          set_xint.f90     sft.f90         tinvit.f90       \
          tridib.f90       xint.f90        qtdis.f90        \
          qsnorm.f90       yule.f90        nnls.f90         \
	  spline.f90       wv_spec.f90     jackspec.f90     \
	  df_spec.f90      atanh2.f90      nearn.f90        \
          sine_psd.f90     sine_cohe.f90   dpss_spline.f90  \
	  mt_cohe.f90      nsqi.f90        qrfac.f90        \
	  tred1.f90        trbak1.f90      rsm_eig.f90	    \
	  nsinv.f90        ftest.f90       fdis.f90         \
	  psd_reshape.f90  qiinv.f90       zqrfac.f90       \
	  mt_deconv.f90    mt_transfer.f90 oct_spec.f90     \
	  sym_fft.f90     wv_spec_to_array.f90

PLFILES = gplot.f90

MTPRFILES = mtpsd.f90      mtpad.f90   coherence.f90  deconv.f90 \
	    trfunction.f90 wigner.f90  dual_freq.f90  nsqi_psd.f90

PLPRFILES = xyplot.f90

FIGFILES = fig1_2.f90 fig1_2complex.f90 fig3.f90 fig4_5.f90 fig6.f90 


#--------------------------------------------------------------------
# Objects Files
#--------------------------------------------------------------------

MTOBJS =  $(MTFILES:.f90=.o)
PLOBJS =  $(PLFILES:.f90=.o)

#--------------------------------------------------------------------
# Compile libraries
#--------------------------------------------------------------------

# $@ means the target name
# $? means all dependencies (OBJS) that are new.
# $< similar to $? but just on dependency rules (%.o : %.f90)

shared: $(MTOBJS)
	gcc -g -shared *.o -lgfortran -o mtspec.so

all : gplot.a mwlib.a organize mt_progs pl_progs figs

mwlib.a : $(MTOBJS)
	ar cr $@ $(MTOBJS)
	ranlib $@
	rm *.o

gplot.a : $(PLOBJS)
	ar cr $@ $(PLOBJS)
	ranlib $@
	rm *.o

%.o : $(PLSRC)%.f90
	$(FC) $(FFLAGS) -c $< -o $@

%.o : $(MTSRC)%.f90
	$(FC) $(FFLAGS) -c $< -o $@

plotxy : $(PLXY)/plotxy.f
	$(FC) $(FFLAGS) $< -o $(PROG)$@

organize : gplot.a mwlib.a
	mv *.mod modules
	mv *.a libraries

#--------------------------------------------------------------------
# Compile programs
#--------------------------------------------------------------------

mt_progs : mtpsd wigner dual_freq coherence nsqi_psd mtpad trfunction deconv

%:      $(MTPROG)%.f90 $(LIB)gplot.a $(LIB)mwlib.a
	$(FC) $(FFLAGS) $(MODULE) $< $(LIB)gplot.a $(LIB)mwlib.a -o $(PROG)$@
	mv *.mod modules

pl_progs : xyplot

%:      $(PLPROG)%.f90 $(LIB)gplot.a
	$(FC) $(FFLAGS) $(MODULE) $< $(LIB)gplot.a -o $(PROG)$@
	mv *.mod modules

figs : fig1_2 fig3 fig4_5 fig6 

%:      $(FIGPROG)%.f90 $(LIB)gplot.a $(LIB)mwlib.a
	$(FC) $(FFLAGS) $(MODULE) $< $(LIB)gplot.a $(LIB)mwlib.a \
	-o $(EXAMPLE)$@

#--------------------------------------------------------------------
# Clean
#--------------------------------------------------------------------

clean:
	rm $(MOD)*.mod $(LIB)*.a $(PROG)* $(EXAMPLE)fig*

cleandata:
	rm examples/*.dat examples/*.ps examples/*.txt


