#include <stdio.h>
#include <math.h>                          // each model has 2 err, err2 ignore
#define  nn   99999                        // UBAND SLONG data
#define  m18  1.8                          // UBAND_star fainter 1.8m than U3
#define  usldir  "/home/primefocus/jiang/ucac3/"
double aa[nn],dd[nn];                      // always epoch + proper motion
double rc,dc,a_c,d_c,up_a,dn_a,up_d,dn_d;  // always 2000.0
double epoch;                              // out_put catalog's epoch
double alph_a,delt_a;                      // with epoch, h,d
double hs,cx,cy,w_de,xlimag,hs2,pi,cosdc,xm_min,xm_max;
short  mx[nn],my[nn],ee[nn];                // mod_mag,
int    u3index[471][360],nu3,n;      // n==nu3, all select star
FILE   *fp,*fp0,*fp1;

short  map[511][511];
int    ipg0=0, ipg1=1, ipg,icall=0;
float  xpg0=0.,xpg1=1.,xpg,ypg;

int    q[13]={1964,3013,308,3647,309,1964,4619,308,2211,309,308,1717,309};
float  crox[4]={252,258,252,258}, croy[4]={252,258,258,252};
float  sx[4]={500,500,0,0}, sy[4]={15,10,10,15};
char   lab[70],f1[30];                    // gif file name
int    fi,igif=1,jgif;                    // gif
int    icat=1;
char   type[2][7]={"star","galaxy" };


