#++++++++++++++++
#.IDENTIFICATION Makefile
#.LANGUAGE       Make (makefile script)
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT    Unix
#.KEYWORDS       
#.VERSION  1.0   15-Feb-1994
#.PURPOSE        Generate everything from the original files:
#		- the tabulated isochrones from all papers (below $(ISO))
#		- the tabulated evolution tracks (below $(TRK))
#		- the tabulated luminosity functions published in the two
#			most recent papers (below $(PV91) and $(BV92))
#.COMMENTS       
#----------------

SHELL=/bin/sh
# Defaut rule (e.g. copy from a public directory)
.DEFAULT:

# DEFINITIONS:
SHELL=/bin/sh
F77 = f77
AWK = awk
FFLAGS = -O

# FILES:
TRK  =  v85trk vl87trk
ISO  =  v85iso vb85iso vl87iso pv91iso bv92iso
PV91 =  pv911m4d.lf pv911m4s.lf pv911m3d.lf pv911m3s.lf\
	pv914m3d.lf pv914m3s.lf
BV92 =  bv921m4.lf bv922m4.lf bv923m4.lf bv924m4.lf bv926m4.lf \
	bv921m3.lf bv922m3.lf bv923m3.lf bv924m3.lf bv926m3.lf 

# RULES: 
.SUFFIXES: 
.SUFFIXES: .iso .lf 

# Computation of Luminosity Functions from Isochrones
#    The published tabulated limits for each composition are recalled below
#    We use the table to get the parameters used in the published tables
#	via the "set" shell command which generates a parameter array.
.iso.lf: 
	@(echo $< ; echo $@ ; \
	  set `echo '\
	  pv911m4s.lf  0.0  7.5 11 isoch. [Fe/H]=-2.26 [O/Fe]=0.75 Y=0.2350#\
	  pv911m3s.lf -0.5  7.5 11 isoch. [Fe/H]=-1.26 [O/Fe]=0.55 Y=0.2358#\
	  pv914m3s.lf -1.0  7.5 11 isoch. [Fe/H]=-0.65 [O/Fe]=0.30 Y=0.2413#\
	  pv911m4d.lf  0.0  7.5 11 isoch. [Fe/H]=-2.26 [O/Fe]=0.75 Y=0.2350#\
	  pv911m3d.lf -0.5  7.5 11 isoch. [Fe/H]=-1.26 [O/Fe]=0.55 Y=0.2358#\
	  pv914m3d.lf -1.0  7.5 11 isoch. [Fe/H]=-0.65 [O/Fe]=0.30 Y=0.2413#\
	  bv921m4.lf   0.0 11.4 11 isoch. [Fe/H]=-2.26 [O/Fe]=0.75 Y=0.2350#\
	  bv922m4.lf   0.0 11.6 11 isoch. [Fe/H]=-2.03 [O/Fe]=0.70 Y=0.2350#\
	  bv923m4.lf   0.0 11.8 11 isoch. [Fe/H]=-1.78 [O/Fe]=0.66 Y=0.2351#\
	  bv924m4.lf   0.0 11.8 11 isoch. [Fe/H]=-1.66 [O/Fe]=0.63 Y=0.2352#\
	  bv926m4.lf  -0.5 12.0 11 isoch. [Fe/H]=-1.48 [O/Fe]=0.60 Y=0.2354#\
	  bv921m3.lf  -0.5 12.2 11 isoch. [Fe/H]=-1.26 [O/Fe]=0.55 Y=0.2358#\
	  bv922m3.lf  -0.5 12.6 11 isoch. [Fe/H]=-1.03 [O/Fe]=0.50 Y=0.2368#\
	  bv923m3.lf  -1.0 13.0 14 isoch. [Fe/H]=-0.78 [O/Fe]=0.39 Y=0.2391#\
	  bv924m3.lf  -1.0 13.2 14 isoch. [Fe/H]=-0.65 [O/Fe]=0.30 Y=0.2413#\
	  bv926m3.lf  -1.0 13.4 14 isoch. [Fe/H]=-0.47 [O/Fe]=0.23 Y=0.2454#\
	  ' | tr '#' '\012' | fgrep $@` ; x=$$2; mv=$$3; n=$$4; \
	  echo 2.0 ; echo 10.0 ; echo $$x ; i=$$n; \
	  while [ $$i -gt 0 ]; do \
	    echo $$mv ; echo 0.2 ; i=`expr $$i - 1` ; \
	  done ;\
	) | lumu > /dev/null
	@ echo "----File $@:" ; \
	  head -1 $@ | sed 's/FE/Fe/g' | sed 's/ISOCHRONES/isochrones/'

# Creation of FITS versions
# DEPENDENCIES:
all: lumu $(ISO) $(TRK) $(BV92) $(PV91) 

lumu: lumu.f
	$(F77) $(FFLAGS) $? -o $@

$(FITS): tofits.sh
	sh tofits.sh `echo $@ | cut -d. -f1` > $@

# VandenBerg 1985, ApJS 58, 711
v85iso:
	cat v852m2.iso v851m2.iso v856m3.iso v853m3.iso v852m3.iso \
	| sed 's/,//g' | $(AWK) '/ Y =/ { Y=$$5} \
	    / Z =/ { Z=substr($$7,2); next} /^[ END]*$$/{next}\
	    / AGE =/ {A=$$NF; if (A ==  0) A = "0.000"; next}\
	    {printf("%4s %-6s %6s%s\n", Y, Z, A, $$0)}' \
	| cut -c1-18,25-32,34-41,43- > $@
