/****   C language I/O subroutines (CSub)  ****/

/*** CSub.1  **/
/****************************************************************************/
/**           Subroutine for Reading the Original Observed Data from Files **/
/**      Named as L*.dat,U*.dat,Q*.dat,T*.dat,V*.dat with two-Byte data,and */
/**      S*.dat,D*.dat,H*.dat with one-Byte data.                           */
/**                                             (Modify By WangTongjiang)   */
/**                                                     -- July 5,1993 --   */
/**							--Jan 25, 1996--    */
/****************************************************************************/
#include <stdio.h>
#define SEEK_SET        0l

	r_data(name,it)
        float it[512][512];
        char  name[12];
	{
	int i,j,n;
        FILE *p;
	char file[12];
        unsigned char  img[512][512];
        unsigned char  fld[512][1024];
        short r,a,b,xx;
        long poil=524290l;

  strcpy(file,name);
  file[12]='\0';
  printf("file=%s\n",file);
printf("file name--->%s",file);
 n=strlen(file);
for(i=0;i<n;i++){
   if((file[i]==' ')||(file[i]<48)||(file[i]>122)||(file[i]>57&&file[i]<65)
        ||(file[i]>90&&file[i]<97)){ 
     printf("n=%d  i=%d\n",n,i+1);
     if(file[i]!='.'&&file[i]!='_'&&file[i]!='-'){
        file[i]=0;
        break;
      }
   }
}

/*	printf("file=%s\n",file);*/
	p=fopen(file,"rb");
/*	printf("pointer=%d\n",p);*/
	if(p==0){
	  printf("Can't open file\n --- Bad File Name! or File Not Exist!\n");
	  printf("File name in <Read_data.c>--->[%s]\n",file);
	  return;
	}


	switch(file[0])
	{
        case 'l':
        case 'q':
        case 'u':
        case 'v':
                    for(j=0;j<512;j++)
                        fread(fld[j],sizeof(char),1024,p);
                    for(j=0;j<512;j++){
                        for(i=0;i<512;i++){
                        a=fld[j][2*i];b=fld[j][2*i+1];
                        b=b<<8;xx=a|b;
                        it[j][i]=xx;
                        }
                     }
		    return;
        default:
                    for(j=0;j<512;j++)
                        fread(img[j],sizeof(char),512,p);
	}
	for(i=0;i<512;i++){
	   for(j=0;j<512;j++){
		it[i][j]=255.-(float)img[i][j];
           }
	}

      fclose(p);
      }


