home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / imdisp79.zip / LABUTIL.C < prev    next >
C/C++ Source or Header  |  1993-09-03  |  59KB  |  1,658 lines

  1. /***  IMDISP module LABUTIL.C
  2.  
  3.         LABUTIL contains routines for processing PDS and FITS labels.
  4.  
  5. ***/
  6.  
  7. #define __MSC
  8.  
  9. /* * * * INCLUDE files * * * */
  10.  
  11. #include <io.h>
  12. #include <malloc.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "mshell.h"
  17. #include "imdef.h"
  18. #include "imdisp.h"
  19. #include "imdutil.h"
  20. #include "dispio.h"
  21. #include "disputil.h"
  22. #include "fileio.h"
  23. #include "keywutil.h"
  24. #include "textutil.h"
  25. #include "display.h"
  26.  
  27. /* * * * External functions * * * */
  28.  
  29. /* * * * Function declarations * * * */
  30.  
  31. int ExtractLabelString (char *, int, char *, int *, char *);
  32. int GetLabelInteger (char *, int, char *, int, int *, int *);
  33. int GetLabelIntegerN (char *, int, char *, int, int, int *, int *);
  34. int GetLabelLong (char *, int, char *, long, long *, int *);
  35. int GetLabelLongN (char *, int, char *, long, int, long *, int *);
  36. int GetLabelReal (char *, int, char *, float, float *, int *);
  37. int GetLabelRealN (char *, int, char *, float, int, float *, int *);
  38. int GetLabelString (char *, int, char *, char *, char *, int *);
  39. int GetLabelStringN (char *, int, char *, char *, int, char * [], int *);
  40. int InterpretLabel (char *,int,unsigned int *,int *,int *,int *,int *,int *,char *,char *,int);
  41. void DoNoLabel (char *,int,unsigned int *,int *,int *,int *,int *,int *,char *,int *);
  42. void DoFITSLabel (char *,int,unsigned int *,int *,int *,int *,int *,int *,int *,int);
  43. void DoVicarLabel(char *,int,unsigned int *,int *,int *,int *,int *,int *,int *);
  44. int PutLabel (char *, int *, char *);
  45. int PutLabelValue (char *, int *, char *, int);
  46. int MakeLabel (char *, int *, int, int, int, int);
  47. int GetCommand (char *,char *);
  48.  
  49. /* * * * Global Variables * * * */
  50.  
  51. int   PDSused = 0, switched = 1;
  52. long  GiantLabel;
  53. int   LastSearch;
  54. float maxlat, minlat, maxlon, minlon;
  55. char  Object[32];
  56.  
  57.  
  58. int ExtractLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
  59.                         int * p_flag, char * value)
  60.  
  61. /*  ExtractLabelString extracts the string "value" following the
  62.     keyword in the label buffer.
  63.  
  64. */
  65.  
  66. {
  67.     int i, j, k, l, m;
  68.     char  *searchptr;
  69.  
  70.  
  71.     /* Find where the keyword is in the buffer */
  72.     LabelBuf[LabelBufLen-1] = 0; /* add a null */
  73.     searchptr = strstr (LabelBuf, Keyword);
  74.     if (searchptr == NULL)
  75.        *p_flag = -1;
  76.     else
  77.     {
  78.        j = searchptr - LabelBuf;
  79.        if ((j > 0) && (LabelBuf[j-1] > ' '))
  80.        {
  81.        /* found something like end_object */
  82.            *p_flag = 0;
  83.            LastSearch += j+6;
  84.        }
  85.        else
  86.        {
  87.            LastSearch += j+6;
  88.            *p_flag = 1;
  89.            l = LabelBufLen - 1;
  90.            do         /* scan past the keyword */
  91.                j++;
  92.            while ((LabelBuf[j] != ' ') && (LabelBuf[j] != '=') && (j != l));
  93.            if (j == l)  *p_flag = 0;
  94.  
  95.            do    /* scan to the next non-blank, paren, or non equal char */
  96.                j++;
  97.            while ( ((LabelBuf[j] <= ' ') || (LabelBuf[j] == '=') ||
  98.                     (LabelBuf[j] == '(')) && (j < l) );
  99.  
  100.            k = j;
  101.            if (LabelBuf[j] == '"')
  102.            {
  103.                j++;     /* if value begins with a quote, read to next quote */
  104.                do
  105.                     k++;
  106.                while ( (LabelBuf[k] != '"') && (k <= l) );
  107.  
  108.                m=k+1;
  109.                if (LabelBuf[m] == ':' || LabelBuf[m] == ',') /* check for file record offset */
  110.                {
  111.                    do
  112.                        m++;
  113.                    while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
  114.                    strncpy(value,LabelBuf+k+2,m-k+2);
  115.                    value[m-k+2] = 0;      /* add a null */
  116.                    *p_flag = atoi(value) + 1;
  117.                }
  118.            }
  119.            else if (LabelBuf[j] == '\'')
  120.            {
  121.                j++;  /* if value begins with a single quote, read to next quote */
  122.                do
  123.                    k++;
  124.                while ( (LabelBuf[k] != '\'') && (k <= l) );
  125.                m=k+1;
  126.                if (LabelBuf[m] == ':' || LabelBuf[m] == ',')
  127.                /* check for file record offset */
  128.                {
  129.                    do
  130.                        m++;
  131.                    while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
  132.                    strncpy(value,LabelBuf+k+2,m-k+2);
  133.                    value[m-k+2] = 0;      /* add a null */
  134.                    *p_flag = atoi(value) + 1;
  135.                }
  136.            }
  137.            else
  138.            {
  139.                do               /* scan past the value to next space */
  140.                    k++;
  141.                while ( (LabelBuf[k] > ' ') && (k <= l) );
  142.            }
  143.  
  144.            strncpy (value, LabelBuf+j, k-j);
  145.            value[k-j] = 0;
  146.        }
  147.     }
  148. }
  149.  
  150. int GetLabelInteger (char * LabelBuf, int LabelBufLen, char * Keyword,
  151.                      int defaul, int * p_value, int * p_flag)
  152.  
  153. /***  GetLabelInteger returns the integer value following the keyword in
  154.       the label buffer.  If the keyword is not there then the default
  155.       value is returned.  The flag indicates whether the keyword is
  156.       there.
  157.  
  158.    Parameter    Type    In/out   Description
  159.     LabelBuf  char ptr   in      The buffer containing the label
  160.     LabelBufLen  int     in      The length in bytes of the label buffer
  161.     Keyword   char ptr   in      The keyword string to match
  162.     defaul       int     in      The value to return if no keyword or value
  163.     p_value    int ptr   out     The returned value
  164.     p_flag     int ptr   out     The flag: -1 if no keyword match
  165.                                    0 if illegal value, 1 if keyword and value
  166.  
  167. ***/
  168.  
  169. {   int    i;
  170.     char   stringvalue[80];
  171.  
  172.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  173.     if (*p_flag >= 1)
  174.     {
  175.        /* new patch to eliminate GALILEO SSI file names which begin */
  176.        /* with numbers from being interpreted as image offsets.     */
  177.        /* If there is any non-numeric in the string it is rejected. */
  178.        /* MDM 1991-11-14 */
  179.        for (i=0;i<strlen(stringvalue);i++)
  180.          if (stringvalue[i] < 48 || stringvalue[i] > 57)
  181.           {
  182.             *p_flag  = 0;
  183.             *p_value = defaul;
  184.             return(0);
  185.            }
  186.         if (sscanf (stringvalue, "%d", p_value) == 0)
  187.         {
  188.             *p_flag  = 0;
  189.             *p_value = defaul;
  190.         }
  191.     }
  192.     else
  193.     {
  194.         *p_value = defaul;
  195.     }
  196. }
  197.  
  198. int GetLabelIntegerN (char * LabelBuf, int LabelBufLen, char * Keyword,
  199.                      int defaul, int naxes, int p_value[], int * p_flag)
  200.  
  201. /***  GetLabelIntegerN returns integer values following the keyword in
  202.       the label buffer.  If the keyword is not there then the default
  203.       values are returned.  The flag indicates whether the keyword is
  204.       there.
  205.  
  206.    Parameter    Type    In/out   Description
  207.     LabelBuf  char ptr   in      The buffer containing the label
  208.     LabelBufLen  int     in      The length in bytes of the label buffer
  209.     Keyword   char ptr   in      The keyword string to match
  210.     defaul       int     in      The value to return if no keyword or value
  211.     naxes        int     in      The number of values to return
  212.     p_value[]  int ptr   out     The array of returned values
  213.     p_flag     int ptr   out     The flag: -1 if no keyword match
  214.                                    0 if illegal value, 1 if keyword and value
  215.  
  216. ***/
  217.  
  218. {   int    i;
  219.     char   stringvalue[80];
  220.     char   *tmp;
  221.  
  222.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  223.     if (*p_flag >= 1)
  224.     {
  225.        tmp = strtok(stringvalue, ",)" );
  226.        p_value[0] = atoi(tmp);
  227.  
  228.        for (i=1; i<naxes; i++)
  229.        {
  230.            tmp = strtok(NULL, ",)" );
  231.            if ((p_value[i] = atoi(tmp)) == 0)
  232.            {
  233.                *p_flag  = 0;
  234.                p_value[i] = defaul;
  235.            }
  236.        }
  237.     }
  238.     else
  239.     {
  240.        for (i=0; i<naxes; i++)
  241.        {
  242.            p_value[i] = defaul;
  243.        }
  244.     }
  245. }
  246.  
  247. int GetLabelLong (char * LabelBuf, int LabelBufLen, char * Keyword,
  248.                   long defaul, long * p_value, int * p_flag)
  249.  
  250. /***  GetLabelInteger returns the integer value following the keyword in
  251.       the label buffer.  If the keyword is not there then the default
  252.       value is returned.  The flag indicates whether the keyword is
  253.       there.
  254.  
  255.    Parameter    Type    In/out   Description
  256.     LabelBuf  char ptr   in      The buffer containing the label
  257.     LabelBufLen  int     in      The length in bytes of the label buffer
  258.     Keyword   char ptr   in      The keyword string to match
  259.     defaul       long    in      The value to return if no keyword or value
  260.     p_value    long ptr  out     The returned value
  261.     p_flag     int  ptr  out     The flag: -1 if no keyword match
  262.                                    0 if illegal value, 1 if keyword and value
  263.  
  264. ***/
  265.  
  266. {
  267.     char   stringvalue[80];
  268.  
  269.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  270.     if (*p_flag == 1)
  271.     {
  272.         if (sscanf (stringvalue, "%D", p_value) == 0)
  273.         {
  274.             *p_flag = 0;
  275.             *p_value = defaul;
  276.         }
  277.     }
  278.     else
  279.     {
  280.         *p_value = defaul;
  281.     }
  282. }
  283.  
  284. int GetLabelLongN (char * LabelBuf, int LabelBufLen, char * Keyword,
  285.                      long defaul, int naxes, long p_value[], int * p_flag)
  286.  
  287. /***  GetLabelIntegerN returns integer values following the keyword in
  288.       the label buffer.  If the keyword is not there then the default
  289.       values are returned.  The flag indicates whether the keyword is
  290.       there.
  291.  
  292.    Parameter    Type    In/out   Description
  293.     LabelBuf  char ptr   in      The buffer containing the label
  294.     LabelBufLen  int     in      The length in bytes of the label buffer
  295.     Keyword   char ptr   in      The keyword string to match
  296.     defaul      long     in      The value to return if no keyword or value
  297.     naxes        int     in      The number of values to return
  298.     p_value[] long ptr   out     The array of returned values
  299.     p_flag     int ptr   out     The flag: -1 if no keyword match
  300.                                    0 if illegal value, 1 if keyword and value
  301.  
  302. ***/
  303.  
  304. {   int    i;
  305.     char   stringvalue[80];
  306.     char   *tmp;
  307.  
  308.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  309.     if (*p_flag >= 1)
  310.     {
  311.        tmp = strtok(stringvalue, ",)" );
  312.        p_value[0] = atol(tmp);
  313.  
  314.        for (i=1; i<naxes; i++)
  315.        {
  316.            tmp = strtok(NULL, ",)" );
  317.            if ((p_value[i] = atol(tmp)) == 0)
  318.            {
  319.                *p_flag  = 0;
  320.                p_value[i] = defaul;
  321.            }
  322.        }
  323.     }
  324.     else
  325.     {
  326.        for (i=0; i<naxes; i++)
  327.        {
  328.            p_value[i] = defaul;
  329.        }
  330.     }
  331. }
  332.  
  333. int GetLabelReal (char * LabelBuf, int LabelBufLen, char * Keyword,
  334.                   float defaul, float * p_value, int * p_flag)
  335.  
  336. /***  GetLabelReal returns the real value following the keyword in the
  337.       label buffer.  If the keyword is not there then the default value
  338.       is returned.  The flag indicates whether the keyword is there.
  339.  
  340.    Parameter    Type    In/out   Description
  341.     LabelBuf  char ptr   in      The buffer containing the label
  342.     LabelBufLen  int     in      The length in bytes of the label buffer
  343.     Keyword   char ptr   in      The keyword string to match
  344.     defaul      float    in      The value to return if no keyword or value
  345.     p_value   float ptr  out     The returned value
  346.     p_flag     int ptr   out     The flag: -1 if no keyword match
  347.                                    0 if illegal value, 1 if keyword and value
  348.  
  349. ***/
  350.  
  351. {
  352.     char   stringvalue[80];
  353.  
  354.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  355.     if (*p_flag == 1)
  356.     {
  357.         if (sscanf (stringvalue, "%f", p_value) == 0)
  358.         {
  359.             *p_flag = 0;
  360.             *p_value = defaul;
  361.         }
  362.     }
  363.     else
  364.     {
  365.         *p_value = defaul;
  366.     }
  367. }
  368.  
  369. int GetLabelRealN (char * LabelBuf, int LabelBufLen, char * Keyword,
  370.                   float defaul, int naxes, float * p_value, int * p_flag)
  371.  
  372. /***  GetLabelReal returns the real value following the keyword in the
  373.       label buffer.  If the keyword is not there then the default value
  374.       is returned.  The flag indicates whether the keyword is there.
  375.  
  376.    Parameter    Type    In/out   Description
  377.     LabelBuf  char ptr   in      The buffer containing the label
  378.     LabelBufLen  int     in      The length in bytes of the label buffer
  379.     Keyword   char ptr   in      The keyword string to match
  380.     defaul      float    in      The value to return if no keyword or value
  381.     naxes        int     in      The number of values to return
  382.     p_value   float ptr  out     The returned value
  383.     p_flag     int ptr   out     The flag: -1 if no keyword match
  384.                                    0 if illegal value, 1 if keyword and value
  385.  
  386. ***/
  387.  
  388. {   int    i;
  389.     char   stringvalue[80];
  390.     char   *tmp;
  391.  
  392.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  393.     if (*p_flag >= 1)
  394.     {
  395.        tmp = strtok(stringvalue, ",)" );
  396.        p_value[0] = atof(tmp);
  397.  
  398.        for (i=1; i<naxes; i++)
  399.        {
  400.            tmp = strtok(NULL, ",)" );
  401.            if ((p_value[i] = atof(tmp)) == 0)
  402.            {
  403.                *p_flag  = 0;
  404.                p_value[i] = defaul;
  405.            }
  406.        }
  407.     }
  408.     else
  409.     {
  410.        for (i=0; i<naxes; i++)
  411.        {
  412.            p_value[i] = defaul;
  413.        }
  414.     }
  415. }
  416.  
  417. int GetLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
  418.                     char * defaul, char * value, int * p_flag)
  419.  
  420. /***  GetLabelString returns the string value following the keyword in
  421.       the label buffer.  If the keyword is not there then the default
  422.       value is returned.  The flag indicates whether the keyword is
  423.       there.
  424.  
  425.    Parameter    Type    In/out   Description
  426.     LabelBuf  char ptr   in      The buffer containing the label
  427.     LabelBufLen  int     in      The length in bytes of the label buffer
  428.     Keyword   char ptr   in      The keyword string to match
  429.     defaul    char ptr   in      The value to return if no keyword or value
  430.     p_value   char ptr   out     The returned value
  431.     p_flag     int ptr   out     The flag: -1 if no keyword match
  432.                                    0 if illegal value, 1 if keyword and value
  433.                                    >1 returns record offset +1
  434. ***/
  435.  
  436. {
  437.     char   stringvalue[255];
  438.  
  439.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  440.     if (*p_flag >= 1)
  441.     {
  442.         strcpy (value, stringvalue);
  443.         strupr (value);
  444.     }
  445.     else
  446.     {
  447.         strcpy (value, defaul);
  448.     }
  449.   return(*p_flag);
  450. }
  451.  
  452. int GetLabelStringN (char * LabelBuf, int LabelBufLen, char * Keyword,
  453.                     char * defaul, int naxes, char * p_value[], int * p_flag)
  454.  
  455. /***  GetLabelStringN returns the string value following the keyword in
  456.       the label buffer.  If the keyword is not there then the default
  457.       value is returned.  The flag indicates whether the keyword is
  458.       there.
  459.  
  460.    Parameter    Type    In/out   Description
  461.     LabelBuf  char ptr   in      The buffer containing the label
  462.     LabelBufLen  int     in      The length in bytes of the label buffer
  463.     Keyword   char ptr   in      The keyword string to match
  464.     defaul    char ptr   in      The value to return if no keyword or value
  465.     naxes        int     in      The number of values to return
  466.     p_value  char ptr [] out     The array of pointers to the returned values
  467.     p_flag     int ptr   out     The flag: -1 if no keyword match
  468.                                    0 if illegal value, 1 if keyword and value
  469.                                    >1 returns record offset +1
  470. ***/
  471.  
  472. {   int    i;
  473.     char   stringvalue[255];
  474.     char    *tmp;
  475.  
  476.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  477.     if (*p_flag >= 1)
  478.     {
  479.        tmp = strtok(stringvalue, ",)" );
  480.        strcpy (p_value[0], tmp);
  481.        strupr (p_value[0]);
  482.  
  483.        for (i=1; i<naxes; i++)
  484.        {
  485.            tmp = strtok(NULL, ",)" );
  486.            if (strcpy(p_value[i], tmp) == NULL)
  487.            {
  488.                *p_flag  = 0;
  489.                strcpy(p_value[i], defaul);
  490.            }
  491.        }
  492.     }
  493.     else
  494.     {
  495.        for (i=0; i<naxes; i++)
  496.        {
  497.            strcpy(p_value[i], defaul);
  498.        }
  499.     }
  500.   return(*p_flag);
  501. }
  502.  
  503. int InterpretLabel (char * buf, int len, unsigned int * p_labelsize,
  504.                     int * p_nline, int * p_nsamp, int * p_bitsperpix,
  505.                     int * p_reclen, int * p_lineheadbytes,
  506.                     char * DetachedFileName, char * DetachedPaletteName,
  507.                     int unit)
  508.  
  509. /***  InterpretLabel interprets the label in the buffer to extract the
  510.       image information.  First something is identified to determine
  511.       whether the image has a PDS, FITS or Vicar2 label or no label.
  512.       If the image is labeled then the appropriate keyword values are
  513.       extracted, otherwise the user is prompted for the into.
  514.  
  515.    Parameter         Type    In/out  Description
  516.     buf             char ptr   in    The label buffer
  517.     len               int      in    The length in bytes of the label buffer
  518.   p_labelsize      uns int ptr out   The size of the label or header to skip
  519.   p_nline            int ptr   out   The number of lines in the image
  520.   p_nsamp            int ptr   out   The number of samples in the image
  521.   p_bitsperpix       int ptr   out   The pixel format (16,8,4,1)
  522.   p_reclen           int ptr   out   The record length in bytes
  523.  p_lineheadbytes     int ptr   out   The header bytes at beginning of each line
  524. DetachedFileName    char ptr   out   The name of the file of data
  525.                                          null string if no detached label
  526. DetachedPaletteName char ptr   out   The name of the detached color palette
  527.                                          null string if no detached palette
  528.    unit               int      in    The number to the current file being
  529.                                          processed
  530.  
  531. ***/
  532.  
  533. {
  534.     char    FileType[32], SampleType[32], inpstr[32];
  535.     char    ObjectName[5][32];
  536.     int     image_loc[5];
  537.     char    *object_loc;
  538.     int     images=0, Detached;
  539.     int     i, j, k, flag, irflag, labelrec, bitspixdef;
  540.     int     totalrecs, labelrecs, labelreclen;
  541.     int     headerrecs, headerreclen, nlinedef, linereclen, image_offset=1;
  542.     int     imagerecs, imagereclen, imrecldef;
  543.     int     *p_linetrailbytes;
  544.     int     done, bytecount, read_an_end, finallength;
  545.     char    *tempbufptr;
  546.     char    line[81];
  547.     FILE    *labels;
  548.     char    tmpch;
  549.     char    *count, tmpname[32];
  550.     char    colname[32];
  551.  
  552.     int     naxes, items[10], item_bytes[10];
  553. /*    char    item_type[10][33], parm_name[10][33], parm_unit[10][33]; */
  554.     char    *item_type[10], *parm_name[10], *parm_unit[10];
  555.     float   parm_interval[10], parm_min[10];
  556.     float   dn_max, dn_min, dn_offset, dn_scaling;
  557.     char    dn_name[33];
  558.  
  559.     strcpy (Source_File_Name, "");
  560.  
  561.     if ((p_linetrailbytes = (int *) malloc(sizeof(int)))==NULL)
  562.     {
  563.         FatalError( "Not enough memory for trail.\n");
  564.     }
  565.  
  566. /* check for variable length file */
  567.     GetLabelString (buf,len,"RECORD_TYPE","FIXED_LENGTH",FileType,&flag);
  568.     if (strncmp(FileType,"VARIABLE_LENGTH",15) == 0)
  569.     {
  570.        TextLine = TextHeight + 5;  TextSample = 1;
  571.        ClearDisplay(0);
  572.        WriteText ("This is a compressed image.");
  573.        WriteText (" ");
  574.        WriteText ("Use the PCDCOMP program in the SOFTWARE directory");
  575.        WriteText ("to decompress it, then display the uncompressed image.");
  576.        *p_reclen = 0; *p_bitsperpix = 8;
  577.        free(p_linetrailbytes);
  578.        return(0);
  579.     }
  580.  
  581.     strcpy (DetachedFileName, "");
  582.     strcpy (DetachedPaletteName,"");
  583.     GiantLabel = 0L;
  584.     LastSearch = 0;
  585.     strcpy(Object,"^");
  586. /* Check for multiple objects */
  587.     do
  588.     {
  589.     /*** fix this to not need object + trailing blank */
  590.        GetLabelString (buf+LastSearch, len-LastSearch, "OBJECT ",
  591.                        "", ObjectName[images], &flag);
  592.        if (flag > 0)            /* if an object = argument has been found */
  593.        {
  594.           strcpy(tmpname, ObjectName[images]);
  595.           if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
  596.           {
  597.           /* _ not found */
  598.                tmpch= tmpname[0];
  599.           }
  600.           else
  601.           {
  602.           /* put last keyword string into tmpname */
  603.               strcpy(tmpname,count+1);
  604.               tmpch= tmpname[0];
  605.           }
  606.  
  607.           /* Check for objects other than IMAGE */
  608.           switch(tmpch)
  609.           {
  610.           case 'I':
  611.  
  612.               if     (tmpname[1] == 'M')  /* IMAGE */
  613.                   object_loc = strstr(ObjectName[images],"IMAGE");
  614.               else if(tmpname[1] == 'N')  /* INTENSITY_SPECTRUM */
  615.                   object_loc = strstr(ObjectName[images],"INTEN");
  616.               else
  617.                   object_loc = NULL;
  618.               break;
  619.  
  620.           case 'S':
  621.  
  622.               object_loc = strstr(ObjectName[images],"SPECT");
  623.               break;
  624. /*
  625.           case 'A':
  626.  
  627.               object_loc = strstr(ObjectName[images],"ARRAY");
  628.               break;
  629. */
  630.           case 'P':
  631.  
  632.               object_loc = strstr(ObjectName[images],"POSIT");
  633.               break;
  634.  
  635.          case 'T':
  636.  
  637.               if   (tmpname[1] == 'A')  /* TABLE */
  638.               {
  639.                   object_loc = strstr(ObjectName[images],"TABLE");
  640.               /* Added the next line to ignore tables for now, take out if
  641.                  you want tables to be included  - 06/23/90 Ron Baalke   */
  642.                   object_loc = NULL;
  643.               }
  644.               else if(tmpname[1] == 'E')  /* TEXT */
  645.                   object_loc = strstr(ObjectName[images],"TEXT");
  646.               else
  647.                   object_loc = NULL;
  648.               break;
  649.  
  650.           case 'L':
  651.  
  652.               if     (tmpname[1] == 'A')  /* LABEL */
  653. /*                  object_loc = strstr(ObjectName[images],"LABEL"); */
  654.                   object_loc = NULL; /* don't process labels */
  655.               else if(tmpname[1] == 'H') /* LHC_POLARIZATION_SPECTRUM */
  656.                   object_loc = strstr(ObjectName[images],"LHC_P");
  657.               else
  658.                   object_loc = NULL;
  659.               break;
  660.  
  661.           case 'R':  /* RHC_POLARIZATION_SPECTRUM */
  662.  
  663.               object_loc = strstr(ObjectName[images],"RHC_P");
  664.               break;
  665.  
  666.           default:
  667.  
  668.               object_loc = NULL;
  669.               break;
  670.           }
  671.  
  672.  
  673.           if (object_loc != NULL &&
  674.              ((i=object_loc - ObjectName[images]) ==
  675.              (strlen(ObjectName[images])-strlen(tmpname) )) )
  676.           {
  677.               image_loc[images] = LastSearch;
  678.               images++;
  679.           }
  680.        }  /* end OBJECT found */
  681.     } while (flag >= 0);
  682.  
  683.     if (images == 1) strcat(Object,ObjectName[0]);
  684.     if (images > 1)
  685.     {
  686.        ClearDisplay(0);
  687.        TextLine = TextHeight + 5;  TextSample = 1;
  688.        WriteText ("More than one image object in file.");
  689.        for (i=0;i<images;i++)
  690.        {
  691.            sprintf(line,"  %d. %s",i+1,ObjectName[i]);
  692.            WriteText (line);
  693.        }
  694.        TypeText ("Select the image to be displayed: ");
  695.        do
  696.        {
  697.            AcceptText (inpstr);
  698.            flag = sscanf (inpstr, "%d", &i);
  699.        } while (!flag);
  700.        i--;
  701.        if (i < 0 || i > images) i = 0;
  702.        strcat(Object,ObjectName[i]);
  703.        strcpy(line,ObjectName[i]);
  704.        strcpy(ObjectName[0],line);
  705.        for (j=0;j<images;j++)
  706.        {
  707.            if (j != i) /* blank out labels for this object */
  708.            {
  709.                object_loc = strstr(buf+image_loc[j],"END_OBJECT");
  710.                if (object_loc != NULL)
  711.                    for (k=image_loc[j];k<(int)(object_loc-buf);k++) buf[k] = ' ';
  712.             }
  713.        }
  714.     }
  715.  
  716. /* This part for PDS labels */
  717.     GetLabelString (buf, len, "FILE_TYPE", " ", FileType, &flag);
  718.     if (flag < 0 && Object[1] != '\0')
  719.     {
  720.        strcat(Object," ");
  721.  
  722. /*       tmpch=ObjectName[0][0]; */
  723. /*  Multiple object fix (start) */
  724.           strcpy(tmpname, ObjectName[0]);
  725.           if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
  726.           {
  727.           /* _ not found */
  728.                tmpch= tmpname[0];
  729.           }
  730.           else
  731.           {
  732.           /* put last keyword string into tmpname */
  733.               strcpy(tmpname,count+1);
  734.               tmpch= tmpname[0];
  735.           }
  736. /*  Multiple object fix (end) */
  737.        GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
  738.        if (flag < 0)
  739.        {
  740.            switch(tmpch)
  741.            {
  742.            case 'E':
  743.  
  744.                strcpy(Object,"^TABLE");
  745.                break;
  746.  
  747.            case 'I':
  748.  
  749.                if(tmpname[1] == 'M')
  750.                    strcpy(Object,"^IMAGE"); /* if pointer not found try this */
  751.                if(ObjectName[0][1] == 'N')
  752.                    strcpy(Object,"^INTEN");
  753.                break;
  754.  
  755.            case 'S':
  756.  
  757.                strcpy(Object,"^SPECT");
  758.                break;
  759.  
  760.            case 'A':
  761.  
  762.                strcpy(Object,"^ARRAY");
  763.                break;
  764.  
  765.            case 'P':
  766.  
  767.                strcpy(Object,"^POSIT");
  768.                break;
  769.  
  770.            case 'T':
  771.  
  772.                if(ObjectName[0][1] == 'A')
  773.                    strcpy(Object,"^TABLE");
  774.                if(ObjectName[0][1] == 'E')
  775.                    strcpy(Object,"^TEXT");
  776.                break;
  777.  
  778.            case 'L':
  779.  
  780.                if(ObjectName[0][1] == 'A')
  781.                    strcpy(Object,"^LABEL");
  782.                if(ObjectName[0][1] == 'H')
  783.                    strcpy(Object,"^LHC_P");
  784.                break;
  785.  
  786.            case 'R':
  787.  
  788.                strcpy(Object,"^RHC_P");
  789.                break;
  790.            }
  791.  
  792.            GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
  793.        }
  794.  
  795.        /* Switch over the various objects */
  796.        if (flag >= 0)
  797.        switch(tmpch)
  798.        {
  799.            case 'E':
  800.  
  801.                strcpy(FileType,"TABLE");
  802.                break;
  803.  
  804.            case 'I':
  805.  
  806.                if(tmpname[1] == 'M')
  807.                    strcpy(FileType,"IMAGE");
  808.                if(ObjectName[0][1] == 'N')
  809.                    strcpy(FileType,"INTEN");
  810.                break;
  811.  
  812.            case 'S':
  813.  
  814.                strcpy(FileType,"SPECT");
  815.                break;
  816.  
  817.            case 'A':
  818.  
  819.                strcpy(FileType,"ARRAY");
  820.                break;
  821.  
  822.            case 'P':
  823.  
  824.                strcpy(FileType,"POSIT");
  825.                break;
  826.  
  827.            case 'T':
  828.  
  829.                if(ObjectName[0][1] == 'A')
  830.                    strcpy(FileType,"TABLE");
  831.                if(ObjectName[0][1] == 'E')
  832.                    strcpy(FileType,"TEXT");
  833.                break;
  834.  
  835.            case 'L':
  836.  
  837.                if(ObjectName[0][1] == 'A')
  838.                    strcpy(FileType,"LABEL");
  839.                if(ObjectName[0][1] == 'H')
  840.                    strcpy(FileType,"LHC_P");
  841.                break;
  842.  
  843.            case 'R':
  844.  
  845.                strcpy(FileType,"RHC_P");
  846.                break;
  847.        }
  848.        if (flag == 0)
  849.        {
  850.            Detached = TRUE;
  851.            GetLabelString (buf, len, Object, "", DetachedFileName, &flag);
  852.            if (flag >  1)
  853.                image_offset = flag - 1;
  854.            if (flag == 1)
  855.                image_offset = 1;
  856.        }
  857.     }
  858.  
  859.     if ( strncmp( FileType, "IMAGE", 5) == 0
  860. /*      || strncmp( FileType, "SPECT", 5) == 0 */
  861.       || strncmp( FileType, "POSIT", 5) == 0
  862.       || strncmp( FileType, "INTEN", 5) == 0
  863.       || strncmp( FileType, "LHC_P", 5) == 0
  864.       || strncmp( FileType, "RHC_P", 5) == 0
  865.       || strncmp( FileType, "TABLE", 5) == 0
  866.       || strncmp( FileType, "TEXT",  4) == 0
  867.       || strncmp( FileType, "LABEL", 5) == 0 )
  868.     {
  869.        GetLabelInteger (buf, len, "RECORD_BYTES", 512, p_reclen, &flag);
  870.        GetLabelInteger (buf, len, "FILE_RECORDS", 513, &totalrecs, &flag);
  871.        GetLabelInteger (buf, len, "LABEL_RECORDS", 1, &labelrecs, &flag);
  872.        GetLabelInteger (buf, len, "LABEL_RECORD_BYTES", *p_reclen,
  873.                          &labelreclen, &flag);
  874.        GetLabelInteger (buf, len, "HEADER_RECORDS", 0,
  875.                          &headerrecs, &flag);
  876.        if (flag == 1)
  877.        {
  878.            if (Detached) labelrecs = 0;
  879.            GetLabelInteger (buf, len, "HEADER_RECORD_BYTES", *p_reclen,
  880.                          &headerreclen, &flag);
  881.            if (flag == 1)
  882.                image_offset = 0;
  883.        }
  884.        else
  885.        {
  886.            /*  Patch for pbm+ portable graymap files from USGS */
  887.            GetLabelInteger (buf, len, "HEADER", *p_reclen,
  888.                          &headerreclen, &flag);
  889.            if (flag == 1)
  890.            {
  891.                image_offset = 0;
  892.                headerrecs = 1;
  893.                if (Detached) labelrecs = 0;
  894.            }
  895.        }
  896.  
  897.        GetLabelLong    (buf, len, "^IMAGE_HISTOGRAM", 0L,
  898.                          &Histogram, &flag);
  899.        if (Histogram != 0L) Histogram = (Histogram-1)* (long)*p_reclen;
  900.        GetLabelLong    (buf, len, "^PALETTE", 0L,&Palette, &flag);
  901.        if (Palette != 0L) Palette = (Palette-1)* (long)*p_reclen;
  902.        if (flag == 0)
  903. /*         GetLabelString(buf,len,"^PALETTE","",PaletteFileName,&flag); */
  904.          GetLabelString(buf,len,"^PALETTE","",DetachedPaletteName,&flag);
  905.        if (Object[1] == '\0')
  906.        {
  907.            GetLabelInteger (buf, len, "^IMAGE ",0,&image_offset, &flag);
  908.            if(flag < 1)
  909.                GetLabelInteger(buf,len,"^SPECT",0,&image_offset,&flag);
  910.            if(flag < 1)
  911.                GetLabelInteger(buf,len,"^POSIT",0,&image_offset,&flag);
  912.            if(flag < 1)
  913.                GetLabelInteger(buf,len,"^INTEN",0,&image_offset,&flag);
  914.            if(flag < 1)
  915.                GetLabelInteger(buf,len,"^LHC_P",0,&image_offset,&flag);
  916.            if(flag < 1)
  917.                GetLabelInteger(buf,len,"^RHC_P",0,&image_offset,&flag);
  918.            if(flag < 1)
  919.                GetLabelInteger(buf,len,"^TABLE",0,&image_offset,&flag);
  920.            if(flag < 1)
  921.                GetLabelInteger(buf,len,"^TEXT",0,&image_offset,&flag);
  922.            if(flag < 1)
  923.                GetLabelInteger(buf,len,"^LABEL",0,&image_offset,&flag);
  924.        }
  925.  
  926.        if (image_offset == 0)
  927.            GiantLabel  = (long)labelrecs*(long)labelreclen
  928.                        + (long)headerrecs*(long)headerreclen;
  929.        else
  930.            GiantLabel  = (long)(image_offset-1) * (long)*p_reclen;
  931.  
  932.        if (GiantLabel < 65535L)
  933.            *p_labelsize = (unsigned int)GiantLabel;
  934.        else
  935.            *p_labelsize = 0;
  936.  
  937.     /* Find nline */
  938.        if (*p_labelsize == 0)
  939.            nlinedef = totalrecs;
  940.        else
  941.            nlinedef = totalrecs - (((*p_labelsize-1) / *p_reclen) + 1);
  942.  
  943.        GetLabelInteger (buf, len, "IMAGE_RECORDS", nlinedef, &imagerecs, &irflag);
  944.        GetLabelInteger (buf, len, "IMAGE_LINES", imagerecs, p_nline, &flag);
  945.        if (flag < 1)
  946.            GetLabelInteger (buf, len, "LINES", imagerecs,p_nline, &flag);
  947.        if (flag < 1)
  948.            GetLabelInteger(buf,len,"ROWS",imagerecs,p_nline,&flag);
  949.  
  950.        if (irflag < 1)  imagerecs = *p_nline;
  951.  
  952.     /* Find reclen */
  953.        GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen,p_reclen, &flag);
  954.        if (flag < 1)
  955.            GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_reclen,&flag);
  956.        GetLabelInteger (buf, len, "RECORD_BYTES", *p_reclen, p_reclen, &flag);
  957.        imrecldef = *p_reclen * (*p_nline / imagerecs);
  958.        GetLabelInteger (buf, len, "IMAGE_RECORD_BYTES", imrecldef,
  959.                          &imagereclen, &flag);
  960.        *p_reclen = imagereclen/ (*p_nline /imagerecs);
  961.  
  962.     /* Find nsamp */
  963.        GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen, p_nsamp, &flag);
  964.        if (flag < 1)
  965.            GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_nsamp,&flag);
  966.  
  967.     /* patch for magellan cdrom MG_0001 version 2 browse images */
  968.        if (*p_nsamp == 896 && *p_nline == 1024)
  969.         {
  970.           GetLabelString(buf,len,"SPACECRAFT_NAME","",line,&flag);
  971.           if (flag > 0 && (strncmp(line,"MAGELLAN",8) == 0))
  972.             /* switch line and sample values */
  973.             *p_nsamp = 1024; *p_nline = 896;
  974.         }
  975.  
  976.           GetLabelString(buf,len,"SOURCE_FILE_NAME","",
  977.                          Source_File_Name,&flag);
  978.           GetLabelInteger(buf,len,"SAMPLING_FACTOR",1,&Sampling_Factor,&flag);
  979.  
  980.           GetLabelString(buf,len,"DATA_SET_ID","",line,&flag);
  981.           if (flag > 0)
  982.             {
  983.             if (strncmp(line,"MGN-V-RDRS-5-MIDR-FULL-RES",26) == 0 &&
  984.                 strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
  985.                 strcpy(Source_File_Name,"FFXX.LBL");
  986.             else if (strncmp(line,"MGN-V-RDRS-5-MIDR-C1",20) == 0 &&
  987.                 strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
  988.                 strcpy(Source_File_Name,"C1FXX.LBL");
  989.             else if (strncmp(line,"MGN-V-RDRS-5-MIDR-C2",20) == 0 &&
  990.                 strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
  991.                 strcpy(Source_File_Name,"C2FXX.LBL");
  992.             else if (strncmp(line,"MGN-V-RDRS-5-MIDR-C3",20) == 0 &&
  993.                 strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
  994.                 strcpy(Source_File_Name,"C3FXX.LBL");
  995.             else if (strncmp(line,"VO1/VO2-M-VIS-5-DIM",19) == 0)
  996.                 {
  997.                  GetLabelString(buf,len,"IMAGE_ID","",
  998.                          Source_File_Name,&flag);
  999.                  strcpy(line,"\\");
  1000.                  strncat(line,Source_File_Name,5);
  1001.                  line[2] = 'I';
  1002.                  strcat(line,"XXX");
  1003.                  strcat(line,"\\");
  1004.                  strncat(line,Source_File_Name,8);
  1005.                  line[11] = 'I';
  1006.                  strcat(line,".IMG");
  1007.                  strcpy(Source_File_Name,line);
  1008.                  Sampling_Factor = 4;
  1009.                 }
  1010.             }
  1011.     /* Find bitsperpix */
  1012.        bitspixdef      = (8L* (*p_reclen)) / *p_nsamp;
  1013.        if (bitspixdef <= 1)
  1014.            bitspixdef = 1;
  1015.        else
  1016.            if (bitspixdef <= 4)
  1017.                bitspixdef = 4;
  1018.  
  1019. /*  removed next line, AEE 4-21-89
  1020.            else
  1021.                bitspixdef = 8;
  1022. */
  1023.        GetLabelInteger (buf, len, "SAMPLE_BITS", bitspixdef,
  1024.                         p_bitsperpix, &flag);
  1025.        if (flag < 1)
  1026.            GetLabelInteger(buf,len,"COLUMN_BITS",bitspixdef,p_bitsperpix,&flag);
  1027.  
  1028.        GetLabelString(buf,len,"COLUMN_NAME"," ",colname,&flag);
  1029.        if (strncmp(colname,"(WAVELENGTH",11)==0
  1030.         || strncmp(colname,"(POSITION",9)==0)
  1031.        {
  1032.            switched= 0;
  1033.            ByteSwap= TRUE;
  1034.        }
  1035.        else
  1036.        {
  1037.            switched= 1;
  1038.            ByteSwap= FALSE;
  1039.        }
  1040.  
  1041.        GetLabelInteger (buf, len, "LINE_PREFIX_BYTES", 0,
  1042.                          p_lineheadbytes, &flag);
  1043.        GetLabelInteger (buf, len, "LINE_SUFFIX_BYTES", 0,
  1044.                          p_linetrailbytes, &flag);
  1045.        if (p_lineheadbytes != 0 || p_linetrailbytes != 0)
  1046.            *p_reclen = (((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1)
  1047.                      +(long)*p_lineheadbytes + (long)*p_linetrailbytes;
  1048.  
  1049.     /* Check for non-byte swapped sample value */
  1050.        GetLabelString (buf, len, "SAMPLE_TYPE", "LSB_INTEGER",
  1051.             SampleType, &flag);
  1052.        if (flag < 1)
  1053.            GetLabelString(buf,len,"COLUMN_TYPE","LSB_INTEGER",SampleType,&flag);
  1054.        if (flag > 0)
  1055.        {
  1056.            if (strncmp(SampleType,"MSB_INTEGER",11) == 0)      ByteSwap = TRUE;
  1057.            if (strncmp(SampleType,"UNSIGNED_INTEGER",16) == 0) ByteSwap = TRUE;
  1058.            if (strncmp(SampleType,"VAX_INTEGER",11) == 0)      ByteSwap = FALSE;
  1059.            if (strncmp(SampleType,"LSB_INTEGER",11) == 0)      ByteSwap = FALSE;
  1060.        }
  1061.  
  1062. /* Get plot information for X-axis */
  1063.  
  1064.        xflag = FALSE;
  1065.        GetLabelReal( buf, len, "START_WAVELENGTH", 0.0, &xstart, &flag);
  1066.        if (flag > 0)
  1067.            xflag = TRUE;
  1068.        GetLabelReal( buf, len, "WAVELENGTH_INTERVAL", 1.0, &xinterval, &flag);
  1069.        GetLabelString( buf, len, "WAVELENGTH_UNIT", "PIXEL", xunit, &flag);
  1070.  
  1071. /* Get DN scaling information -- Look for PDS-style first, FITS next */
  1072.        GetLabelReal( buf, len, "SCALING_FACTOR", 1.0, &ThisImage.DNscale, &flag);
  1073.        GetLabelReal( buf, len, "OFFSET", 1.0, &ThisImage.DNoffset, &flag);
  1074.  
  1075. /* Find maxlat and lon */
  1076.        GetLabelReal(buf, len, "MAXIMUM_LATITUDE", 0.0, &maxlat, &flag);
  1077.        GetLabelReal(buf, len, "MINIMUM_LATITUDE", 0.0, &minlat, &flag);
  1078.        GetLabelReal(buf, len, "MAXIMUM_LONGITUDE", 0.0, &maxlon, &flag);
  1079.        GetLabelReal(buf, len, "MINIMUM_LONGITUDE", 0.0, &minlon, &flag);
  1080.        flag = 0;
  1081.        if (strlen(DetachedFileName) == 0)
  1082.            GetLabelString (buf, len, "IMAGE_POINTER", "",
  1083.                     DetachedFileName, &flag);
  1084.        if (flag > 0)
  1085.        {
  1086.            GiantLabel = (long)headerrecs*(long)headerreclen;
  1087.            if (GiantLabel < 65535L)
  1088.                *p_labelsize = (unsigned int)GiantLabel;
  1089.            else
  1090.                *p_labelsize = 0;
  1091.        }
  1092.        if (strlen(DetachedPaletteName) == 0)
  1093.            GetLabelString(buf, len, "PALETTE_POINTER", "",
  1094.                     DetachedPaletteName, &flag);
  1095.  
  1096.        free(p_linetrailbytes);
  1097.        PDSused= 1;
  1098.        return(0);
  1099.     }
  1100.     else if ((strncmp( FileType, "ARRAY", 5) == 0 ) ||
  1101.              (strncmp( FileType, "SPECT", 5) == 0 ))
  1102.     {
  1103.        GetLabelInteger (buf, len, "RECORD_BYTES", 512, p_reclen, &flag);
  1104.        GetLabelInteger (buf, len, "FILE_RECORDS", 513, &totalrecs, &flag);
  1105.        GetLabelInteger (buf, len, "LABEL_RECORDS", 1, &labelrecs, &flag);
  1106.        GetLabelInteger (buf, len, "LABEL_RECORD_BYTES", *p_reclen,
  1107.                          &labelreclen, &flag);
  1108.        GetLabelInteger (buf, len, "HEADER_RECORDS", 0,
  1109.                          &headerrecs, &flag);
  1110.        GetLabelInteger (buf, len, "HEADER_RECORD_BYTES", *p_reclen,
  1111.                          &headerreclen, &flag);
  1112. /*  Patch for USGS portable graymap files w/ 17 byte headers */
  1113.        if (flag == 0)
  1114.           GetLabelInteger (buf, len, "HEADER", *p_reclen,
  1115.                          &headerreclen, &flag);
  1116.        if (Object[1] == '\0')
  1117.        {
  1118.            GetLabelInteger(buf,len,"^SPECT",0,&image_offset,&flag);
  1119.        }
  1120.  
  1121.        if (image_offset == 0)
  1122.            GiantLabel  = (long)labelrecs*(long)labelreclen
  1123.                        + (long)headerrecs*(long)headerreclen;
  1124.        else
  1125.            GiantLabel  = (long)(image_offset-1) * (long)*p_reclen;
  1126.  
  1127.        if (GiantLabel < 65535L)
  1128.            *p_labelsize = (unsigned int)GiantLabel;
  1129.        else
  1130.            *p_labelsize = 0;
  1131.  
  1132.     /* Find nline */
  1133.        if (*p_labelsize == 0)
  1134.            nlinedef = totalrecs;
  1135.        else
  1136.            nlinedef = totalrecs - (((*p_labelsize-1) / *p_reclen) + 1);
  1137.  
  1138.        GetLabelInteger (buf, len, "AXES", 1, &naxes, &flag);
  1139.        GetLabelReal (buf, len, "DERIVED_MAXIMUM", 0.0, &dn_max, &flag);
  1140.        GetLabelReal (buf, len, "DERIVED_MINIMUM", 0.0, &dn_min, &flag);
  1141.        GetLabelReal (buf, len, "OFFSET", 0.0, &dn_offset, &flag);
  1142.        GetLabelReal (buf, len, "SCALING_FACTOR", 1.0, &dn_scaling, &flag);
  1143.        GetLabelString (buf, len, "NAME", "DN", dn_name, &flag);
  1144.        GetLabelIntegerN (buf, len, "ITEMS", 1, naxes, items, &flag);
  1145.        GetLabelIntegerN (buf, len, "ITEM_BYTES", 1, naxes, item_bytes, &flag);
  1146.        GetLabelStringN (buf, len, "ITEM_TYPE", "", 1, item_type, &flag);
  1147.        GetLabelStringN (buf, len, "SAMPLING_PARAMETER_NAME", "", 1, parm_name,&flag);
  1148.        GetLabelStringN (buf, len, "SAMPLING_PARAMETER_UNIT", "", 1, parm_unit,&flag);
  1149.        GetLabelReal (buf, len, "SAMPLING_PARAMETER_INTERVAL", 1, &parm_interval[0],&flag);
  1150.        GetLabelReal (buf, len, "MINIMUM_SAMPLING_PARAMETER", 1, &parm_min[0],&flag);
  1151.  
  1152.        GetLabelInteger (buf, len, "IMAGE_RECORDS", nlinedef, &imagerecs, &irflag);
  1153.        GetLabelInteger (buf, len, "IMAGE_LINES", imagerecs, p_nline, &flag);
  1154.        if (flag < 1)
  1155.            GetLabelInteger (buf, len, "LINES", imagerecs,p_nline, &flag);
  1156.        if (flag < 1)
  1157.            GetLabelInteger(buf,len,"ROWS",imagerecs,p_nline,&flag);
  1158.  
  1159.        if (irflag < 1)  imagerecs = *p_nline;
  1160.  
  1161.     /* Find reclen */
  1162.        GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen,p_reclen, &flag);
  1163.        if (flag < 1)
  1164.            GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_reclen,&flag);
  1165.        GetLabelInteger (buf, len, "RECORD_BYTES", *p_reclen, p_reclen, &flag);
  1166.        imrecldef = *p_reclen * (*p_nline / imagerecs);
  1167.        GetLabelInteger (buf, len, "IMAGE_RECORD_BYTES", imrecldef,
  1168.                          &imagereclen, &flag);
  1169.        *p_reclen = imagereclen/ (*p_nline /imagerecs);
  1170.  
  1171.     /* Find nsamp */
  1172.        GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen, p_nsamp, &flag);
  1173.        if (flag < 1)
  1174.            GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_nsamp,&flag);
  1175.  
  1176.           GetLabelString(buf,len,"SOURCE_FILE_NAME","",
  1177.                          Source_File_Name,&flag);
  1178.           GetLabelInteger(buf,len,"SAMPLING_FACTOR",1,&Sampling_Factor,&flag);
  1179.  
  1180.           GetLabelString(buf,len,"DATA_SET_ID","",line,&flag);
  1181.     /* Find bitsperpix */
  1182.        bitspixdef      = (8L* (*p_reclen)) / *p_nsamp;
  1183.        if (bitspixdef <= 1)
  1184.            bitspixdef = 1;
  1185.        else
  1186.            if (bitspixdef <= 4)
  1187.                bitspixdef = 4;
  1188.  
  1189.        GetLabelInteger (buf, len, "SAMPLE_BITS", bitspixdef,
  1190.                         p_bitsperpix, &flag);
  1191.        if (flag < 1)
  1192.            GetLabelInteger(buf,len,"COLUMN_BITS",bitspixdef,p_bitsperpix,&flag);
  1193.  
  1194.        GetLabelString(buf,len,"COLUMN_NAME"," ",colname,&flag);
  1195.        if (strncmp(colname,"(WAVELENGTH",11)==0
  1196.         || strncmp(colname,"(POSITION",9)==0)
  1197.        {
  1198.            switched= 0;
  1199.            ByteSwap= TRUE;
  1200.        }
  1201.        else
  1202.        {
  1203.            switched= 1;
  1204.            ByteSwap= FALSE;
  1205.        }
  1206.  
  1207.        GetLabelInteger (buf, len, "LINE_PREFIX_BYTES", 0,
  1208.                          p_lineheadbytes, &flag);
  1209.        GetLabelInteger (buf, len, "LINE_SUFFIX_BYTES", 0,
  1210.                          p_linetrailbytes, &flag);
  1211.        if (p_lineheadbytes != 0 || p_linetrailbytes != 0)
  1212.            *p_reclen = (((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1)
  1213.                      +(long)*p_lineheadbytes + (long)*p_linetrailbytes;
  1214.  
  1215.     /* Check for non-byte swapped sample value */
  1216.        GetLabelString (buf, len, "SAMPLE_TYPE", "LSB_INTEGER",
  1217.             SampleType, &flag);
  1218.        if (flag < 1)
  1219.            GetLabelString(buf,len,"COLUMN_TYPE","LSB_INTEGER",SampleType,&flag);
  1220.        if (flag > 0)
  1221.        {
  1222.            if (strncmp(SampleType,"MSB_INTEGER",11) == 0)      ByteSwap = TRUE;
  1223.            if (strncmp(SampleType,"UNSIGNED_INTEGER",16) == 0) ByteSwap = TRUE;
  1224.            if (strncmp(SampleType,"VAX_INTEGER",11) == 0)      ByteSwap = FALSE;
  1225.            if (strncmp(SampleType,"LSB_INTEGER",11) == 0)      ByteSwap = FALSE;
  1226.        }
  1227.  
  1228. /* Get plot information for X-axis */
  1229.  
  1230.        xflag = FALSE;
  1231.        GetLabelReal( buf, len, "START_WAVELENGTH", 0.0, &xstart, &flag);
  1232.        if (flag > 0)
  1233.            xflag = TRUE;
  1234.        GetLabelReal( buf, len, "WAVELENGTH_INTERVAL", 1.0, &xinterval, &flag);
  1235.        GetLabelString( buf, len, "WAVELENGTH_UNIT", "PIXEL", xunit, &flag);
  1236.  
  1237. /* Get DN scaling information -- Look for PDS-style first, FITS next */
  1238.        GetLabelReal( buf, len, "SCALING_FACTOR", 1.0, &ThisImage.DNscale, &flag);
  1239.        GetLabelReal( buf, len, "OFFSET", 1.0, &ThisImage.DNoffset, &flag);
  1240.  
  1241.        flag = 0;
  1242.        if (strlen(DetachedFileName) == 0)
  1243.            GetLabelString (buf, len, "IMAGE_POINTER", "",
  1244.                     DetachedFileName, &flag);
  1245.        if (flag > 0)
  1246.        {
  1247.            GiantLabel = (long)headerrecs*(long)headerreclen;
  1248.            if (GiantLabel < 65535L)
  1249.                *p_labelsize = (unsigned int)GiantLabel;
  1250.            else
  1251.                *p_labelsize = 0;
  1252.        }
  1253.        if (strlen(DetachedPaletteName) == 0)
  1254.            GetLabelString(buf, len, "PALETTE_POINTER", "",
  1255.                     DetachedPaletteName, &flag);
  1256.  
  1257.        free(p_linetrailbytes);
  1258.        PDSused= 1;
  1259.        return(0);
  1260.     }
  1261.  
  1262. /* This part for VICAR2 labels */
  1263.     GetLabelString (buf, len, "TYPE", " ", FileType, &flag);
  1264.     if ( stricmp (FileType, "IMAGE") == 0 )
  1265.     {
  1266.        DoVicarLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
  1267.              p_reclen, p_lineheadbytes, p_linetrailbytes);
  1268.        return(0);
  1269.     }
  1270.  
  1271. /* This part for FITS labels */
  1272.     GetLabelString (buf, len, "SIMPLE", " ", FileType, &flag);
  1273.     if ( (stricmp (FileType, "T") == 0) ||
  1274.          (stricmp (FileType, "F") == 0) ) /* this is a FITS file */
  1275.     {
  1276.        DoFITSLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
  1277.              p_reclen, p_lineheadbytes, p_linetrailbytes, unit);
  1278.  
  1279.        PDSused= 0;
  1280.        return(0);
  1281.     }
  1282.  
  1283. /* Do this if no recognized label */
  1284.     DoNoLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
  1285.              p_reclen, p_lineheadbytes, FileType, p_linetrailbytes);
  1286. }
  1287.  
  1288. void DoVicarLabel (char * buf, int len, unsigned int * p_labelsize,
  1289.                int * p_nline, int * p_nsamp, int * p_bitsperpix,
  1290.                int * p_reclen, int * p_lineheadbytes, int * p_linetrailbytes)
  1291.  
  1292. /***  DoVicarLabel interprets the Vicar label in the buffer to extract the
  1293.       image information.
  1294.  
  1295.    Parameter     Type    In/out  Description
  1296.     buf         char ptr   in    The label buffer
  1297.     len           int      in    The length in bytes of the label buffer
  1298.   p_labelsize  uns int ptr out   The size of the label or header to skip
  1299.   p_nline        int ptr   out   The number of lines in the image
  1300.   p_nsamp        int ptr   out   The number of samples in the image
  1301.   p_bitsperpix   int ptr   out   The pixel format (16,8,4,1)
  1302.   p_reclen       int ptr   out   The record length in bytes
  1303. p_lineheadbytes  int ptr   out   The header bytes at beginning of each line
  1304. p_linetrailbytes int ptr   in    The number of bytes at the end of each line
  1305.  
  1306. ***/
  1307. {
  1308.     char   format[16];
  1309.     int    flag;
  1310.  
  1311.     GetLabelInteger (buf, len, "LBLSIZE", 0, p_labelsize, &flag);
  1312.     GetLabelInteger (buf, len, "NL", 1, p_nline, &flag);
  1313.     GetLabelInteger (buf, len, "NS", 1, p_nsamp, &flag);
  1314.     GetLabelString (buf, len, "FORMAT", "BYTE", format, &flag);
  1315.     if (format[0] == 'H')
  1316.         *p_bitsperpix = 16;
  1317.     else
  1318.         *p_bitsperpix = 8;
  1319.  
  1320.     *p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
  1321.     GetLabelInteger (buf, len, "RECSIZE", *p_reclen, p_reclen, &flag);
  1322.     *p_lineheadbytes = 0;
  1323.  
  1324.     free(p_linetrailbytes);
  1325. }
  1326.  
  1327.  
  1328. void DoFITSLabel (char * buf, int len, unsigned int * p_labelsize, int * p_nline,
  1329.                int * p_nsamp, int * p_bitsperpix, int * p_reclen,
  1330.                int * p_lineheadbytes, int * p_linetrailbytes, int unit)
  1331.  
  1332. /***  DoFITSLabel interprets the FITS label in the buffer to extract the
  1333.       image information.
  1334.  
  1335.    Parameter     Type    In/out  Description
  1336.     buf         char ptr   in    The label buffer
  1337.     len           int      in    The length in bytes of the label buffer
  1338.   p_labelsize  uns int ptr out   The size of the label or header to skip
  1339.   p_nline        int ptr   out   The number of lines in the image
  1340.   p_nsamp        int ptr   out   The number of samples in the image
  1341.   p_bitsperpix   int ptr   out   The pixel format (16,8,4,1)
  1342.   p_reclen       int ptr   out   The record length in bytes
  1343. p_lineheadbytes  int ptr   out   The header bytes at beginning of each line
  1344. p_linetrailbytes int ptr   in    The number of bytes at the end of each line
  1345.    unit          int       in    The number to the current file being
  1346.                                     processed
  1347.  
  1348. ***/
  1349. {
  1350.  
  1351.     int    flag, naxis, bytecount, i;
  1352.     char   line[81];
  1353.     char   axis_tmp[8];
  1354.     int    n_axis_tmp;
  1355.     float  dmax, dmin;
  1356.  
  1357.     ByteSwap = TRUE;
  1358.     GetLabelInteger (buf, len, "NAXIS",0, &naxis, &flag);
  1359.     if (naxis == 1)
  1360.     {
  1361.         GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
  1362.         GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
  1363.         *p_nline = 1;
  1364.     }
  1365.     else if (naxis == 2)
  1366.     {
  1367.         GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
  1368.         GetLabelInteger (buf, len, "NAXIS2", 1, p_nline, &flag);
  1369.         GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
  1370.     }
  1371.     else if (naxis == 0)
  1372.     {
  1373.         *p_nline      = 0;
  1374.         *p_nsamp      = 0;
  1375.         *p_bitsperpix = 16;
  1376. /*        ClearDisplay(0); Commented out - AW3 */
  1377.         StatusLine(0,"No lines in image.");
  1378.     }
  1379.     else /* naxis greater than 2 */
  1380.     {
  1381.         /* Loop over NAXISn w/ n>2 to see if any are greater than 1 */
  1382.         for (i=3; i<=naxis; i++)
  1383.         {
  1384.            sprintf (axis_tmp, "NAXIS%d", i);
  1385.            GetLabelInteger (buf, len, axis_tmp, 1, &n_axis_tmp, &flag);
  1386.            if (n_axis_tmp != 1)
  1387.            {
  1388.                 *p_nline      = 0;
  1389.                 *p_nsamp      = 0;
  1390.                 *p_bitsperpix = 16;
  1391.         /*        ClearDisplay(0); Commented out - AW3 */
  1392.                 StatusLine(0,"Too many axes.");
  1393.            }
  1394.            else /* Treat like a 2-D image */
  1395.            {
  1396.                 GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
  1397.                 GetLabelInteger (buf, len, "NAXIS2", 1, p_nline, &flag);
  1398.                 GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
  1399.            }
  1400.         }
  1401.     }
  1402.  
  1403.     *p_reclen = ((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
  1404.     *p_lineheadbytes = 0;
  1405.     GetLabelReal( buf, len, "BSCALE", 1.0, &ThisImage.DNscale, &flag);
  1406.     GetLabelReal( buf, len, "BZERO", 0.0, &ThisImage.DNoffset, &flag);
  1407.     GetLabelReal( buf, len, "DATAMAX", -1.0e+38, &dmax, &flag);
  1408.     if (flag == 1)
  1409.        ThisImage.DNmax = (long) ((dmax - ThisImage.DNoffset) /
  1410.                                   ThisImage.DNscale);
  1411.     GetLabelReal( buf, len, "DATAMIN", 1.0e+38, &dmin, &flag);
  1412.     if (flag == 1)
  1413.        ThisImage.DNmin = (long) ((dmin - ThisImage.DNoffset) /
  1414.                                   ThisImage.DNscale);
  1415.  
  1416. /** added by DIN /revised - mdm/ to read END in FITS header **/
  1417.  
  1418.     bytecount = 0;
  1419.     i = 0; /* changed from i= 1 to i= 0 to handle END when it is in the
  1420.               last line of label. AEE, 4-4-89 */
  1421.  
  1422. /*    labels = fdopen(FCB[unit].handle,"r"); */
  1423.     lseek(FCB[unit].handle,0L,SEEK_SET);
  1424.     do
  1425.     {
  1426.         read(FCB[unit].handle,line,80);
  1427.         ++i;
  1428.     } while (strnicmp(line,"END     ",8) != 0);
  1429.  
  1430.     bytecount = i*80;
  1431.     i         = bytecount/2880;
  1432.     if (bytecount % 2880 != 0) i++;
  1433.     *p_labelsize = i*2880;
  1434.     free(p_linetrailbytes);
  1435. }
  1436.  
  1437.  
  1438. void DoNoLabel(char * buf, int len, unsigned int * p_labelsize,
  1439.                int * p_nline, int * p_nsamp, int * p_bitsperpix,
  1440.                int * p_reclen, int * p_lineheadbytes, char * FileType,
  1441.                int * p_linetrailbytes)
  1442.  
  1443. /***  DoNoLabel prompts the user to enter the image parameters.
  1444.       PDS-format compressed images are handled (skipped) here, too.
  1445.  
  1446.    Parameter     Type    In/out  Description
  1447.     buf         char ptr   in    The label buffer
  1448.     len           int      in    The length in bytes of the label buffer
  1449.   p_labelsize  uns int ptr out   The size of the label or header to skip
  1450.   p_nline        int ptr   out   The number of lines in the image
  1451.   p_nsamp        int ptr   out   The number of samples in the image
  1452.   p_bitsperpix   int ptr   out   The pixel format (16,8,4,1)
  1453.   p_reclen       int ptr   out   The record length in bytes
  1454. p_lineheadbytes  int ptr   out   The header bytes at beginning of each line
  1455.    FileType      char ptr  in    The type of the file of data (from FILE_TYPE)
  1456. p_linetrailbytes int ptr   in    The number of bytes at the end of each line
  1457.  
  1458. ***/
  1459. {
  1460.  
  1461.     int    flag;
  1462.     char   inpstr[32];
  1463.  
  1464.     *p_bitsperpix = 8;
  1465.     *p_labelsize = 0;
  1466.  
  1467.     if (OneScreen)
  1468.     {
  1469.         ClearDisplay(0);
  1470.  
  1471.         TextLine = TextHeight + 5;  TextSample = 1;
  1472.         TypeText ("Image file does not have a proper label.");
  1473.  
  1474.         TextLine += TextHeight + 5;  TextSample = 1;
  1475.         TypeText ("Input number of lines : ");
  1476.         AcceptText (inpstr);
  1477.         sscanf (inpstr, "%d", p_nline);
  1478.  
  1479.         TextLine += TextHeight + 5;  TextSample = 1;
  1480.         TypeText ("Input number of samples : ");
  1481.         AcceptText (inpstr);
  1482.         sscanf (inpstr, "%d", p_nsamp);
  1483.  
  1484.         TextLine += TextHeight + 5;  TextSample = 1;
  1485.         TypeText ("Input bits per pixel : ");
  1486.         AcceptText (inpstr);
  1487.         sscanf (inpstr, "%d", p_bitsperpix);
  1488.  
  1489.         TextLine += TextHeight + 5;  TextSample = 1;
  1490.         TypeText ("Input number of header bytes : ");
  1491.         AcceptText (inpstr);
  1492.         sscanf (inpstr, "%d", p_labelsize);
  1493.  
  1494.         if(*p_bitsperpix > 8)
  1495.         {
  1496.             TextLine += TextHeight + 5;  TextSample = 1;
  1497.             TypeText ("Swap byte order? (default=n) : ");
  1498.             AcceptText (inpstr);
  1499.             if (stricmp(inpstr,"N") == 0)
  1500.                 ByteSwap = FALSE;
  1501.             else
  1502.                 ByteSwap = TRUE;
  1503.         }
  1504.  
  1505.         ClearDisplay(0);
  1506.     }
  1507.     else
  1508.     {
  1509.         printf ("%s", "Image file does not have a proper label.\n");
  1510.         printf ("%s", "Input number of lines : ");
  1511.         gets (inpstr);
  1512.         sscanf (inpstr, "%d", p_nline);
  1513.  
  1514.         printf ("%s", "Input number of samples : ");
  1515.         gets (inpstr);
  1516.         sscanf (inpstr, "%d", p_nsamp);
  1517.  
  1518.         printf ("%s", "Input bits per pixel : ");
  1519.         gets (inpstr);
  1520.         sscanf (inpstr, "%d", p_bitsperpix);
  1521.  
  1522.         printf ("%s", "Input number of header bytes : ");
  1523.         gets (inpstr);
  1524.         sscanf (inpstr, "%d", p_labelsize);
  1525.     }
  1526.     *p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1 )/8 + 1;
  1527.     *p_lineheadbytes = 0;
  1528.  
  1529.     free(p_linetrailbytes);
  1530. }
  1531.  
  1532. int PutLabel (char * LabelBuf, int * p_pos, char * LabelString)
  1533. {
  1534.     int   i;
  1535.  
  1536.     for (i = 0; LabelString[i] > 0; i++)
  1537.         LabelBuf[(*p_pos)++] = LabelString[i];
  1538.     LabelBuf[(*p_pos)++] = 13;
  1539.     LabelBuf[(*p_pos)++] = 10;
  1540. }
  1541.  
  1542.  
  1543. int PutLabelValue (char * LabelBuf, int * p_pos, char * LabelString, int value)
  1544. {
  1545.     char     temp1[128], temp2[32];
  1546.  
  1547.     strcpy (temp1, LabelString);
  1548.     sprintf (temp2, "%d", value);
  1549.     strcat (temp1, temp2);
  1550.     PutLabel (LabelBuf, p_pos, temp1);
  1551. }
  1552.  
  1553. int MakeLabel (char * Buf, int * p_labelsize, int nline, int nsamp,
  1554.                int bitsperpix, int reclen)
  1555.  
  1556. /***  MakeLabel creates a PDS label in the buffer, having the
  1557.       appropriate image parameters.
  1558.  
  1559.    Parameter    Type   In/out  Description
  1560.     Buf        char ptr  out   The label buffer
  1561.   p_labelsize  int ptr   out   The size of the label
  1562.     nline       int      out   The number of lines in the image
  1563.     nsamp       int      out   The number of samples in the image
  1564.    bitsperpix   int      out   The pixel format (16,8,4,1)
  1565.     reclen      int      out   The record length in bytes
  1566. ***/
  1567.  
  1568. {
  1569.     int       j, labelrecs, totalrecs;
  1570.     long int  filesize;
  1571.     char      labstr[128];
  1572.  
  1573.  
  1574.     labelrecs = (2047/reclen) + 1;
  1575.     *p_labelsize = reclen*labelrecs;
  1576.     totalrecs = nline + labelrecs;
  1577.     filesize = (long)reclen*(long)totalrecs - 20;
  1578.  
  1579.     j = 0;
  1580.  
  1581.     sprintf (labstr,   "NJPL1I00PDS1%08ld            = SFDU_LABEL",
  1582.                                                                   filesize);
  1583.     PutLabel (Buf, &j, labstr);
  1584.     PutLabel (Buf, &j, "RECORD_TYPE                     = FIXED_LENGTH");
  1585.     PutLabelValue (Buf, &j, "RECORD_BYTES                    = ", reclen);
  1586.     PutLabelValue (Buf, &j, "FILE_RECORDS                    = ", totalrecs);
  1587.     PutLabelValue (Buf, &j, "LABEL_RECORDS                   = ", labelrecs);
  1588.     PutLabelValue (Buf, &j, "^IMAGE                          = ", labelrecs+1);
  1589.     PutLabel (Buf, &j,      "OBJECT                          = IMAGE");
  1590.     PutLabelValue (Buf, &j, "  LINES                         = ", nline);
  1591.     PutLabelValue (Buf, &j, "  LINE_SAMPLES                  = ", nsamp);
  1592.     PutLabelValue (Buf, &j, "  SAMPLE_BITS                   = ", bitsperpix);
  1593.     PutLabel (Buf, &j, "END_OBJECT");
  1594.     PutLabel (Buf, &j, "END");
  1595.  
  1596.     while (j < *p_labelsize)
  1597.         Buf[j++] = ' ';
  1598. }
  1599.  
  1600.  
  1601. int GetCommand (char * CommandString, char * CommandList)
  1602.  
  1603. /***  GetCommand is an integer function that returns the number of the
  1604.       command that is in the beginning of the CommandString parameter.
  1605.       The command is assigned a number according to the list of commands
  1606.       in the CommandList string parameter.  The list of commands should
  1607.       be in uppercase and have spaces between the commmand.
  1608.  
  1609. ***/
  1610.  
  1611. {
  1612.     int i, j, k, l, n;
  1613.     char    List[NCOMMANDS][35],  Word[35], temp[35]; /* Max # of commands */
  1614.  
  1615.    /* Parse the command list string up into a list of strings */
  1616.     n = 0;
  1617.     l = strlen(CommandList) - 1;
  1618.     k = 0;
  1619.     do
  1620.     {
  1621.        j = k;
  1622.        while ( (CommandList[j] == ' ') && (j < l) )
  1623.             j++;
  1624.        if (j == l)  break;
  1625.        k = j;
  1626.        do
  1627.             k++;
  1628.        while ( (CommandList[k] != ' ') && (k<=l) );
  1629.        strncpy (List[n], &CommandList[j], k-j);
  1630.        List[n][k-j] = 0;
  1631.        n++;
  1632.     } while (k <= l);
  1633.  
  1634.  
  1635.     /* Get the first word from the command string */
  1636.     l = strlen(CommandString);
  1637.     j = 0;
  1638.     while ( (CommandString[j] == ' ') && (j < l) )
  1639.         j++;
  1640.     k = j;
  1641.     do
  1642.         k++;
  1643.     while ( (CommandString[k] != ' ') && (k<=l) );
  1644.     strncpy (Word, &CommandString[j], k-j);
  1645.     Word[k-j] = 0;
  1646.  
  1647.           /* Search through the list of commands to find a match */
  1648.     i = n-1;
  1649.     do
  1650.     {
  1651.         strcpy (temp, Word);
  1652.         temp[strlen(List[i])] = 0;
  1653.     } while ( (stricmp(temp, List[i]) != 0) && (--i >= 0) );
  1654.  
  1655.     return (i+1);
  1656. }
  1657.  
  1658.