/**********************                                                       
* standard C includes *                                                       
**********************/                                                       
#include <stdio.h>                                                            
#include <string.h>                                                           
#include <stdlib.h>                                                           
#include <ctype.h>                                                            
#include <time.h>                                                             
#include <math.h>                                                             
                                                                              
#define SEEK_SET 0                                                            
#define SEEK_CUR 1                                                            
                                                                              
/**************************                                                   
* subroutine declarations *                                                   
**************************/                                                   
void check_host();                                                            
void rpdslabs();                                                              
void grdmax();                                                                
void order();                                                                 
void status();                                                                
void hist();                                                                  
void wpdslabs();                                                              
long int swap_long();                                                         
                                                                              
/*******************                                                          
* global variables *                                                          
*******************/                                                          
unsigned char *inarray;                                                       
unsigned char *ouarray;                                                       
unsigned char *inbuf;                                                         
unsigned char *oubuf;                                                         
unsigned char *iloc;                                                          
unsigned char *oloc;                                                          
char *ipdslab;                                                                
char *opdslab;                                                                
char inname[80];                                                              
char outname[80];                                                             
char prtname[80];                                                             
FILE *printptr;                                                               
FILE *infile;                                                                 
FILE *outfile;	                                                               
char proj[5]="SINU";                                                          
char tech[3]="NN";                                                            
short int host;                                                               
float minlon,maxlon;                                                          
long int hstrec;                                                              
                                                                              
struct {                                                                      
	long int rec_bytes;                                                          
	long int file_rec;                                                           
	long int lab_rec[2];                                                         
        long int hstptr;                                                      
	long int imgptr;                                                             
	char img_id[9];                                                              
	long int lines[2];                                                           
	long int samps[2];                                                           
	long int samp_bit;                                                           
	long int chksum;                                                             
	long int hist[256];                                                          
	char map_proj[5];                                                            
	float map_scale;                                                             
	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;	  /* command line argument counter */                    
   long int i,j,inull,isos,isoe,inl,ilim1;                                    
   long int is,ilnxti,icnt,i1,i2,iline,isamp;                                 
   short int iflag,sflag;                                                     
   float rlat,rval1,rval2,rval3,p1,p2,b1,b2,b3,b4;                            
   float xmini,ymini,rval4,rval5,cosclato,xmino,ymino;                        
   float y1,y2,x,y,plat,plon,coslat,sample,clonin,clonou;                     
   char *version="22-JUL-94"; /* version date of program */                   
   unsigned char blank=32;                                                    
   time_t lt;      /* used to obtain the date and time that this program */   
   struct tm *ptr; /* was executed and the print file was created */          
                                                                              
/*****************************************************************            
* 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("\nNEWMAP Image Processing Program. Command line format:\n\n");   
     printf("NEWMAP from to -p -r -n null -c clon -f prtf\n\n");              
     printf("from    - Name of the input image file.\n");                     
     printf("to      - Name of the output image file.\n");                    
     printf("-p      - Specifies that the map projection of the output ");    
     printf("image file is to be\n          Simple Cylindrical (the ");       
     printf("default is to create an output image file\n          in ");      
     printf("the Sinusoidal Equal-Area map projection).\n");                  
     printf("-r      - Specifies that the Bilinear resampling technique ");   
     printf("be used for\n          reprojection and scale changes (the ");   
     printf("default is to use the Nearest\n          Neighbor ");            
     printf("resampling technique).\n");                                      
     printf("-n null - The null value to use in the Bilinear resampling ");   
     printf("technique. Pixels\n          with this density value will be "); 
     printf("ignored in calculations performed\n          during Bilinear "); 
     printf("resampling. The null value must fall in the range 0\n");         
     printf("          to 255 (the default null value is 0).\n");             
     printf("-c clon - The center longitude of the output image file (the "); 
     printf("default is to use\n          the same center longitude as ");    
     printf("the input image file).\n");                                      
     printf("-f prtf - The location and name of the output log file. This "); 
     printf("file is used to\n          store information relevant to the "); 
     printf("input and output images. If you\n          wish to run the ");   
     printf("software directly from the CD, you must specify a\n");           
     printf("          location other than the CD for the location of ");     
     printf("this file (the\n          default is to create a file called "); 
     printf("print.prt in the same directory\n          as the software).\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 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*** NEWMAP ***  Version: %s  Date: %s\n",            
             version,asctime(ptr));                                           
     printf("\n*** NEWMAP ***  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 *                  
***********************************************************/                  
   rpdslabs();                                                                
                                                                              
/*******************************************                                  
* read in remaining command line arguments *                                  
*******************************************/                                  
   inum = 3;                                                                  
   k.ctr_lon[1] = k.ctr_lon[0];                                               
   inull = 0;                                                                 
                                                                              
