# ecRad Makefile, modded for MAR inclusion.
# Autumn 2022: first version (J.-F. Grailet).
# Summer 2023: modifications for beta version of MAR v3.14 (J.-F. Grailet).
# September 2023: further modifications for MAR v3.14 (X. Fettweis).

#############################
### --- CONFIGURATION --- ###
#############################

# J.-F. Grailet remark (02/11/2022): the original Makefile from the ecRad 
# archive provides lots of options, including the possibility to rely on side 
# files "Makefile_include.(profile)" to choose between various compilers (e.g. 
# gfortran and ifort). For the MAR inclusion, the options associated to ifort 
# are hard-coded here, since MAR is intended to be compiled with ifort.

# Paths to my versions of NetCDF for ifort; get simplified a lot on climato.be
NETCDF =
NETCDF_INCLUDE =
NETCDF_LIB = -L/srv1_tmp5/fettweis/MAR-GRD/netcdf/lib -lnetcdff -lnetcdf

# Variables tied to ifort compilation (MAR always compiled with ifort)
FC = gfortran-11
CC = gcc-11
CPPFLAGS = 

ifndef DEBUG
OPTFLAGS = -Ofast
else
OPTFLAGS = -O0
endif

# WARNING (J.-F. Grailet): -heap-arrays is mandatory with ifort !!! Otherwise 
# ecRad will crash systematically due to a bad default handling of automatic 
# arrays (which are properly handled without any special flag by gfortran).
LDFLAGS = -lrt

# Debug options
#WARNFLAGS = -warn all
#BASICFLAGS = -heap-arrays -module ../mod -convert big_endian -fpe0 -fp-model precise -ftz -fp-speculation safe
#DEBUGFLAGS = -g

# JFG: removed -I../ifsrrtm -I../utilities -I../aux -I../mod
# Production options (for clusters)
WARNFLAGS = 
BASICFLAGS = -J../mod -I/srv1_tmp5/fettweis/MAR-GRD/netcdf/include -fconvert=big-endian -w -march=native
DEBUGFLAGS = 

OMPFLAG = -fopenmp

# J.-F. Grailet (02/11/2022): MAR is intended to be compiled in simple 
# precision, so the "single precision" option of the original Makefile is 
# reversed (i.e., flag needed to have double precision).
ifndef DOUBLE_PRECISION
CPPFLAGS += -DPARKIND1_SINGLE
endif

# Allows writing NetCDF4/HDF5 files
#CPPFLAGS += -DNC_NETCDF4

# Consolidate flags
export FC
export FCFLAGS = $(WARNFLAGS) $(BASICFLAGS) $(CPPFLAGS) -I../include \
	$(OPTFLAGS) $(DEBUGFLAGS) $(NETCDF_INCLUDE) $(OMPFLAG)

# Flags for compiling the files in this very folder
export FCFMAIN = $(WARNFLAGS) $(BASICFLAGS) $(CPPFLAGS) -I./include -I./mod \
	$(OPTFLAGS) $(DEBUGFLAGS) $(NETCDF_INCLUDE) $(OMPFLAG)

# LIBS is used to compile the final object that ties everything together
export LIBS    = $(LDFLAGS) -L. -lecrad $(NETCDF_LIB) $(OMPFLAG)

#############################
### --- BUILD TARGETS --- ###
#############################

SOURCES = PHYrad_ecRad_init.F90 PHYrad_ecRad.F90
OBJECTS := $(SOURCES:.F90=.o)

all: build

help:
	@echo "Optional arguments for this Makefile are:"
	@echo "  DEBUG=1              Compile with debug settings on and optimizations off"
	@echo "  DOUBLE_PRECISION=1   Compile with double precision (MAR uses simple precision)"
	@echo "  clean                Remove all compiled files"

build: directories libaux libutilities libifsrrtm libradiation \
	libdrivers $(OBJECTS) libecrad \

# git cannot store empty directories so they may need to be created 
directories: mod lib objs
mod:
	mkdir -p mod
lib:
	mkdir -p lib
objs:
	mkdir -p objs

libaux:
	cd aux && $(MAKE)

libutilities:
	cd utilities && $(MAKE)

libifsrrtm:
	cd ifsrrtm && $(MAKE) -j 8

libradiation:
	cd radiation && $(MAKE)

libdrivers:
	cd drivers && $(MAKE)

%.o: %.F90
	$(FC) $(FCFMAIN) -c $<

# Important remark: there is no name overlap among all .o files, so the 
# following commands should not cause any trouble.
libecrad:
	cd objs && ar -x ../lib/libaux.a && ar -x ../lib/libdrivers.a && ar -x ../lib/libifsrrtm.a
	cd objs && ar -x ../lib/libradiation.a && ar -x ../lib/libutilities.a
	ar r libecrad.a objs/*.o PHYrad_ecRad_init.o PHYrad_ecRad.o

clean: clean-toplevel clean-utilities clean-mods clean-objs clean-main

clean-toplevel:
	cd drivers && $(MAKE) clean
	cd radiation && $(MAKE) clean

clean-utilities:
	cd aux && $(MAKE) clean
	cd utilities && $(MAKE) clean
	cd ifsrrtm && $(MAKE) clean

clean-mods:
	rm -f mod/*.mod

clean-objs:
	rm -f objs/*.o

clean-main:
	rm -f ./*.o
	rm libecrad.a

clean-autosaves:
	rm -f *~ .gitignore~ */*~ */*/*~

.PHONY: all build help libaux libutilities libifsrrtm \
	libradiation libdrivers clean clean-toplevel clean-utilities \
	clean-mods clean-objs clean-main
