SHELL = /bin/sh
#----------------------------------------------------------------
#Set options for compilation of the 1D-Var above the next section
#  *** BEWARE: No trailing whitespace on any variable!!! ***
#----------------------------------------------------------------
#Define FORTRAN compiler
#-----------------------
FC = ifort
#FC = nagfor
#FC = gfortran
#FC = pgf90
#FC = xlf95_r

#Where to find RTTOV. Replace this with your own path to the object files and modules
#------------------------------------------------------------------------------------
RTTOV_Version=12
RTM_DIR = /path/to/rttov12_ifort12
RTM_INC = $(RTM_DIR)/include
RTM_MOD = $(RTM_DIR)/mod
RTM_LIB = $(RTM_DIR)/lib
RTMODEL = rttov12

#Options for compilation depending on use of libraries and atlases
#-----------------------------------------------------------------
#If you aren't using the emissivity atlas and have ascii coefficients, you don't
# need to provide either of NetCDF or HDF libraries. Set EmissAtlas=0 and HDF=0
#
#To use RTTOV HDF5-format coefficients, set HDF=1 and provide location of 
#libraries.
#
#To use the Emissivity Atlas, Set EmissAtlas = 1 and location of libraries.
#The NWPSAF-1DVar supports one of two options:
#  HDF-format Atlas. -> Compile RTTOV with HDF, and in this file set HDF=1
#  NetCDF-format Atlas -> Compile RTTOV with NetCDF Version 4.2+ and set HDF=0
#NetCDF support for the Atlas is provided for RTTOV-11 only
#If you use an older version of NetCDF you will need to modify the linker flags:
#see RTTOV/build/Makefile.local for instructions.

#If any of these confusing options cause problems, please see the RTTOV release 
#documentation.

#Are you using HDF atlas/coeffs? Set HDF = 1 if you are.
HDF=1

#Are you using the atlas? Set EmissAtlas = 1 if you are.
EmissAtlas=0

#Specify path to HDF5 installation if required
#HDF5_DIR = /path/to/hdf

#Specify path to NetCDF-4.2 or higher Library if required for RTTOV Emissivity
#atlas
#NCDF_LIB = /path/to/netcdf-4.2/lib

#----------------------------------------------------------------------------
#No Editing below this line should be necessary, unless you are using a new 
#compiler or are having trouble compiling/running and/or wish to add debug flags
#----------------------------------------------------------------------------

#Locations to search
SOURCE_DIR = ../src/main
INCLUDE_DIR = ../include
SIMSPEC_DIR = ../src/sim_spec

#Set up preprocessor dependent flags that are required to only execute
#certain bits of code inside the 1D-Var depndent on how they're set.
ifeq ($(FC),xlf95_r)
  IFDEF=-WF,-D
else
  IFDEF=-D
endif


# Preprocesssor flags
#--------------------
CPPFLAGS = -I$(RTM_MOD) -I$(RTM_INC)
ifeq ($(EmissAtlas),1)
  CPPFLAGS += $(IFDEF)_CompileEmissAtlas
endif
ifeq ($(HDF),1)
  CPPFLAGS += -I$(HDF5_DIR)/include
endif
ifeq ($(RTTOV_Version),11)
  CPPFLAGS += $(IFDEF)_RTTOV11
endif
ifeq ($(RTTOV_Version),12)
  CPPFLAGS += $(IFDEF)_RTTOV12
endif

# Compiler flags dependent on compiler
#-------------------------------------
ifeq ($(FC),ifort)
  FFLAGS = -cpp -g -r8 -warn all -gen-interface $(CPPFLAGS)
#  FFLAGS = -cpp -check bounds -check pointers -check uninit -g -traceback -r8 -warn all -nogen-interface $(CPPFLAGS)
#  FFLAGS = -cpp -check all -g -pg -debug -traceback -fp-stack-check -r8 -warn all -nogen-interface $(CPPFLAGS)
else ifeq ($(FC),nagfor)
  FFLAGS = -gline -C -fpp -kind=byte $(CPPFLAGS)
