
/*
SIS0SS  This program takes the sorted output from SORTCODE and distributes
the file names to separate parameter files to be used by fmerge to
recombine the FITS files.

Input: stdin = sorted output from SORTCODE
Output: stderr = Error messages for shell job job log
       Files with lists of FITS files to be fmerged
        s000101h.prelist
        ||--------------- instrument (s0/s1/g2/g3)
          |||------------ group sequence number (001-999)
             ||---------- frfread mode number
               |--------- bit-rate (h,m,l)
                ||||||||- file id
        Note that a file is only created if data exists to be merged.

*/

#include <stdio.h>
#include <string.h>

#include "sis0sortcode.h"

main () {


    char last_sortcode[256];
    char output_filename[60];
    char split_seq[4];
    char *ptr;
    int open_file=1;
    FILE *fp;
    int merge_cnt=0;
    int filename_cnt=0;
    int file_cnt=0;
    int file_seq=0;
    int total_events=0;
    int nevents=0;

    fprintf (stderr,"SIS0SS:LO:Start program\n");
    fflush(stderr);
    strcpy(last_sortcode,"99999");
/* loop over the stdin input */
    while (gets(&sortcode.instrument) != NULL) {
	filename_cnt++; /* count the total number of input file names */
        sortcode.file_time[0]='\0';   
        if(strncmp(last_sortcode,&sortcode.instrument,2)!=0) {
	   file_seq=0;
	   }
	if (strcmp(last_sortcode,&sortcode.instrument)!=0) {
	   strcpy(last_sortcode,&sortcode.instrument); /* reset new break code */
	   if(open_file==0) { /* if a split file was open already */
	     fclose(fp);
	     open_file=1;
	     fprintf(stderr,
                     "SIS0SS:LO:%s merge count = %d photon cnt = %d\n",
                     output_filename,merge_cnt,total_events);
             fflush(stderr);
             fprintf(stdout,"%09d %s \'%-170.170s\'\n",
		total_events,output_filename,&sortcode.instrument);
	     file_cnt++; /* keep track of total files written */
	     merge_cnt=0; /* reset on new output file */
             total_events=0;
	     }
	   /* 1st part of output file name based on instrument */
	   if (sortcode.instrument=='0')
	      strcpy(output_filename,"s0");
	   else if (sortcode.instrument=='1')
	      strcpy(output_filename,"s1");
	   else {
	      fprintf(stderr,"SIS0SS:E3:Invalid instrument code\n");
	      fprintf(stderr,"SIS0SS:E3:%s\n",sortcode);
              fflush(stderr);
	      return 3 ;
	      }
           /* add seq number for now */
	   file_seq++;
	   sprintf(split_seq,"%03d", file_seq);
           strcat(output_filename,split_seq);
	   /* 3rd part of output file name based on data mode */
           if (strncmp(sortcode.mode_code,"01",2)==0) 
                strcat(output_filename,"01");
           else if (strncmp(sortcode.mode_code,"02",2)==0)
                strcat(output_filename,"02");
           else if (strncmp(sortcode.mode_code,"03",2)==0)
                strcat(output_filename,"03");
           else if (strncmp(sortcode.mode_code,"04",2)==0)
                strcat(output_filename,"04");
           else if (strncmp(sortcode.mode_code,"05",2)==0)
                strcat(output_filename,"05");
           else if (strncmp(sortcode.mode_code,"06",2)==0)
                strcat(output_filename,"06");
           else if (strncmp(sortcode.mode_code,"07",2)==0)
                strcat(output_filename,"07");
           else if (strncmp(sortcode.mode_code,"12",2)==0)
                strcat(output_filename,"12");
           else if (strncmp(sortcode.mode_code,"15",2)==0)
                strcat(output_filename,"15");
	   else if (strncmp(sortcode.mode_code,"99",2)==0)
		strcat(output_filename,"99");
	   else if (strncmp(sortcode.mode_code,"00",2)==0)
		strcat(output_filename,"00");
	   else {
		fprintf(stderr,"SIS0SS:E3:Invalid mode code\n");
		fprintf(stderr,"SIS0SS:E3:%s\n",sortcode);
                fflush(stderr);
		return 3 ;
		}
           /* 3rd part is bit rate */
           if (sortcode.bit_rate!='X') {
		fprintf(stderr,"SIS0SS:E3:Invalid bit rate found\n");
		fprintf(stderr,"SIS0SS:E3:%s\n",sortcode);
                fflush(stderr);
                return 3 ;
                }
	   /* dummy extention to make it easy to ls *.list */
	   strcat(output_filename,".prelist");
	   fp = fopen(output_filename,"w");
	   if (fp==NULL) {
	      fprintf(stderr,"SIS0SS:E3:Unable to open output file\n");
	      fprintf(stderr,"SIS0SS:E3:%s\n",output_filename);
              fflush(stderr);
	      return 3 ;
	      }
	   open_file=0; /* mark file as now open */
	   fprintf(fp,"%s\n",sortcode.sort_filename); /* write 1st merge file name */
	   merge_cnt++;
           sscanf(sortcode.events,"%d",&nevents);
           total_events=total_events+nevents;
           }
        else {
	   fprintf (fp,"%s\n",sortcode.sort_filename); /* write Nth merge file name */
	   merge_cnt++;
           sscanf(sortcode.events,"%d",&nevents);
           total_events=total_events+nevents;
	   }
    }

   if(open_file==0) { /* if a split file was open already */
      fclose(fp);
      open_file=1;
      fprintf(stderr,
                     "SIS0SS:LO:%s merge count = %d photon cnt = %d\n",
                     output_filename,merge_cnt,total_events);
      fflush(stderr);
      fprintf(stdout,"%09d %s \'%-170.170s\'\n",
	total_events,output_filename,&sortcode.instrument);
      file_cnt++; /* keep track of total files written */
      merge_cnt=0; /* reset on new output file */
      total_events=0;
     }

    fprintf (stderr,"SIS0SS:LO:Total filenames split = %d\n",filename_cnt);
    fprintf (stderr,"SIS0SS:LO:Total split file cnt = %d\n",file_cnt);
    fprintf (stderr,"SIS0SS:LO:End program\n");
    fflush(stderr);
}