   while (inum < argc) {                                                      
     if (strcmp(argv[inum],"-p") == 0)                                        
       strncpy(proj,"SIMP",4);                                                
     else if (strcmp(argv[inum],"-r") == 0)                                   
       strncpy(tech,"BI",2);                                                  
     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 (strcmp(argv[inum],"-c") == 0) {                                 
       if (inum < argc) {                                                     
         k.ctr_lon[1] = atof(argv[++inum]);                                   
         while (k.ctr_lon[1] < 0.)                                            
           k.ctr_lon[1]+=360.;                                                
         k.ctr_lon[1] = fmod(k.ctr_lon[1],360.);                              
       }                                                                      
     }                                                                        
     inum++;                                                                  
   }                                                                          
                                                                              
/**********************************************                               
* initialize some input image file parameters *                               
**********************************************/                               
   xmini = k.line_poff[0]+.5;                                                 
   ymini = k.samp_poff[0]+.5;                                                 
                                                                              
/***********************************************                              
* initialize some output image file parameters *                              
***********************************************/                              
   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];                                             
                                                                              
   if (strncmp(proj,"SIMP",4) == 0)                                           
     k.ctr_lon[1] = (k.east_lon[1] + k.west_lon[1])/2.;                       
   k.ctr_lat[1] = 0.;                                                         
                                                                              
/***************************************************                          
* find rectangular boundaries of output projection *                          
***************************************************/                          
   grdmax();                                                                  
                                                                              