#  FFLAGS = -C=all -g -gline -nan -ieee=stop -kind=byte -g90 -maxcontin=120 -PIC -f2003 -fpp $(CPPFLAGS)
else ifeq ($(FC),pgf90)
  FFLAGS = -g  -Kieee -fPIC -C -Mfree -Mpreprocess $(CPPFLAGS)
# FFLAGS = -g  -Mbounds -Mchkptr -traceback -Kieee -fPIC -C -Mfree -Mpreprocess $(CPPFLAGS)
else ifeq ($(FC),gfortran)
  FFLAGS = -g -cpp -fdefault-real-8 -ffree-form $(CPPFLAGS)
#  FFLAGS = -g -cpp -fPIC -fbounds-check -finit-integer=-1 -finit-real=nan \
#            -finit-character=127 -pedantic -Wall -fbacktrace -fdefault-real-8 \
#            -fcheck-array-temporaries -ffree-line-length-none $(CPPFLAGS)
else ifeq ($(FC),xlf95_r)
  FFLAGS = -pg -qsuffix=cpp=f90 -qextname $(CPPFLAGS)
else 
  FFLAGS = -g -cpp -fPIC -fbounds-check -finit-integer=-1 -finit-real=nan \
            -finit-character=127 -pedantic -Wall -fbacktrace -fdefault-real-8 \
            -fcheck-array-temporaries -ffree-line-length-none $(CPPFLAGS)
endif

#Linker flags 
#------------
ifeq ($(HDF),1)
# Linker flags for using HDF
  LFLAGS = -L$(RTM_LIB) -l$(RTMODEL)_coef_io -l$(RTMODEL)_hdf -l$(RTMODEL)_other \
           -l$(RTMODEL)_main -L$(HDF5_DIR)/lib \
           -lhdf5hl_fortran -lhdf5_hl -lhdf5_fortran -lhdf5 -lz -ldl
  ifeq ($(EmissAtlas),1)
# Linker flags if you are adding the Emissivity Atlas
    LFLAGS += -l$(RTMODEL)_emis_atlas
  endif
else
# Linker flags if no Atlas and without HDF
  LFLAGS = -L$(RTM_LIB) -l$(RTMODEL)_coef_io -l$(RTMODEL)_main -l$(RTMODEL)_other
  ifeq ($(EmissAtlas),1)
# Linker flags if you are using the Emissivity Atlas - requires NetCDF4.2+
    LFLAGS += -l$(RTMODEL)_emis_atlas -L$(NCDF_LIB) -lnetcdff
  endif
endif



#List of objects
#---------------

ifeq ($(RTTOV_Version),11)
  RTOBJ = NWPSAF_RTTOV11_Interface.o \
	NWPSAF_RTTOV11_Allocate.o \
	NWPSAF_RTTOV11_GetHMatrix.o
endif
ifeq ($(RTTOV_Version),12)
  RTOBJ = NWPSAF_RTTOV12_Interface.o \
	NWPSAF_RTTOV12_Allocate.o \
	NWPSAF_RTTOV12_GetHMatrix.o
endif

SOURCE = NWPSAF_1DVar_Driver.f90

MODULES = NWPSAFMod_Constants.o \
	NWPSAFMod_Params.o \
	NWPSAFMod_CovarianceMatrices.o \
	NWPSAFMod_Channellist.o \
	NWPSAFMod_RTmodel.o \
	NWPSAFMod_ObsInfo.o \
	NWPSAFMod_LiquidWater.o