main(ac,av)
int ac; char *av[];
{
  double hw_de,hw_al,xmag,xi,xn,xa,xd,z,r,rr;
  float  x,y;
  char   ch,cc,c1[14],c2[14],dis[70],dis1[182],sc[5];
  int    i,j,k,k1,k2,ix,iy,m,num;
  int    gifno=0;                         // for gif

  pi=4.*atan(1.); cx=pi/12.; cy=pi/180.;

  epoch=2000.0;                          // out_put_catalog  epoch
  xlimag=20.0;
  w_de=30.;

  if(ac<3){
    printf("\n\t*********** UBAND_SL(shrink) finding chart ****************\n");
    printf("\n\t                                          jiang 20111220\n");
    printf("\n\t magnitude:  u_mag < 22.5;      field:  120'~~4'\n");
    printf("\n\t Display: Num RA DEC u_mod_mag star/galaxy\n");
    printf("\n\t Usage: usl");
    printf("\n\t        usl !                 (display all usl_star pic.)"); 
    printf("\n\t        usl ra dec");
    printf("\n\t        usl ra dec field");
    printf("\n\t        usl ra dec field GIFname");
    printf("\n\t        usl ra dec field CATname mag_limited");
    printf("\n\tNotice:");
    printf("\n\t    Here using shrink_data, Max_err differ from origion < 0.02 arcsec\n\n"); 
  }

  if(ac==2){ sprintf(lab,"eog %suslall.gif &",usldir); system(lab); exit(0); }
  sprintf(lab,"%sUband_sl.225",usldir); fp=fopen(lab,"rb");
  if(fp==0){ printf("\n\tnot found usl data_base\n"); exit(0); }
  fread(u3index,360*471,4,fp); fclose(fp);
  if(ac>2){
    strcpy(dis,av[1]); strcat(dis," "); strcat(dis,av[2]);
    j=0; for(i=0;i<strlen(dis);i++)if(dis[i]==':')j=1;
    if(j==0){ sscanf(dis,"%lf",&rc); sscanf(av[2],"%lf",&dc); }
    else itohd(dis,&rc,&dc,&z);
    if(ac>3){ sscanf(av[3],"%lf",&w_de); if(w_de<4.)w_de*=4;}
    if(ac<5)goto l10;
// produce GIF directly
    strcpy(f1,av[4]);
    igif=0;
    if(ac<6)goto l10;
// produce CAT directly
    sscanf(av[5],"%lf",&xlimag);
    igif=1; icat=0;
    goto l10;
  }
  printf("Input: alpha,delta,epoch  (Ex.3:46:02.11 23:35:47.8 [2000.0])\n");
  gets(dis);
  itohd(dis,&rc,&dc,&z); if(z<1950. || z>2050. )z=2000.0;
  if(z!=2000.0){ astprs2(rc,dc,z,&xa,&xd,2000.0); rc=xa; dc=xd; }

  printf("finding chart epoch [2000.0]\n");
  gets(lab); sscanf(lab,"%lf",&epoch);
  if(epoch<1950. || epoch>2050.) epoch=2000.0;
  astprs2(rc,dc,2000.,&xa,&xd,epoch);
  toms2(xa,c1,1); toms2(xd,c2,0);
  printf("%s --> %s %s (%6.1lf)\n",dis,c1,c2,epoch);

  printf("magnitude limited [20.0]\n");
  gets(dis);  sscanf(dis,"%lf",&xlimag);  if(xlimag<15.)xlimag=15.;
  printf("limited mag: %6.1lf\n",xlimag);

  printf("Finding chart size, in arcmin.  [30]\n");
  gets(dis);  sscanf(dis,"%lf",&w_de);
  if(w_de>120.)w_de=120.; if(w_de<4.)w_de=4.;
  printf("Field: %d\n\n",(int)w_de);

l10:
  if(icall==0 && igif && icat==1){
    printf("\n*********** Help messages ************\n\n");
    printf("  left button (A) ----  pick out a star\n");
    printf("middle button (D) (or q)----  quit\n");
    printf(" right button (X) ----  new center\n");
    printf(" keyborad type(C,c) --  save catalog (usl.cat, C more color)\n");
    printf(" keyborad type(G,g) --  save as GIF  (usl01.gif...)\n");
    printf("use small keyboard, (Num Look) OR digital_key:\n");
    printf(" Arrow  up(8), down(2), left(4), right(6) ----> move Field\n");
    printf(" PageUp(9), PageDown(3)                   ----> size Field\n");
    printf("\n######################################\n\n");
  }
  while(rc>24.)rc-=24.;     while(rc<0.)rc+=24.;
  if(dc>85.)dc=85.;  if(dc<-25.25)dc=-25.25;
  astprs2(rc,dc,2000.,&xa,&xd,epoch);
  alph_a=xa; delt_a=xd;
  if(icall==1){
    fclose(fp);
    pgsci_(&ipg0); xpg=-25.; ypg=-25.; pgtext_(&xpg,&ypg,dis,strlen(dis));
    pgsch_(&xpg1); k=strlen(lab);
    pglabel_("","",lab,0,0,k);
  }
  if(w_de<4.)w_de=4.;  if(w_de>120.)w_de=120.;  //field:  10~~120'
  fi=w_de*0.25+0.5; sprintf(sc,"%3d'",fi);
  sx[2]=sx[3]=500.-fi/w_de*511.;

  hw_de=w_de/120.;
  up_d=dc+hw_de;  
  dn_d=dc-hw_de; 

  x=cos(up_d*cy);   y=cos(dn_d*cy);  z=x; if(z>y)z=y;
  hw_al=hw_de/z/15.;  if(hw_al>12.)hw_al=12.;
  up_a=rc+hw_al;      if(up_a>=24.)up_a-=24.;
  dn_a=rc-hw_al;      if(dn_a<  0.)dn_a+=24.;

  hs=hw_de*cy;   hs2=255./hs;
  a_c=rc*cx;     d_c=dc*cy;
  cosdc=cos(d_c); if(cosdc<.05)cosdc=.05;
  nu3=0;

  k1=(dn_d+25.25)*4.;  k2=(up_d+25.25)*4.+0.9999;
  if(k1<0)k1=0; if(k2>440)k2=440;
  for(i=k1;i<k2;i++){ cat_u3(i); printf(" usl_star--> %d\n",nu3); }
  n=nu3;                         // total object
  if(n==0){
    printf("#### SORRY, UBAND_SL not supply object in this area!!!\7\n");
    if(xlimag<20.0)printf("May be you choiced field too small !!\n");
//    exit(0);
  }
  xm_max=0.; xm_min=99.;
  for(i=0;i<511;i++)for(j=0;j<511;j++)map[i][j]=3.; // project star
  k=35-20*w_de/120; if(k>33)k=33; if(k<15)k=15; j=(33-k)/2;
  for(i=0;i<nu3;i++){
     cc='g'; xmag=ee[i]*0.001;
     if(xmag<0.){ xmag=-xmag; cc='s'; }
     g89(xmag-m18,mx[i],my[i],j,cc);
  }
  if(icat==0){
    sortd4(aa,dd,ee,mx,my,n);
    fp0=fopen(f1,"w");
    toms2(rc,c1,1); toms2(dc,c2,0);
    sprintf(lab,"%s %s (%6.1lf) %4.1lf~~%4.1lfm  %8d'",
           c1,c2,epoch,xm_min+m18,xm_max+m18,(int)w_de); 
    strcpy(dis1,lab); sprintf(&dis1[48],"Field"); dis1[53]=':';
    fprintf(fp0,"#center:%s\n#",dis1);
    for(i=0;i<52;i++)fprintf(fp0,"-"); 
    fprintf(fp0,"   u_psf        u_mod   typ\n");
    for(i=0;i<n;i++){  
      toms2(aa[i],c1,1);  toms2(dd[i],c2,0);  j=0; y=0.;
      cc='g'; x=ee[i]*0.001; if(x<0.){ x=-x; cc='s'; }
      sprintf(dis1,"%5d%s %s %010d%09d %6.3f%6.3f %6.3f%6.3f %c",
                     i+1,c1,c2,j,j,y,y,x,y,cc);
      fprintf(fp0,"%s\n",dis1);
    }
    fclose(fp0);
    printf("\n\t%s catalog produced!\n\n",f1); exit(0);
  }

  fp=fopen("usl.tmp","w");
  if(icall==0 && igif){                            // pic begin
    icall=pgbegin_(&ipg0,"/xw",&ipg1,&ipg1,3L);
    xpg=6.5; pgpap_(&xpg,&xpg1);
    pgsci_(&ipg1); pgsch_(&xpg1); pgslw_(&ipg1);
    xpg=510.; ipg=-1; pgenv_(&xpg0,&xpg,&xpg0,&xpg,&ipg1,&ipg);  // 0.~~511.
    xpg=400.; ypg=-50.; k=5;  pgxchinese_(&xpg,&ypg,q,&k,&ipg0,&ipg0);
    xpg=-45.; ypg=400.; j=90; pgxchinese_(&xpg,&ypg,&q[5],&k,&ipg0,&j);
    xpg=0.;   ypg=540.; k=1;  pgxchinese_(&xpg,&ypg,&q[11],&k,&ipg0,&ipg0);
  }
  ipg=511;  xpg=20.;
  if(igif)pgxgray_(map,&ipg,&ipg,&ipg1,&ipg,&ipg1,&ipg,&xpg1,&xpg,&ipg1);
  astprs2(rc,dc,2000.,&xa,&xd,epoch);
  toms2(xa,c1,1); toms2(xd,c2,0);
  sprintf(lab,"%s %s (%6.1lf) %4.1lf~~%4.1lf\\um\\d%6d'",
           c1,c2,epoch,xm_min+m18,xm_max+m18,(int)w_de); k=strlen(lab);
  if(igif==0){ togif(); printf("GIF file: %s produced!\n",f1); exit(0); }
  pgsci_(&ipg1); pgsch_(&xpg1);
  pglabel_("          R.A. South","(UBAND_SL)   Decl. East",lab,20,23,k);
  strcpy(dis,lab);  dis[46]=0;
  printf("********** %sm%s\n",dis,&dis[52]);
  ipg=2; pgsci_(&ipg);
  k=2;   pgline_(&k,crox,croy); pgline_(&k,&crox[2],&croy[2]);
  k=4;   pgline_(&k,sx,sy);
  xpg=450.; ypg=15.; pgtext_(&xpg,&ypg,sc,4);
  xpg=0.8;  pgsch_(&xpg);
  x=y=255.;
  num=0;
//****************************************************************
l110:
  pgcurse_(&x,&y,&ch,1);           //  printf("%6.1lf %6.1lf %c\n",x,y,ch);
  if(ch<='9' && ch >'1'){
    xd=w_de/60.; xa=w_de/60./15./cosdc;
    if(ch=='8'){ dc-=xd/50.;  printf("arrow_up\n");    }
    if(ch=='2'){ dc+=xd/50.;  printf("arrow_dow\n");   }
    if(ch=='4'){ rc-=xa/50.;  printf("arrow_lef\n");   }
    if(ch=='6'){ rc+=xa/50.;  printf("arrow_right\n"); }
    if(ch=='9'){ r=w_de*1.05; printf("enlarge field\n");
                 k=r+0.5; r=k; if(r==w_de)r+=1.; w_de=r; }
    if(ch=='3'){ r=w_de/1.05; printf("shrink field\n");
                 k=r+0.5; r=k; if(r==w_de)r-=1.; w_de=r; }
//    toms2(rc,c1,1); toms2(dc,c2,0);
//    printf("  new center: %s %s\n",c1,c2);
    goto l10;
  }
  if(ch=='D' || ch=='q'){             // right button************ exit
    fclose(fp); //pgend_();
    exit(0);
  }
  if(ch=='X'){                        // middle  button********* new center
    r=10000.;
    for(i=0;i<n;i++){
      rr=(mx[i]-x)*(mx[i]-x)+(my[i]-y)*(my[i]-y);
      if(rr<r){ m=i; r=rr; }
    }
    if(r>99.){
      xi=hs-x/hs2;              // accordint cursor position to cal alpha delta
      xn=hs+(y-510.)/hs2;
      astand(a_c,d_c,xi,xn,&xa,&xd);
      rc=xa/cx; dc=xd/cy;
    } else astprs2(aa[m],dd[m],epoch,&rc,&dc,2000.);

    printf("New center:\n");
    goto l10;
  }
  if(ch=='A'){                        // left button************ pick star
    r=10000.;
    for(i=0;i<n;i++){
      rr=(mx[i]-x)*(mx[i]-x)+(my[i]-y)*(my[i]-y);
      if(rr<r){ m=i; r=rr; }
    }
    if(r>99.)goto l999;               // ring only
    x=mx[m]; y=my[m];
    num++; sprintf(sc,"%3d",num);
    ipg=6;  pgsci_(&ipg);
    xpg=x-3.; ypg=y+4.; pgtext_(&xpg,&ypg,sc,3);
    pgsci_(&ipg0); xpg=-25.; ypg=-25.; pgtext_(&xpg,&ypg,dis,strlen(dis));
    toms2(aa[m],c1,1);  toms2(dd[m],c2,0); 
    j=1; z=ee[m]*0.001; if(z<0.){ z=-z; j=0; }
    sprintf(dis,"%3d) %s %s (2000.0) mod_mag: %6.3lf %s",num,c1,c2,z,type[j]);
    ipg=9; pgsci_(&ipg);
    pgtext_(&xpg,&ypg,dis,strlen(dis));         // disply
    printf("%s\n",dis);                         // terminal
    fprintf(fp,"%s %3d %3d\n",dis,mx[m],my[m]);  fflush(fp);        // file
    goto l110;
  }
  if(ch=='C' || ch=='c'){
    sortd4(aa,dd,ee,mx,my,n);
    strcpy(f1,"usl.cat"); if(ch=='C')f1[0]='U';
    fp0=fopen(f1,"w");
    strcpy(dis1,lab); sprintf(&dis1[46],"m Field"); dis1[53]=':';
    fprintf(fp0,"#center:%s\n#",dis1);
    for(i=0;i<52;i++)fprintf(fp0,"-"); 
    fprintf(fp0,"   u_psf        u_mod   typ\n");
    for(i=0;i<n;i++){
      toms2(aa[i],c1,1);  toms2(dd[i],c2,0);  j=0; y=0.;
      cc='g'; x=ee[i]*0.001; if(x<0.){ x=-x; cc='s'; }
      sprintf(dis1,"%5d%s %s %010d%09d %6.3f%6.3f %6.3f%6.3f %c",
                     i+1,c1,c2,j,j,x,y,y,y,cc);
      fprintf(fp0,"%s\n",dis1);
    }
    fclose(fp0);
    printf("\n##### file: %s produed ! %d\n\n",f1,n);
    goto l110;
  }
  if(ch=='G' || ch=='g'){
    system("cp usl.tmp usl.coo");
    sprintf(f1,"usl%02d.gif",++gifno); if(gifno==99)gifno--;
    togif();
    printf("\n&&&&& file: %s produced !\n\n",f1);
  }
l999:
  printf("\7");  fflush(stdout);
  goto l110;
}

