#!/bin/csh
#++++++++++++++++
#.IDENTIFICATION make_ucd
#.LANGUAGE       C-shell
#.AUTHOR         Daniel Egret [IPAC/CDS]
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   26-May-2002
#.VERSION  1.1   09-Apr-2005: Edit alsot UCD1+
#.VERSION  1.2   11-Mar-2006: Linux port
#.PURPOSE        Generate the files for UCDs
#.COMMENTS       
#----------------
#  newcats: re-writes the html files of catalogues and tables

if (! $?METADB) then
    #setenv METADB "metaviz@SYBASE asu asu4VizieR"
    setenv METADB "metaviz@PCDS asu asu4VizieR"
endif
#set verbose
######################################################################
#	Create the table tabname name unit UCD ucd Explanation
######################################################################
echo "....Generating metacol.tsv (a big file)..."
setenv LD_LIBRARY_PATH /usr/local/lib:/opt/sybase/OCS-15_0/lib/
setenv SHLIB_PATH  $LD_LIBRARY_PATH
#printenv | sort
echo "    METADB=$METADB"
echo "    LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
ls -l `which asu`
#set verbose
asu -b -data <<====getTSV \
| acut -f1-2 -i -f3's/^$/---/' -i -f4's/^$/?/' -i, -f5- >! metacol.tsv
-source=METAcol
-out=_tabname(), name, unit, _UCD(), _ucd2(), explain
-out.max=999999
====getTSV

### Verify !
if (-z metacol.tsv) then
    echo "****PROBLEM generating metacol.tsv:"
    ls -l metacol.tsv
    exit 1
endif
### Count #tables having a UCD
acut -f1, -f4's/,.*$//' metacol.tsv \
|    sort -u | acut -f2 | sort | uniq -c >! ucd1.lis
acut -f1, -f4's/^.*,//' metacol.tsv \
|    sort -u | acut -f2 | sort | uniq -c >! ucd2.lis


### List of explanations
asu -b -data -source=METAfam -out=name,explain > metafam.tsv
#sqsh -Uasu -Pasu4VizieR << ====getTSV
#\set bcp_colsep='	'
#\go
#\set colsep='	'
#\go
#Select name, explain
#  from METAfam
#\go -m bcp | sed 's/\|\$//' > metafam.tsv
#====getTSV

### Generate the Indexes
echo "....Generating the Indexes..."
parfile -d -i1 -i- -lw 8 metacol.tsv
parfile -d -i1 -i- -lw 8 metafam.tsv

exit 0

### OLD CODE
sqsh -Uasu -Pasu4VizieR << ====getTSV
\set bcp_colsep='	'
\go
\set colsep='	'
\go
Select METAfam.famid, METAfam.name, METAfam.explain, usage=count(*) 
  from METAfam, METAcol
 Where METAcol.famid = METAfam.famid
 group by METAfam.name
\go -m bcp | sed 's/\|\$//' > metafam.tsv
Select tab=METAtab.name, col=METAcol.name, unit, 
  ucd=METAfam.name, METAcol.explain
  into #u
  From METAcol, METAfam, METAtab
 Where METAcol.famid = METAfam.famid
   and METAcol.catid = METAtab.catid
   and METAcol.tabid = METAtab.tabid
go
Update #u set unit = "---" where unit = ""
go
Select * from #u
\go -m bcp | sed 's/\|\$//' > metacol.tsv
====getTSV

### Generate the Indexes
echo "....Generating the Indexes..."
parfile -d -i1 -i- -lw 8 metacol.tsv
parfile -d -i1 -i- -lw 8 metafam.tsv

### Generate the UCD counts
rm -f metacol.ucd
acut -d -f4,1 metacol.tsv | rev | acut -d/ -f2- | rev \
| sort | acut -d -f1 | uniq -c > metacol.ucd