/***** CSub.2   ***/
/****************************************************************************/
/**       Subroutine for Reading the File-Header about the Time ,the Date , */
/**   the three kind of  Active Region Coordinates,Seeing, Frame etc. from  */
/**   the tail of file with Sizes of 79 Bytes.                              */
/**                                           ( Modify By Wangtongjiang )   */
/**                                                -- July 5,1993 --        */
/**						   --Jan 25, 1996--         */
/****************************************************************************/
#include <stdio.h>
#define SEEK_SET 	0l
char buf[6];

	r_head(name,date,time,ql,al,bt,B,L,xx,yy,frame,posit)
	char name[12],date[11],time[8],ql[2];
        char yy[6],xx[6],B[6],L[6],al[6],bt[6],frame[4];
        float posit[7];
	{
	char file[12];
	long poil=524290l;
        unsigned char iposi[14];
        short aa,bb,cc,dd,p0,p1,p6,p3,p2,p4,p5;
	int i,j,n;
	FILE *p;

strcpy(file,name);
  file[12]='\0';
  printf("file=%s\n",file);
printf("file name--->%s",file);
 n=strlen(file);
for(i=0;i<n;i++){
   if((file[i]==' ')||(file[i]<48)||(file[i]>122)||(file[i]>57&&file[i]<65)
        ||(file[i]>90&&file[i]<97)){ 
     printf("n=%d  i=%d\n",n,i+1);
     if(file[i]!='.'&&file[i]!='_'&&file[i]!='-'){
        file[i]=0;
        break;
      }
   }
}


/*        printf("file=%s\n",file);*/
        p=fopen(file,"rb");
/*        printf("pointer=%d\n",p);*/
        if(p==0){
          printf("Can't open file\n --- Bad File Name! or File Not Exist!\n");
          printf("File name in <Read_pointer.c> ---> [%s]\n",file);
          return;
        }

	switch(file[0])
		{
		case 's':
		case 't':
		case 'd':
		case 'c':
	        case 'h':
			poil=262146;
			break;
		} 

	p=fopen(file,"rb");
	fseek(p,poil,SEEK_SET);
	fscanf(p,"%s ",date);

        fseek(p,(poil+13l),SEEK_SET);
        fscanf(p,"%s ",time);

/*       fseek(p,(poil+52l),SEEK_SET);
        fscanf(p,"%s ",temp);
*/
       fseek(p,(poil+60l),SEEK_SET);
        fscanf(p,"%s ",ql);

        fseek(p,(poil+63l),SEEK_SET);
        fread(iposi,sizeof(char),7*2,p);
        fclose(p);


/*  Sun Disk Coordinates (al,bt). Define the cross point between the equator 
   and the longitudinal loop throught the center of solar disk as the Origin
   Point, the unit of al and bt is Degree. Generally,mark (23.5,-15.6) as
   (N23.5,E15.6). */
        aa=iposi[0];bb=iposi[1];cc=iposi[2];dd=iposi[3];
        bb=bb<<8;p0=aa|bb;
        dd=dd<<8;p1=cc|dd;
        posit[0]=(float)(p0/10.);posit[1]=(float)(p1/10.0);

/*  Orthogonal Coordinates (xx,yy). Define the center of solar disk as the
   Origin point, and the x-axis, y-axis parallel to  the herizonal axis ,the
   vertical axis of the observational area, respectively.   xx=1000*X/R  
   yy=1000*Y/R */ 
        aa=iposi[4];bb=iposi[5];cc=iposi[6];dd=iposi[7];
        bb=bb<<8;p2=aa|bb;
        dd=dd<<8;p3=cc|dd;
        posit[2]=(float)(p2/10.);posit[3]=(float)(p3/10.);
 
/*  Carrington Coordinates (B,L).  Notes: B=al, L=L0+bt , L0 is the Carrington
  Longintude of solar disk center at the time of observing. */
        aa=iposi[8];bb=iposi[9];cc=iposi[10];dd=iposi[11];
        bb=bb<<8;p4=aa|bb;
        dd=dd<<8;p5=cc|dd;
        posit[4]=(float)(p4/10.);posit[5]=(float)(p5/10.);

/*   Frame number: frame */  
        aa=iposi[12];bb=iposi[13];
        bb=bb<<8;p6=aa|bb;
        posit[6]=(float)(p6/10.);


	gcvt(posit[0],6,buf);
	strcpy(al,buf);
	al[6]='\0';
	for(i=5;i>0;i--)
	{
	j=test(al,i);
	if(j==0) break;
	}

        gcvt(posit[1],6,buf);
        strcpy(bt,buf);
        bt[6]='\0'; 
        for(i=5;i>0;i--)
        {
        j=test(bt,i);
        if(j==0) break; 
        } 
 
        gcvt(posit[4],6,buf);
        strcpy(B,buf);
        B[6]='\0';
        for(i=5;i>0;i--)
        {
        j=test(B,i);
        if(j==0) break;
        }

        gcvt(posit[5],6,buf);
        strcpy(L,buf);
        L[6]='\0'; 
        for(i=5;i>0;i--) 
        { 
        j=test(L,i); 
        if(j==0) break;
        } 
 
        gcvt(posit[2],6,buf);
        strcpy(xx,buf);
        xx[6]='\0'; 
        for(i=5;i>0;i--) 
        { 
        j=test(xx,i); 
        if(j==0) break;
        } 
 
        gcvt(posit[3],6,buf);
        strcpy(yy,buf);
        yy[6]='\0'; 
        for(i=5;i>0;i--) 
        { 
        j=test(yy,i); 
        if(j==0) break;
        } 
 
        gcvt(posit[6],4,buf);
        strcpy(frame,buf);
	}

	test(s,i)
	char *s;
	int i;
	{
        if(s[i]>'9'||s[i]<'0') 
                {
		  s[i]=' ';return 1;
                } 
        return 0; 
	}