gauss(y,m,sigma)
double y[],sigma;
int m;
{
  int i; double x;
  for(i=0;i<m;i++){
    x=i/sigma; x*=x; if(x>80.)return;
    y[i]=exp(-.5*x);
  }
}

g89(mag,ix,iy,nj1,n36)              // also get min,max_mag
double mag;
short  ix,iy;
int    nj1;
char   n36;
{
  double y[33],x,xmag,w,high,sigma,rr,r;
  short  a[33][33];
  int    i,j,k,ii,jj,nj2,kr;

  nj2=32-nj1;  xmag=mag;  w=w_de;
  if(xmag>22.5)xmag=22.5;
  for(i=0;i<33;i++)y[i]=0;
  high=16.; x=60./w+15.-xmag;
  if(w<=8.)x=60./w+(15.-xmag)*2.;
  if(xmag<6.)x=((6.-xmag)*0.3+1.)*x;
  sigma=.5+x*.25; gauss(y,33,sigma);
  rr=(sigma*0.5+0.5)/sqrt(40./w); if(rr<1.)rr=1.;
  for(j=nj1;j<17;j++){
    jj=(j-16); jj*=jj;
    for(i=nj1;i<17;i++){
      r=jj+(i-16)*(i-16);
      kr=sqrt(r)+0.5; r=y[kr]*high+0.5;
      if(n36=='s')if(j==16 || i==16)r*=rr;
      a[i][j]= a[i][32-j]= a[32-i][j]= a[32-i][32-j]=r;
    }
  }
  if(mag>xm_max)xm_max=mag; if(mag<xm_min)xm_min=mag;

  for(j=nj1;j<=nj2;j++){
    jj=iy-16+j; if(jj<0 || jj>510)continue;
    for(k=nj1;k<=nj2;k++){
      ii=ix-16+k; if(ii>=0 && ii <511)map[jj][ii]+=a[k][j];
    }
  }
}

i_star(ra,dc,ix,iy)
double ra,dc; short *ix,*iy;
{
  double xa,xd,xi,xn;
  xa=ra*cx; xd=dc*cy;
  standc(a_c,d_c,xa,xd,&xi,&xn);
  *ix=hs2*(hs-xi)+.5;
  *iy=hs2*(xn-hs)+510.5;
  if(*ix < 0 || *ix >510)return(0);
  if(*iy < 0 || *iy >510)return(0);
  return(1);
}

struct { float ra,dc; short umag; } u;
cat_u3(i360)
int i360;
{
  double x1,x2, ra,dc,xa,xd;
  short  ix,iy,limag,xmag;
  char   f1[70], c1[14],c2[14];
  int    i,j,k;

  i=i360; limag=xlimag*1000;
  x1=dn_a; x2=up_a;  if(x2<x1)x2+=24.;
  j=x1*15.; j--; if(j<0)k=0; else  k=u3index[i][j];
  if(i360<101)sprintf(f1,"%sR%02d%c\0",usldir,(100-i)/4,((100-i)%4)+97);
  else        sprintf(f1,"%sM%02d%c\0",usldir,(i-101)/4,((i-101)%4)+97);
  printf("%s: ",f1);

  fp=fopen(f1,"rb");
  if(fp==0){ printf("datafile not found! %s\n",f1); exit(0); }
l05:
  fseek(fp,k*10,0);
l10:
  fread(&u,1,10,fp);   if(feof(fp))goto l20;
  k++;

  ra=u.ra;  if(ra<0.)ra+=24.;
  if(ra>x2)goto l100;               // normal exit
  if(ra<x1)goto l10;
  dc=u.dc*8.;    if(dc<dn_d || dc>up_d)goto l10;
  xmag=u.umag; if(xmag<0)xmag=-xmag; if(xmag > limag)goto l10;
  if(i_star(ra,dc,&ix,&iy)){
    if(epoch!=2000.0){ astprs2(ra,dc,2000.0,&xa,&xd,epoch); ra =xa; dc=xd; }
    aa[nu3]=ra;
    dd[nu3]=dc;
    ee[nu3] =u.umag;
    mx[nu3]=ix;
    my[nu3]=iy;
    nu3++; if(nu3==nn)nu3--;
  }
  goto l10;
l20:
  x1-=24.; x2-=24.;  k=0;
  goto l05;
l100:
  fclose(fp);
}

toms2(aa,c,k)
double aa;           // input
char c[14];          // output
int k;               // input
/*
 hour or degree to char_line
 if k=1 (hour case) in **:**:**.***
    k=0 (degree       -**:**:**.**
*/
{
  int  i,j;
  double a,b,x;
  a=aa;
  if(k==1){ if(a>24.)a-=24.; if(a<0.)a+=24.; }
  c[0]=32; if(a<0.){ a=-a; c[0]='-'; }
  i=a; b=(a-i)*60.;
  j=b; x=(b-j)*60.;
  if(j==60){ j=0; i++; }
  if(k==1)sprintf(&c[1],"%2.2d:%2.2d:%6.3f",i,j,x);
  if(k==0)sprintf(&c[1],"%2.2d:%2.2d:%5.2f",i,j,x);
  if(c[7]==32)c[7]='0';   if(c[8]==32)c[8]='0';
  if(c[7]=='6'){
    c[7]='0'; j++; sprintf(&c[4],"%2.2d",j);
    if(c[4]=='6'){
      c[4]='0'; i++; sprintf(&c[1],"%2.2d",i);
    }
    c[3]=':'; c[6]=':';
  }
}