/**************************************************                           
* record output image file parameters in log file *                           
**************************************************/                           
   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);                            
   printf("BIT TYPE = %d\n",k.samp_bit);                                      
   if (strncmp(proj,"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);                
   printf("MAP_SCALE = %f <DEG/PIX>\n",k.map_scale);                          
   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]-1);        
   printf("LINE_PROJECTION_OFFSET = %f\n",k.line_poff[1]-1);                  
   fprintf(printptr,"SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[1]-1);      
   printf("SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[1]-1);                
   if (strncmp(tech,"NN",2) == 0) {                                           
     fprintf(printptr,"\nResampling technique used: NEAREST NEIGHBOR\n");     
     printf("\nResampling technique used: NEAREST NEIGHBOR\n");               
   }                                                                          
   else {                                                                     
     fprintf(printptr,"\nResampling technique used: BI-LINEAR\n");            
     fprintf(printptr,"Null value: %d\n",inull);                              
     printf("\nResampling technique used: BI-LINEAR\n");                      
     printf("Null value: %d\n",inull);                                        
   }                                                                          
                                                                              
/**********************************************                               
* initialize some output image file parameters *                              
**********************************************/                               
   xmino = k.line_poff[1] - .5;                                               
   ymino = k.samp_poff[1] - .5;                                               
   k.line_poff[1] = k.line_poff[1] - 1;                                       
   k.samp_poff[1] = k.samp_poff[1] - 1;                                       
                                                                              
   if (strncmp(proj,"SIMP",4) == 0) {                                         
     cosclato = cos(k.ctr_lat[1]*.0174533);                                   
     rval1 = k.map_scale/cosclato;                                            
     y1 = -ymino + (k.west_lon[1]/k.map_scale)*cosclato;                      
     y2 = -ymino + (k.east_lon[1]/k.map_scale)*cosclato;                      
     order(&y1,&y2);                                                          
     p1 = y1 + .5;                                                            
     isos = p1;                                                               
     p1 = isos;                                                               
     if ((y1 - p1) > 0.) isos = isos + 1;                                     
     p1 = y2 + .5;                                                            
     isoe = p1;                                                               
     p1 = isoe;                                                               
     if ((y2 - p1) < 0.) isoe = isoe - 1;                                     
   }                                                                          
                                                                              
   if (k.lines[0] != k.lines[1]) {                                            
     rlat = -(1. + xmino)*k.map_scale;                                        
     ilnxti = -rlat/k.map_scale - xmini + .5;                                 
     if (ilnxti < 1) ilnxti = 1;                                              
     if (ilnxti > k.lines[0]) ilnxti = k.lines[0];                            
   }                                                                          
   else                                                                       
     ilnxti = 1;                                                              
                                                                              
   minlon = k.west_lon[0];                                                    
   maxlon = k.east_lon[0];                                                    
   if (minlon > maxlon)                                                       
     minlon-=360.;                                                            
                                                                              
/***********************                                                      
* main processing loop *                                                      
***********************/                                                      
   if (k.samps[0] > k.samps[1])                                               
     inl = 60000/k.samps[0];                                                  
   else                                                                       
     inl = 60000/k.samps[1];                                                  
                                                                              
   i = inl*k.samps[0];                                                        
   if ((inbuf = (unsigned char *) malloc(60416)) == NULL) {                   
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   if ((inarray = (unsigned char *) malloc(i)) == NULL) {                     
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   if ((infile = fopen(inname,"rb")) == NULL) {                               
     printf("*** ERROR *** Can't open the input image file: %s\n",            
            inname);                                                          
     exit(0);                                                                 
   }                                                                          
   setvbuf(infile,inbuf,_IOFBF,60416);                                        
                                                                              
   i = inl*k.samps[1];                                                        
   if ((oubuf = (unsigned char *) malloc(60416)) == NULL) {                   
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   if ((ouarray = (unsigned char *) malloc(i)) == NULL) {                     
     printf("*** ERROR *** Unable to allocate memory for processing\n");      
     exit(0);                                                                 
   }                                                                          
   if ((outfile = fopen(outname,"wb")) == NULL) {                             
     printf("*** ERROR *** Can't open the output image file: %s\n",           
            outname);                                                         
     exit(0);                                                                 
   }                                                                          
   setvbuf(outfile,oubuf,_IOFBF,60416);                                       
                                                                              
   printf(" \n");                                                             
   i = 0;                                                                     
   status(i,k.lines[1]);                                                      
                                                                              
   i = (k.imgptr+ilnxti-2)*k.samps[0];                                        
   if ((j = fseek(infile,i,SEEK_SET)) != 0) {                                 
     printf("*** ERROR *** Unable to read image data from ");                 
     printf("input image file\n");                                            
     exit(0);                                                                 
   }                                                                          
                                                                              
   i = strlen(ipdslab);                                                       
   k.lab_rec[1] = (i+k.samps[1]-1)/k.samps[1];                                
   hstrec = (1023 + k.samps[1])/k.samps[1];                                   
                                                                              
   oloc = ouarray;                                                            
   for (j=0; j<k.samps[1]; j++)                                               
     oloc[j] = 0;                                                             
   i = k.lab_rec[1] + hstrec;                                                 
   for (icnt=0; icnt<i; icnt++) {                                             
     if (fwrite(oloc,k.samps[1],1,outfile) != 1) {                            
       printf("*** ERROR *** Unable to write to the output image file\n");    
       exit(0);                                                               
     }                                                                        
   }                                                                          
   fflush(outfile);                                                           
                                                                              
   for (icnt=0; icnt<256; icnt++)                                             
     k.hist[i] = 0;                                                           
                                                                              
   if (strncmp(tech,"NN",2) == 0)                                             
     sflag = 1;                                                               
   else                                                                       
     sflag = 3;                                                               
                                                                              
   if (strncmp(proj,"SINU",4) == 0) iflag = 0;                                
   else iflag = 1;                                                            
                                                                              
   clonou = -k.ctr_lon[1];                                                    
   clonin = -k.ctr_lon[0];                                                    
   ilim1 = k.lines[0] - ilnxti + 1;                                           
   if (ilim1 > k.lines[1]) ilim1 = k.lines[1];                                
   plat = -(ilnxti - 1 + xmini)*k.map_scale;                                  
   rval4 = minlon - k.ctr_lon[1];                                             
   rval5 = maxlon - k.ctr_lon[1];                                             
   inum = 0;                                                                  
                                                                              
   for (icnt=1; icnt<=ilim1; icnt++) {                                        
                                                                              
/**************************************************                           
* read a line from the input image file into iloc *                           
**************************************************/                           
     if (inum == 0) {                                                         
       if (icnt > 1) {                                                        
         fflush(outfile);                                                     
         i = (inl-1)*k.samps[1];                                              
         if (fwrite(ouarray,i,1,outfile) != 1) {                              
           printf("*** ERROR *** Problem writing to the output image file\n");
           exit(0);                                                           
         }                                                                    
         fflush(outfile);                                                     
       }                                                                      
       i = ilim1 - icnt + 1;                                                  
       if (inl > i) {                                                         
         inl = i;                                                             
         i = inl*k.samps[0];                                                  
         if ((fread(inarray,i,1,infile)) != 1) {                              
           printf("*** ERROR *** Problem reading the image data from ");      
           printf("the input image file\n");                                  
           exit(0);                                                           
         }                                                                    
         fflush(infile);                                                      
         iloc = inarray + i;                                                  
         j = i - k.samps[0];                                                  
         for (i=0; i<k.samps[0]; i++) {                                       
           iloc[i] = inarray[j];                                              
           j++;                                                               
         }                                                                    
         inum = inl;                                                          
       }                                                                      
       else {                                                                 
         i = inl*k.samps[0];                                                  
         if ((fread(inarray,i,1,infile)) != 1) {                              
           printf("*** ERROR *** Problem reading the image data from ");      
           printf("the input image file\n");                                  
           exit(0);                                                           
         }                                                                    
         fflush(infile);                                                      
         if ((fseek(infile,-k.samps[0],SEEK_CUR)) != 0) {                     
           printf("*** ERROR *** Problem reading the image data from ");      
           printf("the input image file\n");                                  
           exit(0);                                                           
         }                                                                    
         fflush(infile);                                                      
         inum = inl - 1;                                                      
       }                                                                      
       iloc = inarray;                                                        
       oloc = ouarray;                                                        
     }                                                                        
     inum--;                                                                  
                                                                              
     plat = plat - k.map_scale;                                               
     coslat = cos(plat*.0174533);                                             
     rval2 = coslat/k.map_scale;                                              
     if (coslat != 0.)                                                        
       rval3 = k.map_scale/coslat;                                            
     else                                                                     
       rval3 = 0.;                                                            
                                                                              
     if (strncmp(proj,"SINU",4) == 0) {                                       
       y1 = rval4*rval2 - ymino;                                              
       y2 = rval5*rval2 - ymino;                                              
       order(&y1,&y2);                                                        
       p1 = y1 + .5;                                                          
       isos = p1;                                                             
       p1 = isos;                                                             
       if ((y1 - p1) > 0.) isos = isos + 1;                                   
       p1 = y2 + .5;                                                          
       isoe = p1;                                                             
       p1 = isoe;                                                             
       if ((y2 - p1) < 0.) isoe = isoe - 1;                                   
     }                                                                        
                                                                              
     strncpy(oloc,iloc,k.samps[0]);                                           
     if (k.samps[1] > k.samps[0]) {                                           
       for (i=k.samps[0]; i<k.samps[1]; i++)                                  
         oloc[i] = 0;                                                         
     }                                                                        
                                                                              
     for (j=isos; j<=isoe; j++) {                                             
       y = j + ymino;                                                         
       if (iflag == 0)                                                        
         plon = clonou - y*rval3;                                             
       else                                                                   
         plon = -y*rval1;                                                     
                                                                              
       y = (clonin - plon)*rval2;                                             
                                                                              
       sample = y - ymini;                                                    
       is = sample + .5;                                                      
                                                                              
       if (is > 0) {                                                          
         if (sflag == 1)                                                      
           oloc[j-1] = iloc[is-1];                                            
         else {                                                               
           x = (k.max_lat[0] - plat)/k.map_scale;                             
           i1 = is - 1;                                                       
           if (is > sample) {                                                 
             i2 = is - 2;                                                     
             if (i2 < 0) i2 = 0;                                              
           }                                                                  
           else {                                                             
             i2 = is;                                                         
             if (i2 >= k.samps[0]) i2 = i1;                                   
           }                                                                  
           b1 = iloc[i1];                                                     
           b2 = iloc[i2];                                                     
           b3 = iloc[i1+k.samps[0]];                                          
           b4 = iloc[i2+k.samps[0]];                                          
           iline = x;                                                         
           isamp = y;                                                         
           p1 = x - iline;                                                    
           p2 = y - isamp;                                                    
           if (b1 != inull) {                                                 
             if (b2 == inull) b2 = b1;                                        
             if (b3 == inull) b3 = b1;                                        
             if (b4 == inull) b4 = b1;                                        
           }                                                                  
           else if (b2 != inull) {                                            
             b1 = b2;                                                         
             if (b3 == inull) b3 = b2;                                        
             if (b4 == inull) b4 = b2;                                        
           }                                                                  
           else if (b3 != inull) {                                            
             b1 = b3;                                                         
             b2 = b3;                                                         
             if (b4 == inull) b4 = b3;                                        
           }                                                                  
           else if (b4 != inull) {                                            
             b1 = b4;                                                         
             b2 = b4;                                                         
             b3 = b4;                                                         
           }                                                                  
           oloc[j-1] = (b3 - b1)*p1 + (b2 - b1)*p2 + b1 +                     
                       (b1 - b2 - b3 + b4)*p1*p2;                             
         }                                                                    
       }                                                                      
     }                                                                        
     hist();                                                                  
     iloc+=k.samps[0];                                                        
     oloc+=k.samps[1];                                                        
     status(icnt,ilim1);                                                      
   }                                                                          
                                                                              
   i = inl*k.samps[1];                                                        
   if ((fwrite(ouarray,i,1,outfile)) != 1) {                                  
     printf("*** ERROR *** Problem writing to the output image file\n");      
     exit(0);                                                                 
   }                                                                          
   fflush(outfile);                                                           
                                                                              
/****************************************************                         
* free the memory taken up by the image data arrays *                         
****************************************************/                         
   free(inarray);                                                             
   free(ouarray);                                                             
                                                                              
/**************************************************************               
* update the checksum and histogram information in the labels *               
**************************************************************/               
   if (fseek(outfile,0,SEEK_SET) != 0) {                                      
     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();                                                                
                                                                              
   fwrite((char *)k.hist,1024,1,outfile);                                     
   i = hstrec*k.samps[1] - 1024;                                              
   for (icnt=0; icnt<i; icnt++)                                               
     fputc(blank,outfile);                                                    
                                                                              
   fflush(outfile);                                                           
   free(ipdslab);                                                             
   free(opdslab);                                                             
                                                                              
   fclose(infile);                                                            
   fclose(outfile);                                                           
   free(inbuf);                                                               
   free(oubuf);                                                               
                                                                              
   fprintf(printptr,"\n*** END OF NEWMAP ***\n");                             
   printf("\n*** END OF NEWMAP ***\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 input image file *                 
************************************************************/                 
void rpdslabs()                                                               
{                                                                             
   char *bufr;                                                                
   char *ptr,*ptr2;                                                           
   long int i,j;                                                              
                                                                              
   if ((infile = fopen(inname,"rb")) == 0) {                                  
     printf("*** ERROR *** Can't open the input image file: %s\n",inname);    
     exit(0);                                                                 
   }                                                                          
   if ((bufr = malloc(800)) == NULL) {                                        
     printf("*** ERROR *** Unable to allocate memory for input image ");      
     printf("labels\n");                                                      
     exit(0);                                                                 
   }                                                                          
   if ((i = fread(bufr,1,800,infile)) != 800) {                               
     printf("*** ERROR *** Problem reading the input 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 = 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 = 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[0] = atoi((ptr=strchr(ptr,'=')+2));                            
   free(bufr);                                                                
                                                                              
   i = k.lab_rec[0]*k.rec_bytes;                                              
   if ((ipdslab = malloc(i)) == NULL) {                                       
     printf("*** ERROR *** Unable to allocate memory for labels\n");          
     exit(0);                                                                 
   }                                                                          
   if (fseek(infile,0,SEEK_SET) != 0) {                                       
     printf("*** ERROR *** Problem reading the input file labels\n");         
     exit(0);                                                                 
   }                                                                          
   j = 0;                                                                     
   for (i=0; i<k.lab_rec[0]; i++) {                                           
     if ((fread(&(ipdslab[j]),1,k.rec_bytes,infile)) != k.rec_bytes) {        
       printf("*** ERROR *** Problem reading the input file labels\n");       
       exit(0);                                                               
     }                                                                        
     j+=k.rec_bytes;                                                          
   }                                                                          
                                                                              
   fprintf(printptr,"\n** INPUT IMAGE FILE PARAMETERS **\n\n");               
   printf("\n** INPUT IMAGE FILE PARAMETERS **\n\n");                         
   if ((ptr = strstr(ipdslab,"^IMAGE_HISTOGRAM")) != NULL)                    
     k.hstptr = atoi((ptr=strchr(ptr,'=')+2));                                
   if (ptr == NULL || k.hstptr < 0) k.hstptr = 0;                             
   if ((ptr = strstr(ipdslab,"^IMAGE  ")) != NULL)                            
     k.imgptr = atoi((ptr=strchr(ptr,'=')+2));                                
   else {                                                                     
     printf("*** ERROR *** ^IMAGE keyword is missing\n");                     
     exit(0);                                                                 
   }                                                                          
   if (k.imgptr <= 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[0] = 0;                                                            
   if ((ptr = strstr(ipdslab,"LINES")) != NULL)                               
     k.lines[0] = atoi((ptr=strchr(ptr,'=')+2));                              
   fprintf(printptr,"NUMBER OF LINES = %d\n",k.lines[0]);                     
   printf("NUMBER OF LINES = %d\n",k.lines[0]);                               
   k.samps[0] = 0;                                                            
   if ((ptr = strstr(ipdslab,"LINE_SAMPLES")) != NULL)                        
     k.samps[0] = atoi((ptr=strchr(ptr,'=')+2));                              
   fprintf(printptr,"NUMBER OF SAMPLES = %d\n",k.samps[0]);                   
   printf("NUMBER OF SAMPLES = %d\n",k.samps[0]);                             
   k.samp_bit = 0;                                                            
   if ((ptr = strstr(ipdslab,"SAMPLE_BITS")) != NULL)                         
     k.samp_bit = atoi((ptr=strchr(ptr,'=')+2));                              
   fprintf(printptr,"BIT TYPE = %d\n",k.samp_bit);                            
   printf("BIT TYPE = %d\n",k.samp_bit);                                      
   strcpy(k.map_proj,"");                                                     
   if ((ptr = strstr(ipdslab,"MAP_PROJECTION_TYPE")) != NULL) {               
     ptr2 = strchr((ptr=strchr(ptr,'=')+2),13);                               
     strncpy(k.map_proj,ptr,4);                                               
   }                                                                          
   if (strncmp(k.map_proj,"STER",4) == 0) {                                   
     fprintf(printptr,"MAP_PROJECTION_TYPE = STEREOGRAPHIC\n");               
     printf("MAP_PROJECTION_TYPE = STEREOGRAPHIC\n");                         
   }                                                                          
   else if (strncmp(k.map_proj,"SIMP",4) == 0) {                              
     fprintf(printptr,"MAP_PROJECTION_TYPE = SIMPLE CYLINDRICAL\n");          
     printf("MAP_PROJECTION_TYPE = SIMPLE CYLINDRICAL\n");                    
   }                                                                          
   else if (strncmp(k.map_proj,"SINU",4) == 0) {                              
     fprintf(printptr,"MAP_PROJECTION_TYPE = SINUSOIDAL\n");                  
     printf("MAP_PROJECTION_TYPE = SINUSOIDAL\n");                            
   }                                                                          
   k.map_scale = 0.;                                                          
   if ((ptr = strstr(ipdslab,"MAP_SCALE")) != NULL)                           
     sscanf((ptr=strchr(ptr,'=')+2),"%f",&k.map_scale);                       
   k.map_scale = k.map_scale/6051./.0174533;                                  
   fprintf(printptr,"MAP_SCALE = %f <DEG/PIX>\n",k.map_scale);                
   printf("MAP_SCALE = %f <DEG/PIX>\n",k.map_scale);                          
   k.max_lat[0] = 0.;                                                         
   if ((ptr = strstr(ipdslab,"MAXIMUM_LATITUDE")) != NULL)                    
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.max_lat[0]);                    
   fprintf(printptr,"MAXIMUM_LATITUDE = %f\n",k.max_lat[0]);                  
   printf("MAXIMUM_LATITUDE = %f\n",k.max_lat[0]);                            
   k.min_lat[0] = 0.;                                                         
   if ((ptr = strstr(ipdslab,"MINIMUM_LATITUDE")) != NULL)                    
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.min_lat[0]);                    
   fprintf(printptr,"MINIMUM_LATITUDE = %f\n",k.min_lat[0]);                  
   printf("MINIMUM_LATITUDE = %f\n",k.min_lat[0]);                            
   k.east_lon[0] = 0.;                                                        
   if ((ptr = strstr(ipdslab,"EASTERNMOST_LONGITUDE")) != NULL)               
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.east_lon[0]);                   
   fprintf(printptr,"EASTERNMOST_LONGITUDE = %f\n",k.east_lon[0]);            
   printf("EASTERNMOST_LONGITUDE = %f\n",k.east_lon[0]);                      
   k.west_lon[0] = 0.;                                                        
   if ((ptr = strstr(ipdslab,"WESTERNMOST_LONGITUDE")) != NULL)               
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.west_lon[0]);                   
   fprintf(printptr,"WESTERNMOST_LONGITUDE = %f\n",k.west_lon[0]);            
   printf("WESTERNMOST_LONGITUDE = %f\n",k.west_lon[0]);                      
   k.ctr_lat[0] = 0.;                                                         
   if ((ptr = strstr(ipdslab,"CENTER_LATITUDE")) != NULL)                     
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.ctr_lat[0]);                    
   fprintf(printptr,"CENTER_LATITUDE = %f\n",k.ctr_lat[0]);                   
   printf("CENTER_LATITUDE = %f\n",k.ctr_lat[0]);                             
   k.ctr_lon[0] = 0.;                                                         
   if ((ptr = strstr(ipdslab,"CENTER_LONGITUDE")) != NULL)                    
     sscanf((ptr=strchr(ptr,'=')+2),"%f.5",&k.ctr_lon[0]);                    
   fprintf(printptr,"CENTER_LONGITUDE = %f\n",k.ctr_lon[0]);                  
   printf("CENTER_LONGITUDE = %f\n",k.ctr_lon[0]);                            
   k.line_poff[0] = 0.;                                                       
   if ((ptr = strstr(ipdslab,"LINE_PROJECTION_OFFSET")) != NULL)              
     sscanf((ptr=strchr(ptr,'=')+2),"%f.4",&k.line_poff[0]);                  
   fprintf(printptr,"LINE_PROJECTION_OFFSET = %f\n",k.line_poff[0]);          
   printf("LINE_PROJECTION_OFFSET = %f\n",k.line_poff[0]);                    
   k.samp_poff[0] = 0.;                                                       
   if ((ptr = strstr(ipdslab,"SAMPLE_PROJECTION_OFFSET")) != NULL)            
     sscanf((ptr=strchr(ptr,'=')+2),"%f.4",&k.samp_poff[0]);                  
   fprintf(printptr,"SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[0]);        
   printf("SAMPLE_PROJECTION_OFFSET = %f\n",k.samp_poff[0]);                  
                                                                              
   if (k.samp_bit != 8) {                                                     
     printf("*** ERROR *** Input image must be 8 bit\n");                     
     exit(0);                                                                 
   }                                                                          
   if (strncmp(k.map_proj,"SINU",4) != 0) {                                   
     printf("*** ERROR *** Input image file must be in SINUSOIDAL ");         
     printf("projection.\n");                                                 
     exit(0);                                                                 
   }                                                                          
   fclose(infile);                                                            
}                                                                             
                                                                              
/****************************************************                         
* 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(proj,"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 (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 (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(proj,"SIMP",4) == 0) {                                         
     cclat = cos(k.ctr_lat[1]*.0174533);                                      
     *x = -lat/k.map_scale;                                                   
     *y = lon/k.map_scale*cclat;                                              
   }                                                                          
   else {                                                                     
     cclat = cos(lat*.0174533);                                               
     *x = -lat/k.map_scale;                                                   
     *y = ((-k.ctr_lon[1] + lon)/k.map_scale)*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;                                                     
   }                                                                          
}                                                                             
                                                                              
/***********************************************************                  
* 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);                   
   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(proj,"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 = 4;                                                                  
   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);                                       
   }                                                                          
                                                                              
   if (fwrite(opdslab,ilen,1,outfile) != 1) {                                 
     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()                                                                   
{                                                                             
   long int i;                                                                
                                                                              
   for (i=0; i<k.samps[1]; i++)                                               
     k.hist[oloc[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;                                                         
   strncpy(&(opdslab[*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;                                                         
   strncpy(&(opdslab[*itot]),"",1);                                           
}