Attachment 'mergePhase.c'

Download

   1 /*
   2 cc -O3 mergePhase.c -o t.mergePhase
   3  -- not permanent because you have to update
   4 		offset (based on phase.img)
   5 		5064 if required (based on the file itself - currently a single record)
   6 		scaling factor
   7 */
   8 #include <stdio.h>
   9 #include <stdlib.h>
  10 #include <string.h>
  11 #define SIZE 5064
  12 #define	VERSION 2
  13 
  14 int readLine (FILE *in, char *buf) {
  15 	int	i;
  16 	char	val;
  17 	for (i=0; i<255; i++) {
  18 		val = fgetc (in);
  19 		buf [i] = val;
  20 		buf [i+1] = '\0';
  21 		if (val == '\n')
  22 			return i;
  23 	}//fori
  24 	return i;
  25 
  26 }
  27 
  28 int main (int argc, char *argv []) {
  29 	int	j;
  30 	
  31 	char headFile [100];
  32 	char *root ;
  33 	char headerStr [255];
  34 	char phaseStr [255];
  35 	char outFile [100];
  36 
  37 	printf ("Version %d\n", VERSION);
  38 	if (argc == 3) {
  39 		strcpy (headerStr, argv [1]);
  40 		strcpy (phaseStr, argv [2]);
  41 		}
  42 	else {
  43 		printf ("Batch usage: %s <header.IMG> <phase.img> \n", argv [0]);
  44 		printf ("Enter source image to clone header from:  ");
  45 		scanf ("%s", headerStr);
  46 		printf ("Enter phase ISIS-generated img file:  ");
  47 		scanf ("%s", phaseStr);
  48 	}//else 
  49 
  50 
  51 	// Set up the output by using the source header file and change 2nd char to E
  52 	sprintf (headFile, "%s", headerStr);
  53 	sprintf (outFile,  "%s", headerStr);
  54 	outFile [1] = 'P';
  55 
  56 	FILE *headF = fopen (headFile, "r");
  57 	if (! headF ) { fprintf (stderr, "Can't open %s\n", headFile); exit (1); }
  58 	FILE *dataF = fopen (phaseStr, "r");
  59 	if (! dataF ) { fprintf (stderr, "Can't open %s\n", phaseStr); exit (1); }
  60 
  61 	FILE *outF = fopen (outFile, "w");
  62 	if (! outF ) { fprintf (stderr, "Can't open %s\n", outFile); exit (1); }
  63 
  64 	sprintf (outFile,  "%s-meta", headerStr);
  65 	outFile [1] = 'P';
  66 	FILE *metaF = fopen (outFile, "w");
  67 	if (! metaF ) { fprintf (stderr, "Can't open %s\n", outFile); exit (1); }
  68 
  69 
  70 	// Copy header from template image into the new output image
  71 	int	i;
  72 	for (i=0; i<SIZE; i++) {
  73 		char ch = fgetc (headF);
  74 		fputc (ch, outF);
  75 	}//for
  76 
  77 	char buf [256], other [255];
  78 	char keyword [256];
  79 	short	byteOffset = 9999;
  80 	float offset = -1;
  81 	float scalingFactor = -1;
  82 	long sum = 0;
  83 	char offsetStr [100];
  84 	char scalingStr [100];
  85 	int flag=0;				// Tracks how many important options are set so we can stop
  86 	// Read in header for the phase file, stop when we get the buf char
  87 	// 1000 is a crazy max, and we should never get there
  88 	for (i=0; i<1000; i++) {
  89 		sum += readLine (dataF, buf);
  90 		sscanf (buf, "%s = %s", keyword, other);
  91 
  92 		// Find the keywords and set values
  93 		fprintf (stderr, "%ld) %s  <-->  %s\n", sum, keyword, other);
  94 		if (strcmp ("LABEL_RECORDS", keyword) == 0) {
  95 			byteOffset = atoi (other);
  96 			printf ("################ - found byte %d rec\n", byteOffset);
  97 			flag++;
  98 		}//if
  99 
 100 		if (strcmp ("OFFSET", keyword) == 0) {
 101 			strcpy (offsetStr, other);
 102 			offset = atof (other);
 103 			flag++;
 104 		}//if
 105 
 106 		if (strcmp ("SCALING_FACTOR", keyword) == 0)  {
 107 			strcpy (scalingStr, other);
 108 			scalingFactor = atof (other);
 109 			flag++;
 110 		}//if
 111 
 112 
 113 		// We found all 3 parameters, break and scan lines
 114 		if (flag >= 3)
 115 			break;
 116 
 117 		// The loop is done
 118 		if (sum >= byteOffset) {
 119 			printf ("%ld) ##-->%d<--\n", sum, buf [0]);
 120 			break;
 121 		}//if
 122 
 123 	}//for i
 124 
 125 	// Finish skipping crummy the header
 126 	printf ("# Finish skipping %ld %d\n", sum, byteOffset);
 127 	for (i=sum; i<byteOffset-15; i++) {
 128 		char ch = fgetc (dataF);
 129 		fputc (ch, stdout);
 130 	}//for
 131 	printf ("\n");
 132 
 133 	printf ("## Sum: %ld\n", sum);
 134 	printf ("## Offset: %s %f\n", offsetStr, offset);
 135 	printf ("## scale: %s %f\n", scalingStr, scalingFactor);
 136 	printf ("## ByteOffset: %d\n", byteOffset);
 137 
 138 	// Save the metadata scaling factor
 139 	fprintf (metaF, "Image: %s\n", headerStr);
 140 	fprintf (metaF, "## Offset: %s\n", offsetStr);
 141 	fprintf (metaF, "## scale: %s\n", scalingStr);
 142 	fprintf (metaF, "## ByteOffs e t: %d\n", byteOffset);
 143 	fclose (metaF);
 144 
 145 
 146 	unsigned short bufferA [SIZE];
 147 	i = 0;
 148 	int min = 99999;
 149 	int max = 0;
 150 	unsigned char a[2];
 151 	unsigned short val;
 152 /*		// just used to show the top of the stuff
 153 fread (bufferA, sizeof (short)*SIZE, 1, dataF);
 154 for (j=0; j<20; j++) 
 155 printf ("%u\n", bufferA [j]);
 156 exit(0);
 157 */
 158 
 159 	while ( feof (dataF) == 0) {
 160 		fread (bufferA, sizeof (short)*SIZE, 1, dataF);
 161 		for (j=0; j<SIZE; j++) {
 162 			bufferA [j] /= 2;			// Yes, this should be kept as an int
 163 			if (bufferA [j] < min) min = bufferA [j];
 164 			if (bufferA [j] > max) max = bufferA [j];
 165 		}//for
 166 		if (i % 1000 == 0)
 167 			printf ("%dk)	%u	=> %3.5f	\n", i/1000, bufferA[3], bufferA[3] * scalingFactor + offset);
 168 		fwrite (bufferA, sizeof (short)*SIZE, 1, outF);
 169 		i++;
 170 	}//while
 171 
 172 	printf ("# Short line\n");
 173 	for (j=0; j<30; j++) 
 174 		printf ("%d ", bufferA[j]);
 175 	printf ("\n");
 176 	printf ("\n");
 177 	printf ("\n");
 178 
 179 	printf ("# This values are divided by 2 to deal with unsigned\n");
 180 	printf ("Min/Max  %d/%d		%5.3f/%5.3f\n", min, max, min*scalingFactor+offset, max*scalingFactor+offset);
 181 
 182 	printf ("other side)	%u	=> %3.5f	\n", bufferA[SIZE-1], bufferA[SIZE-1] * scalingFactor + offset);
 183 	printf ("Offset bytes were %d\n", byteOffset);
 184 
 185 
 186 }//main
 187 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2019-08-07 10:43:36, 4.8 KB) [[attachment:mergePhase.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.