#
# Makefile with descriptions to make .opt .pas and .cyc files in $(RADSROOT)/tables
#
SHELL		= /bin/bash
RADSINDEX	= radsindex.html
ALL		:= $(shell echo {gs,e1,tx,pn,e2,g1,j1,n1,j2,c2,sa,j3,3a,3b}{0,1,2,3,4,5,a,b,c,d,e,f,g,n,r})

.SUFFIXES:	.opt .pas .cyc .nc .FILES $(PHASE).pas
.PRECIOUS:	%.cyc %.pas %.opt

all:		past present
		$(MAKE) web

past:		gs e1 e2 tx pn g1 n1 j1
present:	c2 j2 j3 sa 3a 3b
3a:		3aa
3b:		3ba 3b1 3b2 3bb
c2:		c2a
e1:		e1a e1b e1c e1d e1e e1f e1g
e2:		e2a
g1:		g1a
gs:		gsa gsb gsd
j1:		j1a j1b j1c
j2:		j2a j2b j2c j2d
j3:		j3a
n1:		n1b n1c
pn:		pna
sa:		saa sab
tx:		txa txn txb

$(ALL):
#	@( phase=$@ ; if [ '$phase' = 'c2a' ]; then BRANCH='BRANCH=.op'; fi; $(MAKE) --no-print-directory SAT=$${phase:0:2} $$BRANCH PHASE=$${phase:2:1} $@.FILES);
	@( phase=$@ ; if [ $$phase = 'c2a' ]; then BRANCH='BRANCH=.op'; fi; $(MAKE) --no-print-directory SAT=$${phase:0:2} $$BRANCH PHASE=$${phase:2:1} $@.FILES);

%.FILES:	%.opt %.cyc
	@echo $* is up to date

gsa.opt:	$(shell echo $(RADSROOT)/data/{gs/a/c001/gsp0280c001.nc,nml/getraw_gs.rmf,nml/getraw.rmf})
	radscont -o sat=gs/a cycle=1 pass=280 | sort -un > $@

gsb.opt:	$(shell echo $(RADSROOT)/data/{gs/b/c026/gsp0280c026.nc,nml/getraw_gs.rmf,nml/getraw.rmf})
	radscont -o sat=gs/b cycle=26 pass=280 | sort -un > $@

gsd.opt:	$(shell echo $(RADSROOT)/data/{gs/d/c001/gsp0280c001.nc,nml/getraw_gs.rmf,nml/getraw.rmf})
	radscont -o sat=gs/d cycle=1 pass=280 | sort -un > $@