/**** CSub.3.  *****/
/****************************************************************************/
/**     Subroutine of Reading Data for Drawing Gray-Maps From the Observed  */
/**  Original Data-files.                                                   */
/**                                             ( Modify By WangTongjiang ) */
/**                                                    -- July 5,1993 --    */
/**						--1996, Jan.	  	    */
/****************************************************************************/
#include  <stdio.h>
	r_map(fil,it)
        float it[512][512];
        char fil[12];
	{
	int i,j;
	FILE *p;
        unsigned char  img[512][512];
        unsigned char  fld[512][1024];
	char file[12];
        short r,a,b,xx;
        long poil=524290l;

	strcpy(file,fil);
	file[12]='\0';
	p=fopen(file,"rb");
	if(p==0){
	  printf("Can't open file! --- Bad File Name! or File Not Exist!\n");
	  printf("File name in <Readcc.c>---> [%s]\n",file);
	  return;
	}

	p=fopen(file,"rb");

	switch(file[0])
	{
        case 'l':
        case 'q':
        case 'u':
        case 'v':

                    for(j=0;j<512;j++)
                        fread(fld[j],sizeof(char),1024,p);
                    for(j=0;j<512;j++)
                        for(i=0;i<512;i++)
                        {
                        a=fld[j][2*i];b=fld[j][2*i+1];
                        b=b<<8;xx=a|b;
                        xx=xx/10+128;
                        if(xx<0) xx=0;
                        if(xx>255) xx=255;
                        img[j][i]=xx;
                        }
                    break;
        default:
                    for(j=0;j<512;j++)
                        fread(img[j],sizeof(char),512,p);
	}

	for(i=0;i<512;i++)
	for(j=0;j<512;j++)
		it[i][j]=255.-(float)img[i][j];
	}

/**** CSub.4.  ****/
/*****************************************************************************/
/**    Subroutine of Reading or Writing Data in Standard Fits Format with   **/
/**  A Header of the 2880L Bytes.                                           **/
/**     < Watching Careful !> the Array  FF(170,128) in FORTRAN Program     **/
/**  Corresponding to the Array ff[128][170] in C Program .                 **/
/**                                            ( Wang Tong Jiang )          **/
/**                                                -- July 5, 1993 --       **/ 
/**				                   -- Jan 25, 1995          **/
/*****************************************************************************/ 
#include <stdio.h>
rw_fit(name,ff,meg,rg)
char name[13],*rg;
float ff[128][170];
char *meg;
{
static char str[36][80]={"SIMPLE  =                    T  /",
                         "BITPIX  =                   16  /",
                         "NAXIS   =                    2  /",   
                         "NAXIS1  =                  170  /",
                         "NAXIS2  =                  128  /",
                         "BSCALE  =      1.0000000000E00  /",
                         "BZERO   =         0.00000000E0  /",
                         "IRAFNAME= 'pix      '           /",
                         "IRAF-MAX=               1.0E04  /", 
                         "IRAF-MIN=              -1.0E04  /", 
                         "IRAF-B/P=                   16  /",  
                         "IRAFTYPE= 'SHORT  '             /", 
                         "OBSERV  = ' HUAIROU BEIJING ASTRO. OBS.'/ ",   
                         "CONTENTS= '                     '/ ",       
                         "TIME    = '                     '/time of obs ",
                         "DATE    = '                     '/ ",
                         "SUNDISK = '                     '/ ",
                         "ORTHOGO = '                     '/ ",
                         "CARRING = '                     '/ ",
                         "FRAME   = '                     '/ ",
                         "WAVELENG= '                     '/ ",
                         "SEEING  = '                     '/ ", 
                         "FILENAME= '                     '/ ",    
                         "END                              "};
                       

FILE *fpr,*fpw;
FILE *hpr;
int i,j,len,n;
char spc[81],file[13],mesg[400],rag[2];
short xx,zz;

strcpy(file,name);
strcpy(mesg,meg);
strcpy(rag,rg);
  file[12]='\0';
  printf("file=%s\n",file);
printf("file name--->%s",file);
 n=strlen(file);
for(i=0;i<n;i++){
   if((file[i]==' ')||(file[i]<48)||(file[i]>122)||(file[i]>57&&file[i]<65)
        ||(file[i]>90&&file[i]<97)){ 
     printf("n=%d  i=%d\n",n,i+1);
     if(file[i]!='.'&&file[i]!='_'&&file[i]!='-'){
        file[i]=0;
        break;
      }
   }
}

switch(rag[0]){
  case 'w':
    fpw=fopen(file,"wb");
    if(fpw==0){
       printf("Can' Open File! --- Bad File Name! or File Not Exist!\n");
       printf("File name in <Rw_fit.c> --> [%s]\n",file);
       return;
    }

   printf("%s\n",mesg);

    for(i=0;i<24;i++){
       len=strlen(str[i]);
        space(spc,80-len);
        fprintf(fpw,"%s%s",str[i],spc);
        printf("i=%d len=%d str=%s \n",i+1,len,str[i]);
    }
      for(i=0;i<12;i++){
	space(spc,80);
	fprintf(fpw,"%80s",spc);
      }
    
    for(i=0;i<128;i++){
      for(j=0;j<170;j++){
	xx=(short)ff[i][j];
        put2b(&xx,fpw);
      }
     }
    fwrite(mesg,sizeof(char),400,fpw);
    fclose(fpw);
    printf("***** write data OK!****\n file name==> %s\n\n",file);
    break;

  case 'r':
    fpr=fopen(file,"rb");
    if(fpr==0){
       printf("Can' Open File! --- Bad File Name! or File Not Exist!\n");
       printf("File name in <Rw_fit.c> ---> [%s]\n",file);
       return;
    }

    fseek(fpr,2880L,0);
    for(i=0;i<128;i++){
       for(j=0;j<170;j++){
	  get2b(&zz,fpr);
	  ff[i][j]=(float)zz;
       }
    }
    fread(meg,sizeof(char),400,fpr);
    printf("%s\n",meg);
    fclose(fpr);
    printf("**** read data OK!***\n file name==> %s\n\n",file);
    break;
 }
}

