home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************/
- /* Copyright (C) 1989, California Institute of Technology */
- /* U. S. Government Sponsorship under NASA Contract */
- /* NAS7-918 is acknowledged. */
- /*************************************************************/
-
- /*** IMDISP module LABUTIL.C
-
- LABUTIL contains routines for processing PDS and FITS labels.
-
- ***/
-
- /* * * * INCLUDE files * * * */
-
- #include <io.h>
- #include <malloc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "imdef.h"
- #include "imdisp.h"
- #include "dispio.h"
- #include "disputil.h"
- #include "fileio.h"
- #include "refresh.h"
-
- /* * * * External functions * * * */
-
- /* * * * Function declarations * * * */
-
- int ExtractLabelString (char *, int, char *, int *, char *);
- int GetLabelInteger (char *, int, char *, int, int *, int *);
- int GetLabelLong (char *, int, char *, long, long *, int *);
- int GetLabelReal (char *, int, char *, float, float *, int *);
- int GetLabelString (char *, int, char *, char *, char *, int *);
- int InterpretLabel (char *, int, unsigned int *, int *, int *, int *,
- int *, int *, char *, char *, int);
- void DoNoLabel (char *, int, unsigned int *, int *, int *, int *, int *,
- int *, char *, int *);
- void DoFITSLabel (char *, int, unsigned int *, int *, int *, int *, int *,
- int *, int *, int);
- void DoVicarLabel(char *, int, unsigned int *, int *, int *, int *,
- int *, int *, int *);
- int PutLabel (char *, int *, char *);
- int PutLabelValue (char *, int *, char *, int);
- int MakeLabel (char *, int *, int, int, int, int);
- int GetCommand (char *,char *);
- int ExtractKeywordString (char *, char *, int *, char *);
- int GetKeywordInteger (char *,char *,int ,int *,int *);
- int GetKeywordLong (char *, char *, long, long *, int *);
- int GetKeywordReal (char *, char *, float, float *, int *);
- int GetKeywordString (char *, char *, char *, char *, int *);
- int GetKeywordSubcommand (char *, char *, int *);
-
- /* * * * Global Variables * * * */
-
- int PDSused = 0, switched = 1;
- long GiantLabel;
- int LastSearch;
- float maxlat, minlat, maxlon, minlon;
- char Object[32];
- float xstart, xinterval;
- char xunit[30];
-
-
- int ExtractLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
- int * p_flag, char * value)
-
- /* ExtractLabelString extracts the string "value" following the
- keyword in the label buffer.
-
- */
-
- {
- int i, j, k, l, m;
- char *searchptr;
-
-
- /* Find where the keyword is in the buffer */
- LabelBuf[LabelBufLen-1] = 0; /* add a null */
- searchptr = strstr (LabelBuf, Keyword);
- if (searchptr == NULL)
- *p_flag = -1;
- else
- {
- j = searchptr - LabelBuf;
- if ((j > 0) && (LabelBuf[j-1] > ' '))
- {
- /* found something like end_object */
- *p_flag = 0;
- LastSearch += j+6;
- }
- else
- {
- LastSearch += j+6;
- *p_flag = 1;
- l = LabelBufLen - 1;
- do /* scan past the keyword */
- j++;
- while ((LabelBuf[j] != ' ') && (LabelBuf[j] != '=') && (j != l));
- if (j == l) *p_flag = 0;
-
- do /* scan to the next non-blank, paren, or non equal char */
- j++;
- while ( ((LabelBuf[j] <= ' ') || (LabelBuf[j] == '=') ||
- (LabelBuf[j] == '(')) && (j < l) );
-
- k = j;
- if (LabelBuf[j] == '"')
- {
- j++; /* if value begins with a quote, read to next quote */
- do
- k++;
- while ( (LabelBuf[k] != '"') && (k <= l) );
-
- m=k+1;
- if (LabelBuf[m] == ':' || LabelBuf[m] == ',') /* check for file record offset */
- {
- do
- m++;
- while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
- strncpy(value,LabelBuf+k+2,m-k+2);
- value[m-k+2] = 0; /* add a null */
- *p_flag = atoi(value) + 1;
- }
- }
- else if (LabelBuf[j] == '\'')
- {
- j++; /* if value begins with a single quote, read to next quote */
- do
- k++;
- while ( (LabelBuf[k] != '\'') && (k <= l) );
- m=k+1;
- if (LabelBuf[m] == ':' || LabelBuf[m] == ',')
- /* check for file record offset */
- {
- do
- m++;
- while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
- strncpy(value,LabelBuf+k+2,m-k+2);
- value[m-k+2] = 0; /* add a null */
- *p_flag = atoi(value) + 1;
- }
- }
- else
- {
- do /* scan past the value to next space */
- k++;
- while ( (LabelBuf[k] > ' ') && (k <= l) );
- }
-
- strncpy (value, LabelBuf+j, k-j);
- value[k-j] = 0;
- }
- }
- }
-
-
-
- int GetLabelInteger (char * LabelBuf, int LabelBufLen, char * Keyword,
- int defaul, int * p_value, int * p_flag)
-
- /*** GetLabelInteger returns the integer value following the keyword in
- the label buffer. If the keyword is not there then the default
- value is returned. The flag indicates whether the keyword is
- there.
-
- Parameter Type In/out Description
- LabelBuf char ptr in The buffer containing the label
- LabelBufLen int in The length in bytes of the label buffer
- Keyword char ptr in The keyword string to match
- defaul int in The value to return if no keyword or value
- p_value int ptr out The returned value
- p_flag int ptr out The flag: -1 if no keyword match
- 0 if illegal value, 1 if keyword and value
-
- ***/
-
- {
- char stringvalue[80];
-
- ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
- if (*p_flag >= 1)
- {
- if (sscanf (stringvalue, "%d", p_value) == 0)
- {
- *p_flag = 0;
- *p_value = defaul;
- }
- }
- else
- {
- *p_value = defaul;
- }
- }
-
- int GetLabelLong (char * LabelBuf, int LabelBufLen, char * Keyword,
- long defaul, long * p_value, int * p_flag)
-
- /*** GetLabelInteger returns the integer value following the keyword in
- the label buffer. If the keyword is not there then the default
- value is returned. The flag indicates whether the keyword is
- there.
-
- Parameter Type In/out Description
- LabelBuf char ptr in The buffer containing the label
- LabelBufLen int in The length in bytes of the label buffer
- Keyword char ptr in The keyword string to match
- defaul long in The value to return if no keyword or value
- p_value long ptr out The returned value
- p_flag int ptr out The flag: -1 if no keyword match
- 0 if illegal value, 1 if keyword and value
-
- ***/
-
- {
- char stringvalue[80];
-
- ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
- if (*p_flag == 1)
- {
- if (sscanf (stringvalue, "%D", p_value) == 0)
- {
- *p_flag = 0;
- *p_value = defaul;
- }
- }
- else
- {
- *p_value = defaul;
- }
- }
-
-
-
-
- int GetLabelReal (char * LabelBuf, int LabelBufLen, char * Keyword,
- float defaul, float * p_value, int * p_flag)
-
- /*** GetLabelReal returns the real value following the keyword in the
- label buffer. If the keyword is not there then the default value
- is returned. The flag indicates whether the keyword is there.
-
- Parameter Type In/out Description
- LabelBuf char ptr in The buffer containing the label
- LabelBufLen int in The length in bytes of the label buffer
- Keyword char ptr in The keyword string to match
- defaul float in The value to return if no keyword or value
- p_value float ptr out The returned value
- p_flag int ptr out The flag: -1 if no keyword match
- 0 if illegal value, 1 if keyword and value
-
- ***/
-
- {
- char stringvalue[80];
-
- ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
- if (*p_flag == 1)
- {
- if (sscanf (stringvalue, "%f", p_value) == 0)
- {
- *p_flag = 0;
- *p_value = defaul;
- }
- }
- else
- {
- *p_value = defaul;
- }
- }
-
- int GetLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
- char * defaul, char * value, int * p_flag)
-
- /*** GetLabelString returns the string value following the keyword in
- the label buffer. If the keyword is not there then the default
- value is returned. The flag indicates whether the keyword is
- there.
-
- Parameter Type In/out Description
- LabelBuf char ptr in The buffer containing the label
- LabelBufLen int in The length in bytes of the label buffer
- Keyword char ptr in The keyword string to match
- defaul char ptr in The value to return if no keyword or value
- p_value char ptr out The returned value
- p_flag int ptr out The flag: -1 if no keyword match
- 0 if illegal value, 1 if keyword and value
- >1 returns record offset +1
- ***/
-
- {
- char stringvalue[255];
-
- ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
- if (*p_flag >= 1)
- {
- strcpy (value, stringvalue);
- strupr (value);
- }
- else
- {
- strcpy (value, defaul);
- }
- return(*p_flag);
- }
-
- int InterpretLabel (char * buf, int len, unsigned int * p_labelsize,
- int * p_nline, int * p_nsamp, int * p_bitsperpix,
- int * p_reclen, int * p_lineheadbytes,
- char * DetachedFileName, char * DetachedPaletteName,
- int unit)
-
- /*** InterpretLabel interprets the label in the buffer to extract the
- image information. First something is identified to determine
- whether the image has a PDS, FITS or Vicar2 label or no label.
- If the image is labeled then the appropriate keyword values are
- extracted, otherwise the user is prompted for the into.
-
- Parameter Type In/out Description
- buf char ptr in The label buffer
- len int in The length in bytes of the label buffer
- p_labelsize uns int ptr out The size of the label or header to skip
- p_nline int ptr out The number of lines in the image
- p_nsamp int ptr out The number of samples in the image
- p_bitsperpix int ptr out The pixel format (16,8,4,1)
- p_reclen int ptr out The record length in bytes
- p_lineheadbytes int ptr out The header bytes at beginning of each line
- DetachedFileName char ptr out The name of the file of data
- null string if no detached label
- DetachedPaletteName char ptr out The name of the detached color palette
- null string if no detached palette
- unit int in The number to the current file being
- processed
-
- ***/
-
- {
- char FileType[32], SampleType[32], inpstr[32];
- char ObjectName[5][32];
- int image_loc[5];
- char *object_loc;
- int images=0;
- int i, j, k, flag, irflag, labelrec, bitspixdef;
- int totalrecs, labelrecs, labelreclen;
- int headerrecs, headerreclen, nlinedef, linereclen, image_offset=1;
- int imagerecs, imagereclen, imrecldef;
- int *p_linetrailbytes;
- int done, bytecount, read_an_end, finallength;
- char *tempbufptr;
- char line[81];
- FILE *labels;
- char tmpch;
- char *count, tmpname[32];
- char colname[32];
-
- while ((p_linetrailbytes = (int *) malloc(sizeof(int)))==NULL)
- FreeRefresh("trail");
-
- /* check for variable length file */
- GetLabelString (buf,len,"RECORD_TYPE","FIXED_LENGTH",FileType,&flag);
- if (strncmp(FileType,"VARIABLE_LENGTH",15) == 0)
- {
- TextLine = TextHeight + 5; TextSample = 1;
- ClearDisplay(0);
- WriteText ("This is a compressed image.");
- WriteText (" ");
- WriteText ("Use the PCDCOMP program in the SOFTWARE directory");
- WriteText ("to decompress it, then display the uncompressed image.");
- *p_reclen = 0; *p_bitsperpix = 8;
- return(0);
- }
-
- strcpy (DetachedFileName, "");
- strcpy (DetachedPaletteName,"");
- GiantLabel = 0L;
- LastSearch = 0;
- strcpy(Object,"^");
- /* Check for multiple objects */
- do
- {
- /*** fix this to not need object + trailing blank */
- GetLabelString (buf+LastSearch, len-LastSearch, "OBJECT ",
- "", ObjectName[images], &flag);
- if (flag > 0) /* if an object = argument has been found */
- {
- strcpy(tmpname, ObjectName[images]);
- if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
- {
- /* _ not found */
- tmpch= tmpname[0];
- }
- else
- {
- /* put last keyword string into tmpname */
- strcpy(tmpname,count+1);
- tmpch= tmpname[0];
- }
-
- /* Check for objects other than IMAGE */
- switch(tmpch)
- {
- case 'I':
-
- if (tmpname[1] == 'M') /* IMAGE */
- object_loc = strstr(ObjectName[images],"IMAGE");
- else if(tmpname[1] == 'N') /* INTENSITY_SPECTRUM */
- object_loc = strstr(ObjectName[images],"INTEN");
- else
- object_loc = NULL;
- break;
-
- case 'S':
-
- object_loc = strstr(ObjectName[images],"SPECT");
- break;
-
- case 'P':
-
- object_loc = strstr(ObjectName[images],"POSIT");
- break;
-
- case 'T':
-
- if (tmpname[1] == 'A') /* TABLE */
- {
- object_loc = strstr(ObjectName[images],"TABLE");
- /* Added the next line to ignore tables for now, take out if
- you want tables to be included - 06/23/90 Ron Baalke */
- object_loc = NULL;
- }
- else if(tmpname[1] == 'E') /* TEXT */
- object_loc = strstr(ObjectName[images],"TEXT");
- else
- object_loc = NULL;
- break;
-
- case 'L':
-
- if (tmpname[1] == 'A') /* LABEL */
- /* object_loc = strstr(ObjectName[images],"LABEL"); */
- object_loc = NULL; /* don't process labels */
- else if(tmpname[1] == 'H') /* LHC_POLARIZATION_SPECTRUM */
- object_loc = strstr(ObjectName[images],"LHC_P");
- else
- object_loc = NULL;
- break;
-
- case 'R': /* RHC_POLARIZATION_SPECTRUM */
-
- object_loc = strstr(ObjectName[images],"RHC_P");
- break;
-
- default:
-
- object_loc = NULL;
- break;
- }
-
-
- if (object_loc != NULL &&
- ((i=object_loc - ObjectName[images]) ==
- (strlen(ObjectName[images])-strlen(tmpname) )) )
- {
- image_loc[images] = LastSearch;
- images++;
- }
- } /* end OBJECT found */
- } while (flag >= 0);
-
- if (images == 1) strcat(Object,ObjectName[0]);
- if (images > 1)
- {
- ClearDisplay(0);
- TextLine = TextHeight + 5; TextSample = 1;
- WriteText ("More than one image object in file.");
- for (i=0;i<images;i++)
- {
- sprintf(line," %d. %s",i+1,ObjectName[i]);
- WriteText (line);
- }
- TypeText ("Select the image to be displayed: ");
- do
- {
- AcceptText (inpstr);
- flag = sscanf (inpstr, "%d", &i);
- } while (!flag);
- i--;
- if (i < 0 || i > images) i = 0;
- strcat(Object,ObjectName[i]);
- strcpy(line,ObjectName[i]);
- strcpy(ObjectName[0],line);
- for (j=0;j<images;j++)
- {
- if (j != i) /* blank out labels for this object */
- {
- object_loc = strstr(buf+image_loc[j],"END_OBJECT");
- if (object_loc != NULL)
- for (k=image_loc[j];k<(int)(object_loc-buf);k++) buf[k] = ' ';
- }
- }
- }
-
- /* This part for PDS labels */
- GetLabelString (buf, len, "FILE_TYPE", " ", FileType, &flag);
- if (flag < 0 && Object[1] != '\0')
- {
- strcat(Object," ");
-
- /* tmpch=ObjectName[0][0]; */
- /* Multiple object fix (start) */
- strcpy(tmpname, ObjectName[0]);
- if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
- {
- /* _ not found */
- tmpch= tmpname[0];
- }
- else
- {
- /* put last keyword string into tmpname */
- strcpy(tmpname,count+1);
- tmpch= tmpname[0];
- }
- /* Multiple object fix (end) */
- GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
- if (flag < 0)
- {
- switch(tmpch)
- {
- case 'E':
- strcpy(Object,"^TABLE");
- break;
- case 'I':
-
- /* if(ObjectName[0][1] == 'M') */
- if(tmpname[1] == 'M')
- strcpy(Object,"^IMAGE"); /* if pointer not found try this */
- if(ObjectName[0][1] == 'N')
- strcpy(Object,"^INTEN");
- break;
-
- case 'S':
-
- strcpy(Object,"^SPECT");
- break;
-
- case 'P':
-
- strcpy(Object,"^POSIT");
- break;
-
- case 'T':
-
- if(ObjectName[0][1] == 'A')
- strcpy(Object,"^TABLE");
- if(ObjectName[0][1] == 'E')
- strcpy(Object,"^TEXT");
- break;
-
- case 'L':
-
- if(ObjectName[0][1] == 'A')
- strcpy(Object,"^LABEL");
- if(ObjectName[0][1] == 'H')
- strcpy(Object,"^LHC_P");
- break;
-
- case 'R':
-
- strcpy(Object,"^RHC_P");
- break;
- }
-
- GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
- }
-
- /* Switch over the various objects */
- if (flag >= 0)
- switch(tmpch)
- {
- case 'E':
- strcpy(FileType,"TABLE");
- break;
- case 'I':
-
- /* if(ObjectName[0][1] == 'M') */
- if(tmpname[1] == 'M')
- strcpy(FileType,"IMAGE");
- if(ObjectName[0][1] == 'N')
- strcpy(FileType,"INTEN");
- break;
-
- case 'S':
-
- strcpy(FileType,"SPECT");
- break;
-
- case 'P':
-
- strcpy(FileType,"POSIT");
- break;
-
- case 'T':
-
- if(ObjectName[0][1] == 'A')
- strcpy(FileType,"TABLE");
- if(ObjectName[0][1] == 'E')
- strcpy(FileType,"TEXT");
- break;
-
- case 'L':
-
- if(ObjectName[0][1] == 'A')
- strcpy(FileType,"LABEL");
- if(ObjectName[0][1] == 'H')
- strcpy(FileType,"LHC_P");
- break;
-
- case 'R':
-
- strcpy(FileType,"RHC_P");
- break;
- }
- if (flag == 0)
- {
- GetLabelString (buf, len, Object, "", DetachedFileName, &flag);
- if (flag > 1)
- image_offset = flag - 1;
- if (flag == 1)
- image_offset = 1;
- }
- }
-
- if ( strncmp( FileType, "IMAGE", 5) == 0
- || strncmp( FileType, "SPECT", 5) == 0
- || strncmp( FileType, "POSIT", 5) == 0
- || strncmp( FileType, "INTEN", 5) == 0
- || strncmp( FileType, "LHC_P", 5) == 0
- || strncmp( FileType, "RHC_P", 5) == 0
- || strncmp( FileType, "TABLE", 5) == 0
- || strncmp( FileType, "TEXT", 4) == 0
- || strncmp( FileType, "LABEL", 5) == 0 )
- {
- GetLabelInteger (buf, len, "RECORD_BYTES", 512, p_reclen, &flag);
- GetLabelInteger (buf, len, "FILE_RECORDS", 513, &totalrecs, &flag);
- GetLabelInteger (buf, len, "LABEL_RECORDS", 1, &labelrecs, &flag);
- GetLabelInteger (buf, len, "LABEL_RECORD_BYTES", *p_reclen,
- &labelreclen, &flag);
- GetLabelInteger (buf, len, "HEADER_RECORDS", 0,
- &headerrecs, &flag);
- GetLabelInteger (buf, len, "HEADER_RECORD_BYTES", *p_reclen,
- &headerreclen, &flag);
- GetLabelLong (buf, len, "^IMAGE_HISTOGRAM", 0L,
- &Histogram, &flag);
- if (Histogram != 0L) Histogram = (Histogram-1)* (long)*p_reclen;
- GetLabelLong (buf, len, "^PALETTE", 0L,&Palette, &flag);
- if (Palette != 0L) Palette = (Palette-1)* (long)*p_reclen;
- if (flag == 0)
- /* GetLabelString(buf,len,"^PALETTE","",PaletteFileName,&flag); */
- GetLabelString(buf,len,"^PALETTE","",DetachedPaletteName,&flag);
- if (Object[1] == '\0')
- {
- GetLabelInteger (buf, len, "^IMAGE ",0,&image_offset, &flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^SPECT",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^POSIT",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^INTEN",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^LHC_P",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^RHC_P",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^TABLE",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^TEXT",0,&image_offset,&flag);
- if(flag < 1)
- GetLabelInteger(buf,len,"^LABEL",0,&image_offset,&flag);
- }
-
- if (image_offset == 0)
- GiantLabel = (long)labelrecs*(long)labelreclen
- + (long)headerrecs*(long)headerreclen;
- else
- GiantLabel = (long)(image_offset-1) * (long)*p_reclen;
-
- if (GiantLabel < 65535L)
- *p_labelsize = (unsigned int)GiantLabel;
- else
- *p_labelsize = 0;
-
- /* Find nline */
- if (*p_labelsize == 0)
- nlinedef = totalrecs;
- else
- nlinedef = totalrecs - (((*p_labelsize-1) / *p_reclen) + 1);
-
- GetLabelInteger (buf, len, "IMAGE_RECORDS", nlinedef, &imagerecs, &irflag);
- GetLabelInteger (buf, len, "IMAGE_LINES", imagerecs, p_nline, &flag);
- if (flag < 1)
- GetLabelInteger (buf, len, "LINES", imagerecs,p_nline, &flag);
- if (flag < 1)
- GetLabelInteger(buf,len,"ROWS",imagerecs,p_nline,&flag);
-
- if (irflag < 1) imagerecs = *p_nline;
-
- /* Find reclen */
- GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen,p_reclen, &flag);
- if (flag < 1)
- GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_reclen,&flag);
- GetLabelInteger (buf, len, "RECORD_BYTES", *p_reclen, p_reclen, &flag);
- imrecldef = *p_reclen * (*p_nline / imagerecs);
- GetLabelInteger (buf, len, "IMAGE_RECORD_BYTES", imrecldef,
- &imagereclen, &flag);
- *p_reclen = imagereclen/ (*p_nline /imagerecs);
-
- /* Find nsamp */
- GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen, p_nsamp, &flag);
- if (flag < 1)
- GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_nsamp,&flag);
-
- /* Find bitsperpix */
- bitspixdef = (8L* (*p_reclen)) / *p_nsamp;
- if (bitspixdef <= 1)
- bitspixdef = 1;
- else
- if (bitspixdef <= 4)
- bitspixdef = 4;
-
- /* removed next line, AEE 4-21-89
- else
- bitspixdef = 8;
- */
- GetLabelInteger (buf, len, "SAMPLE_BITS", bitspixdef,
- p_bitsperpix, &flag);
- if (flag < 1)
- GetLabelInteger(buf,len,"COLUMN_BITS",bitspixdef,p_bitsperpix,&flag);
-
- GetLabelString(buf,len,"COLUMN_NAME"," ",colname,&flag);
- if (strncmp(colname,"(WAVELENGTH",11)==0
- || strncmp(colname,"(POSITION",9)==0)
- {
- switched= 0;
- ByteSwap= TRUE;
- }
- else
- {
- switched= 1;
- ByteSwap= FALSE;
- }
-
- GetLabelInteger (buf, len, "LINE_PREFIX_BYTES", 0,
- p_lineheadbytes, &flag);
- GetLabelInteger (buf, len, "LINE_SUFFIX_BYTES", 0,
- p_linetrailbytes, &flag);
- if (p_lineheadbytes != 0 || p_linetrailbytes != 0)
- *p_reclen = (((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1)
- +(long)*p_lineheadbytes + (long)*p_linetrailbytes;
-
- /* Check for non-byte swapped sample value */
- GetLabelString (buf, len, "SAMPLE_TYPE", "LSB_INTEGER",
- SampleType, &flag);
- if (flag < 1)
- GetLabelString(buf,len,"COLUMN_TYPE","LSB_INTEGER",SampleType,&flag);
- if (flag > 0)
- {
- if (strncmp(SampleType,"MSB_INTEGER",11) == 0) ByteSwap = TRUE;
- if (strncmp(SampleType,"UNSIGNED_INTEGER",11) == 0) ByteSwap = TRUE;
- if (strncmp(SampleType,"VAX_INTEGER",11) == 0) ByteSwap = FALSE;
- if (strncmp(SampleType,"LSB_INTEGER",11) == 0) ByteSwap = FALSE;
- }
-
- /* Get plot information for X-axis */
-
- GetLabelReal( buf, len, "START_WAVELENGTH", 0.0, &xstart, &flag);
- GetLabelReal( buf, len, "WAVELENGTH_INTERVAL", 1.0, &xinterval, &flag);
- GetLabelString( buf, len, "WAVELENGTH_UNIT", "PIXEL", xunit, &flag);
-
- /*
- GetLabelReal(buf,len,"START_",1.0,&xstart,&flag);
- if(flag >= 1)
- {
- sprintf(xlabel,"%.12s",strstr(buf,"START_")+6);
- strtok(xlabel," ");
-
- strcpy(intervaltoken,xlabel);
- strcat(intervaltoken,"_INTERVAL");
-
- strcpy(unittoken,xlabel);
- strcat(unittoken,"_UNIT");
- GetLabelReal(buf,len,intervaltoken,1.0,&xinterval,&flag);
- GetLabelString(buf,len,unittoken,"",xunit,&flag);
- }
- */
- /* Find maxlat and lon */
- GetLabelReal(buf, len, "MAXIMUM_LATITUDE", 0.0, &maxlat, &flag);
- GetLabelReal(buf, len, "MINIMUM_LATITUDE", 0.0, &minlat, &flag);
- GetLabelReal(buf, len, "MAXIMUM_LONGITUDE", 0.0, &maxlon, &flag);
- GetLabelReal(buf, len, "MINIMUM_LONGITUDE", 0.0, &minlon, &flag);
- flag = 0;
- if (strlen(DetachedFileName) == 0)
- GetLabelString (buf, len, "IMAGE_POINTER", "",
- DetachedFileName, &flag);
- if (flag > 0)
- {
- GiantLabel = (long)headerrecs*(long)headerreclen;
- if (GiantLabel < 65535L)
- *p_labelsize = (unsigned int)GiantLabel;
- else
- *p_labelsize = 0;
- }
- if (strlen(DetachedPaletteName) == 0)
- GetLabelString(buf, len, "PALETTE_POINTER", "",
- DetachedPaletteName, &flag);
-
- free(p_linetrailbytes);
- PDSused= 1;
- return;
- }
-
- /* This part for VICAR2 labels */
- GetLabelString (buf, len, "TYPE", " ", FileType, &flag);
- if ( stricmp (FileType, "IMAGE") == 0 )
- {
- DoVicarLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
- p_reclen, p_lineheadbytes, p_linetrailbytes);
- return;
- }
-
- /* This part for FITS labels */
- GetLabelString (buf, len, "SIMPLE", " ", FileType, &flag);
- if ( (stricmp (FileType, "T") == 0) ||
- (stricmp (FileType, "F") == 0) ) /* this is a FITS file */
- {
- DoFITSLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
- p_reclen, p_lineheadbytes, p_linetrailbytes, unit);
-
- PDSused= 0;
- return;
- }
-
- /* Do this if no recognized label */
- DoNoLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
- p_reclen, p_lineheadbytes, FileType, p_linetrailbytes);
- }
-
-
-
- void DoVicarLabel (char * buf, int len, unsigned int * p_labelsize,
- int * p_nline, int * p_nsamp, int * p_bitsperpix,
- int * p_reclen, int * p_lineheadbytes, int * p_linetrailbytes)
-
- /*** DoVicarLabel interprets the Vicar label in the buffer to extract the
- image information.
-
- Parameter Type In/out Description
- buf char ptr in The label buffer
- len int in The length in bytes of the label buffer
- p_labelsize uns int ptr out The size of the label or header to skip
- p_nline int ptr out The number of lines in the image
- p_nsamp int ptr out The number of samples in the image
- p_bitsperpix int ptr out The pixel format (16,8,4,1)
- p_reclen int ptr out The record length in bytes
- p_lineheadbytes int ptr out The header bytes at beginning of each line
- p_linetrailbytes int ptr in The number of bytes at the end of each line
-
- ***/
- {
- char format[16];
- int flag;
-
- GetLabelInteger (buf, len, "LBLSIZE", 0, p_labelsize, &flag);
- GetLabelInteger (buf, len, "NL", 1, p_nline, &flag);
- GetLabelInteger (buf, len, "NS", 1, p_nsamp, &flag);
- GetLabelString (buf, len, "FORMAT", "BYTE", format, &flag);
- if (format[0] == 'H')
- *p_bitsperpix = 16;
- else
- *p_bitsperpix = 8;
-
- *p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
- GetLabelInteger (buf, len, "RECSIZE", *p_reclen, p_reclen, &flag);
- *p_lineheadbytes = 0;
-
- free(p_linetrailbytes);
- }
-
-
- void DoFITSLabel (char * buf, int len, unsigned int * p_labelsize, int * p_nline,
- int * p_nsamp, int * p_bitsperpix, int * p_reclen,
- int * p_lineheadbytes, int * p_linetrailbytes, int unit)
-
- /*** DoFITSLabel interprets the FITS label in the buffer to extract the
- image information.
-
- Parameter Type In/out Description
- buf char ptr in The label buffer
- len int in The length in bytes of the label buffer
- p_labelsize uns int ptr out The size of the label or header to skip
- p_nline int ptr out The number of lines in the image
- p_nsamp int ptr out The number of samples in the image
- p_bitsperpix int ptr out The pixel format (16,8,4,1)
- p_reclen int ptr out The record length in bytes
- p_lineheadbytes int ptr out The header bytes at beginning of each line
- p_linetrailbytes int ptr in The number of bytes at the end of each line
- unit int in The number to the current file being
- processed
-
- ***/
- {
-
- int flag, naxis, bytecount, i;
- char line[81];
-
- ByteSwap = TRUE;
- GetLabelInteger (buf, len, "NAXIS",0, &naxis, &flag);
- if (naxis == 1)
- {
- GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
- GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
- *p_nline = 1;
- }
- else if (naxis == 2)
- {
- GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
- GetLabelInteger (buf, len, "NAXIS2", 1, p_nline, &flag);
- GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
- }
- else /* naxis 0 or greater than 2, can't display */
- {
- *p_nline = 0;
- *p_nsamp = 0;
- *p_bitsperpix = 16;
- ClearDisplay(0);
- StatusLine(0,"No lines in image, or too many axes.");
- }
-
- *p_reclen = ((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
- *p_lineheadbytes = 0;
-
- /** added by DIN /revised - mdm/ to read END in FITS header **/
-
- bytecount = 0;
- i = 0; /* changed from i= 1 to i= 0 to handle END when it is in the
- last line of label. AEE, 4-4-89 */
-
- /* labels = fdopen(FCB[unit].handle,"r"); */
- lseek(FCB[unit].handle,0L,SEEK_SET);
- do
- {
- read(FCB[unit].handle,line,80);
- ++i;
- } while (strnicmp(line,"END",3) != 0);
-
- bytecount = i*80;
- i = bytecount/2880;
- if (bytecount % 2880 != 0) i++;
- *p_labelsize = i*2880;
- free(p_linetrailbytes);
- }
-
-
- void DoNoLabel(char * buf, int len, unsigned int * p_labelsize,
- int * p_nline, int * p_nsamp, int * p_bitsperpix,
- int * p_reclen, int * p_lineheadbytes, char * FileType,
- int * p_linetrailbytes)
-
- /*** DoNoLabel prompts the user to enter the image parameters.
- PDS-format compressed images are handled (skipped) here, too.
-
- Parameter Type In/out Description
- buf char ptr in The label buffer
- len int in The length in bytes of the label buffer
- p_labelsize uns int ptr out The size of the label or header to skip
- p_nline int ptr out The number of lines in the image
- p_nsamp int ptr out The number of samples in the image
- p_bitsperpix int ptr out The pixel format (16,8,4,1)
- p_reclen int ptr out The record length in bytes
- p_lineheadbytes int ptr out The header bytes at beginning of each line
- FileType char ptr in The type of the file of data (from FILE_TYPE)
- p_linetrailbytes int ptr in The number of bytes at the end of each line
-
- ***/
- {
-
- int flag;
- char inpstr[32];
-
- *p_bitsperpix = 8;
- *p_labelsize = 0;
-
- if (OneScreen)
- {
- ClearDisplay(0);
- TextLine = TextHeight + 5; TextSample = 1;
- TypeText ("Image file does not have a proper label.");
- TextLine += TextHeight + 5; TextSample = 1;
- TypeText ("Input number of lines : ");
- AcceptText (inpstr);
- sscanf (inpstr, "%d", p_nline);
- TextLine += TextHeight + 5; TextSample = 1;
- TypeText ("Input number of samples : ");
- AcceptText (inpstr);
- sscanf (inpstr, "%d", p_nsamp);
- TextLine += TextHeight + 5; TextSample = 1;
- TypeText ("Input bits per pixel : ");
- AcceptText (inpstr);
- sscanf (inpstr, "%d", p_bitsperpix);
- TextLine += TextHeight + 5; TextSample = 1;
- TypeText ("Input number of header bytes : ");
- AcceptText (inpstr);
- sscanf (inpstr, "%d", p_labelsize);
- ClearDisplay(0);
- }
- else
- {
- printf ("%s", "Image file does not have a proper label.\n");
- printf ("%s", "Input number of lines : ");
- gets (inpstr);
- sscanf (inpstr, "%d", p_nline);
-
- printf ("%s", "Input number of samples : ");
- gets (inpstr);
- sscanf (inpstr, "%d", p_nsamp);
-
- printf ("%s", "Input bits per pixel : ");
- gets (inpstr);
- sscanf (inpstr, "%d", p_bitsperpix);
-
- printf ("%s", "Input number of header bytes : ");
- gets (inpstr);
- sscanf (inpstr, "%d", p_labelsize);
- }
- *p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1 )/8 + 1;
- *p_lineheadbytes = 0;
-
- free(p_linetrailbytes);
- }
-
- int PutLabel (char * LabelBuf, int * p_pos, char * LabelString)
-
- {
- int i;
-
- for (i = 0; LabelString[i] > 0; i++)
- LabelBuf[(*p_pos)++] = LabelString[i];
- LabelBuf[(*p_pos)++] = 13;
- LabelBuf[(*p_pos)++] = 10;
- }
-
-
- int PutLabelValue (char * LabelBuf, int * p_pos, char * LabelString, int value)
-
- {
- char temp1[128], temp2[32];
-
- strcpy (temp1, LabelString);
- sprintf (temp2, "%d", value);
- strcat (temp1, temp2);
- PutLabel (LabelBuf, p_pos, temp1);
- }
-
-
-
-
- int MakeLabel (char * Buf, int * p_labelsize, int nline, int nsamp,
- int bitsperpix, int reclen)
-
- /*** MakeLabel creates a PDS label in the buffer, having the
- appropriate image parameters.
-
- Parameter Type In/out Description
- Buf char ptr out The label buffer
- p_labelsize int ptr out The size of the label
- nline int out The number of lines in the image
- nsamp int out The number of samples in the image
- bitsperpix int out The pixel format (16,8,4,1)
- reclen int out The record length in bytes
- ***/
-
- {
- int j, labelrecs, totalrecs;
- long int filesize;
- char labstr[128];
-
-
- labelrecs = (2047/reclen) + 1;
- *p_labelsize = reclen*labelrecs;
- totalrecs = nline + labelrecs;
- filesize = (long)reclen*(long)totalrecs - 20;
-
- j = 0;
-
- sprintf (labstr, "NJPL1I00PDS1%08ld = SFDU_LABEL",
- filesize);
- PutLabel (Buf, &j, labstr);
- PutLabel (Buf, &j, "RECORD_TYPE = FIXED_LENGTH");
- PutLabelValue (Buf, &j, "RECORD_BYTES = ", reclen);
- PutLabelValue (Buf, &j, "FILE_RECORDS = ", totalrecs);
- PutLabelValue (Buf, &j, "LABEL_RECORDS = ", labelrecs);
- PutLabelValue (Buf, &j, "^IMAGE = ", labelrecs+1);
- PutLabel (Buf, &j, "OBJECT = IMAGE");
- PutLabelValue (Buf, &j, " LINES = ", nline);
- PutLabelValue (Buf, &j, " LINE_SAMPLES = ", nsamp);
- PutLabelValue (Buf, &j, " SAMPLE_BITS = ", bitsperpix);
- PutLabel (Buf, &j, "END_OBJECT");
- PutLabel (Buf, &j, "END");
-
- while (j < *p_labelsize)
- Buf[j++] = ' ';
- }
-
-
- int GetCommand (char * CommandString, char * CommandList)
-
- /*** GetCommand is an integer function that returns the number of the
- command that is in the beginning of the CommandString parameter.
- The command is assigned a number according to the list of commands
- in the CommandList string parameter. The list of commands should
- be in uppercase and have spaces between the commmand.
-
- ***/
-
- {
- int i, j, k, l, n;
- char List[NCOMMANDS][32], Word[32], temp[32]; /* Max # of commands */
-
- /* Parse the command list string up into a list of strings */
- n = 0;
- l = strlen(CommandList) - 1;
- k = 0;
- do
- {
- j = k;
- while ( (CommandList[j] == ' ') && (j < l) )
- j++;
- if (j == l) break;
- k = j;
- do
- k++;
- while ( (CommandList[k] != ' ') && (k<=l) );
- strncpy (List[n], &CommandList[j], k-j);
- List[n][k-j] = 0;
- n++;
- } while (k <= l);
-
-
- /* Get the first word from the command string */
- l = strlen(CommandString);
- j = 0;
- while ( (CommandString[j] == ' ') && (j < l) )
- j++;
- k = j;
- do
- k++;
- while ( (CommandString[k] != ' ') && (k<=l) );
- strncpy (Word, &CommandString[j], k-j);
- Word[k-j] = 0;
-
- /* Search through the list of commands to find a match */
- i = n-1;
- do
- {
- strcpy (temp, Word);
- temp[strlen(List[i])] = 0;
- } while ( (stricmp(temp, List[i]) != 0) && (--i >= 0) );
-
- return (i+1);
- }
-
- int ExtractKeywordString (char * CommandString, char * Keyword, int * p_flag,
- char * value)
-
- /* ExtractKeywordString extracts the string following the keyword from
- the CommandString.
- flag=-1 if there is no keyword match.
- flag= 0 if keyword with no value.
- flag= 1 if keyword and value string.
-
- */
-
- {
- int i, j, k, l;
- char *searchptr;
-
-
- /* Find where the keyword is in the buffer */
- searchptr = strstr (CommandString, Keyword);
- j = searchptr - CommandString;
-
- if ( (searchptr == NULL) || ((j > 0) && (CommandString[j-1] > ' ')) )
- *p_flag = -1;
- else
- {
- *p_flag = 1;
- l = strlen(CommandString) - 1;
- do /* scan past the keyword */
- j++;
- while ( (CommandString[j] != ' ') &&
- (CommandString[j] != '=') && (j != l) );
-
- if (j == l) *p_flag = 0;
-
- do /* scan to the next non blank and non equal char */
- j++;
- while ( ( (CommandString[j] <= ' ') ||
- (CommandString[j] == '=') ) && (j < l) );
-
-
- k = j;
- if (CommandString[j] == '"')
- {
- j++; /* if value begins with a quote, read to next quote */
- do
- k++;
- while ( (CommandString[k] != '"') && (k <= l) );
- }
- else
- {
- do /* scan past the value to next space */
- k++;
- while ( (CommandString[k] > ' ') && (k <= l) );
- }
-
- strncpy (value, CommandString+j, k-j);
- value[k-j] = 0;
- }
-
- }
-
- int GetKeywordInteger (char * CommandString, char * Keyword, int defaul,
- int * p_value, int * p_flag)
-
- /*** GetKeywordInteger scans the CommandString for the Keyword and
- returns the value of the following integer.
-
- Parameter Type In/out Description
- CommandString char ptr in The command string to scan thru
- Keyword char ptr in The keyword string to match
- defaul int in The default value to return if
- no keyword or illegal value
- p_value int ptr out The returned value
- p_flag int ptr out The flag: -1 for no keyword match
- 0 for keyword with no/bad value
- 1 for keyword and good value
-
- ***/
-
- {
- char stringvalue[80];
-
- ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
- if (*p_flag == 1)
- {
- if (sscanf (stringvalue, "%d", p_value) == 0)
- {
- *p_flag = 0;
- *p_value = defaul;
- }
- }
- else
- {
- *p_value = defaul;
- }
- }
-
- int GetKeywordLong (char * CommandString, char * Keyword, long defaul,
- long * p_value, int * p_flag)
-
- /*** GetKeywordInteger scans the CommandString for the Keyword and
- returns the value of the following integer.
-
- Parameter Type In/out Description
- CommandString char ptr in The command string to scan thru
- Keyword char ptr in The keyword string to match
- defaul long in The default value to return if
- no keyword or illegal value
- p_value long ptr out The returned value
- p_flag int ptr out The flag: -1 for no keyword match
- 0 for keyword with no/bad value
- 1 for keyword and good value
-
- ***/
-
- {
- char stringvalue[80];
-
- ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
- if (*p_flag == 1)
- {
- if (sscanf (stringvalue, "%ld", p_value) == 0)
- {
- *p_flag = 0;
- *p_value = defaul;
- }
- }
- else
- {
- *p_value = defaul;
- }
- }
-
- int GetKeywordReal (char * CommandString, char * Keyword, float defaul,
- float * p_value, int * p_flag)
-
- /*** GetKeywordReal scans the CommandString for the Keyword and returns
- the value of the following real.
-
- Parameter Type In/out Description
- CommandString char ptr in The command string to scan thru
- Keyword char ptr in The keyword string to match
- defaul float in The default value to return if
- no keyword or illegal value
- p_value float ptr out The returned value
- p_flag int ptr out The flag: -1 for no keyword match
- 0 for keyword with no/bad value
- 1 for keyword and good value
-
- ***/
- {
- char stringvalue[80];
-
- ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
- if (*p_flag == 1)
- {
- if (sscanf (stringvalue, "%f", p_value) == 0)
- {
- *p_flag = 0;
- *p_value = defaul;
- }
- }
- else
- {
- *p_value = defaul;
- }
- }
-
- int GetKeywordString (char * CommandString, char * Keyword, char * defaul,
- char * value, int * p_flag)
-
- /*** GetKeywordString scans the CommandString for the Keyword
- and returns the value of the following string.
-
- Parameter Type In/out Description
- CommandString char ptr in The command string to scan thru
- Keyword char ptr in The keyword string to match
- defaul char ptr in The default value to return if
- no keyword or illegal value
- value char ptr out The returned value
- p_flag int ptr out The flag: -1 for no keyword match
- 0 for keyword with no/bad value
- 1 for keyword and good value
-
- ***/
-
- {
- char stringvalue[255];
-
- ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
- if (*p_flag == 1)
- {
- strcpy (value, stringvalue);
- }
- else
- {
- strcpy (value, defaul);
- }
- }
-
-
- int GetKeywordSubcommand (char * CommandString, char * Keyword, int * p_flag)
-
- /*** GetKeywordSubcommand scans the CommandString for the Keyword
- and returns the value of the following string.
-
- Parameter Type In/out Description
- CommandString char ptr in The command string to scan thru
- Keyword char ptr in The keyword string to match
- p_flag int ptr out The flag: -1 for no keyword match
- 1 for keyword found
-
- ***/
-
- {
- int j;
- char *searchptr;
-
-
- /* Find where the keyword is in the buffer */
- searchptr = strstr (CommandString, Keyword);
- j = searchptr - CommandString;
-
- if ( (searchptr == NULL) || ((j > 0) && (CommandString[j-1] > ' ')) )
- *p_flag = -1;
- else
- {
- *p_flag = 1;
- }
- }