/**********************                                                       
* standard C includes *                                                       
**********************/                                                       
#include <stdio.h>                                                            
#include <string.h>                                                           
#include <stdlib.h>                                                           
#include <ctype.h>                                                            
#include <time.h>                                                             
#include <math.h>                                                             
#ifdef unix                                                                   
#include <unistd.h>                                                           
#include </sys/sys/stat.h>                                                    
#include </sys/sys/fcntlcom.h>                                                
#endif                                                                        
                                                                              
#define SEEK_SET 0                                                            
#define SEEK_CUR 1                                                            
                                                                              
/**************************                                                   
* subroutine declarations *                                                   
**************************/                                                   
void check_host();                                                            
void rpdslabs();                                                              
void grdmax();                                                                
void order();                                                                 
void status();                                                                
void readimg();                                                               
void writeimg();                                                              
void wpdslabs();                                                              
long int swap_long();                                                         
void projec();                                                                
                                                                              
/*******************                                                          
* global variables *                                                          
*******************/                                                          
unsigned char *inbuf;                                                         
unsigned char *oubuf;                                                         
unsigned char *inarray;                                                       
unsigned char *ouarray;                                                       
unsigned char *iloc;                                                          
unsigned char *oloc;                                                          
char *ipdslab;                                                                
char *opdslab;                                                                
char inname[80];                                                              
char outname[80];                                                             
char prtname[80];                                                             
FILE *printptr;                                                               
#ifdef unix                                                                   
long int outfile;                                                             
long int infile;                                                              
#else                                                                         
FILE *outfile;                                                                
FILE *infile;                                                                 
#endif                                                                        
short int host,init=0;                                                        
long int hstrec;                                                              
char *ascval="";                                                              
                                                                              
struct {                                                                      
	long int rec_bytes[2];                                                       
	long int file_rec[2];                                                        
	long int lab_rec[2];                                                         
        long int hstptr[2];                                                   
	long int imgptr[2];                                                          
	char img_id[9];                                                              
	long int lines[2];                                                           
	long int samps[2];                                                           
	long int samp_bit[2];                                                        
	long int chksum;                                                             
	long int hist[256];                                                          
	char map_proj[2][5];                                                         
	float map_scale[2];                                                          
	float max_lat[2];                                                            
	float min_lat[2];                                                            
	float east_lon[2];                                                           
	float west_lon[2];                                                           
	float line_poff[2];                                                          
	float samp_poff[2];                                                          
	float ctr_lat[2];                                                            
	float ctr_lon[2];                                                            
} k;                                                                          
                                                                              