space(sp,ln)
char *sp;
int ln;
{
int i;
for(i=0;i<ln;i++) sp[i]=' ';
sp[i]='\0';
}

 put2b(p,fpw)
        char *p;
        FILE *fpw;
        {
        char hib,lob;
        hib=p[0];
        lob=p[1];
        fputc(hib,fpw);
        fputc(lob,fpw);
/*	printf("hib=%d lob=%d\n",hib,lob);*/
        }

get2b(s,fpr)
FILE *fpr;
char *s; 
{
s[0]=fgetc(fpr);
s[1]=fgetc(fpr);
/*printf("s[0]=%d s[1]=%d\n",s[0],s[1]);*/
}


/***  CSub.5. ****/
/**************** SAVE THE MONO IMAGE DATA(512,512) WITH HUAI ROU FORMAT
************  WITHOUT TAILS  .  JUNE 29,1995    WANG TONG JIANG ****/
#include <stdio.h>
#define SEEK_SET        0l

	s_data(filg,it)
        float it[512][512];
        char  filg[13];
	{
	int i,j,n;
        FILE *p;
        unsigned char  img[512][512];
        short r,a,b,xx;
	char file[13];

  filg[12]='\0';
strcpy(file,filg);
  printf("file=%s\n",file);
printf("file name--->%s",file);
 n=strlen(file);
for(i=0;i<n;i++){
   if((file[i]==' ')||(file[i]<48)||(file[i]>122)||(file[i]>57&&file[i]<65)
        ||(file[i]>90&&file[i]<97)){ 
     printf("n=%d  i=%d\n",n,i+1);
     if(file[i]!='.'&&file[i]!='_'&&file[i]!='-'){
        file[i]=0;
        break;
      }
   }
}
	p=fopen(file,"wb");
	if(p==0){
	  printf("Can't open file\n --- Bad File Name! or File Not Exist!\n");
	  printf("File name in <Read_data.c>--->[%s]\n",file);
	  return;
	}
	for(i=0;i<512;i++){
	   for(j=0;j<512;j++){
		img[i][j]=255-(char)it[i][j];
           }
	}
	for(j=0;j<512;j++)
	   fwrite(img[j],sizeof(char),512,p);
      fclose(p);
      }


/**** CSub. 6.  *****/
/*** read the 2D-ha image or velocity image from Nanjing Solar Tower ***/
#include <stdio.h>
void rspecmap(zz)
float zz[40][208];
{
  int i,j,i0=40,j0=208;
  char str[20],name[20];
  FILE *rfp;
  printf("-----input Spectrum Map File Name===>\n");
  scanf("%s",name);
  getchar();
  rfp=fopen(name,"r");
  printf("%s\n",name);
  if(rfp==NULL){
    printf("Bad File name  or File not Exist!\n");
    return;
  }
  for(i=i0-1;i>=0;i--)
  for(j=0;j<j0;j++){
   fscanf(rfp,"%s",str);
   zz[i][j]=(float)atoi(str); 
  }
}


/**** CSub.7.  read a string from the keyboard ****/
#include <stdio.h>
get_str(mesg,length)
char *mesg;
int *length;
{
int k;
char asc;

  k=0;
  do{
     asc=getc(stdin);
     mesg[k++]=asc;
  }while(asc!=10);
  mesg[k-1]=0;
  length[0]=k-1;
  printf("Input mesg===>%s  length==>%d\n",mesg,k-1);
}


/**** Csub.8.  execute a shell command ***/
#include<stdio.h>
exel()
{
  char cmd[100];
  
  sprintf(cmd,"more $dcphlp/Readme\0");
  system(cmd);
}