e1a.opt:	$(shell echo $(RADSROOT)/data/{e1/a/c002/e1p0001c002.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/a cycle=2 pass=1 | sort -un > $@

e1b.opt:	$(shell echo $(RADSROOT)/data/{e1/b/c047/e1p0001c047.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/b cycle=47 pass=1 | sort -un > $@

e1c.opt:	$(shell echo $(RADSROOT)/data/{e1/c/c084/e1p0001c084.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/c cycle=84 pass=1 | sort -un > $@

e1d.opt:	$(shell echo $(RADSROOT)/data/{e1/d/c103/e1p0001c103.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/d cycle=103 pass=1 | sort -un > $@

e1e.opt:	$(shell echo $(RADSROOT)/data/{e1/e/c140/e1p0001c140.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/e cycle=140 pass=1 | sort -un > $@

e1f.opt:	$(shell echo $(RADSROOT)/data/{e1/f/c142/e1p0001c142.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/f cycle=142 pass=1 | sort -un > $@

e1g.opt:	$(shell echo $(RADSROOT)/data/{e1/g/c145/e1p0001c145.nc,nml/getraw_e1.rmf,nml/getraw.rmf})
	radscont -o sat=e1/g cycle=145 pass=1 | sort -un > $@

txa.opt:	$(shell echo $(RADSROOT)/data/{tx/a/c300/txp0001c300.nc,nml/getraw_tx.rmf,nml/getraw.rmf})
	radscont -o sat=tx/a cycle=300 pass=1 | sort -un > $@

txb.opt:	$(shell echo $(RADSROOT)/data/{tx/b/c370/txp0001c370.nc,nml/getraw_tx.rmf,nml/getraw.rmf})
	radscont -o sat=tx/b cycle=370 pass=1 | sort -un > $@

txn.opt:	$(shell echo $(RADSROOT)/data/{tx/n/c365/txp0001c365.nc,nml/getraw_tx.rmf,nml/getraw.rmf})
	radscont -o sat=tx/n cycle=365 pass=1 | sort -un > $@

pna.opt:	$(shell echo $(RADSROOT)/data/{pn/a/c307/pnp0004c307.nc,nml/getraw_pn.rmf,nml/getraw.rmf})
	radscont -o sat=pn/a cycle=307 pass=4 | sort -un > $@

e2a.opt:	$(shell echo $(RADSROOT)/data/{e2/a/c070/e2p0001c070.nc,nml/getraw_e2.rmf,nml/getraw.rmf})
	radscont -o sat=e2/a cycle=70 pass=1 | sort -un > $@

e2b.opt:	$(shell echo $(RADSROOT)/data/{e2/b/c070/e2p0001c070.nc,nml/getraw_e2.rmf,nml/getraw.rmf})
	radscont -o sat=e2/b cycle=70 pass=1 | sort -un > $@

g1a.opt:	$(shell echo $(RADSROOT)/data/{g1/a/c040/g1p0001c040.nc,nml/getraw_g1.rmf,nml/getraw.rmf})
	radscont -o sat=g1/a cycle=40 pass=1 | sort -un > $@

j1a.opt:	$(shell echo $(RADSROOT)/data/{j1/a/c100/j1p0001c100.nc,nml/getraw_j1.rmf,nml/getraw.rmf})
	radscont -o sat=j1/a cycle=100 pass=1 | sort -un > $@

j1b.opt:	$(shell echo $(RADSROOT)/data/{j1/b/c262/j1p0013c262.nc,nml/getraw_j1.rmf,nml/getraw.rmf})
	radscont -o sat=j1/b cycle=262 pass=13 | sort -un > $@

j1c.opt:	$(shell echo $(RADSROOT)/data/{j1/c/c382/j1p0116c382.nc,nml/getraw_j1.rmf,nml/getraw.rmf})
	radscont -o sat=j1/c cycle=382 pass=116 | sort -un > $@

n1b.opt:	$(shell echo $(RADSROOT)/data/{n1/b/c009/n1p0001c009.nc,nml/getraw_n1.rmf,nml/getraw.rmf})
	radscont -o sat=n1/b cycle=9 pass=1 | sort -un > $@

n1c.opt:	$(shell echo $(RADSROOT)/data/{n1/c/c099/n1p0001c099.nc,nml/getraw_n1.rmf,nml/getraw.rmf})
	radscont -o sat=n1/c cycle=99 pass=1 | sort -un > $@

j2a.opt:	$(shell echo $(RADSROOT)/data/{j2/a/c002/j2p0001c002.nc,nml/getraw_j2.rmf,nml/getraw.rmf})
	radscont -o sat=j2/a cycle=2 pass=1 | sort -un > $@

j2b.opt:	$(shell echo $(RADSROOT)/data/{j2/b/c305/j2p0200c305.nc,nml/getraw_j2.rmf,nml/getraw.rmf})
	radscont -o sat=j2/b cycle=305 pass=200 | sort -un > $@

j2c.opt:	$(shell echo $(RADSROOT)/data/{j2/c/c332/j2p0033c332.nc,nml/getraw_j2.rmf,nml/getraw.rmf})
	radscont -o sat=j2/c cycle=332 pass=33 | sort -un > $@

j2d.opt:        $(shell echo $(RADSROOT)/data/{j2/d/c356/j2p0318c356.nc,nml/getraw_j2.rmf,nml/getraw.rmf})
	radscont -o sat=j2/d cycle=356 pass=318 | sort -un > $@

j3a.opt:        $(shell echo $(RADSROOT)/data/{j3/a/c001/j3p0001c001.nc,nml/getraw_j3.rmf,nml/getraw.rmf})
	radscont -o sat=j3/a cycle=1 pass=2 | sort -un > $@

c2a.opt:	$(shell echo $(RADSROOT)/data/{c2/a/c014/c2p0001c014.nc,nml/getraw_c2.rmf,nml/getraw.rmf})
	radscont -o sat=c2/a cycle=14 pass=1 | sort -un > $@

saa.opt:	$(shell echo $(RADSROOT)/data/{sa/a/c001/sap0128c001.nc,nml/getraw_sa.rmf,nml/getraw.rmf})
	radscont -o sat=sa/a cycle=1 pass=128 | sort -un > $@

sab.opt:	$(shell echo $(RADSROOT)/data/{sa/b/c036/sap0001c036.nc,nml/getraw_sa.rmf,nml/getraw.rmf})
	radscont -o sat=sa/b cycle=36 pass=1 | sort -un > $@

3aa.opt:	$(shell echo $(RADSROOT)/data/{3a/a/c005/3ap0615c005.nc,nml/getraw_3a.rmf,nml/getraw.rmf})
	radscont -o sat=3a/a cycle=5 pass=615 | sort -un > $@

3ba.opt:	$(shell echo $(RADSROOT)/data/{3b/a/c010/3bp0615c010.nc,nml/getraw_3b.rmf,nml/getraw.rmf})
	radscont -o sat=3b/a cycle=10 pass=615 | sort -un > $@

3bb.opt:	$(shell echo $(RADSROOT)/data/{3b/b/c019/3bp0101c019.nc,nml/getraw_3b.rmf,nml/getraw.rmf})
	radscont -o sat=3b/b cycle=19 pass=101 | sort -un > $@

3b1.opt:	$(shell echo $(RADSROOT)/data/{3b/1/c008/3bp0148c008.nc,nml/getraw_3b.rmf,nml/getraw.rmf})
	radscont -o sat=3b/1 cycle=8 pass=148 | sort -un > $@

3b2.opt:	$(shell echo $(RADSROOT)/data/{3b/2/c015/3bp0073c015.nc,nml/getraw_3b.rmf,nml/getraw.rmf})
	radscont -o sat=3b/2 cycle=15 pass=73 | sort -un > $@

$(SAT)$(PHASE).pas:	$(foreach dir,$(shell echo $(RADSROOT)/data/$(SAT)$(BRANCH)/$(PHASE)/c???),$(dir)/.passindex)
	sort -k4 $(RADSROOT)/data/$(SAT)$(BRANCH)/$(PHASE)/c???/.passindex > $@

%.cyc:	%.pas
	radscyclesindex < $*.pas | sort -k2 > $@

%/.passindex:	$(shell echo %/??p????c???.nc)
	radspassesindex sat=$(SAT)$(BRANCH)/$(PHASE) $(subst c,cycle=,$(*F)) > $@
	last=`ls -t $*/??p????c???.nc|head -n 1` ; touch -r $$last $@ $*

web:	$(RADSINDEX)
$(RADSINDEX):	$(shell echo *.pas) webtotals.pl
	webtotals.pl > $@