main(argc,argv)                                                               
long int argc;                                                                
char **argv;                                                                  
{                                                                             
   short int inum=3,mosflg;                                                   
   long int i,j,inull,il,ilo,isli,issi,insi,inli,inl;                         
   long int icnt,islm=0,issm=0,nlm,nsm,nlines,ns;                             
   float xmini,ymini,xmino,ymino;                                             
   float x,y,newlat,newlon,newlin,newsmp,d1;                                  
   char top[2],ans[2],mosall[2];                                              
   char *version="27-JUL-94";                                                 
   unsigned char blank=32;                                                    
   time_t lt;                                                                 
   struct tm *ptr;                                                            
                                                                              
/*****************************************************************            
* if the user has not input parameters through the command line, *            
* then give an example of the command line format and exit       *            
*****************************************************************/            
   if (argc <= 2) {                                                           
     printf("\nMOSAIC Image Processing Program. Command line format:\n\n");   
     printf("MOSAIC from to -p sl -q ss -r inl -s ins -b -a -n null ");       
     printf("-h mnlat -i mxlat\n       -j mnlon -k mxlon -c mnl -d mns ");    
     printf("-t lat -u line -v lon -w samp -f fprt\n\n");                     
     printf("from     - Name of the input image file.\n\n");                  
     printf("to       - Name of the output mosaic file.\n\n");                
     printf("-p sl    - Starting line of the input image file subarea to ");  
     printf("be transferred\n           to the output mosaic file (the ");    
     printf("default starting line is 1).\n\n");                              
     printf("-q ss    - Starting sample of the input image file subarea ");   
     printf("to be transferred\n           to the output mosaic file ");      
     printf("(the default starting sample is 1).\n\n");                       
     printf("-r inl   - Number of lines of the input image file to be ");     
     printf("transferred to the\n           output mosaic file (the ");       
     printf("default is all lines).\n\n");                                    
     printf("-s ins   - Number of samples of the input image file to be ");   
     printf("transferred to the\n           output mosaic file (the ");       
     printf("default is all samples).\n\n\n\n");                              
     printf("Press Return for more: ");                                       
     gets(ascval);                                                            
     printf("-b       - Specifies that the input image file is to be ");      
     printf("mosaicked on the\n           bottom of the output mosaic ");     
     printf("file (the default is to mosaic the\n           input file ");    
     printf("on top of the mosaic file).\n\n");                               
     printf("-a       - Specifies that all data (including null data) of ");  
     printf("the input image\n           file is to be mosaicked on the ");   
     printf("output mosaic file (the default is\n           to mosaic ");     
     printf("only non-null data).\n\n");                                      
     printf("-n null  - This is the null density value of the input ");       
     printf("image file. Pixels in\n           the input file which have ");  
     printf("this density value will not be\n           transferred to ");    
     printf("the output mosaic file unless the -a option is\n           ");   
     printf("specified (the default null value is 0).\n\n");                  
     printf("-h mnlat - This option sets the minimum latitude extent of ");   
     printf("the output mosaic\n           file. If the output mosaic ");     
     printf("file exists prior to execution of this\n           program, ");  
     printf("this option will be ignored.\n\n");                              
     printf("-i mxlat - This option sets the maximum latitude extent of ");   
     printf("the output mosaic\n           file. If the output mosaic ");     
     printf("file exists prior to execution of this\n           program, ");  
     printf("this option will be ignored.\n\n\n\n");                          
     printf("Press Return for more: ");                                       
     gets(ascval);                                                            
     printf("-j mnlon - This option sets the minimum longitude extent of ");  
     printf("the output mosaic\n           file. If the output mosaic ");     
     printf("file exists prior to execution of this\n           program, ");  
     printf("this option will be ignored.\n\n");                              
     printf("-k mxlon - This option sets the maximum longitude extent of ");  
     printf("the output mosaic\n           file. If the output mosaic ");     
     printf("file exists prior to execution of this\n           program, ");  
     printf("this option will be ignored.\n\n");                              
     printf("-c mnl   - This option sets the number of lines of the ");       
     printf("output mosaic file.\n           The program will ");             
     printf("automatically determine the number of lines ");                  
     printf("in\n           the mosaic file if you specify a minimum ");      
     printf("and maximum latitude\n           extent. This option will ");    
     printf("override the computed number of lines. If\n           the ");    
     printf("output mosaic file exists prior to execution of this ");         
     printf("program,\n           this option will be ignored.\n\n");         
     printf("-d mns   - This option sets the number of samples of the ");     
     printf("output mosaic file.\n           The program will ");             
     printf("automatically determine the number of samples ");                
     printf("in\n           the mosaic file if you specify a minimum ");      
     printf("and maximum longitude\n           extent. This option ");        
     printf("will override the computed number of samples.\n           ");    
     printf("If the output mosaic file exists prior to execution of ");       
     printf("this\n           program, this option will be ignored.\n\n\n");  
     printf("Press Return for more: ");                                       
     gets(ascval);                                                            
     printf("-t lat   - Use this option to change the projection ");          
     printf("translation of the output\n           mosaic file. The ");       
     printf("line specified by the -u option will be mapped ");               
     printf("to\n           this latitude. If you use the -t option ");       
     printf("without the -u option,\n           then the default is to ");    
     printf("map line 1 to the latitude specified. If\n           the ");     
     printf("output mosaic file exists prior to execution of this ");         
     printf("program,\n           this option will be ignored.\n\n");         
     printf("-u line  - Use this option to change the projection ");          
     printf("translation of the output\n           mosaic file. The ");       
     printf("latitude specified by the -t option will be ");                  
     printf("mapped\n           to this line. If you use the -u option ");    
     printf("without the -t option, then\n           the default is to ");    
     printf("map the northernmost latitude of the image to ");                
     printf("the\n           line specified. If the output mosaic file ");    
     printf("exists prior to\n           execution of this program, this ");  
     printf("option will be ignored.\n\n");                                   
     printf("-v lon   - Use this option to change the projection ");          
     printf("translation of the output\n           mosaic file. The ");       
     printf("sample specified by the -w option will be ");                    
     printf("mapped\n           to this longitude. If you use the -v ");      
     printf("option without the -w option,\n           then the default ");   
     printf("is to map sample 1 to the longitude specified. If\n");           
     printf("           the output mosaic file exists prior to execution ");  
     printf("of this program,\n           this option will be ");             
     printf("ignored.\n\n\n\n");                                              
     printf("Press Return for more: ");                                       
     gets(ascval);                                                            
     printf("-w samp  - Use this option to change the projection ");          
     printf("translation of the output\n           mosaic file. The ");       
     printf("longitude specified by the -v option will ");                    
     printf("be\n           mapped to this sample. If you use the -w ");      
     printf("option without the -v\n           option, then the default ");   
     printf("is to map the westernmost longitude of\n           the ");       
     printf("image to the sample specified. If the output mosaic file ");     
     printf("exists\n           prior to execution of this program, this ");  
     printf("option will be ignored.\n\n");                                   
     printf("-f prtf  - The location and name of the output log file. ");     
     printf("This file is used to\n           store information relevant ");  
     printf("to the input and output images. If you\n           wish to ");   
     printf("run the software directly from the CD, you must specify ");      
     printf("a\n           location other than the CD for the location ");    
     printf("of this file (the\n           default is to create a file ");    
     printf("called print.prt in the same directory\n           as the ");    
     printf("software).\n\n");                                                
     printf("NOTE:  The input file and output mosaic file need to have ");    
     printf("the same scale,\n       center longitude, and map ");            
     printf("projection. If the output mosaic file does\n       not ");       
     printf("exist prior to execution of this program, then these ");         
     printf("mapping\n       parameters are set to the same values as ");     
     printf("are in the input file labels.\n\n\n\n\n\n");                     
     exit(0);                                                                 
   }                                                                          
                                                                              
/***********************************************************************      
* change the name and location of the output log file if user requests *      
***********************************************************************/      
   strcpy(inname,argv[1]);                                                    
   strcpy(outname,argv[2]);                                                   
   strcpy(prtname,"print.prt");                                               
                                                                              
   while (inum < argc) {                                                      
     if (strcmp(argv[inum],"-f") == 0) {                                      
       if (inum < argc)                                                       
         strcpy(prtname,argv[++inum]);                                        
     }                                                                        
     inum++;                                                                  
   }                                                                          
                                                                              
/***************************                                                  
* open the output log file *                                                  
***************************/                                                  
   lt = time(NULL);                                                           
   ptr = localtime(&lt);                                                      
   if ((printptr = fopen(prtname,"a")) == NULL) {                             
     printf("*** ERROR *** Can't open the output log file: %s\n",prtname);    
     exit(0);                                                                 
   }                                                                          
   else {                                                                     
     fprintf(printptr,"\n*** MOSAIC ***  Version: %s  Date: %s\n",            
             version,asctime(ptr));                                           
     printf("\n*** MOSAIC ***  Version: %s  Date: %s\n",version,              
            asctime(ptr));                                                    
     fprintf(printptr,"Input image file: %s\n",inname);                       
     printf("Input image file: %s\n",inname);                                 
   }                                                                          
                                                                              
/************************************************************                 
* determine what kind of computer the program is running on *                 
************************************************************/                 
   check_host();                                                              
                                                                              
/***********************************************************                  
* read the pds label information from the input image file *                  
***********************************************************/                  
#ifdef unix                                                                   
   if ((infile = open(inname,O_RDONLY)) <= 0) {                               
#else                                                                         
   if ((infile = fopen(inname,"rb")) == 0) {                                  
#endif                                                                        
     printf("*** ERROR *** Can't open the input image file: %s\n",inname);    
     exit(0);                                                                 
   }                                                                          
   fprintf(printptr,"\n** INPUT IMAGE FILE PARAMETERS **\n\n");               
   printf("\n** INPUT IMAGE FILE PARAMETERS **\n\n");                         
   rpdslabs(infile,0);                                                        
                                                                              
/******************************                                               
* open the output mosaic file *                                               
******************************/                                               
#ifdef unix                                                                   
   if (access(outname,0) == 0) {                                              
     if ((i = chmod(outname,S_IREAD | S_IWRITE)) != 0) {                      
       printf("*** ERROR *** Unable to open output image file for ");         
       printf("read/write access\n");                                         
       exit(0);                                                               
     }                                                                        
   }                                                                          
   if ((outfile = open(outname,O_RDWR)) <= 0) init = 1;                       
#else                                                                         
   if ((outfile = fopen(outname,"rb+")) == NULL) init = 1;                    
#endif                                                                        
                                                                              
/*******************************************                                  
* read in remaining command line arguments *                                  
*******************************************/                                  
   inum = 3;                                                                  
   isli = 1;                                                                  
   issi = 1;                                                                  
   inli = k.lines[0];                                                         
   insi = k.samps[0];                                                         
   strcpy(top,"Y");                                                           
   strcpy(mosall,"N");                                                        
   inull = 0;                                                                 
   if (init == 1) {                                                           
     k.min_lat[1] = k.min_lat[0];                                             
     k.max_lat[1] = k.max_lat[0];                                             
     k.west_lon[1] = k.west_lon[0];                                           
     k.east_lon[1] = k.east_lon[0];                                           
     nlm = 0;                                                                 
     nsm = 0;                                                                 
     strcpy(ans,"N");                                                         
     newlat = k.max_lat[0];                                                   
     newlon = k.west_lon[0];                                                  
     newlin = 1.;                                                             
     newsmp = 1.;                                                             
   }                                                                          
                                                                              
   while (inum < argc) {                                                      
     if (strcmp(argv[inum],"-p") == 0) {                                      
       if (inum < argc)                                                       
         isli = atoi(argv[++inum]);                                           
     }                                                                        
     else if (strcmp(argv[inum],"-q") == 0) {                                 
       if (inum < argc)                                                       
         issi = atoi(argv[++inum]);                                           
     }                                                                        
     else if (strcmp(argv[inum],"-r") == 0) {                                 
       if (inum < argc)                                                       
         inli = atoi(argv[++inum]);                                           
     }                                                                        
     else if (strcmp(argv[inum],"-s") == 0) {                                 
       if (inum < argc)                                                       
         insi = atoi(argv[++inum]);                                           
     }                                                                        
     else if (strcmp(argv[inum],"-b") == 0)                                   
       strcpy(top,"N");                                                       
     else if (strcmp(argv[inum],"-a") == 0)                                   
       strcpy(mosall,"Y");                                                    
     else if (strcmp(argv[inum],"-n") == 0) {                                 
       if (inum < argc) {                                                     
         inull = atoi(argv[++inum]);                                          
         if (inull < 0 || inull > 255) {                                      
           printf("*** ERROR *** Invalid null value specified on command ");  
           printf("line.\n              The null value must fall in the ");   
           printf("range 0 to 255.\n");                                       
           exit(0);                                                           
         }                                                                    
       }                                                                      
     }                                                                        
     else if (init == 1) {                                                    
       if (strcmp(argv[inum],"-h") == 0) {                                    
         if (inum < argc) {                                                   
           k.min_lat[1] = atof(argv[++inum]);                                 
           if (k.min_lat[1] < -90. || k.min_lat[1] > 90.) {                   
             printf("*** ERROR *** Invalid minimum latitude specified on ");  
             printf("command line.\n              The minimum latitude ");    
             printf("must fall in the range -90 to 90.\n");                   
             exit(0);                                                         
           }                                                                  
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-i") == 0) {                               
         if (inum < argc) {                                                   
           k.max_lat[1] = atof(argv[++inum]);                                 
           if (k.max_lat[1] < -90. || k.max_lat[1] > 90.) {                   
             printf("*** ERROR *** Invalid maximum latitude specified on ");  
             printf("command line.\n              The maximum latitude ");    
             printf("must fall in the range -90 to 90.\n");                   
             exit(0);                                                         
           }                                                                  
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-j") == 0) {                               
         if (inum < argc) {                                                   
           k.west_lon[1] = atof(argv[++inum]);                                
           while (k.west_lon[1] < 0.)                                         
             k.west_lon[1]+=360.;                                             
           k.west_lon[1] = fmod(k.west_lon[1],360.);                          
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-k") == 0) {                               
         if (inum < argc) {                                                   
           k.east_lon[1] = atof(argv[++inum]);                                
           while (k.east_lon[1] < 0.)                                         
             k.east_lon[1]+=360.;                                             
           k.east_lon[1] = fmod(k.east_lon[1],360.);                          
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-c") == 0) {                               
         if (inum < argc) {                                                   
           nlm = atoi(argv[++inum]);                                          
           if (nlm <= 0) {                                                    
             printf("*** ERROR *** Invalid mosaic line size specified on ");  
             printf("command line.\n              The line size must be ");   
             printf("greater than 0.\n");                                     
             exit(0);                                                         
           }                                                                  
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-d") == 0) {                               
         if (inum < argc) {                                                   
           nsm = atoi(argv[++inum]);                                          
           if (nsm <= 0) {                                                    
             printf("*** ERROR *** Invalid mosaic sample size specified on ");
             printf("command line.\n              The sample size must be "); 
             printf("greater than 0.\n");                                     
             exit(0);                                                         
           }                                                                  
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-t") == 0) {                               
         if (inum < argc) {                                                   
           strcpy(ans,"Y");                                                   
           newlat = atof(argv[++inum]);                                       
           if (newlat < -90. || newlat > 90.) {                               
             printf("*** ERROR *** Invalid projection translation ");         
             printf("latitude specified on command\n              line. ");   
             printf("The latitude must fall in the range -90 to 90.\n");      
             exit(0);                                                         
           }                                                                  
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-v") == 0) {                               
         if (inum < argc) {                                                   
           strcpy(ans,"Y");                                                   
           newlon = atof(argv[++inum]);                                       
           while (newlon < 0.)                                                
             newlon+=360.;                                                    
           newlon = fmod(newlon,360.);                                        
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-u") == 0) {                               
         if (inum < argc) {                                                   
           strcpy(ans,"Y");                                                   
           newlin = atof(argv[++inum]);                                       
         }                                                                    
       }                                                                      
       else if (strcmp(argv[inum],"-w") == 0) {                               
         if (inum < argc) {                                                   
           strcpy(ans,"Y");                                                   
           newsmp = atof(argv[++inum]);                                       
         }                                                                    
       }                                                                      
     }                                                                        
     inum++;                                                                  
   }                                                                          
                                                                              
   order(&k.min_lat[1],&k.max_lat[1]);                                        
                                                                              
/*******************************************************************          
* if the mosaic file already exists, read in the label information *          
*******************************************************************/          
   xmini = k.line_poff[0] + 1.;                                               
   ymini = k.samp_poff[0] + 1.;                                               
                                                                              
   if (init == 0) {                                                           
     fprintf(printptr,"\n** OUTPUT IMAGE FILE PARAMETERS **\n\n");            
     printf("\n** OUTPUT IMAGE FILE PARAMETERS **\n\n");                      
     rpdslabs(outfile,1);                                                     
                                                                              
/*************************************************************                
* make sure the mapping parameter values agree between files *                
*************************************************************/                
     if (strncmp(k.map_proj[0],k.map_proj[1],4) != 0) {                       
       printf("\n*** ERROR *** Files must be of the same map ");              
       printf("projection before they can be mosaicked");                     
       exit(0);                                                               
     }                                                                        
     i = k.map_scale[0]/k.map_scale[1] + .5;                                  
     if (i != 1) {                                                            
       printf("\n*** ERROR *** Files must have the same map ");               
       printf("scale before they can be mosaicked");                          
       exit(0);                                                               
     }                                                                        
     if (strncmp(k.map_proj[0],"SINU",4) == 0) {                              
       if (k.ctr_lon[0] != k.ctr_lon[1]) {                                    
         printf("\n*** ERROR *** Sinusoidal files must have the ");           
         printf("same center longitude before they can be mosaicked");        
         exit(0);                                                             
       }                                                                      
     }                                                                        
     if (strncmp(k.map_proj[0],"SIMP",4) == 0) {                              
       if (k.ctr_lat[0] != k.ctr_lat[1]) {                                    
         printf("\n*** ERROR *** Simple Cylindrical files must have the ");   
         printf("same center latitude before they can be mosaicked");         
         exit(0);                                                             
       }                                                                      
     }                                                                        
     xmino = k.line_poff[1] + 1.;                                             
     ymino = k.samp_poff[1] + 1.;                                             
     if (islm == 0) islm = xmini - xmino + 1;                                 
     if (issm == 0) issm = ymini - ymino + 1;                                 
   }                                                                          
                                                                              
/******************************************************************           
* if the mosaic file does not exist, initialize label information *           
******************************************************************/           
   else {                                                                     
                                                                              
/***************************************************                          
* find rectangular boundaries of output projection *                          
***************************************************/                          
     grdmax();                                                                
     if (strncmp(ans,"Y",1) == 0) {                                           
       projec(&newlat,&newlon,&x,&y,&d1);                                     
       k.line_poff[1] = x - newlin + 1;                                       
       k.samp_poff[1] = y - newsmp + 1;                                       
     }                                                                        
     xmino = k.line_poff[1];                                                  
     ymino = k.samp_poff[1];                                                  
     if (nlm == 0) nlm = k.lines[1];                                          
     if (nsm == 0) nsm = k.samps[1];                                          
     if (nlm != 0) k.lines[1] = nlm;                                          
     if (nsm != 0) k.samps[1] = nsm;                                          
     if (islm == 0) islm = xmini - xmino + 1;                                 
     if (issm == 0) issm = ymini - ymino + 1;                                 
     k.samp_bit[1] = k.samp_bit[0];                                           
     strncpy(k.map_proj[1],k.map_proj[0],4);                                  
     k.map_scale[1] = k.map_scale[0];                                         
     k.ctr_lon[1] = k.ctr_lon[0];                                             
     k.ctr_lat[1] = k.ctr_lat[0];                                             
     k.line_poff[1] = k.line_poff[1] - 1.;                                    
     k.samp_poff[1] = k.samp_poff[1] - 1.;                                    
                                                                              
/**************************************************                           
* record output image file parameters in log file *                           
**************************************************/                           
     strcpy(k.img_id,"FLMOSAIC");                                             
     fprintf(printptr,"\nOutput image file: %s\n",outname);                   
     printf("\nOutput image file: %s\n",outname);                             
     fprintf(printptr,"\n** OUTPUT IMAGE FILE PARAMETERS **\n\n");            
     printf("\n** OUTPUT IMAGE FILE PARAMETERS **\n\n");                      
     fprintf(printptr,"IMAGE_ID = %s\n",k.img_id);                            
     printf("IMAGE_ID = %s\n",k.img_id);                                      
     fprintf(printptr,"NUMBER OF LINES = %d\n",k.lines[1]);                   
     printf("NUMBER OF LINES = %d\n",k.lines[1]);                             
     fprintf(printptr,"NUMBER OF SAMPLES = %d\n",k.samps[1]);                 
     printf("NUMBER OF SAMPLES = %d\n",k.samps[1]);                           
     fprintf(printptr,"BIT TYPE = %d\n",k.samp_bit[1]);                       
     printf("BIT TYPE = %d\n",k.samp_bit[1]);                                 
     if (strncmp(k.map_proj[1],"SINU",4) == 0) {                              
       fprintf(printptr,"MAP_PROJECTION_TYPE = SINUSOIDAL\n");                
       printf("MAP_PROJECTION_TYPE = SINUSOIDAL\n");                          
     }                                                                        
     else {                                                                   
       fprintf(printptr,"MAP_PROJECTION_TYPE = SIMPLE CYLINDRICAL\n");        
       printf("MAP_PROJECTION_TYPE = SIMPLE CYLINDRICAL\n");                  
     }                                                                        
     fprintf(printptr,"MAP_SCALE = %f <DEG/PIX>\n",k.map_scale[1]);           
     printf("MAP_SCALE = %f <DEG/PIX>\n",k.map_scale[1]);                     
     fprintf(printptr,"MAXIMUM_LATITUDE = %f\n",k.max_lat[1]);                
     printf("MAXIMUM_LATITUDE = %f\n",k.max_lat[1]);                          
     fprintf(printptr,"MINIMUM_LATITUDE = %f\n",k.min_lat[1]);                
     printf("MINIMUM_LATITUDE = %f\n",k.min_lat[1]);                          
     fprintf(printptr,"EASTERNMOST_LONGITUDE = %f\n",k.east_lon[1]);          
     printf("EASTERNMOST_LONGITUDE = %f\n",k.east_lon[1]);                    
     fprintf(printptr,"WESTERNMOST_LONGITUDE = %f\n",k.west_lon[1]);          
     printf("WESTERNMOST_LONGITUDE = %f\n",k.west_lon[1]);                    
     fprintf(printptr,"CENTER_LATITUDE = %f\n",k.ctr_lat[1]);                 
     printf("CENTER_LATITUDE = %f\n",k.ctr_lat[1]);                           
     fprintf(printptr,"CENTER_LONGITUDE = %f\n",k.ctr_lon[1]);                
     printf("CENTER_LONGITUDE = %f\n",k.ctr_lon[1]);                          
     fprintf(printptr,"LINE_PROJECTION_OFFSET = %f\n",k.line_poff[1]);        
     printf("LINE_PROJECTION_OFFSET = %f\n",k.line_poff[1]);                  
     fprintf(printptr,"SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[1]);      
     printf("SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[1]);                
                                                                              
     i = strlen(ipdslab);                                                     
     k.lab_rec[1] = (i+k.samps[1]-1)/k.samps[1];                              
   }                                                                          
                                                                              
   fprintf(printptr,"\nInput image subarea to be transferred ");              
   printf("\nInput image subarea to be transferred ");                        
   fprintf(printptr,"(sl,ss,nl,ns): %d,%d,%d,%d\n",isli,issi,inli,insi);      
   printf("(sl,ss,nl,ns): %d,%d,%d,%d\n",isli,issi,inli,insi);                
   if (strncmp(top,"Y",1) == 0) {                                             
     fprintf(printptr,"Input image will be mosaicked on top of mosaic ");     
     fprintf(printptr,"file\n");                                              
     printf("Input image will be mosaicked on top of mosaic ");               
     printf("file\n");                                                        
   }                                                                          
   else {                                                                     
     fprintf(printptr,"Input image will not be mosaicked on top of ");        
     fprintf(printptr,"mosaic file\n");                                       
     printf("Input image will not be mosaicked on top of ");                  
     printf("mosaic file\n");                                                 
   }                                                                          
   fprintf(printptr,"Null value: %d\n",inull);                                
   printf("Null value: %d\n",inull);                                          
   if (strncmp(mosall,"Y",1) == 0) {                                          
     fprintf(printptr,"All data (including null data) will be mosaicked\n");  
     printf("All data (including null data) will be mosaicked\n");            
   }                                                                          
   else {                                                                     
     fprintf(printptr,"All data (except for null data) will be mosaicked\n"); 
     printf("All data (except for null data) will be mosaicked\n");           
   }                                                                          
                                                                              
/*************************************************                            
* initialize parameters for main processing loop *                            
*************************************************/                            
   if (islm < 1) {                                                            
     isli = -islm + 1 + isli;                                                 
     islm = 1;                                                                
   }                                                                          
   if (issm < 1) {                                                            
     issi = -issm + 1 + issi;                                                 
     issm = 1;                                                                
   }                                                                          
   if (isli < 1) {                                                            
     islm = -isli + 1 + islm;                                                 
     isli = 1;                                                                
   }                                                                          
   if (issi < 1) {                                                            
     issm = -issi + 1 + issm;                                                 
     issi = 1;                                                                
   }                                                                          
                                                                              
   if (inli == 0) inli = k.lines[0] - isli + 1;                               
   if (insi == 0) insi = k.samps[0] - issi + 1;                               
   if ((isli + inli - 1) > k.lines[0]) inli = k.lines[0] - isli + 1;          
   if ((issi + insi - 1) > k.samps[0]) insi = k.samps[0] - issi + 1;          
   nlines = k.lines[1] - islm + 1;                                            
   if (nlines > inli) nlines = inli;                                          
   ns = k.samps[1] - issm + 1;                                                
   if (ns > insi) ns = insi;                                                  
                                                                              
   if (strncmp(top,"Y",1) == 0) {                                             
     if (strncmp(mosall,"N",1) == 0) mosflg = 0;                              
     else mosflg = 1;                                                         
   }                                                                          
   else                                                                       
     mosflg = 2;                                                              
                                                                              
/***********************                                                      
* main processing loop *                                                      
***********************/                                                      
   printf(" \n");                                                             
   i = 0;                                                                     
   status(i,nlines);                                                          
                                                                              
   if (k.samps[0] > k.samps[1])                                               
     inli = 60000/k.samps[0];                                                 
   else                                                                       
     inli = 60000/k.samps[1];                                                 
                                                                              
   if ((inbuf = (unsigned char *) malloc(60416)) == NULL) {                   
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   i = inli*k.samps[0];                                                       
   if ((inarray = (unsigned char *) malloc(i)) == NULL) {                     
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
#ifdef unix                                                                   
   close(infile);                                                             
   if ((infile = open(inname,O_RDONLY)) <= 0) {                               
#else                                                                         
   fclose(infile);                                                            
   if ((infile = fopen(inname,"rb")) == NULL) {                               
#endif                                                                        
     printf("*** ERROR *** Can't open the input image file: %s\n",inname);    
     exit(0);                                                                 
   }                                                                          
#ifdef unix                                                                   
#else                                                                         
   setvbuf(infile,inbuf,_IOFBF,60416);                                        
#endif                                                                        
                                                                              
   if ((oubuf = (unsigned char *) malloc(60416)) == NULL) {                   
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   i = inli*k.samps[1];                                                       
   if ((ouarray = (unsigned char *) malloc(i)) == NULL) {                     
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   if (init == 0) {                                                           
#ifdef unix                                                                   
     close(outfile);                                                          
     if ((outfile = open(outname,O_RDWR)) < 0) {                              
#else                                                                         
     fclose(outfile);                                                         
     if ((outfile = fopen(outname,"rb+")) == NULL) {                          
#endif                                                                        
       printf("*** ERROR *** Can't open the output image file: %s\n",outname);
       exit(0);                                                               
     }                                                                        
   }                                                                          
   else {                                                                     
#ifdef unix                                                                   
     if ((outfile = open(outname,O_RDWR|O_CREAT)) < 0) {                      
#else                                                                         
     if ((outfile = fopen(outname,"wb")) == NULL) {                           
#endif                                                                        
       printf("*** ERROR *** Can't open the output image file: %s\n",         
              outname);                                                       
       exit(0);                                                               
     }                                                                        
   }                                                                          
#ifdef unix                                                                   
#else                                                                         
   setvbuf(outfile,oubuf,_IOFBF,60416);                                       
#endif                                                                        
                                                                              
   i = (k.imgptr[0]+isli-2)*k.samps[0];                                       
#ifdef unix                                                                   
   if ((j = lseek(infile,i,SEEK_SET)) != i) {                                 
#else                                                                         
   if ((j = fseek(infile,i,SEEK_SET)) != 0) {                                 
#endif                                                                        
     printf("*** ERROR *** Unable to read image data from ");                 
     printf("input image file\n");                                            
     exit(0);                                                                 
   }                                                                          
                                                                              
   hstrec = (1023 + k.samps[1])/k.samps[1];                                   
   if (init == 0) {                                                           
     ilo = 1;                                                                 
     if (islm > 1) ilo = islm;                                                
     i = (k.imgptr[1]+islm-2)*k.samps[1];                                     
#ifdef unix                                                                   
     if ((j = lseek(outfile,i,SEEK_SET)) != i) {                              
#else                                                                         
     if ((j = fseek(outfile,i,SEEK_SET)) != 0) {                              
#endif                                                                        
       printf("*** ERROR *** Unable to read image data from ");               
       printf("output image file\n");                                         
       exit(0);                                                               
     }                                                                        
   }                                                                          
   else {                                                                     
     for (j=0; j<k.samps[1]; j++)                                             
       ouarray[j] = 0;                                                        
     i = k.lab_rec[1] + hstrec;                                               
     for (icnt=0; icnt<i; icnt++) {                                           
#ifdef unix                                                                   
       if (write(outfile,ouarray,k.samps[1]) != k.samps[1]) {                 
#else                                                                         
       if (fwrite(ouarray,k.samps[1],1,outfile) != 1) {                       
#endif                                                                        
         printf("*** ERROR *** Unable to write to the output image file\n");  
         exit(0);                                                             
       }                                                                      
     }                                                                        
     ilo = 1;                                                                 
     if (islm > 1) {                                                          
       inl = 1;                                                               
       for (icnt=0; icnt<islm-1; icnt++)                                      
         writeimg(&inl);                                                      
       ilo = islm;                                                            
     }                                                                        
     else {                                                                   
#ifdef unix                                                                   
#else                                                                         
       fflush(outfile);                                                       
#endif                                                                        
     }                                                                        
   }                                                                          
                                                                              
   for (i=0; i<256; i++)                                                      
     k.hist[i] = 0;                                                           
                                                                              
   inl = inli - 1;                                                            
   inum = 0;                                                                  
   icnt = inli*k.samps[1];                                                    
   for (il=1; il<=nlines; il++) {                                             
                                                                              
/*****************************************************                        
* read a line from the input image file into inarray *                        
*****************************************************/                        
     if (inum == 0) {                                                         
       if (il > 1) writeimg(&inl);                                            
       i = nlines - il + 1;                                                   
       if (inl > i) inl = i;                                                  
       j = 0;                                                                 
       readimg(&inl,&j);                                                      
       inum = inl;                                                            
       iloc = inarray + issi - 1;                                             
       oloc = ouarray + issm - 1;                                             
       if (init == 1) {                                                       
         for (j=0; j<icnt; j++)                                               
           ouarray[j] = 0;                                                    
       }                                                                      
       else {                                                                 
         j = 1;                                                               
         readimg(&inl,&j);                                                    
       }                                                                      
     }                                                                        
     inum--;                                                                  
                                                                              
     switch (mosflg) {                                                        
       case 0:                                                                
         for (i=0; i<ns; i++) {                                               
           if (iloc[i] != inull) oloc[i] = iloc[i];                           
         }                                                                    
         break;                                                               
       case 1:                                                                
         for (i=0; i<ns; i++)                                                 
           oloc[i] = iloc[i];                                                 
         break;                                                               
       case 2:                                                                
         for (i=0; i<ns; i++) {                                               
           if (oloc[i] == inull) oloc[i] = iloc[i];                           
         }                                                                    
         break;                                                               
     }                                                                        
                                                                              
     ilo++;                                                                   
     iloc+=k.samps[0];                                                        
     oloc+=k.samps[1];                                                        
     status(il,nlines);                                                       
   }                                                                          
                                                                              
   writeimg(&inl);                                                            
   if (ilo < k.lines[1]) {                                                    
     inl = 60000/k.samps[1];                                                  
     i = k.lines[1] - ilo + 1;                                                
     if (inl > i) inl = i;                                                    
     i = inl*k.samps[1];                                                      
     for (j=0; j<i; j++)                                                      
       ouarray[j] = 0;                                                        
     while (ilo < k.lines[1]) {                                               
       writeimg(&inl);                                                        
       ilo+=inl;                                                              
       i = k.lines[1] - ilo + 1;                                              
       if (inl > i) inl = i;                                                  
     }                                                                        
   }                                                                          
                                                                              
/****************************************************                         
* free the memory taken up by the image data arrays *                         
****************************************************/                         
   free(inarray);                                                             
   free(ouarray);                                                             
                                                                              
/**************************************************************               
* update the checksum and histogram information in the labels *               
**************************************************************/               
#ifdef unix                                                                   
   if (lseek(outfile,0,SEEK_SET) != 0) {                                      
#else                                                                         
   if (fseek(outfile,0,SEEK_SET) != 0) {                                      
#endif                                                                        
     printf("*** ERROR *** Problem updating the output file labels\n");       
     exit(0);                                                                 
   }                                                                          
                                                                              
   k.chksum = 0;                                                              
   for (i=0; i<256; i++)                                                      
     k.chksum = k.chksum + k.hist[i]*i;                                       
                                                                              
   if (host == 2 || host == 4) {                                              
     for (i=0; i<256; i++)                                                    
       k.hist[i] = swap_long(k.hist[i]);                                      
   }                                                                          
                                                                              
   wpdslabs();                                                                
                                                                              
#ifdef unix                                                                   
   write(outfile,(char *)k.hist,1024);                                        
#else                                                                         
   fwrite((char *)k.hist,1024,1,outfile);                                     
#endif                                                                        
   i = hstrec*k.samps[1] - 1024;                                              
   for (icnt=0; icnt<i; icnt++)                                               
#ifdef unix                                                                   
     write(outfile,blank,1);                                                  
#else                                                                         
     fputc(blank,outfile);                                                    
#endif                                                                        
                                                                              
#ifdef unix                                                                   
#else                                                                         
   fflush(outfile);                                                           
#endif                                                                        
   free(ipdslab);                                                             
   free(opdslab);                                                             
                                                                              
#ifdef unix                                                                   
   close(infile);                                                             
   close(outfile);                                                            
#else                                                                         
   fclose(infile);                                                            
   fclose(outfile);                                                           
#endif                                                                        
   free(inbuf);                                                               
   free(oubuf);                                                               
                                                                              
   fprintf(printptr,"\n*** END OF MOSAIC ***\n");                             
   printf("\n*** END OF MOSAIC ***\n");                                       
   fclose(printptr);                                                          
}                                                                             
                                                                              
/*************************************************************************    
* check_host - determine what kind of computer the program is running on *    
*************************************************************************/    
void check_host()                                                             
{                                                                             
   int swap,bits;                                                             
   union {                                                                    
     char  ichar[2];                                                          
     short int ilen;                                                          
   } onion;                                                                   
                                                                              
   if (sizeof(swap) == 4)                                                     
     bits = 32;                                                               
   else                                                                       
     bits = 16;                                                               
                                                                              
   onion.ichar[0] = 1;                                                        
   onion.ichar[1] = 0;                                                        
                                                                              
   if (onion.ilen == 1)                                                       
     swap = 1;                                                                
   else                                                                       
     swap = 0;                                                                
                                                                              
   if (bits == 16 && swap == 1)                                               
     host = 1;                                                                
   else if (bits == 16 && swap == 0)                                          
     host = 2;                                                                
   else if (bits == 32 && swap == 1)                                          
     host = 3;                                                                
   else if (bits == 32 && swap == 0)                                          
     host = 4;                                                                
}                                                                             
                                                                              
/******************************************************                       
* rpdslabs - read in the pds labels of the image file *                       
******************************************************/                       
void rpdslabs(ifile,idx)                                                      
#ifdef unix                                                                   
long int *ifile;                                                              
#else                                                                         
FILE *ifile;                                                                  
#endif                                                                        
short int idx;                                                                
{                                                                             
   char *bufr;                                                                
   char *ptr,*ptr2;                                                           
   long int i,j;                                                              
                                                                              
   if ((bufr = malloc(800)) == NULL) {                                        
     printf("*** ERROR *** Unable to allocate memory for image labels\n");    
     exit(0);                                                                 
   }                                                                          
#ifdef unix                                                                   
   if ((i = read(ifile,bufr,800)) != 800) {                                   
#else                                                                         
   if ((i = fread(bufr,1,800,ifile)) != 800) {                                
#endif                                                                        
     printf("*** ERROR *** Problem reading the file labels\n");               
     exit(0);                                                                 
   }                                                                          
   if (strstr(bufr,"= FIXED_LENGTH") == NULL) {                               
     printf("*** ERROR *** The input file has not been decompressed\n");      
     exit(0);                                                                 
   }                                                                          
   if ((ptr = strstr(bufr,"RECORD_BYTES")) == NULL) {                         
     printf("*** ERROR *** RECORD_BYTES keyword is missing\n");               
     exit(0);                                                                 
   }                                                                          
   else                                                                       
     k.rec_bytes[idx] = atoi((ptr=strchr(ptr,'=')+2));                        
   if ((ptr = strstr(bufr,"FILE_RECORDS")) == NULL) {                         
     printf("*** ERROR *** FILE_RECORDS keyword is missing\n");               
     exit(0);                                                                 
   }                                                                          
   else                                                                       
     k.file_rec[idx] = atoi((ptr=strchr(ptr,'=')+2));                         
   if ((ptr = strstr(bufr,"LABEL_RECORDS")) == NULL) {                        
     printf("*** ERROR *** LABEL_RECORDS keyword is missing\n");              
     exit(0);                                                                 
   }                                                                          
   else                                                                       
     k.lab_rec[idx] = atoi((ptr=strchr(ptr,'=')+2));                          
   free(bufr);                                                                
                                                                              
   i = k.lab_rec[idx]*k.rec_bytes[idx];                                       
   if ((ipdslab = malloc(i)) == NULL) {                                       
     printf("*** ERROR *** Unable to allocate memory for labels\n");          
     exit(0);                                                                 
   }                                                                          
#ifdef unix                                                                   
   if (lseek(ifile,0,SEEK_SET) != 0) {                                        
#else                                                                         
   if (fseek(ifile,0,SEEK_SET) != 0) {                                        
#endif                                                                        
     printf("*** ERROR *** Problem reading the file labels\n");               
     exit(0);                                                                 
   }                                                                          
   j = 0;                                                                     
   for (i=0; i<k.lab_rec[idx]; i++) {                                         
#ifdef unix                                                                   
     if ((read(ifile,&(ipdslab[j]),k.rec_bytes[idx])) !=                      
         k.rec_bytes[idx]) {                                                  
#else                                                                         
     if ((fread(&(ipdslab[j]),1,k.rec_bytes[idx],ifile)) !=                   
         k.rec_bytes[idx]) {                                                  
#endif                                                                        
       printf("*** ERROR *** Problem reading the file labels\n");             
       exit(0);                                                               
     }                                                                        
     j+=k.rec_bytes[idx];                                                     
   }                                                                          
                                                                              
   if ((ptr = strstr(ipdslab,"^IMAGE_HISTOGRAM")) != NULL)                    
     k.hstptr[idx] = atoi((ptr=strchr(ptr,'=')+2));                           
   if (ptr == NULL || k.hstptr[idx] < 0) k.hstptr[idx] = 0;                   
   if ((ptr = strstr(ipdslab,"^IMAGE  ")) != NULL)                            
     k.imgptr[idx] = atoi((ptr=strchr(ptr,'=')+2));                           
   else {                                                                     
     printf("*** ERROR *** ^IMAGE keyword is missing\n");                     
     exit(0);                                                                 
   }                                                                          
   if (k.imgptr[idx] <= 0) {                                                  
     printf("*** ERROR *** Invalid image pointer value in labels\n");         
     exit(0);                                                                 
   }                                                                          
   strcpy(k.img_id,"");                                                       
   if ((ptr = strstr(ipdslab,"IMAGE_ID")) != NULL) {                          
     ptr2 = strchr((ptr=strchr(ptr,'=')+2),13);                               
     strncpy(k.img_id,ptr,(i=ptr2-ptr));                                      
   }                                                                          
   fprintf(printptr,"IMAGE_ID = %s\n",k.img_id);                              
   printf("IMAGE_ID = %s\n",k.img_id);                                        
   k.lines[idx] = 0;                                                          
   if ((ptr = strstr(ipdslab,"LINES")) != NULL)                               
     k.lines[idx] = atoi((ptr=strchr(ptr,'=')+2));                            
   fprintf(printptr,"NUMBER OF LINES = %d\n",k.lines[idx]);                   
   printf("NUMBER OF LINES = %d\n",k.lines[idx]);                             
   k.samps[idx] = 0;                                                          
   if ((ptr = strstr(ipdslab,"LINE_SAMPLES")) != NULL)                        
     k.samps[idx] = atoi((ptr=strchr(ptr,'=')+2));                            
   fprintf(printptr,"NUMBER OF SAMPLES = %d\n",k.samps[idx]);                 
   printf("NUMBER OF SAMPLES = %d\n",k.samps[idx]);                           
   k.samp_bit[idx] = 0;                                                       
   if ((ptr = strstr(ipdslab,"SAMPLE_BITS")) != NULL)                         
     k.samp_bit[idx] = atoi((ptr=strchr(ptr,'=')+2));                         
   fprintf(printptr,"BIT TYPE = %d\n",k.samp_bit[idx]);                       
   printf("BIT TYPE = %d\n",k.samp_bit[idx]);                                 
   strcpy(k.map_proj[idx],"");                                                
   if ((ptr = strstr(ipdslab,"MAP_PROJECTION_TYPE")) != NULL) {               
     ptr2 = strchr((ptr=strchr(ptr,'=')+2),13);                               
     strncpy(k.map_proj[idx],ptr,4);                                          
   }                                                                          
   if (strncmp(k.map_proj[idx],"STER",4) == 0) {                              
     fprintf(printptr,"MAP_PROJECTION_TYPE = STEREOGRAPHIC\n");               
     printf("MAP_PROJECTION_TYPE = STEREOGRAPHIC\n");                         
   }                                                                          
   else if (strncmp(k.map_proj[idx],"SIMP",4) == 0) {                         
     fprintf(printptr,"MAP_PROJECTION_TYPE = SIMPLE CYLINDRICAL\n");          
     printf("MAP_PROJECTION_TYPE = SIMPLE CYLINDRICAL\n");                    
   }                                                                          
   else if (strncmp(k.map_proj[idx],"SINU",4) == 0) {                         
     fprintf(printptr,"MAP_PROJECTION_TYPE = SINUSOIDAL\n");                  
     printf("MAP_PROJECTION_TYPE = SINUSOIDAL\n");                            
   }                                                                          
   k.map_scale[idx] = 0.;                                                     
   if ((ptr = strstr(ipdslab,"MAP_SCALE")) != NULL)                           
     sscanf((ptr=strchr(ptr,'=')+2),"%f",&k.map_scale[idx]);                  
   k.map_scale[idx] = k.map_scale[idx]/6051./.0174533;                        
   fprintf(printptr,"MAP_SCALE = %f <DEG/PIX>\n",k.map_scale[idx]);           
   printf("MAP_SCALE = %f <DEG/PIX>\n",k.map_scale[idx]);                     
   k.max_lat[idx] = 0.;                                                       
   if ((ptr = strstr(ipdslab,"MAXIMUM_LATITUDE")) != NULL)                    
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.max_lat[idx]);                  
   fprintf(printptr,"MAXIMUM_LATITUDE = %f\n",k.max_lat[idx]);                
   printf("MAXIMUM_LATITUDE = %f\n",k.max_lat[idx]);                          
   k.min_lat[idx] = 0.;                                                       
   if ((ptr = strstr(ipdslab,"MINIMUM_LATITUDE")) != NULL)                    
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.min_lat[idx]);                  
   fprintf(printptr,"MINIMUM_LATITUDE = %f\n",k.min_lat[idx]);                
   printf("MINIMUM_LATITUDE = %f\n",k.min_lat[idx]);                          
   k.east_lon[idx] = 0.;                                                      
   if ((ptr = strstr(ipdslab,"EASTERNMOST_LONGITUDE")) != NULL)               
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.east_lon[idx]);                 
   fprintf(printptr,"EASTERNMOST_LONGITUDE = %f\n",k.east_lon[idx]);          
   printf("EASTERNMOST_LONGITUDE = %f\n",k.east_lon[idx]);                    
   k.west_lon[idx] = 0.;                                                      
   if ((ptr = strstr(ipdslab,"WESTERNMOST_LONGITUDE")) != NULL)               
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.west_lon[idx]);                 
   fprintf(printptr,"WESTERNMOST_LONGITUDE = %f\n",k.west_lon[idx]);          
   printf("WESTERNMOST_LONGITUDE = %f\n",k.west_lon[idx]);                    
   k.ctr_lat[idx] = 0.;                                                       
   if ((ptr = strstr(ipdslab,"CENTER_LATITUDE")) != NULL)                     
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.ctr_lat[idx]);                  
   fprintf(printptr,"CENTER_LATITUDE = %f\n",k.ctr_lat[idx]);                 
   printf("CENTER_LATITUDE = %f\n",k.ctr_lat[idx]);                           
   k.ctr_lon[idx] = 0.;                                                       
   if ((ptr = strstr(ipdslab,"CENTER_LONGITUDE")) != NULL)                    
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.ctr_lon[idx]);                  
   fprintf(printptr,"CENTER_LONGITUDE = %f\n",k.ctr_lon[idx]);                
   printf("CENTER_LONGITUDE = %f\n",k.ctr_lon[idx]);                          
   k.line_poff[idx] = 0.;                                                     
   if ((ptr = strstr(ipdslab,"LINE_PROJECTION_OFFSET")) != NULL)              
     sscanf((ptr=strchr(ptr,'=')+2),"%f.3",&k.line_poff[idx]);                
   fprintf(printptr,"LINE_PROJECTION_OFFSET = %f\n",k.line_poff[idx]);        
   printf("LINE_PROJECTION_OFFSET = %f\n",k.line_poff[idx]);                  
   k.samp_poff[idx] = 0.;                                                     
   if ((ptr = strstr(ipdslab,"SAMPLE_PROJECTION_OFFSET")) != NULL)            
     sscanf((ptr=strchr(ptr,'=')+2),"%f.3",&k.samp_poff[idx]);                
   fprintf(printptr,"SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[idx]);      
   printf("SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[idx]);                
                                                                              
   if (k.samp_bit[idx] != 8) {                                                
     printf("*** ERROR *** Image must be 8 bit\n");                           
     exit(0);                                                                 
   }                                                                          
}                                                                             
                                                                              
/****************************************************                         
* order - order the input arguments from min to max *                         
****************************************************/                         
void order(in1,in2)                                                           
float *in1,*in2;                                                              
{                                                                             
   float rtmp;                                                                
                                                                              
   if (*in1 > *in2) {                                                         
     rtmp = *in1;                                                             
     *in1 = *in2;                                                             
     *in2 = rtmp;                                                             
   }                                                                          
}                                                                             
                                                                              
/************************************************************                 
* grdmax - find rectangular boundaries of output projection *                 
************************************************************/                 
void grdmax()                                                                 
{                                                                             
   float lat,lon,diflat,diflon,d1;                                            
   float x,y,rmax1,rmax2,rlat,rlon;                                           
                                                                              
   void projec();                                                             
                                                                              
   diflat = k.max_lat[1] - k.min_lat[1];                                      
   diflon = k.east_lon[1] - k.west_lon[1];                                    
                                                                              
   for (lat=k.min_lat[1]; lat<=k.max_lat[1]; lat+=diflat) {                   
     for (lon=k.west_lon[1]; lon<=k.east_lon[1]; lon+=diflon) {               
       projec(&lat,&lon,&x,&y,&d1);                                           
       if (d1 == 0.) {                                                        
         if (lat == k.min_lat[1] && lon == k.west_lon[1]) {                   
           k.line_poff[1] = x;                                                
           k.samp_poff[1] = y;                                                
           rmax1 = x;                                                         
           rmax2 = y;                                                         
         }                                                                    
         else {                                                               
           if (x < k.line_poff[1]) k.line_poff[1] = x;                        
           if (y < k.samp_poff[1]) k.samp_poff[1] = y;                        
           if (x > rmax1) rmax1 = x;                                          
           if (y > rmax2) rmax2 = y;                                          
         }                                                                    
       }                                                                      
     }                                                                        
   }                                                                          
   if (strncmp(k.map_proj[0],"SINU",4) == 0) {                                
     for (lat=k.min_lat[1]; lat<=k.max_lat[1]; lat+=diflat) {                 
       for (lon=k.west_lon[1]; lon<=k.east_lon[1]+1.; lon+=1.) {              
         rlon = lon;                                                          
         rlat = lat;                                                          
         if (rlon > k.east_lon[1]) rlon = k.east_lon[1];                      
         if (rlat > k.max_lat[1]) rlat = k.max_lat[1];                        
         projec(&rlat,&rlon,&x,&y,&d1);                                       
         if (d1 == 0) {                                                       
           if (lat == k.min_lat[1] && lon == k.west_lon[1]) {                 
             k.line_poff[1] = x;                                              
             k.samp_poff[1] = y;                                              
             rmax1 = x;                                                       
             rmax2 = y;                                                       
           }                                                                  
           else {                                                             
             if (x < k.line_poff[1]) k.line_poff[1] = x;                      
             if (y < k.samp_poff[1]) k.samp_poff[1] = y;                      
             if (x > rmax1) rmax1 = x;                                        
             if (y > rmax2) rmax2 = y;                                        
           }                                                                  
         }                                                                    
       }                                                                      
     }                                                                        
     for (lon=k.west_lon[1]; lon<=k.east_lon[1]; lon+=diflon) {               
       for (lat=k.min_lat[1]; lat<=k.max_lat[1]+1.; lat+=1.) {                
         rlon = lon;                                                          
         rlat = lat;                                                          
         if (rlon > k.east_lon[1]) rlon = k.east_lon[1];                      
         if (rlat > k.max_lat[1]) rlat = k.max_lat[1];                        
         projec(&rlat,&rlon,&x,&y,&d1);                                       
         if (d1 == 0) {                                                       
           if (lon == k.west_lon[1] && lat == k.min_lat[1]) {                 
             k.line_poff[1] = x;                                              
             k.samp_poff[1] = y;                                              
             rmax1 = x;                                                       
             rmax2 = y;                                                       
           }                                                                  
           else {                                                             
             if (x < k.line_poff[1]) k.line_poff[1] = x;                      
             if (y < k.samp_poff[1]) k.samp_poff[1] = y;                      
             if (x > rmax1) rmax1 = x;                                        
             if (y > rmax2) rmax2 = y;                                        
           }                                                                  
         }                                                                    
       }                                                                      
     }                                                                        
   }                                                                          
                                                                              
   k.lines[1] = rmax1 - k.line_poff[1] + .5;                                  
   k.samps[1] = rmax2 - k.samp_poff[1] + .5;                                  
}                                                                             
                                                                              
/*************************************************************                
* projec - finds map coordinates (x,y) in km given (lat,lon) *                
*************************************************************/                
void projec(rlat,rlon,x,y,valid)                                              
float *rlat,*rlon;                                                            
float *x,*y,*valid;                                                           
{                                                                             
   float lon,lat,cclat;                                                       
                                                                              
   lon = *rlon;                                                               
   lat = *rlat;                                                               
   if (lat > 89.9999) lat = 89.9999;                                          
   if (lat < -89.9999) lat = -89.9999;                                        
                                                                              
   *valid = 0.;                                                               
   if (strncmp(k.map_proj[0],"SIMP",4) == 0) {                                
     cclat = cos(k.ctr_lat[0]*.0174533);                                      
     *x = -lat/k.map_scale[0];                                                
     *y = lon/k.map_scale[0]*cclat;                                           
   }                                                                          
   else {                                                                     
     cclat = cos(lat*.0174533);                                               
     *x = -lat/k.map_scale[0];                                                
     *y = ((-k.ctr_lon[0] + lon)/k.map_scale[0])*cclat;                       
   }                                                                          
}                                                                             
                                                                              
/**************************************************                           
* status - report status of a function's progress *                           
**************************************************/                           
void status(curval,endval)                                                    
long int curval,endval;                                                       
{                                                                             
   static long int oldsts=0;                                                  
   static long int newsts;                                                    
   static float pct;                                                          
                                                                              
   pct = ((float) curval/(float) endval)*100.;                                
   newsts = (int) pct;                                                        
   if (newsts >= oldsts) {                                                    
     printf("%d %% done\r",newsts);                                           
     fflush(stdout);                                                          
     oldsts = oldsts + 5;                                                     
   }                                                                          
}                                                                             
                                                                              
/****************************************************************             
* readimg - reads a line from the input image file into inarray *             
****************************************************************/             
void readimg(inli,file)                                                       
long int *inli,*file;                                                         
{                                                                             
   long int i,j;                                                              
                                                                              
   if (*file == 0) {                                                          
     i = (*inli)*k.samps[0];                                                  
#ifdef unix                                                                   
     if ((read(infile,inarray,i)) != i) {                                     
#else                                                                         
     if ((fread(inarray,i,1,infile)) != 1) {                                  
#endif                                                                        
       printf("*** ERROR *** Problem reading the image data from ");          
       printf("the input image file\n");                                      
       exit(0);                                                               
     }                                                                        
#ifdef unix                                                                   
#else                                                                         
     fflush(infile);                                                          
#endif                                                                        
   }                                                                          
   else if (*file == 1) {                                                     
     i = (*inli)*k.samps[1];                                                  
#ifdef unix                                                                   
     j = tell(outfile);                                                       
     if ((read(outfile,ouarray,i)) != i) {                                    
#else                                                                         
     if ((fread(ouarray,i,1,outfile)) != 1) {                                 
#endif                                                                        
       printf("*** ERROR *** Problem reading the image data from ");          
       printf("the output mosaic file\n");                                    
       exit(0);                                                               
     }                                                                        
#ifdef unix                                                                   
     if ((lseek(outfile,j,SEEK_SET)) != j) {                                  
#else                                                                         
     fflush(outfile);                                                         
     if ((fseek(outfile,-i,SEEK_CUR)) != 0) {                                 
#endif                                                                        
       printf("*** ERROR *** Unable to read image data from ");               
       printf("output image file\n");                                         
       exit(0);                                                               
     }                                                                        
   }                                                                          
}                                                                             
                                                                              
/*********************************************************************        
* writeimg - writes the contents of ouarray to the output image file *        
*********************************************************************/        
void writeimg(inli)                                                           
long int *inli;                                                               
{                                                                             
   long int i;                                                                
                                                                              
   void hist();                                                               
                                                                              
   i = (*inli)*k.samps[1];                                                    
   hist(&i);                                                                  
#ifdef unix                                                                   
   if (write(outfile,ouarray,i) != i) {                                       
#else                                                                         
   if (fwrite(ouarray,i,1,outfile) != 1) {                                    
#endif                                                                        
     printf("*** ERROR *** Unable to write to the output image file\n");      
     exit(0);                                                                 
   }                                                                          
#ifdef unix                                                                   
#else                                                                         
   fflush(outfile);                                                           
#endif                                                                        
}                                                                             
                                                                              
/***********************************************************                  
* wpdslabs - write the pds labels to the output image file *                  
***********************************************************/                  
void wpdslabs()                                                               
{                                                                             
   char crr[2],lff[2];                                                        
   char *ptr="";                                                              
   char *ptr1="";                                                             
   char *ptr2="";                                                             
   long int itot,i,ilen;                                                      
   long int filrec,hstpnt,imgpnt;                                             
   long int idec;                                                             
   unsigned char cr=13,lf=10,blank=32;                                        
                                                                              
   void intupd();                                                             
   void fltupd();                                                             
                                                                              
   strcpy(crr,"");                                                            
   strcpy(lff,"");                                                            
   strncat(crr,&cr,1);                                                        
   strncat(lff,&lf,1);                                                        
   filrec = k.lab_rec[1] + hstrec + k.lines[1];                               
   hstpnt = k.lab_rec[1] + 1;                                                 
   imgpnt = hstpnt + hstrec;                                                  
   i = k.lab_rec[1]*k.samps[1];                                               
   if ((opdslab = malloc(i)) == NULL) {                                       
     printf("*** ERROR *** Unable to allocate memory for output labels\n");   
     exit(0);                                                                 
   }                                                                          
   strcpy(&(opdslab[0]),"");                                                  
   itot = 0;                                                                  
   intupd("RECORD_BYTES",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.samps[1]);         
   intupd("FILE_RECORDS",&ptr,&ptr1,&ptr2,&itot,crr,lff,&filrec);             
   intupd("LABEL_RECORDS",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.lab_rec[1]);      
   intupd("^IMAGE_HISTOGRAM",&ptr,&ptr1,&ptr2,&itot,crr,lff,&hstpnt);         
   intupd("^IMAGE",&ptr,&ptr1,&ptr2,&itot,crr,lff,&imgpnt);                   
   ptr = ptr2;                                                                
   ptr1 = strstr(ptr,"IMAGE_ID");                                             
   ptr2 = strchr((ptr1=strchr(ptr1,'=')+2),13) + 2;                           
   strncpy(&(opdslab[itot]),ptr,(ilen=ptr1-ptr));                             
   itot = itot + ilen;                                                        
   ilen = 8;                                                                  
   strncpy(&(opdslab[itot]),"FLMOSAIC",ilen);                                 
   itot = itot + ilen;                                                        
   strncpy(&(opdslab[itot]),crr,1);                                           
   itot = itot + 1;                                                           
   strncpy(&(opdslab[itot]),lff,1);                                           
   itot = itot + 1;                                                           
   strncpy(&(opdslab[itot]),"",1);                                            
   intupd("LINES",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.lines[1]);                
   intupd("LINE_SAMPLES",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.samps[1]);         
   intupd("CHECKSUM",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.chksum);               
   ptr = ptr2;                                                                
   ptr1 = strstr(ptr,"MAP_PROJECTION_TYPE");                                  
   ptr2 = strchr((ptr1=strchr(ptr1,'=')+2),13) + 2;                           
   strncpy(&(opdslab[itot]),ptr,(ilen=ptr1-ptr));                             
   itot = itot + ilen;                                                        
   if (strncmp(k.map_proj[0],"SIMP",4) == 0) {                                
     ilen = 18;                                                               
     strncpy(&(opdslab[itot]),"SIMPLE CYLINDRICAL",ilen);                     
   }                                                                          
   else {                                                                     
     ilen = 10;                                                               
     strncpy(&(opdslab[itot]),"SINUSOIDAL",ilen);                             
   }                                                                          
   itot = itot + ilen;                                                        
   strncpy(&(opdslab[itot]),crr,1);                                           
   itot = itot + 1;                                                           
   strncpy(&(opdslab[itot]),lff,1);                                           
   itot = itot + 1;                                                           
   idec = 5;                                                                  
   strncpy(&(opdslab[itot]),"",1);                                            
   fltupd("MAXIMUM_LATITUDE",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,            
          &k.max_lat[1]);                                                     
   fltupd("MINIMUM_LATITUDE",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,            
          &k.min_lat[1]);                                                     
   fltupd("EASTERNMOST_LONGITUDE",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,       
          &k.east_lon[1]);                                                    
   fltupd("WESTERNMOST_LONGITUDE",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,       
          &k.west_lon[1]);                                                    
   idec = 3;                                                                  
   fltupd("LINE_PROJECTION_OFFSET",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,      
          &k.line_poff[1]);                                                   
   fltupd("SAMPLE_PROJECTION_OFFSET",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,    
          &k.samp_poff[1]);                                                   
   idec = 5;                                                                  
   fltupd("CENTER_LATITUDE",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,             
          &k.ctr_lat[1]);                                                     
   fltupd("CENTER_LONGITUDE",&ptr,&ptr1,&ptr2,&itot,&idec,crr,lff,            
          &k.ctr_lon[1]);                                                     
   intupd("LINE_LAST_PIXEL",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.lines[1]);      
   intupd("SAMPLE_LAST_PIXEL",&ptr,&ptr1,&ptr2,&itot,crr,lff,&k.samps[1]);    
   strncpy(&(opdslab[itot]),ptr2,(ilen=strlen(ptr2)));                        
   itot = itot + ilen;                                                        
   strncpy(&(opdslab[itot]),"",1);                                            
   ilen = k.lab_rec[1]*k.samps[1];                                            
   if (itot < ilen) {                                                         
     for (i=itot; i<ilen; i++)                                                
       strncat(&(opdslab[i]),&blank,1);                                       
   }                                                                          
                                                                              
#ifdef unix                                                                   
   if (write(outfile,opdslab,ilen) != ilen) {                                 
#else                                                                         
   if (fwrite(opdslab,ilen,1,outfile) != 1) {                                 
#endif                                                                        
     printf("*** ERROR *** Unable to write labels to the output image ");     
     printf("file\n");                                                        
     exit(0);                                                                 
   }                                                                          
}                                                                             
                                                                              
/******************************************************************           
* hist - generate histogram information for the output image file *           
******************************************************************/           
void hist(inum)                                                               
long int *inum;                                                               
{                                                                             
   long int i;                                                                
                                                                              
   for (i=0; i<*inum; i++)                                                    
     k.hist[ouarray[i]]++;                                                    
}                                                                             
                                                                              
/*********************************************************                    
* inttoasc - convert an integer to its string equivalent *                    
*********************************************************/                    
void inttoasc(inum,bufr)                                                      
long int *inum;                                                               
char *bufr;                                                                   
{                                                                             
   char *numbers="0123456789";                                                
   long int i,j,tmpnum,iset,ival;                                             
                                                                              
   ival = *inum;                                                              
   iset = 0;                                                                  
   strcpy(bufr,"");                                                           
   j = 1000000000;                                                            
   for (i=0; i<10; i++) {                                                     
     tmpnum = ival/j;                                                         
     if (tmpnum > 0 || iset == 1) {                                           
       iset = 1;                                                              
       ival = ival - tmpnum*j;                                                
       strncat(bufr,&numbers[tmpnum],1);                                      
     }                                                                        
     j = j/10;                                                                
   }                                                                          
}                                                                             
                                                                              
/******************************************************                       
* flttoasc - convert a float to its string equivalent *                       
******************************************************/                       
void flttoasc(rnum,idec,bufr)                                                 
float *rnum;                                                                  
long int *idec;                                                               
char *bufr;                                                                   
{                                                                             
   char *numbers="0123456789";                                                
   long int i,j,m,tmpnum,iset,ival;                                           
   float rval;                                                                
                                                                              
   rval = *rnum;                                                              
   ival = rval;                                                               
   rval = rval - ival;                                                        
   for (i=0; i<*idec; i++)                                                    
     rval = rval*10;                                                          
   ival = rval;                                                               
   rval = *rnum;                                                              
   iset = 0;                                                                  
   strcpy(bufr,"");                                                           
   j = 1000000000;                                                            
   if (rval < 0.) {                                                           
     strncat(bufr,"-",1);                                                     
     rval = -rval;                                                            
     ival = -ival;                                                            
   }                                                                          
   for (i=0; i<10; i++) {                                                     
     tmpnum = rval/j;                                                         
     if (tmpnum > 0 || iset == 1) {                                           
       iset = 1;                                                              
       rval = rval - tmpnum*j;                                                
       strncat(bufr,&numbers[tmpnum],1);                                      
     }                                                                        
     j = j/10;                                                                
   }                                                                          
   if (iset == 0) strncat(bufr,"0",1);                                        
   strncat(bufr,".",1);                                                       
   j = 1;                                                                     
   for (m=1; m<*idec; m++)                                                    
     j = j*10;                                                                
   for (i=1; i<=*idec; i++) {                                                 
     tmpnum = ival/j;                                                         
     ival = ival - tmpnum*j;                                                  
     strncat(bufr,&numbers[tmpnum],1);                                        
     j = j/10;                                                                
   }                                                                          
}                                                                             
                                                                              
/**********************************                                           
* swap_long - swap a long integer *                                           
**********************************/                                           
long int swap_long(inval)                                                     
long int inval;                                                               
{                                                                             
   union {                                                                    
     char ichar[4];                                                           
     short int slen;                                                          
     long int llen;                                                           
   } onion;                                                                   
   char temp;                                                                 
                                                                              
   onion.llen = inval;                                                        
   temp = onion.ichar[0];                                                     
   onion.ichar[0] = onion.ichar[3];                                           
   onion.ichar[3] = temp;                                                     
   temp = onion.ichar[1];                                                     
   onion.ichar[1] = onion.ichar[2];                                           
   onion.ichar[2] = temp;                                                     
   return (onion.llen);                                                       
}                                                                             
                                                                              
/*****************************************************                        
* intupd - update an integer value in the pds labels *                        
*****************************************************/                        
void intupd(search,ptr,ptr1,ptr2,itot,crr,lff,ival)                           
char *search;                                                                 
char **ptr,**ptr1,**ptr2;                                                     
long int *itot;                                                               
char *crr,*lff;                                                               
long int *ival;                                                               
{                                                                             
   char bufr[12];                                                             
   long int ilen;                                                             
                                                                              
   void inttoasc();                                                           
                                                                              
   if (*itot == 0)                                                            
     *ptr = ipdslab;                                                          
   else                                                                       
     *ptr = *ptr2;                                                            
                                                                              
   *ptr1 = strstr(*ptr,search);                                               
   *ptr2 = strchr((*ptr1=strchr(*ptr1,'=')+2),13) + 2;                        
   strncpy(&(opdslab[*itot]),*ptr,(ilen=(*ptr1)-(*ptr)));                     
   inttoasc(ival,bufr);                                                       
   *itot = *itot + ilen;                                                      
   strncpy(&(opdslab[*itot]),bufr,(ilen=strlen(bufr)));                       
   *itot = *itot + ilen;                                                      
   strncpy(&(opdslab[*itot]),crr,1);                                          
   *itot = *itot + 1;                                                         
   strncpy(&(opdslab[*itot]),lff,1);                                          
   *itot = *itot + 1;                                                         
}                                                                             
                                                                              
/*************************************************                            
* fltupd - update a real value in the pds labels *                            
*************************************************/                            
void fltupd(search,ptr,ptr1,ptr2,itot,idec,crr,lff,rval)                      
char *search;                                                                 
char **ptr,**ptr1,**ptr2;                                                     
long int *itot,*idec;                                                         
char *crr,*lff;                                                               
float *rval;                                                                  
{                                                                             
   char bufr[15];                                                             
   long int ilen;                                                             
                                                                              
   void flttoasc();                                                           
                                                                              
   if (*itot == 0)                                                            
     *ptr = ipdslab;                                                          
   else                                                                       
     *ptr = *ptr2;                                                            
                                                                              
   *ptr1 = strstr(*ptr,search);                                               
   *ptr2 = strchr((*ptr1=strchr(*ptr1,'=')+2),13) + 2;                        
   strncpy(&(opdslab[*itot]),*ptr,(ilen=(*ptr1)-(*ptr)));                     
   flttoasc(rval,idec,bufr);                                                  
   *itot = *itot + ilen;                                                      
   strncpy(&(opdslab[*itot]),bufr,(ilen=strlen(bufr)));                       
   *itot = *itot + ilen;                                                      
   strncpy(&(opdslab[*itot]),crr,1);                                          
   *itot = *itot + 1;                                                         
   strncpy(&(opdslab[*itot]),lff,1);                                          
   *itot = *itot + 1;                                                         
}