astprs2(ra1, dc1, ep1, ra2, dc2, ep2)   // RA in hour, DEC in degree
double  ra1,dc1,ep1,*ra2,*dc2,ep2;      // only ra2,dc2   is output
{
  double r0[3],r1[3],p[3][3],arc;
  double r2,d2;

  arc=45./atan(1.);
  *ra2=ra1; *dc2=dc1;
  if(ep1 == ep2)return;
  r2 =ra1*15./arc; d2 =dc1/arc;
  r0[0]=cos(r2)*cos(d2); r0[1]=sin(r2)*cos(d2); r0[2]=sin(d2);
  if(ep1 != 2000.){
    astrox(ep1, p);
    r1[0] = p[0][0] * r0[0] + p[0][1] * r0[1] + p[0][2] * r0[2];
    r1[1] = p[1][0] * r0[0] + p[1][1] * r0[1] + p[1][2] * r0[2];
    r1[2] = p[2][0] * r0[0] + p[2][1] * r0[1] + p[2][2] * r0[2];
    r0[0] = r1[0]; r0[1] = r1[1]; r0[2] = r1[2];
  }
  if(ep2 != 2000.){
    astrox(ep2, p);
    r1[0] = p[0][0] * r0[0] + p[1][0] * r0[1] + p[2][0] * r0[2];
    r1[1] = p[0][1] * r0[0] + p[1][1] * r0[1] + p[2][1] * r0[2];
    r1[2] = p[0][2] * r0[0] + p[1][2] * r0[1] + p[2][2] * r0[2];
    r0[0] = r1[0];    r0[1] = r1[1];    r0[2] = r1[2];
  }
  *ra2  = atan2(r0[1], r0[0])/15.*arc;
  *dc2 = asin(r0[2])*arc;
  if(*ra2<0)*ra2+=24.;
}

astrox (epoch, p)
double epoch,p[3][3];
{
  double t,a,b,c,ca,cb,cc,sa,sb,sc,arc;
  arc=45./atan(1.);
  astjuy(epoch,&t);
  t = (t - 2451545.0) / 36525.;
  a = t * (0.6406161 + t * (0.0000839 + t * 0.0000050));
  b = t * (0.6406161 + t * (0.0003041 + t * 0.0000051));
  c = t * (0.5567530 - t * (0.0001185 + t * 0.0000116));
  ca = cos (a/arc);
  sa = sin (a/arc);
  cb = cos (b/arc);
  sb = sin (b/arc);
  cc = cos (c/arc);
  sc = sin (c/arc);
  p[0][0] = ca * cb * cc - sa * sb;
  p[1][0] = -sa * cb * cc - ca * sb;
  p[2][0] = -cb * sc;
  p[0][1] = ca * sb * cc + sa * cb;
  p[1][1] = -sa * sb * cc + ca * cb;
  p[2][1] = -sb * sc;
  p[0][2] = ca * sc;
  p[1][2] = -sa * sc;
  p[2][2] = cc;
}

astjuy (epoch,t)
double epoch,*t;
{
  double jd;
  int year,centuy;
  year = epoch - 1;
  centuy = year / 100;
  jd =1721425.5+365.*year-centuy+ year/4 + centuy/4;
  year=epoch;
  *t= jd + (epoch - year) * 365.25;
}

standc(rc,dc,ra,de,xi,xn)
double rc,dc,ra,de,*xi,*xn;
{
  double sd,cd,td,co,tmp;
  sd=sin(dc);
  cd=cos(dc);
  td=tan(de);
  co=cos(ra-rc);
  tmp=sd*td+cd*co;
  *xi=sin(ra-rc)/tmp;
  *xn=(cd*td-sd*co)/tmp;
}

astand(rc,dc,xi,xn,ra,de)
double rc,dc,xi,xn,*ra,*de;
{
  double cd,td;
  cd=cos(dc);
  td=tan(dc);
  *ra=atan(xi/cd/(1.-xn*td));
  *de=atan((xn+td)*cos(*ra)/(1.-xn*td));
  *ra+=rc;
  cd=(*de)-dc; if(cd<0.)cd=-cd;
  if(cd>2.){  *de=-(*de);  *ra+=pi; }
  if(*ra<0.)*ra+=(pi+pi);
}

itohd(a,rc,dc,epoch)
char *a; double *rc,*dc,*epoch;
{
  int i,j,k;
  int i1,i2,g1,g2,g3,p;
  double x3;
  char b[70];
  strcpy(b,a);
  i1=i2=g1=g2=g3=p=0;
  *rc=*dc=*epoch=0.;
  k=strlen(a);
  for(i=0;i<k;i++)if(b[i]<=',')b[i]=32; //allowed comma and below
  for(i=0;i<k;i++)if(b[i]!=32)break;
  if(i!=k)g1=i; else goto l10;
  for(j=g1;j<k;j++)if(b[j]==32)break;
  for(i=j;i<k;i++)if(b[i]!=32)break;
  if(i!=k)g2=i; else goto l10;
  for(j=g2;j<k;j++)if(b[j]==32)break;
  for(i=j;i<k;i++)if(b[i]!=32)break;
  if(i!=k)g3=i;
l10:
  for(i=0;i<k;i++){ if(b[i]==':')b[i]=32; if(b[i]=='-')p=1; }
  if(g2)b[g2-1]=0;
  sscanf(b,"%d %d %lf",&i1,&i2,&x3);
  *rc=i1+i2/60.+x3/3600.;
  if(g2==0)goto l20;
  if(g3)b[g3-1]=0;
  i2=0; x3=0.; sscanf(&b[g2],"%d %d %lf",&i1,&i2,&x3);
  if(i1<0)i1=-i1;
  *dc=i1+i2/60.+x3/3600.;
  if(p)*dc=-*dc;
  if(g3)sscanf(&b[g3],"%lf",epoch);
l20: ;
//  printf("%d %d %d\n",g1,g2,g3);
}

//    for gif ---------------------
#define i800 980
#define j600 596
unsigned char jmap[j600][i800];
int  ik=40;
int  jk=40;
int  color;