v85trk:
	cat v852m2.trk v851m2.trk v856m3.trk v853m3.trk v852m3.trk \
	| sed 's/,//g' | $(AWK) '/ Y=/ { Y=substr($$4,3)} \
	    / Z=/ { Z=substr($$5,3); next} / M=/ { M=substr($$4,3); next}\
	    / Mv / { next } { printf("%4s %-6s %s\n",Y,Z,$$0) }' \
  	| cut -c1-11,16- > $@

# VandenBerg + Bell 1985, ApJS 58, 561 ; 1987, ApJS 63, 335
vb85iso:
	$pcat vb856m3a.iso vb853m3a.iso vb851m3a.iso vb853m4a.iso \
	      vb851m4a.iso vb856m3b.iso vb853m3b.iso vb851m3b.iso \
	      vb853m4b.iso vb851m4b.iso \
	| sed 's/,//g' | $(AWK) '/ Y =/ { Y=$$5} \
	    / Z =/ { Z=substr($$7,2); next} /^[ END]*$$/{next}\
	    / AGE =/ {A=$$NF; if (A ==  0) A = "0.000"; next}\
	    /^    / {print substr($$0,4); next}\
	    {printf("%4s %-6s %6s%s", Y, Z, A, $$0)}' \
	| cut -c1-18,24-31,33-40,42- > $@

# VandenBerg + Laskarides 1987, ApJS 64, 103
vl87trk:
	cat vl876m2a.trk vl873m2a.trk vl871m1a.trk\
	    vl876m2b.trk vl873m2b.trk vl871m1b.trk\
	| sed 's/,//g' | $(AWK) '/ Y=/ { Y=substr($$4,3)} \
	     / Z=/ { Z=substr($$5,3); next} /M=/{sh=0; Xc=1.0; next}\
	     /Msh/ {next} { X=substr($$0,75)+0; if(X>Xc) sh=1; \
	      printf("%4s %-4s %s %s",Y,Z,substr($$0,1,25),substr($$0,26,32));\
		if (sh > 0) printf "0.0   ";  \
		print substr($$0,75); Xc=X}' \
  	| cut -c1-10,14- > $@

vl87iso:
	cat vl876m2a.iso vl873m2a.iso vl871m1a.iso\
	    vl876m2b.iso vl873m2b.iso vl871m1b.iso\
	| sed 's/,//g' | $(AWK) '/ Y = / { Y=$$5} /Z =/{Z=substr($$7,2);next}\
	    / AGE =/ {A=$$NF; if (A ==  0) A = "0.000"; nw=0; next}\
	    / Z=/ { Z=substr($$5,3); next} /M=/{sh=0; Xc=1.0; next}\
	    /^[ END]*$$/{next}\
	    {for (i=1; i <= NF; i++) { \
	      if ((nw%3) == 0) printf("%4s %-6s %6s", Y, Z, A);\
	      nw++; printf("%8s", $$i); if ((nw%3) == 0) print "";\
	    }}' > $@

# Proffitt + VandenBerg, 1991, ApJS 77, 473
pv91iso:
	for f in pv911m3d.iso pv911m3s.iso pv911m4d.iso pv911m4s.iso\
		pv914m3d.iso pv914m3s.iso; do \
	    (echo $$f ; cat $$f) | $(AWK) '\
	     BEGIN{Z["-2.26"]=" .0001";Z["-2.03"]=" .00017";\
	        Z["-1.78"]=" .0003"; Z["-1.66"]=" .0004";Z["-1.48"]=" .0006";\
			Z["-1.26"]=" .001"; Z["-1.03"]=" .0017"; \
	        Z["-0.78"]=" .003";  Z["-0.65"]=" .004"; Z["-0.47"]=" .006";}\
	    /pv91/{ f=substr($$1, 8, 1);next}\
	    /Y = / {Fe=$$8; O=$$11; Y=$$14; next}\
	    /^[ END]*$$/{next} / AGE =/ {A=$$3; next}\
	    {printf("%6s%-7s%5s%s%5s%s\n",Y,Z[Fe],O,f,A,substr($$0,5,28))}' \
	    >> $@ ;\
	done

# Bergbish + VandenBerg, 1992, ApJS 81, 163
bv92iso: 
	cat `echo $(BV92) | sed 's/\.lf/.iso/g'` \
	| $(AWK) 'BEGIN{Z["-2.26"]=" .0001";Z["-2.03"]=" .00017";\
	     Z["-1.78"]=" .0003"; Z["-1.66"]=" .0004";Z["-1.48"]=" .0006";\
			Z["-1.26"]=" .001"; Z["-1.03"]=" .0017"; \
	     Z["-0.78"]=" .003";  Z["-0.65"]=" .004"; Z["-0.47"]=" .006";}\
	    /Y = / {Fe=$$8; O=$$11; Y=$$14; next}\
	    /^[ END]*$$/{next} / AGE =/ {A=$$3; next}\
	    {printf("%6s%-7s%5s %5s%s\n", Y, Z[Fe], O, A, substr($$0,5,28))}' \
	> $@ 

# Clean up everything
clean:
	rm -f core *.trace *.o *.log $(BV92) $(PV91) lumu

# Clean also the tabular ISO and TRK
CLEAN:	clean
	rm $(ISO) $(TRK)
	

