# Makefile for compilation of MEDUSA
# with boundary conditions read in from the NetCDF
# data from the simulations of Moore et al. (2002)
# obtained with CESM-BEC

.KEEP_STATE:

.SUFFIXES:

.SUFFIXES: .F .o

# First define some useful things like
RM = /bin/rm -f
MV = /bin/mv -f
CP = /bin/cp
LNSYM = /bin/ln -s
MKDIR = /bin/mkdir -p

SRCDIR_MED   := ../../src-med
INCDIR_LOC   := $(abspath include)
PWDIR_LOC    := $(abspath .)

XMLLIST_COUPSIM_NLCD := $(abspath list_coupsim.xml)
XMLLIST_COUPSIM_LCD  := $(abspath list_coupsim_lcd.xml)


###########################################
### Guita: Debian Squeeze with gfortran ###
### Serenata: Ubuntu 16.04 w/ gfortran  ###
###########################################
FC = mpif90
FPP = cpp -E
FFLAGS = -g -O0 -fmax-errors=5
#FFLAGS = -O3 -fcheck=bounds
INCLFLAGS = -I./modmedusa -I.


LDFLAG_MEDUSA = -L. -lmedusa
LDFLAG_THDYCT = -L../../../libthdyct -lthdyct
LDFLAG_NETCDF = -L/usr/lib -lnetcdf -lnetcdff

LDFLAGS = $(LDFLAG_MEDUSA)  $(LDFLAG_NETCDF)  $(LDFLAG_THDYCT)


# Special options:
# -DNCFILES_SAVEALLSTEPS

OPTIONDEFS = -DCALENDAR_365DAYS -DMEDUSA_BASE2DT2D \
             -DALLOW_MPI


########################################################################
# OPTIONDEFS possible definitions (non-exhaustive list)                #
########################################################################
#
#
# Enable the debugging output, according to the finer options in debug.h
#  
#
#
# Enable MPI (default: no MPI -- see mod_execontrol_medusa.F for more
# information)
#  -DALLOW_MPI
#
#
# Select the calendar (default: -DCALENDAR_365DAYS)
#  -DCALENDAR_360DAYS    1 yr = 360 d,    1 d = 24 h, 1 h = 3600 s
#  -DCALENDAR_365DAYS    1 yr = 365 d,    1 d = 24 h, 1 h = 3600 s
#  -DCALENDAR_365P25DAYS 1 yr = 365.25 d, 1 d = 24 h, 1 h = 3600 s
#
#
# Select interface extensions for host model grids organised
# along more than 1 dimension (default: no extensions)
#  -DMEDUSA_BASE2D     host model grid is mapped onto a 2D map
#                      (host model uses (:,:) arrays)
#  -DMEDUSA_BASE2DT2D  host model grid is mapped onto a 2D array
#                      of 2D tiles (host model uses (:,:,:,:) arrays)
#
#
#  -DGRID_CUSTOM          use custom grid (for full control)
#
#  -DBIODIFFUSION_CUSTOM  use custom profile, to be provided in a
#                         source file called "bdiffc_custom.F", to be 
#                         copied or linked into src-med/gen/include
#
#  -DBIOIRRIGATION_CUSTOM use custom profile, to be provided in a
#                         source file called "birric_custom.F", to be 
#                         copied or linked into src-med/gen/include
#
#
########################################################################
 

# then go ahead
SRCFILES = mod_files_medusa.F mod_coupsim_medusa_setup.F \
           mod_coupsim_o2s.F mod_coupsim_s2o.F \
           mod_timecont_medusa_tsi.F \
           store_nc_aux.F mod_coupsim_subr.F medusa_coupsim.F


TIMESTAMP := $(shell date +%Y%m%d%H%M%S)


###############  You need not to change anything below  ###############

OBJFILES = $(SRCFILES:.F=.o)

coupsim_nlcd:
	make codegen "XMLLIST_MCG = $(XMLLIST_COUPSIM_NLCD)"
	make medusa_coupsim_nlcd_mpi2dt2d


coupsim_lcd:
	make codegen "XMLLIST_MCG = $(XMLLIST_COUPSIM_LCD)"
	make medusa_coupsim_lcd_mpi2dt2d


medusa_coupsim_nlcd_mpi2dt2d medusa_coupsim_lcd_mpi2dt2d : libmedusa.a $(OBJFILES)
	$(FC) $(OPTIONDEFS) $(FFLAGS) $(INCLFLAGS) $(OBJFILES) \
	$(LDFLAGS) -o $@.$(TIMESTAMP)
	$(MV) $@.$(TIMESTAMP) ../../work/coupsim_bec


libmedusa.a: tidy
	(cd -P $(SRCDIR_MED); \
	 make libmedusa.a "OPTIONDEFS = $(OPTIONDEFS)" "FFLAGS = $(FFLAGS)" "FC = $(FC)")
	$(MV) $(SRCDIR_MED)/libmedusa.a .
	$(MKDIR) modmedusa
	$(CP) $(SRCDIR_MED)/*.mod modmedusa


codegen:
	(cd -P $(SRCDIR_MED); \
	 make codegen "XMLLIST_MCG = $(XMLLIST_MCG)" "OPTIONDEFS = $(OPTIONDEFS)" "FC = $(FC)"; \
	 make usetemplates)


.F.o:;
	$(FC) $(OPTIONDEFS) -c $(FFLAGS) $(INCLFLAGS) -o $@ $*.F


clean :
	$(RM) *.o *.mod


tidy : clean
	$(RM) libmedusa.a
	$(RM) -r modmedusa
	$(RM) *% *~ ~* *.log *.bak