unsigned char f08[140][7]= {
     {0x00,0x00,0x10,0x38,0x10,0x00,0x00},  /*  0 */
     {0x00,0x00,0x00,0x10,0x00,0x00,0x00},
     {0x00,0x10,0x10,0x7c,0x10,0x10,0x00},  /*  2  + =43 */
     {0x10,0x54,0x38,0xfe,0x38,0x54,0x10},
     {0x00,0x38,0x44,0x44,0x44,0x38,0x00},
     {0x00,0x44,0x28,0x10,0x28,0x44,0x00},
     {0x00,0x7c,0x44,0x44,0x44,0x7c,0x00},
     {0x00,0x00,0x38,0x38,0x38,0x00,0x00},
     {0x38,0x54,0x92,0xfe,0x92,0x54,0x38},
     {0x38,0x44,0x82,0x92,0x82,0x44,0x38},
     {0x06,0x1a,0x62,0x82,0x62,0x1a,0x06},
     {0x10,0x28,0x44,0x82,0x44,0x28,0x10},
     {0x10,0x38,0x7c,0xfe,0x7c,0x38,0x10},
     {0x82,0x44,0x28,0x10,0x28,0x44,0x82},  /*  13 X =88 */
     {0x38,0x28,0xc6,0x82,0xc6,0x28,0x38},
     {0x38,0x38,0xfe,0xfe,0xfe,0x38,0x38},
     {0x00,0x7c,0x7c,0x7c,0x7c,0x7c,0x00},
     {0x00,0x38,0x7c,0x7c,0x7c,0x38,0x00},
     {0x10,0x54,0x10,0xfe,0x10,0x54,0x10},
     {0xfe,0x82,0x82,0x82,0x82,0x82,0xfe},
     {0x7c,0x82,0x82,0x82,0x82,0x82,0x7c}, /*  20 */
     {0x38,0x44,0x82,0x82,0x82,0x44,0x38},
     {0x00,0x00,0x28,0x10,0x28,0x00,0x00},
     {0x00,0x00,0x10,0x28,0x10,0x00,0x00},
     {0x00,0x00,0x10,0x38,0x10,0x00,0x00},
     {0x10,0x10,0x00,0xc6,0x00,0x10,0x10},
     {0x82,0x44,0x00,0x00,0x00,0x44,0x82},
     {0xc6,0x82,0x00,0x00,0x00,0x82,0xc6},
     {0x10,0x38,0x54,0x10,0x10,0x10,0x10},
     {0x10,0x10,0x10,0x10,0x54,0x38,0x10},
     {0x00,0x20,0x40,0xfe,0x40,0x20,0x00}, /*  30 */
     {0x00,0x08,0x04,0xfe,0x04,0x08,0x00},
     {0X00,0X00,0x00,0x00,0x00,0x00,0x00}, /*  32   */
     {0x00,0x00,0x00,0xFA,0x00,0x00,0x00}, /*  33 ! */
     {0x00,0xE0,0x00,0xE0,0x00,0x00,0x00}, /*  34 " */
     {0x28,0xFE,0x28,0x28,0xFE,0x28,0x00}, /*  35 # */
     {0x20,0x54,0x54,0xFE,0x54,0x54,0x08}, /*  36 $ */
     {0xC2,0xC4,0x08,0x10,0x20,0x46,0x86}, /*  37 % */
     {0x0C,0x72,0x92,0x6A,0x04,0x0A,0x12}, /*  38 & */
     {0x00,0x20,0xC0,0x80,0x00,0x00,0x00}, /*  39 ' */
     {0x00,0x00,0x38,0x44,0x82,0x00,0x00}, /*  40 ( */
     {0x00,0x00,0x82,0x44,0x38,0x00,0x00}, /*  41 ) */
     {0x10,0x54,0x38,0x38,0x54,0x10,0x00}, /*  42 * */
     {0x00,0x10,0x10,0x7C,0x10,0x10,0x00}, /*  43 + */
     {0x00,0x01,0x0E,0x0C,0x00,0x00,0x00}, /*  44 , */
     {0x00,0x10,0x10,0x10,0x10,0x10,0x00}, /*  45 - */
     {0x00,0x00,0x06,0x06,0x00,0x00,0x00}, /*  46 . */
     {0x02,0x04,0x08,0x10,0x20,0x40,0x80}, /*  47 / */
     {0x00,0x7C,0x82,0x82,0x82,0x7C,0x00}, /*  48 0 */
     {0x00,0x00,0x42,0xFE,0x02,0x00,0x00}, /*  49 1 */
     {0x42,0x86,0x8A,0x92,0x92,0x62,0x00}, /*  50 2 */
     {0x44,0x82,0x92,0x92,0x92,0x6C,0x00}, /*  51 3 */
     {0x18,0x28,0x48,0x88,0xFE,0x08,0x00}, /*  52 4 */
     {0xE4,0xA2,0xA2,0xA2,0xA2,0x9C,0x00}, /*  53 5 */
     {0x0C,0x12,0x32,0x52,0x92,0x0C,0x00}, /*  54 6 */
     {0x80,0x86,0x88,0x90,0xA0,0xC0,0x00}, /*  55 7 */
     {0x6C,0x92,0x92,0x92,0x92,0x6C,0x00}, /*  56 8 */
     {0x60,0x92,0x94,0x98,0x90,0x60,0x00}, /*  57 9 */
     {0x00,0x00,0x36,0x36,0x00,0x00,0x00}, /*  58 : */
     {0x00,0x01,0x36,0x34,0x00,0x00,0x00}, /*  59 ; */
     {0x00,0x10,0x28,0x44,0x82,0x00,0x00}, /*  60 < */
     {0x00,0x28,0x28,0x28,0x28,0x28,0x00}, /*  61 = */
     {0x00,0x82,0x44,0x28,0x10,0x00,0x00}, /*  62 > */
     {0x40,0x80,0x8A,0x90,0x90,0x60,0x00}, /*  63 ? */
     {0x40,0x8E,0x92,0x9E,0x82,0x7C,0x00}, /*  64 @ */
     {0x1E,0x28,0x48,0x88,0x48,0x28,0x1E}, /*  65 A */
     {0xFE,0x92,0x92,0x92,0x92,0x6C,0x00}, /*  66 B */
     {0x7C,0x82,0x82,0x82,0x82,0x44,0x00}, /*  67 C */
     {0xFE,0x82,0x82,0x82,0x44,0x38,0x00}, /*  68 D */
     {0xFE,0x92,0x92,0x92,0x92,0x82,0x00}, /*  69 E */
     {0xFE,0x90,0x90,0x90,0x90,0x80,0x00}, /*  70 F */
     {0x7C,0x82,0x82,0x92,0x92,0x5C,0x00}, /*  71 G */
     {0xFE,0x10,0x10,0x10,0x10,0xFE,0x00}, /*  72 H */
     {0x00,0x00,0x82,0xFE,0x82,0x00,0x00}, /*  73 I */
     {0x00,0x04,0x82,0x82,0xFC,0x80,0x00}, /*  74 J */
     {0xFE,0x10,0x10,0x28,0x44,0x82,0x00}, /*  75 K */
     {0xFE,0x02,0x02,0x02,0x02,0x02,0x00}, /*  76 L */
     {0xFE,0x40,0x20,0x10,0x20,0x40,0xFE}, /*  77 M */
     {0xFE,0x40,0x20,0x10,0x08,0x04,0xFE}, /*  78 N */
     {0x7C,0x82,0x82,0x82,0x82,0x7C,0x00}, /*  79 O */
     {0xFE,0x90,0x90,0x90,0x90,0x60,0x00}, /*  80 P */
     {0x7C,0x82,0x82,0x8A,0x84,0x7A,0x00}, /*  81 Q */
     {0xFE,0x90,0x90,0x98,0x94,0x62,0x00}, /*  82 R */
     {0x64,0x92,0x92,0x92,0x92,0x4C,0x00}, /*  83 S */
     {0x80,0x80,0x80,0xFE,0x80,0x80,0x80}, /*  84 T */
     {0xFC,0x02,0x02,0x02,0x02,0xFC,0x00}, /*  85 U */
     {0xF0,0x08,0x04,0x02,0x04,0x08,0xF0}, /*  86 V */
     {0xFC,0x02,0x04,0x18,0x04,0x02,0xFC}, /*  87 W */
     {0x82,0x44,0x28,0x10,0x28,0x44,0x82}, /*  88 X */
     {0x80,0x40,0x20,0x1E,0x20,0x40,0x80}, /*  89 Y */
     {0x82,0x86,0x8A,0x92,0xA2,0xC2,0x00}, /*  90 Z */
     {0x00,0x00,0xFE,0x82,0x82,0x00,0x00}, /*  91 [ */
     {0x80,0x40,0x20,0x10,0x08,0x04,0x02}, /*  92 \ */
     {0x00,0x00,0x82,0x82,0xFE,0x00,0x00}, /*  93 ] */
     {0x00,0x20,0x40,0x80,0x40,0x20,0x00}, /*  94 ^ */
     {0x01,0x01,0x01,0x01,0x01,0x01,0x01}, /*  95 _ */
     {0x00,0x80,0xC0,0x20,0x00,0x00,0x00}, /*  96 ` */
     {0x04,0x2A,0x2A,0x2A,0x1C,0x02,0x00}, /*  97 a */
     {0x00,0xFE,0x12,0x12,0x12,0x0C,0x00}, /*  98 b */
     {0x00,0x1C,0x22,0x22,0x22,0x00,0x00}, /*  99 c */
     {0x00,0x0C,0x12,0x12,0x12,0xFE,0x00}, /* 100 d */
     {0x00,0x1C,0x2A,0x2A,0x2A,0x18,0x00}, /* 101 e */
     {0x00,0x08,0x08,0x3F,0x48,0x48,0x00}, /* 102 f */
     {0x00,0x18,0x25,0x25,0x25,0x1E,0x00}, /* 103 g */
     {0x00,0xFE,0x20,0x20,0x20,0x1E,0x00}, /* 104 h */
     {0x00,0x00,0x00,0x5E,0x00,0x00,0x00}, /* 105 i */
     {0x00,0x02,0x01,0x5E,0x00,0x00,0x00}, /* 106 j */
     {0x00,0xFE,0x10,0x18,0x24,0x42,0x00}, /* 107 k */
     {0x00,0x00,0x80,0xFE,0x00,0x00,0x00}, /* 108 l */
     {0x3E,0x20,0x20,0x3E,0x20,0x20,0x1E}, /* 109 m */
     {0x00,0x3E,0x20,0x20,0x20,0x1E,0x00}, /* 110 n */
     {0x00,0x1C,0x22,0x22,0x22,0x1C,0x00}, /* 111 o */
     {0x00,0x3F,0x24,0x24,0x24,0x18,0x00}, /* 112 p */
     {0x00,0x18,0x24,0x24,0x24,0x3F,0x00}, /* 113 q */
     {0x00,0x20,0x1E,0x20,0x20,0x10,0x00}, /* 114 r */
     {0x10,0x2A,0x2A,0x2A,0x2A,0x04,0x00}, /* 115 s */
     {0x00,0x20,0xFC,0x22,0x22,0x00,0x00}, /* 116 t */
     {0x00,0x3C,0x02,0x02,0x02,0x3E,0x00}, /* 117 u */
     {0x00,0x38,0x04,0x02,0x04,0x38,0x00}, /* 118 v */
     {0x3C,0x02,0x04,0x18,0x04,0x02,0x3C}, /* 119 w */
     {0x00,0x22,0x14,0x08,0x14,0x22,0x00}, /* 120 x */
     {0x21,0x12,0x0C,0x08,0x10,0x20,0x00}, /* 121 y */
     {0x00,0x22,0x26,0x2A,0x32,0x22,0x00}, /* 122 z */
     {0x00,0x00,0x82,0x82,0x6C,0x10,0x00}, /* 123 { */
     {0x00,0x00,0x00,0xFF,0x00,0x00,0x00}, /* 124 | */
     {0x00,0x10,0x6C,0x82,0x82,0x00,0x00}, /* 125 } */
     {0x10,0x20,0x20,0x10,0x08,0x08,0x10}, /* 126 ~ */
     {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 127   */
     {0x00,0xf8,0x40,0x40,0x38,0x00,0x00}, /* 128 h */
     {0xf0,0x80,0x70,0x80,0x70,0x00,0x00}, /* 129 m */
     {0x00,0x48,0xa8,0xa8,0x10,0x00,0x00}, /* 130 s */
     {0x00,0x22,0x22,0xfa,0x22,0x22,0x00}, /* 131 +-*/
     {0x00,0x60,0x90,0x90,0x60,0x00,0x00}, /* 132 o */
     {0x00,0x10,0xe0,0x00,0x00,0x00,0x00}, /* 133 ' */
     {0x10,0xe0,0x10,0xe0,0x00,0x00,0x00}, /* 134 " */
     {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 135   */
     {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 136   */
     {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 137   */
     {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 138   */
     {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 139   */
   };

unsigned char f16[94][16]={
    {0x00,0x00,0x18,0x3c,0x3c,0x3c,0x18,0x18,       //33
     0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00},
    {0x00,0x63,0x63,0x63,0x22,0x00,0x00,0x00,       //34
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x6c,0x6c,0xfe,0x6c,0x6c,       //35
     0x6c,0xfe,0x6c,0x6c,0x00,0x00,0x00,0x00},
    {0x18,0x18,0x7c,0xc6,0xc2,0xc0,0x7c,0x06,
     0x86,0xc6,0x7c,0x18,0x18,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0xc2,0xc6,0x0c,0x18,
     0x30,0x60,0xc6,0x86,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x38,0x6c,0x6c,0x38,0x76,0xdc,
     0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00},
    {0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x0c,0x18,0x30,0x30,0x30,0x30,
     0x30,0x30,0x18,0x0c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x30,0x18,0x0c,0x0c,0x0c,0x0c,
     0x0c,0x0c,0x18,0x30,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x66,0x3c,0xff,
     0x3c,0x66,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x18,0x18,0xff,
     0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x18,0x18,0x18,0x30,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x02,0x06,0x0c,0x18,
     0x30,0x60,0xc0,0x80,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,     //48
     0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,
     0x18,0x18,0x18,0x7e,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0x06,0x0c,0x18,0x30,
     0x60,0xc0,0xc6,0xfe,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0x06,0x06,0x3c,0x06,
     0x06,0x06,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x0c,0x1c,0x3c,0x6c,0xcc,0xfe,
     0x0c,0x0c,0x0c,0x1e,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xfe,0xc0,0xc0,0xc0,0xfc,0x0e,
     0x06,0x06,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x38,0x60,0xc0,0xc0,0xfc,0xc6,
     0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xfe,0xc6,0x06,0x06,0x0c,0x18,
     0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0xc6,0xc6,0x7c,0xc6,
     0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0xc6,0xc6,0x7e,0x06,
     0x06,0x06,0x0c,0x78,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,
     0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,
     0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x06,0x0c,0x18,0x30,0x60,
     0x30,0x18,0x0c,0x06,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x00,
     0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x60,0x30,0x18,0x0c,0x06,
     0x0c,0x18,0x30,0x60,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0xc6,0x0c,0x18,0x18,
     0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x7c,0xc6,0xc6,0xde,0xde,
     0xde,0xdc,0xc0,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x10,0x38,0x6c,0xc6,0xc6,0xfe,
     0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x66,
     0x66,0x66,0x66,0xfc,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xc0,
     0xc0,0xc2,0x66,0x3c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xf8,0x6c,0x66,0x66,0x66,0x66,
     0x66,0x66,0x6c,0xf8,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xfe,0x66,0x62,0x68,0x78,0x68,
     0x60,0x62,0x66,0xfe,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xfe,0x66,0x62,0x68,0x78,0x68,
     0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xde,
     0xc6,0xc6,0x66,0x3a,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xfe,0xc6,
     0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x3c,0x18,0x18,0x18,0x18,0x18,
     0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x1e,0x0c,0x0c,0x0c,0x0c,0x0c,
     0xcc,0xcc,0xcc,0x78,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xe6,0x66,0x6c,0x6c,0x78,0x78,
     0x6c,0x66,0x66,0xe6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xf0,0x60,0x60,0x60,0x60,0x60,
     0x60,0x62,0x66,0xfe,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc3,0xe7,0xff,0xdb,0xdb,0xc3,
     0xc3,0xc3,0xc3,0xc3,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc6,0xe6,0xf6,0xfe,0xde,0xce,
     0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x38,0x6c,0xc6,0xc6,0xc6,0xc6,
     0xc6,0xc6,0x6c,0x38,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x60,
     0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,
     0xc6,0xd6,0xde,0x7c,0x0c,0x0e,0x00,0x00},
    {0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x6c,
     0x66,0x66,0x66,0xe6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x7c,0xc6,0xc6,0x60,0x38,0x0c,
     0x06,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xff,0xdb,0x99,0x18,0x18,0x18,
     0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,
     0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,
     0xc6,0x6c,0x38,0x10,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc3,0xc3,0xc3,0xc3,0xc3,0xdb,
     0xdb,0xff,0x66,0x66,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xc6,0xc6,0x6c,0x6c,0x38,0x38,
     0x6c,0x6c,0xc6,0xc6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x66,0x66,0x66,0x66,0x3c,0x18,
     0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xff,0xc3,0x83,0x06,0x0c,0x18,
     0x30,0x61,0xc3,0xff,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x3e,0x30,0x30,0x30,0x30,0x30,
     0x30,0x30,0x30,0x3e,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x80,0xc0,0xe0,0x70,0x38,
     0x1c,0x0e,0x06,0x02,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x3e,0x06,0x06,0x06,0x06,0x06,
     0x06,0x06,0x06,0x3e,0x00,0x00,0x00,0x00},
    {0x10,0x38,0x6c,0xc6,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0xff,0x00,0x00},
    {0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x78,0x0c,0x7c,
     0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00},
    {0x00,0x00,0xe0,0x60,0x60,0x78,0x6c,0x66,
     0x66,0x66,0x66,0xdc,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xc0,
     0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x1c,0x0c,0x0c,0x3c,0x6c,0xcc,
     0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xfe,
     0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x38,0x6c,0x64,0x60,0xf0,0x60,
     0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,
     0xcc,0xcc,0xcc,0x7c,0x0c,0xcc,0x78,0x00},
    {0x00,0x00,0xe0,0x60,0x60,0x6c,0x76,0x66,
     0x66,0x66,0x66,0xe6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x18,0x18,0x00,0x38,0x18,0x18,
     0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x06,0x06,0x00,0x0e,0x06,0x06,
     0x06,0x06,0x06,0x06,0x66,0x66,0x3c,0x00},
    {0x00,0x00,0xe0,0x60,0x60,0x66,0x6c,0x78,
     0x78,0x6c,0x66,0xe6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,
     0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xe6,0xff,0xdb,
     0xdb,0xdb,0xdb,0xdb,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xdc,0x66,0x66,
     0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xc6,
     0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xdc,0x66,0x66,
     0x66,0x66,0x66,0x7c,0x60,0x60,0xf0,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,
     0xcc,0xcc,0xcc,0x7c,0x0c,0x0c,0x1e,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xdc,0x76,0x62,
     0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0x60,
     0x38,0x0c,0xc6,0x7c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x10,0x30,0x30,0xfc,0x30,0x30,
     0x30,0x30,0x36,0x1c,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,
     0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0x66,0x66,0x66,
     0x66,0x66,0x3c,0x18,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xc3,0xc3,0xc3,
     0xdb,0xdb,0xff,0x66,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xc6,0x6c,0x38,
     0x38,0x38,0x6c,0xc6,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xc6,0xc6,0xc6,
     0xc6,0xc6,0xc6,0x7e,0x06,0x0c,0xf8,0x00},
    {0x00,0x00,0x00,0x00,0x00,0xfe,0xcc,0x18,
     0x30,0x60,0xc6,0xfe,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x0e,0x18,0x18,0x18,0x70,0x18,
     0x18,0x18,0x18,0x0e,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x18,0x18,0x18,0x18,0x00,0x18,
     0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x70,0x18,0x18,0x18,0x0e,0x18,
     0x18,0x18,0x18,0x70,0x00,0x00,0x00,0x00},
    {0x00,0x00,0x76,0xdc,0x00,0x00,0x00,0x00,
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  };

void jpgchar(int ix, int iy, int n, int ik)
{
//   ik=1,-1; 2,-2
  int i,j,k,jx,jy,m;
  if(ik<0){
    m=n&0xff;    if(m>140)return;
    for(i=0;i<7;i++){
      k=f08[m][i];
      if(k==0)continue;
      for(j=-7;j<=0;j++){
        if((k%2)!=0){
          if(ik==-1){ jx=ix+i; jy=iy-j; }
          if(ik==-2){ jx=ix-j; jy=iy-i; }
          jmap[jy][jx]=color;
        }
        k=k/2;
      }
    }
  }
  if(ik>0){
    if(n<=32||n>126)return;
    m=n-33;
    for(i=0;i<16;i++){
      k=f16[m][i];
      if(k==0)continue;
      for(j=-7;j<=0;j++){
        if((k%2)!=0){
          if(ik==1){ jx=ix-j; jy=iy+i; }
          if(ik==2){ jx=ix+i; jy=iy+j; }
          jmap[jy][jx]=color;
        }
        k=k/2;
      }
    }
  }
}

void jpgtext(int x, int y, char *c, int k)
{
  int i,ix,iy;
  ix=x; iy=y;
  for(i=0;i<(int)strlen(c);i++){
    jpgchar(ix,iy,c[i],k);
    if(abs(k)==1)ix=ix+8+k;
    if(abs(k)==2)iy=iy-8-k/2;
  }
}
#define MAXCODES 4096
#define TABLESIZES 4999
unsigned char SuffixTable[4999], ByteBuf[2592], BlockBuf[256];
int Encode, RunBits, MaxCodeSize, ByteCount, ShiftBits;
int Code, Width, Height, Dots, Rows;
int PrefixCode, SuffixCode, RandomIndex, Val;
int EncodeTable[4999], PrefixTable[4999];
unsigned char plate[16][3]={
          0,0,0, 255,255,255, 255,0,0, 0,255,0,
        0,0,255, 0,255,255, 255,0,255, 255,255,0,
      255,128,0, 128,255,0, 0,255,128, 0,128,255,
      128,0,255, 255,0,128, 225,235,255, 64,255,64};
unsigned long TempCode;
jcomp()
{
  int i,j,k;
  short head1[7]={0x4947,0x3846,0x6137,0,0,0xf7,0};
  short head2[5]={0,0,0,0,0x800};
  fp0=fopen(f1,"wb");
  head1[3]=head2[2]=Width=i800+(igif*jgif-1)*392;
  head1[4]=head2[3]=Height=j600;
  fwrite(head1,1,13,fp0);
  fwrite(plate,16,3,fp0);
  for(i=16;i<144;i++){
    ByteBuf[0]=ByteBuf[1]=ByteBuf[2]=(i-16)*2;
    fwrite(ByteBuf,1,3,fp0);
  }
  for(i=144;i<239;i++){                      // 100615
    ByteBuf[0]=ByteBuf[1]=ByteBuf[2]=0;
    fwrite(ByteBuf,1,3,fp0);
  }
  for(j=0,i=239;i<256;i++,j++){
    k=j*16; if(k==256)k=255;
    ByteBuf[0]=ByteBuf[1]=ByteBuf[2]=k;
    fwrite(ByteBuf,1,3,fp0);
  }
  fputc(0x2c,fp0);
  fwrite(head2,2,5,fp0);
  ByteCount = ShiftBits = TempCode = 0;
  jClearTable();
  Code = 256;
  jFillBlockBuf();
  for(Rows = 0; Rows < Height; Rows++)  {
    for(i=0;i<Width;i++)ByteBuf[i]=jmap[Rows][i];
    if(Rows == 0) { PrefixCode = ByteBuf[0];  Dots = 1; }
    else Dots = 0;
    while( Dots < Width)  {
      SuffixCode = ByteBuf[Dots++];
      RandomIndex = PrefixCode ^ (SuffixCode << 4);
      if(RandomIndex == 0)  Val = 1;
      else Val = TABLESIZES - RandomIndex;
      while(1)  {
        if(EncodeTable[RandomIndex] == 0)  {
          Code = PrefixCode;
          jFillBlockBuf();
          if(Encode == MAXCODES)  {
            Code = 256;
            jFillBlockBuf();
            jClearTable();
          }
          else  {
            if(Encode == MaxCodeSize)  {
              MaxCodeSize <<= 1;
              RunBits++;
            }
            PrefixTable[RandomIndex] = PrefixCode;
            SuffixTable[RandomIndex] = SuffixCode;
            EncodeTable[RandomIndex] = Encode++;
          }
          PrefixCode = SuffixCode;
          break;
        }
        if(PrefixTable[RandomIndex] == PrefixCode &&
           SuffixTable[RandomIndex] == SuffixCode)  {
          PrefixCode = EncodeTable[RandomIndex];
          break;
        }
        else  {
          RandomIndex -= Val;
          if(RandomIndex < 0)  RandomIndex += TABLESIZES;
        }
      }
    }
  }
  Code = PrefixCode;
  jFillBlockBuf();
  Code = 257;
  jFillBlockBuf();
  if(ShiftBits > 0 || ByteCount > 0)   {
    BlockBuf[++ByteCount] = TempCode & 0x00FF;
    BlockBuf[0] = ByteCount;
    fwrite(BlockBuf, 1, ByteCount+1, fp0);
    ByteCount = 0;
  }
  fputc(0, fp0);  fputc(';', fp0);
  fclose(fp0);
}

jClearTable()
{
  int i;
  Encode = 258;  RunBits = 9;  MaxCodeSize =512;
  for(i = 0; i < TABLESIZES; i++) EncodeTable[i] = 0;
}

jFillBlockBuf()
{
  TempCode |= (unsigned long)Code << ShiftBits;
  ShiftBits += RunBits;
  while(ShiftBits >= 8)  {
    BlockBuf[++ByteCount] = TempCode & 0x00FF;
    if(ByteCount == 255){
      BlockBuf[0] = ByteCount;
      fwrite(BlockBuf, 1, ByteCount+1, fp0);
      ByteCount = 0;
    }
    TempCode >>= 8;
    ShiftBits -= 8;
  }
}

g88(mag,ix,iy,nj1,n36)              // also get min,max_mag
double mag;
short  ix,iy;
int    nj1;
char   n36;
{
  double y[33],x,xmag,w,high,sigma,rr,r;
  short  a[33][33];
  int    i,j,k,ii,jj,nj2,kr;

  nj2=32-nj1;  xmag=mag;  w=w_de;
  if(xmag>24.)xmag=24.;
  for(i=0;i<33;i++)y[i]=0;
  high=16.; x=60./w+15.-xmag;
  if(w<=8.)x=60./w+(15.-xmag)*2.;
  if(xmag<6.)x=((6.-xmag)*0.3+1.)*x;
  sigma=.5+x*.25; gauss(y,33,sigma);
  rr=(sigma*0.5+0.5)/sqrt(40./w); if(rr<1.)rr=1.;
  for(j=nj1;j<17;j++){
    jj=(j-16); jj*=jj;
    for(i=nj1;i<17;i++){
      r=jj+(i-16)*(i-16);
      kr=sqrt(r)+0.5; r=y[kr]*high+0.5;
      if(n36=='s')if(j==16 || i==16)r*=rr;
      a[i][j]= a[i][32-j]= a[32-i][j]= a[32-i][32-j]=r;
    }
  }
  for(j=nj1;j<=nj2;j++){
    jj=510-(iy-16+j);
    if(jj<0 || jj>510)continue;
    for(k=nj1;k<=nj2;k++){
      ii=ix-16+k;
      if(ii<0 || ii>510)continue;
      if(jmap[jj+jk][ii+ik]<a[k][j]) jmap[jj+jk][ii+ik]=a[k][j];
    }
  }
}

togif()
{
  int i,j,k,m;
  int ix,iy,iz;
  char b[70],bb[70],cc;
  double xmag;

// background color
  for(i=0;i<i800;i++)for(j=0;j<j600;j++)jmap[j][i]=14;
// frame
  for(i=ik-1;i<=ik+511;i++)for(j=jk-1;j<=jk+511;j++)jmap[j][i]=0;
// show star
  k=35-20*w_de/120; if(k>33)k=33; if(k<15)k=15; j=(33-k)/2;
  for(i=0;i<n;i++){
    cc='g'; xmag=ee[i]*0.001; if(xmag<0.){ xmag=-xmag; cc='s'; }  
    g88(xmag-m18,mx[i],my[i],j,cc);
  }
// grey revise
  for(i=ik;i<ik+511;i++)for(j=jk;j<jk+511;j++)jmap[j][i]=255-jmap[j][i];
// draw lable
  color=0;
  strcpy(b,lab);
  sprintf(&b[46],"m Field"); b[53]=':';
  jpgtext(30,18,b,1);
  jpgtext(444,562,"R.A. (South)",1);
  jpgtext(18,400,"(UBAND_SL_mod)   Decl. (East)",2);
// cros
  for(i=252;i<259;i++){ jmap[i+jk][i+ik]=2; jmap[i+jk][510-i+ik]=2; }
// field scale
  i=sx[0]; for(j=sy[0];j>=sy[1];j--)jmap[510-j+jk][i+ik]=2;
  j=sy[1]; for(i=sx[1];i>=sx[2];i--)jmap[510-j+jk][i+ik]=2;
  i=sx[2]; for(j=sy[2];j<=sy[3];j++)jmap[510-j+jk][i+ik]=2;
  sprintf(b,"%3d'",fi);
  color=2;
  jpgtext(470,520,b,1);
  color=0;
  jgif=1;  if(igif==0)goto l30;
// u3.tmp
  fp0=fopen("u3.coo","r");
  k=2; ix=555;
l10:
  fgets(b,70,fp0); if(feof(fp0))goto l20;
  b[59]=0; strcpy(bb,b);
  if(k>31)goto l20;
  iy=(k++)*18;
  color=0; jpgtext(ix,iy,b,-1);
  sscanf(&b[60],"%d %d",&iz,&iy);
  color=4;  b[3]=0; jpgtext(iz+ik-3,511-iy+jk-12,b,-1);
  goto l10;
l20:
  fclose(fp0);
  if(k!=2){color=13; jpgtext(25,570,bb,-1); }
  if(k<4)jgif=0;
l30:
// show time
  fdate_(b); k=strlen(b)-1; b[k]=0;
  color=4; jpgtext(igif*jgif*400+400,586,b,-1);
// produce *.gif
  jcomp();
}

sortd4(x,y,s0,s1,s2,n)
int n; double *x,*y; short *s0,*s1,*s2;
{
  int i,j,k=2,l,ii,ifin;
  double xx,yy;
  short  ss0,ss1,ss2;
  while(k<n)k*=2;
  k=(3*k)/4-1; if(n<k)k=n;
l20:
  k/=2;        ifin=n-k;
  for(ii=0;ii<ifin;ii++){
    i=ii;      j=i+k;
    if(y[i]<=y[j])continue;
    xx = x[j];      yy= y[j]; 
    ss0=s0[j];     ss1=s1[j];     ss2=s2[j];
l40:
     x[j]= x[i];  y[j]= y[i];
    s0[j]=s0[i]; s1[j]=s1[i]; s2[j]=s2[i];
    j=i;       i-=k;
    if(i<0)goto l60;
    if(y[i]>yy)goto l40;
l60:
     x[j]=xx;       y[j]=yy;
    s0[j]=ss0;    s1[j]=ss1;     s2[j]=ss2;
  }
  if(k>1)goto l20;
}