OBJS =	NWPSAF_1DVar.o \
	NWPSAF_1DVar_Driver.o \
	NWPSAF_AdditionalCost_Cloud.o \
	NWPSAF_BandInverse.o \
	NWPSAF_BandMultiply.o \
	NWPSAF_Calculate_Cost_Function.o \
	NWPSAF_Channellist.o \
	NWPSAF_CheckIteration.o \
	NWPSAF_Check_Temperatures.o \
	NWPSAF_Cholesky.o \
	NWPSAF_CO2Slice.o \
	NWPSAF_CloudCost.o \
	NWPSAF_CloudyOrNot.o \
	NWPSAF_CloudStructure.o \
	NWPSAF_DeAllocate.o \
	NWPSAF_Fastmodel_Interface.o \
	NWPSAF_FreeUnit.o \
	NWPSAF_GetUnit.o \
	NWPSAF_InitBmatrix.o \
	NWPSAF_InitRmatrix.o \
	NWPSAF_Initialise.o \
	NWPSAF_Qtot_to_q_ql.o \
	NWPSAF_Minimize.o \
	NWPSAF_Minimize_100.o \
	NWPSAF_Minimize_100ML.o \
	NWPSAF_Minimize_101.o \
	NWPSAF_OpenFile.o \
	NWPSAF_ProcessData.o \
	NWPSAF_RMatrix_ChanSelect.o \
	NWPSAF_Read_Background.o \
	NWPSAF_Read_ControlData.o \
	NWPSAF_ReadHeaders.o \
	NWPSAF_Read_Observations.o \
	NWPSAF_Read_EmisEigenVec.o \
	NWPSAF_Read_EmisPCAtlas.o \
	NWPSAF_Report.o \
	NWPSAF_RTTOV_Initialise.o \
	NWPSAF_SatMatInv.o \
	NWPSAF_SetUpBackground.o \
	NWPSAF_SetUpRetrievals.o \
	NWPSAF_StratosExtrap.o \
	NWPSAF_TranslateDataIn.o \
	NWPSAF_TranslateDataOut.o \
	NWPSAF_IntegerSort.o \
	NWPSAF_QSAT.o \
	$(RTOBJ)


# Allowed SUFFIXES:
#------------------
.SUFFIXES:
.SUFFIXES:.f90 .o .mod


ONEDVAR = NWPSAF_1DVar
vpath %.f90 $(SOURCE_DIR):$(SIMSPEC_DIR)

# Object dependencies:
#---------------------

.PHONY: default clean

default: $(ONEDVAR)

$(ONEDVAR) : $(MODULES) $(OBJS)
	$(FC) -o $@ -I$(INCLUDE_DIR) $(MODULES) $(OBJS) $(LFLAGS)

sim_spec_rttov12 : sim_spec_rttov12.o
	$(FC) $(FFLAGS) $< -o $@ $(LFLAGS)

sim_spec_rttov11 : sim_spec_rttov11.o
	$(FC) $(FFLAGS) $< -o $@ $(LFLAGS)

spec_to_pc : spec_to_pc.o
	$(FC) $(FFLAGS) $< -o $@ $(LFLAGS)

pc_to_spec : pc_to_spec.o
	$(FC) $(FFLAGS) $< -o $@ $(LFLAGS)

NWPSAF_1DVar_Driver.o : $(SOURCE) $(MODULES) 
	$(FC) -c $(FFLAGS) -I$(INCLUDE_DIR) $< -o $@

NWPSAFMod_Params.o : NWPSAFMod_Params.f90 NWPSAFMod_Channellist.o \
	NWPSAFMod_Constants.o NWPSAFMod_RTmodel.o
	$(FC) -c $(FFLAGS) -I$(INCLUDE_DIR) $<  -o $@

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

clean_simspec:
	rm -f sim_spec_rttov12 sim_spec_rttov12.o \
	sim_spec_rttov11 sim_spec_rttov11.o \
	spec_to_pc.o pc_to_spec.o spec_to_pc pc_to_spec

clean:
	rm -f $(ONEDVAR) *.mod *genmod.f90 *.o \
	$(SOURCE_DIR)/*.bak $(SOURCE_DIR)/*.bck $(SOURCE_DIR)/*~
