home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / portedOneB.tar / OneB / RAConeb.c < prev    next >
C/C++ Source or Header  |  1999-07-19  |  42KB  |  1,448 lines

  1. #include "RAConeb.h"
  2. #include <unistd.h>
  3. #include <../Ingest/platform.h>
  4.  
  5. #ifdef RAC_LINUX
  6.   #define SWABIT(x,y,z) do { if(x == y) memcpy(y,x,z); } while(0)
  7. #else
  8.     #define SWABIT(x,y,z) swab(x,y,z)
  9. #endif
  10.  
  11. char *c1Filename=0;
  12. char *c2Filename=0;
  13. char *c3Filename=0;
  14. char *c4Filename=0;
  15. char *c5Filename=0;
  16. char *auxFilename=0;
  17. char *syncFilename=0;
  18. char *ephemFilename=0;
  19. char *scanOpt="0";
  20. char *bFilename=0;
  21.  
  22. cmdPair args[] =
  23. {
  24.   "-iC1", &c1Filename,
  25.   "-iC2", &c2Filename,
  26.   "-iC3", &c3Filename,
  27.   "-iC4", &c4Filename,
  28.   "-iC5", &c5Filename,
  29.   "-iAUX", &auxFilename,
  30.   "-iSYNC", &syncFilename,
  31.   "-iEPHEM", &ephemFilename,
  32.   "-pMAXSCANS", &scanOpt,
  33.   "-ob", &bFilename,
  34.   0, 0
  35. };
  36.  
  37. char satname[10][8]={"UNKNOWN","NOAA-11","NOAA-06","NOAA-14","NOAA-07",
  38.                      "NOAA-12","NOAA-08","NOAA-09","NOAA-10","UNKNOWN"};
  39. char typename[4][5]={"XXX", "LAC","GAC","HRPT"};
  40.  
  41. kep_t kepOrbEl;
  42.  
  43. /****  Main  ****/
  44. int main (int argc, char **argv)
  45. {
  46.  
  47. int RC, i;
  48. FILE *auxFile, *ephemFile, *syncFile;
  49. int c1File, c2File, c3File, c4File, c5File, bFile;
  50. ushort c1buf[2048], c2buf[2048], c3buf[2048], c4buf[2048], c5buf[2048];
  51. ushort syncbuf[106], telemdata[103], auxBuf[97];
  52. uchar tbmbuf[512], tbm44buf[50], buffer[13656], btemp[7400], timebuf[6], numbuf[2], locbuf[4];
  53. uchar orbitbuf[7], telembuf[140];
  54. short strhr, strmin, endhr, endmin;
  55. double strsec, endsec;
  56. uchar charbuf, longnumbuf[4];
  57. char tmpchar[4], crap[64], daybuf[3], yrbuf[3];
  58. unsigned int orbnum, orbnum2;
  59. char satid[16], satnum[4];
  60. ttOrbitTime startTime, endTime, scanTime;
  61. int count = 0;
  62. int ascend = 0;
  63. short slixo;
  64. int ilixo;
  65. long lixo;
  66. long tlong;
  67. rct cartEl;
  68. float gains[5], inters[5];
  69. static double *Lats = NULL, *Lons = NULL;
  70. static double *strLats = NULL, *strLons = NULL;
  71. static double *endLats = NULL, *endLons = NULL;
  72. struct stat st;
  73. LDOUBLE zenith, zlat, zlon;
  74. int scans, rc;
  75. int MaxNumScans;
  76. double two30, two22;
  77. HRPTScan auxScan;
  78.  
  79. /* Parse Command Line */
  80.   RC = RACGetArgs(argc, argv, args);
  81.   if (RC != 0)
  82.     exit(RC);
  83.  
  84.   two30 = pow(2.0, 30.0);
  85.   two22 = pow(2.0, 22.0);
  86.   MaxNumScans = atoi(scanOpt);
  87.   if (MaxNumScans == 0) MaxNumScans = 9999;
  88.  
  89.   fprintf(stderr,"\nCommand Line Inputs:\n");
  90.   fprintf(stderr,"- Channel-1  : %s\n", c1Filename);
  91.   fprintf(stderr,"- Channel-2  : %s\n", c2Filename);
  92.   fprintf(stderr,"- Channel-3  : %s\n", c3Filename);
  93.   fprintf(stderr,"- Channel-4  : %s\n", c4Filename);
  94.   fprintf(stderr,"- Channel-5  : %s\n", c5Filename);
  95.   fprintf(stderr,"- Aux File   : %s\n", auxFilename);
  96.   fprintf(stderr,"- Ephem File : %s\n", ephemFilename);
  97.   fprintf(stderr,"\nCommand Line Outputs:\n");
  98.   fprintf(stderr,"- 1B File    : %s\n\n", bFilename);
  99.  
  100.   c1File = open(c1Filename,O_RDONLY);
  101.   c2File = open(c2Filename,O_RDONLY);
  102.   c3File = open(c3Filename,O_RDONLY);
  103.   c4File = open(c4Filename,O_RDONLY);
  104.   c5File = open(c5Filename,O_RDONLY);
  105.   auxFile = fopen(auxFilename,"r");
  106.   syncFile = fopen(syncFilename,"r");
  107.   ephemFile = fopen(ephemFilename,"r");
  108.   bFile = creat(bFilename,0666);
  109.  
  110.   if (c1File == -1 ||
  111.       c2File == -1 ||
  112.       c3File == -1 ||
  113.       c4File == -1 ||
  114.       c5File == -1 ||
  115.       auxFile == 0 ||
  116.       syncFile == 0 ||
  117.       ephemFile == 0 ||
  118.       bFile == -1 )
  119.         exit (1);
  120.  
  121. /* Get Satellite ID */
  122.   fseek(ephemFile, 0, SEEK_SET);
  123.   fscanf(ephemFile,"%s %s",&satid, &satnum);
  124.   fscanf(ephemFile, "%s %s %s %s", &crap, &crap, &crap, &crap);
  125.   sprintf(yrbuf,"%c%c", crap[0], crap[1]);
  126.   sprintf(daybuf,"%c%c%c", crap[2], crap[3], crap[4]);
  127.   scanTime.Year = startTime.Year = endTime.Year = 1900+atoi(yrbuf);
  128.  
  129. /* Get Scan Number */
  130.   stat(c1Filename,&st);
  131.   scans = st.st_size/4096;
  132.  
  133. /* Write TBM Header */
  134.   memset(tbmbuf,' ', 35);
  135.   write (bFile, tbmbuf, 30);
  136.  
  137.   rc = fseek(auxFile, 0, SEEK_SET);
  138.   rc = fread(auxBuf, 2, 97, auxFile);
  139.   SWABIT(auxBuf,auxBuf,97*2);
  140.   memcpy(auxScan.ID, auxBuf, 97*2);
  141.   ReadScanTime(auxScan, &startTime);
  142.   decodeTime(startTime.Millisecond, &strhr, &strmin, &strsec);
  143.   orbnum = ReadLatLon(ephemFile, startTime, &strLats,  &strLons);
  144.   rc = fseek(auxFile, 97L*2L*(scans-1), SEEK_SET);
  145.   rc = fread(auxBuf, 2, 97, auxFile);
  146.   SWABIT(auxBuf,auxBuf,97*2);
  147.   memcpy(auxScan.ID, auxBuf, 97*2);
  148.   ReadScanTime(auxScan, &endTime);
  149.   decodeTime(endTime.Millisecond, &endhr, &endmin, &endsec);
  150.   orbnum2 = ReadLatLon(ephemFile, endTime, &endLats,  &endLons);
  151.   if (strLats[1024] > endLats[1024])
  152.     ascend = 1;
  153.   encodesat(tmpchar, atoi(satnum));
  154.   sprintf(tbm44buf,"NSS.HRPT.N%c.D%2d%3d.S%2d%2d.E%2d%2d.B%5d%2d.WI     ",
  155.                  tmpchar[0], startTime.Year%100, startTime.Day+1, strhr, strmin,
  156.                  endhr, endmin, orbnum, orbnum2%100);
  157.   write(bFile,tbm44buf,44);
  158.   tbmbuf[0] = 'S';
  159.   write (bFile, tbmbuf, 1);
  160.   sprintf (tbmbuf,"%c%2d%c%2d%c%3d%c%3d",
  161.                   (strLats[1023] >= 0)?'+':'-', abs((int)trunc(strLats[1023])),
  162.                   (endLats[1023] >= 0)?'+':'-', abs((int)trunc(endLats[1023])),
  163.                   (strLons[1023] >= 0)?'+':'-', abs((int)trunc(strLons[1023])),
  164.                   (endLons[1023] >= 0)?'+':'-', abs((int)trunc(endLons[1023])));
  165.   write (bFile, tbmbuf, 14);
  166.   sprintf (tbmbuf,"%2d%2d%3d",strhr,strmin,(endhr*60+endmin)-(strhr*60+strmin)); 
  167.   write (bFile, tbmbuf, 7);
  168.   tbmbuf[0] = 'N';
  169.   write (bFile, tbmbuf, 1);
  170.   write (bFile, CHANNELS, 25);
  171.  
  172. /* Write Data Set Header Record (2) */
  173.  
  174.   pack2number(numbuf, encodesat(tmpchar, atoi(satnum)));
  175.   charbuf = numbuf[1] | numbuf[0] << 4;
  176.   write (bFile, &charbuf, 1);
  177.   fprintf(stderr,"Satellite       : %s(%c)\n", satname[(int)charbuf],tmpchar[0]);
  178.   if (!ascend)
  179.     fprintf(stderr,"Direction       : Ascending\n");
  180.   else
  181.     fprintf(stderr,"Direction       : Descending\n");
  182.   setPRTjunk(atoi(satnum));
  183.  
  184. /* Get Data Type */
  185.   pack2number(numbuf, 3);
  186.   charbuf = numbuf[0] | numbuf[1] << 4;
  187.   write (bFile, &charbuf, 1);
  188.   fprintf(stderr,"Data Type       : HRPT\n");
  189.  
  190. /* Get Start Time */
  191.   packscantime(timebuf,(short)startTime.Year, (short)startTime.Day+1, startTime.Millisecond);
  192.   write (bFile, timebuf, 6);  
  193.   fprintf(stderr,"Start Time      : %0.2d.%0.2d %0.2d:%0.2d:%2.3f\n", startTime.Year, startTime.Day+1, strhr, strmin, strsec);
  194.  
  195.   if (MaxNumScans == 9999) pack2number(numbuf, scans);
  196.   else                     pack2number(numbuf, MaxNumScans);
  197.   write (bFile, numbuf, 2);
  198.  
  199. /* Get End Time */
  200.   packscantime(timebuf,(short)endTime.Year, (short)endTime.Day+1, endTime.Millisecond);
  201.   write (bFile, timebuf, 6);  
  202.   fprintf(stderr,"End Time        : %0.2d.%0.2d %0.2d:%0.2d:%2.3f\n", endTime.Year, endTime.Day+1, endhr, endmin, endsec);
  203.  
  204.   if (MaxNumScans == 9999) fprintf(stderr,"Number of Scans : %d\n", scans);
  205.   else                     fprintf(stderr,"Number of Scans : %d\n", MaxNumScans);
  206.  
  207.   memset(btemp, 0, 7400);
  208.   sprintf(orbitbuf, "%.5d%.2d", orbnum, orbnum2%100);
  209.   write (bFile, orbitbuf, 7);
  210.   write (bFile, &btemp[0], 1);
  211.   write (bFile, btemp, 2);
  212.   write (bFile, btemp, 6);
  213.   write (bFile, btemp, 2);
  214.   write (bFile, &btemp[0], 1);
  215.   write (bFile, &btemp[0], 1);
  216.   write (bFile, &btemp[0], 1);
  217.   write (bFile, btemp, 3);
  218.   write (bFile, tbm44buf, 44);
  219.   pack2number(numbuf, kepOrbEl.T.Year % 100);
  220.   write (bFile, numbuf, 2);
  221.   pack2number(numbuf, kepOrbEl.T.Day);
  222.   write (bFile, numbuf, 2);
  223.   pack4number(longnumbuf, kepOrbEl.T.Millisecond);
  224.   write (bFile, longnumbuf, 4);
  225.   pack4number(longnumbuf, (int)kepOrbEl.A);
  226.   write (bFile, longnumbuf, 4);
  227.   pack4number(longnumbuf, (int)(kepOrbEl.e * 100000000.0));
  228.   write (bFile, longnumbuf, 4);
  229.   pack4number(longnumbuf, (int)(kepOrbEl.i * 100000.0));
  230.   write (bFile, longnumbuf, 4);
  231.   pack4number(longnumbuf, (int)(kepOrbEl.w * 100000.0));
  232.   write (bFile, longnumbuf, 4);
  233.   pack4number(longnumbuf, (int)(kepOrbEl.N * 100000.0));
  234.   write (bFile, longnumbuf, 4);
  235.   pack4number(longnumbuf, (int)(kepOrbEl.m * 100000.0));
  236.   write (bFile, longnumbuf, 4);
  237.   cartEl = getCartesian(kepOrbEl);
  238.   pack4number(longnumbuf, (int)(cartEl.xp * 10000.0));
  239.   write (bFile, longnumbuf, 4);
  240.   pack4number(longnumbuf, (int)(cartEl.yp * 10000.0));
  241.   write (bFile, longnumbuf, 4);
  242.   pack4number(longnumbuf, (int)(cartEl.zp * 10000.0));
  243.   write (bFile, longnumbuf, 4);
  244.   pack4number(longnumbuf, (int)(cartEl.xv * 1000000.0));
  245.   write (bFile, longnumbuf, 4);
  246.   pack4number(longnumbuf, (int)(cartEl.yv * 1000000.0));
  247.   write (bFile, longnumbuf, 4);
  248.   pack4number(longnumbuf, (int)(cartEl.zv * 1000000.0));
  249.   write (bFile, longnumbuf, 4);
  250.   write (bFile, btemp, 7260);
  251.   
  252.   write (bFile, btemp, 7400);
  253.  
  254.   tlong = 0;
  255.   fseek (auxFile, 0, SEEK_SET);
  256.   do {
  257.     fread(auxBuf, 2, 97, auxFile);
  258.     SWABIT(auxBuf,auxBuf,97*2);
  259.     memcpy(auxScan.ID, auxBuf, 97*2);
  260.     rc = grabPRTlevels(auxScan.ID);
  261.   } while (rc != 0);
  262.  
  263. /* Write Data Records (2 per scan) */
  264.   while (read(c1File,c1buf, 4096) > 0) {
  265.  
  266.       printf("------------------\n");
  267.  
  268.     SWABIT(c1buf,c1buf,4096);  
  269.     read(c2File,c2buf, 4096);
  270.     SWABIT(c2buf,c2buf,4096);  
  271.     read(c3File,c3buf, 4096);
  272.     SWABIT(c3buf,c3buf,4096);   
  273.     read(c4File,c4buf, 4096);
  274.     SWABIT(c4buf,c4buf,4096);  
  275.     read(c5File,c5buf, 4096);
  276.     SWABIT(c5buf,c5buf,4096);  
  277.  
  278. /* Scan Line Number*/
  279.     pack2number(numbuf, count+1);
  280.     write (bFile, numbuf, 2);
  281.  
  282. /* Time Code */
  283.     rc = fseek (auxFile, 97L*2L*count, SEEK_SET);
  284.     rc = fread (auxBuf, 2, 97, auxFile);
  285.     SWABIT(auxBuf,auxBuf,97*2);
  286.     memcpy(auxScan.ID, auxBuf, 97*2);
  287.     ReadScanTime(auxScan, &scanTime);
  288.     count++;
  289.     packscantime(timebuf,(short)scanTime.Year, (short)scanTime.Day+1, scanTime.Millisecond);
  290.     write (bFile, timebuf, 6);  
  291.  
  292. /* Quality indicators */
  293.     if (ascend) {
  294.       if ((scanTime.Millisecond - tlong) != 167 && (scanTime.Millisecond - tlong) != 166)
  295.         if (count == 1)
  296.           btemp[0] = 0x02;
  297.         else {
  298.           printf ("(descend)Time error: %d at scan %d\n", scanTime.Millisecond-tlong, count);
  299.           btemp[0] = 0x02 | 0x40;
  300.         }
  301.       else
  302.         btemp[0] = 0x02;
  303.     }
  304.     else {
  305.       if ((scanTime.Millisecond - tlong) != 167 && (scanTime.Millisecond - tlong) != 166)
  306.         if (count == 1)
  307.           btemp[0] = 0x00;
  308.         else {
  309.           printf ("(ascend)Time error: %d at scan %d\n", scanTime.Millisecond-tlong, count);
  310.           btemp[0] = 0x00 | 0x40;
  311.         }
  312.       else
  313.         btemp[0] = 0x00;
  314.     }
  315.     btemp[1] = 0x00;
  316.     btemp[2] = 0x00;
  317.     btemp[3] = 0x00;
  318.     tlong = scanTime.Millisecond;
  319.     write (bFile, btemp, 4);
  320.  
  321. /* Calibration */
  322.    grabPRTgains(auxScan.ID, gains, inters);
  323.    for (i=0; i<5; i++) {
  324.        // printf("gains[%d] = %f, casting gains = %d\n", i, gains[i], (int)((double)gains[i] * two30));
  325.      pack4number(longnumbuf, (int)((double)gains[i] * two30));
  326.      write (bFile, longnumbuf, 4);
  327.          // printf("inters[%d] = %f, casting inters = %d\n", i, inters[i], (int)((double)inters[i] * two22));
  328.      pack4number(longnumbuf, (int)((double)inters[i] * two22));
  329.      write (bFile, longnumbuf, 4);
  330.    }
  331.  
  332. /* Meaningful za/el */
  333.     pack2number(numbuf, 51);
  334.     write (bFile, &numbuf[0], 1);
  335.  
  336. /* Solar Zenith Angles */
  337.     ReadLatLon(ephemFile, scanTime, &Lats,  &Lons);
  338.     for (i=25; i<2048; i+=40) {
  339.       zlat = (LDOUBLE)Lats[i];
  340.       zlon = (LDOUBLE)Lons[i];
  341.     //        printf("zlat = %Lf, zlon = %Lf\n", zlat, zlon);
  342.       SolarZenithAngle (btemp, scanTime.Year, scanTime.Day+1, scanTime.Millisecond, scanTime.UnixTime, zlat, zlon);
  343.     //        printf("I'm going to write out : %i = %c\n", i, btemp[0]);
  344.       write (bFile, &btemp[0], 1);
  345.     }
  346.  
  347. /* Earth location */
  348.     for (i=25; i<2048; i+=40) {
  349.       packlocation (locbuf, Lats[i], Lons[i]);
  350.       write (bFile, locbuf, 4);
  351.     }
  352.  
  353. /* Telemetry */
  354.     rc = fread (syncbuf, 2, 106, syncFile);
  355.     SWABIT (syncbuf, syncbuf, 106*2);
  356.     memcpy (telemdata, syncbuf, 12);
  357.     memcpy (&telemdata[6], auxScan.ID, 194);
  358.     packtelem(telembuf, telemdata);
  359.     write (bFile, telembuf, 140);
  360.  
  361. /* Video Data */
  362.     packvideo(buffer, 1, c1buf, c2buf, c3buf, c4buf, c5buf);
  363.     write (bFile, buffer, 13656);
  364.  
  365.     write (bFile, btemp, 20);   /* Solar Zenith Angles decimals */
  366.     write (bFile, btemp, 2);    /* Clock drift */
  367.  
  368. /* Spare */
  369.     memset(btemp, 0, 7400);
  370.     write (bFile, btemp, 674);
  371.   }
  372.   close (c1File);
  373.   close (c2File);
  374.   close (c3File);
  375.   close (c4File);
  376.   close (c5File);
  377.   close (bFile);
  378.   fclose (auxFile);
  379.   fclose (ephemFile);
  380.  
  381.   exit(0);
  382. }
  383.  
  384. int RACGetArgs(int argc, char **argv, cmdPair *args)
  385. {
  386. int i,j;
  387. int ERROR=0;
  388.   for (i=1;i<argc;i+=1)
  389.   {
  390.     switch(*(argv[i]+0))
  391.     {
  392.       case('-'):
  393.         j=0;
  394.         while (args[j].Tag != 0)
  395.         {
  396.           if (strcmp(argv[i], args[j].Tag) == 0)
  397.           {
  398.             switch(*(args[j].Tag+1))
  399.             {
  400.               case('i'):
  401.               case('o'):
  402.               case('a'):
  403.               case('p'):
  404.                 *args[j].Param = argv[i+1];
  405.                /* j += 1; */
  406.                 break;
  407.               case('f'):
  408.                 *args[j].Param = argv[i];
  409.                 break;
  410.             }
  411.           }
  412.           j += 1;
  413.         }
  414.         break;
  415.     }
  416.   }
  417.   j = 0;
  418.   while (args[j].Tag != 0)
  419.   {
  420.     switch(*(args[j].Tag+1))
  421.     {
  422.       case('i'):
  423.       case('o'):
  424.       case('p'):
  425.         if (*args[j].Param == 0)
  426.         {
  427.           fprintf(stderr, "(%s)**ERROR** required argument '%s' not supplied\n", argv[0], args[j].Tag);
  428.           ERROR += 1;
  429.         }
  430.         break;
  431.       case('f'):
  432.         break;
  433.     }
  434.     j += 1;
  435.   }
  436.   return ERROR;
  437. }
  438.  
  439. int ReadScanTime(HRPTScan aScan, ttOrbitTime *oTime)
  440. {
  441. int returnFlag=-1;
  442. int rc;
  443.  
  444.   returnFlag = ttGetHRPTScanTime(oTime, aScan.TimeCode);
  445.   return returnFlag;
  446. }
  447.  
  448. unsigned int ReadLatLon(FILE *EphemFile, ttOrbitTime sTime, double **latList, double **lonList)
  449. {
  450.    double *llLatList, *llLonList;
  451.    kep_t mean;
  452.    kep_t osc;
  453.    LDOUBLE DEL;
  454.    norad_t ephem;
  455.    static double *sig = NULL;
  456.    int i;
  457.    unsigned int orb;
  458.  
  459.    fseek(EphemFile, 0, SEEK_SET);
  460.    ReadNorad(EphemFile, &ephem);
  461.    NoradToKep(&ephem, &mean);
  462.    brwlyd_initialize(mean);
  463.  
  464.    if (*latList == 0)
  465.      *latList = malloc((2048+1024)*sizeof(double));
  466.    if (*lonList == 0)
  467.      *lonList = malloc((2048+1024)*sizeof(double));
  468.    if (sig == 0) {
  469.      sig = malloc((2048+1024)*sizeof(double));
  470.      for(i=0;i<2048;i+=1)
  471.        sig[i] = (55.4*2.0/2048.0) * (float) i-55.4;
  472.    }
  473.  
  474.    kepOrbEl.A = mean.A;
  475.    kepOrbEl.e = mean.e;
  476.    kepOrbEl.i = mean.i * m_rToD;
  477.    kepOrbEl.N = mean.N * m_rToD;
  478.    kepOrbEl.w = mean.w * m_rToD;
  479.    kepOrbEl.m = mean.m * m_rToD;
  480.    kepOrbEl.T = mean.T;
  481.      /*
  482.      printf("A = %f, e = %f, i = %f, N = %f, w = %f, m = %f\n",
  483.          mean.A, mean.e, mean.i, mean.N, mean.w, mean.m);
  484.      printf("mean.T.Year = %d, mean.T.Day = %d, mean.T.fSecond = %f, mean.T.Second = %d\n",
  485.          mean.T.Year, mean.T.Day, mean.T.fSecond, mean.T.Second);
  486.      printf("mean.T.Milli = %d, mean.T.Millisecond = %d, mean.T.UnixTime - unknown.\n",
  487.          mean.T.Milli, mean.T.Millisecond);
  488.     */
  489.  
  490.    /*
  491.    ListKep(&mean);
  492.    printf(">>%f\n%f\n%f\n%f\n%f\n", mean.A, mean.e, mean.i, mean.N, mean.w, mean.m);
  493.    ttListOrbitTime(&mean.T);
  494.      */
  495.  
  496. /*
  497.    printf("sTime.Year = %d, sTime.Day = %d, sTime.fSecond = %f\n",
  498.        sTime.Year, sTime.Day, sTime.fSecond);
  499. */
  500.    DEL = ttTimeDiff(&sTime, &mean.T);
  501. /*
  502.      printf("DEL = %f\n", DEL);
  503. */
  504.    osc = brwlyd_osc(DEL);
  505. /*
  506.      printf("osc.A = %f, osc.e = %f, osc.i = %f, osc.N = %f, osc.w = %f, osc.m = %f\n",
  507.          osc.A, osc.e, osc.i, osc.N, osc.w, osc.m);
  508. */
  509.    orb = osc.Orbit;
  510.    model_earthlocate(osc, 2048, sig, *latList, *lonList);
  511.    return orb;
  512. }
  513.  
  514. int encodesat(char *let, int num)
  515. {
  516.    int temp;
  517.    
  518.    switch (num) {
  519.      case 6: temp = 2; let[0] = 'A'; break;
  520.      case 7: temp = 4; let[0] = 'C'; break;
  521.      case 8: temp = 6; let[0] = 'E'; break;
  522.      case 9: temp = 7; let[0] = 'F'; break;
  523.      case 10: temp = 8; let[0] = 'G'; break;
  524.      case 11: temp = 1; let[0] = 'H'; break;
  525.      case 12: temp = 5; let[0] = 'D'; break;
  526.      case 13: temp = 2; let[0] = 'I'; break;
  527.      case 14: temp = 3; let[0] = 'J'; break;
  528.      default: temp = 0; let[0] = '?'; break;
  529.    }
  530.    return temp;
  531. }
  532.  
  533. rct getCartesian(kep_t kep)
  534. {
  535.   LDOUBLE Ecc;             // eccentric anomaly
  536.   LDOUBLE Esav;            // save previous Ecc
  537.   LDOUBLE cosE,sinE,radius,xp0,xp1,xp2,xp3,F;
  538.   LDOUBLE cosGF,sinGF,cosN,sinN,cosi,sini;
  539.   rct rect;
  540.  
  541.   Ecc = kep.m;
  542.   Esav= 0.0;
  543.  
  544.   while (1) {
  545.     Ecc = kep.m + kep.e * sin (Ecc);
  546.     if (fabs(Ecc-Esav) < 1e-15) break;
  547.     Esav= Ecc;
  548.   }
  549.  
  550.   cosE = cos (Ecc); sinE = sin (Ecc);
  551.   radius = kep.A * (1. - kep.e * cosE);
  552.  
  553.   xp0 = sqrt (1. - kep.e * kep.e);
  554.   xp1 = sqrt (kep.A * ea_GM) / radius;
  555.   xp2 = xp0 * xp1;
  556.   xp3 = kep.e * sinE * xp1;
  557.  
  558.   F = atan2 (sinE * xp0, cosE - kep.e);
  559.  
  560.   cosGF = cos (kep.w + F);
  561.   sinGF = sin (kep.w + F);
  562.  
  563.   cosN  = cos (kep.N);
  564.   sinN  = sin (kep.N);
  565.  
  566.   cosi  = cos (kep.i);
  567.   sini  = sin (kep.i);
  568.                                         // position
  569.   rect.xp = (radius * (cosN * cosGF - sinN * cosi * sinGF)) / 1000.0;
  570.   rect.yp = (radius * (cosN * sinGF * cosi + sinN * cosGF)) / 1000.0;
  571.   rect.zp = (radius * (sini * sinGF)) / 1000.0;
  572.                                         // velocity
  573.   rect.xv = (xp3 * (cosN * cosGF - sinN * cosi * sinGF)
  574.             - xp2 * (sinN * cosGF * cosi + cosN * sinGF)) / 1000.0;
  575.  
  576.   rect.yv = (xp3 * (cosN * sinGF * cosi + sinN * cosGF)
  577.             - xp2 * (sinN * sinGF - cosN * cosi * cosGF)) / 1000.0;
  578.  
  579.   rect.zv = (xp3 * (sini * sinGF)
  580.             + xp2 * (sini * cosGF)) / 1000.0;
  581.  
  582.   return rect;
  583. }
  584.  
  585. int decodeTime(long msec, short *hr, short *min, double *sec)
  586. {
  587.   short temp1, temp2;
  588.   double tempgmt;
  589.  
  590.   tempgmt = (double)(msec)/3600./1000.;
  591.   *hr=temp1=trunc(tempgmt);
  592.   *min=temp2=trunc((tempgmt-(double)(temp1))*60);
  593.   *sec = tempgmt*3600.0 - (double)(temp1)*3600.0 - (double)(temp2)*60.0;
  594.   return 0;
  595. }
  596.  
  597. int pack2number (uchar *buffer, short scannum)
  598. {
  599.    buffer[0] = (uchar)((scannum >> 8)&0x00ff);
  600.    buffer[1] = (uchar)(scannum&0x00ff);
  601.    return 0;
  602. }
  603.  
  604. int pack4number (uchar *buffer, int scannum)
  605. {
  606.     // printf("scannum = %d\n", scannum);
  607.  
  608.    buffer[0] = (uchar)((scannum >> 24)&0x00ff);
  609.    buffer[1] = (uchar)((scannum >> 16)&0x00ff);
  610.    buffer[2] = (uchar)((scannum >> 8)&0x00ff);
  611.    buffer[3] = (uchar)(scannum&0x00ff);
  612.  
  613.      // printf("buf0 = %d, buf1 = %d, buf2 = %d, buf3 = %d\n",
  614.      //     buffer[0], buffer[1], buffer[2], buffer[3]);
  615.  
  616.    return 0;
  617. }
  618.  
  619. int packscantime(uchar *scanline,short scanyear,short scanday,long scangmt)
  620. {
  621.    unsigned int msec;
  622.    double tempfl;
  623.    short tempsh, tempsh2;
  624.    uchar tempyr;
  625.  
  626.    tempyr = scanyear - 1900;
  627.    msec = (unsigned int)scangmt; 
  628.    scanline[0] = (uchar)((tempyr << 1)&0xfe) | (uchar)((scanday >> 8)&0x01);
  629.    scanline[1] = (uchar)(scanday&0x00ff);
  630.    scanline[2] = ((uchar)(msec >> 24)&0x000000ff)&0x07;
  631.    scanline[3] = (uchar)(msec >> 16)&0x000000ff;
  632.    scanline[4] = (uchar)(msec >> 8)&0x000000ff;
  633.    scanline[5] = (uchar)(msec)&0x000000ff;
  634.    return 0;
  635. }
  636.  
  637. int packlocation(uchar *buffer, double aLat, double aLon)
  638. {
  639.    short temp;
  640.  
  641.    temp = (short)(aLat * 128.0);
  642.    buffer[0] = (uchar)((temp >> 8)&0x00ff);
  643.    buffer[1] = (uchar)(temp&0x00ff);
  644.    temp = (short)(aLon * 128.0);
  645.    buffer[2] = (uchar)((temp >> 8)&0x00ff);
  646.    buffer[3] = (uchar)(temp&0x00ff);
  647.    return 0;
  648. }
  649.  
  650. int packtelem(uchar *buffer, ushort *telem)
  651. {
  652.    int i, j;
  653.    int *tmpintPtr;
  654.  
  655.    j = 0;
  656.    for (i=0; i < 34; i++) {
  657.      tmpintPtr = ((int *) &buffer[i*4]);
  658.      *tmpintPtr = (((((int)telem[j])<<20)&0x3ff00000) | ((((int)telem[j+1])<<10)&0x000ffc00) | ((((int)telem[j+2]))&0x000003ff));
  659.      j += 3;
  660.          #ifdef LINUX
  661.      swapint(tmpintPtr);
  662.          #endif LINUX
  663.    }
  664.    tmpintPtr = ((int *) &buffer[i*4]);
  665.    *tmpintPtr = (((((int)telem[j])<<20)&0x3ff00000) | ((((int)0)<<10)&0x000ffc00) | ((((int)0))&0x000003ff));
  666.      #ifdef LINUX
  667.    swapint(tmpintPtr);
  668.      #endif
  669.    return 0;
  670. }
  671.  
  672. int packvideo(uchar *buffer,int datatype, ushort *ch1,ushort *ch2,ushort *ch3,ushort *ch4,ushort *ch5)
  673. {
  674. int i,j,tmpint;
  675. int *tmpintPtr;
  676. unsigned short decode_array[13661];
  677.   for(i=0;i<2048;i+=1)
  678.   {
  679.     decode_array[i*5] = ch1[i];
  680.     decode_array[i*5+1] = ch2[i];
  681.     decode_array[i*5+2] = ch3[i];
  682.     decode_array[i*5+3] = ch4[i];
  683.     decode_array[i*5+4] = ch5[i];
  684.   }
  685.     decode_array[i*5] = ch1[i];
  686.     decode_array[i*5+1] = ch2[i];
  687.     decode_array[i*5+2] = ch3[i];
  688.     decode_array[i*5+3] = ch4[i];
  689.     decode_array[i*5+4] = ch5[i];
  690.   j=0;
  691.   for (i=0;i<3414;i+=1)
  692.   {
  693.     tmpintPtr = ((int *) &buffer[i*4]);
  694.     *tmpintPtr = (((((int)decode_array[j])<<20)&0x3ff00000) | ((((int)decode_array[j+1])<<10)&0x000ffc00) | ((((int)decode_array[j+2]))&0x000003ff));
  695.     j += 3;
  696.         #ifdef LINUX
  697.     swapint(tmpintPtr);
  698.         #endif
  699.   }
  700.   return 0;
  701. }
  702.  
  703. int setPRTjunk(int satellite)
  704. {
  705.   if (satellite == 12)
  706.   {
  707.     PRTCoefficient[0][0] = 0.000000;
  708.     PRTCoefficient[0][1] = 0.000000;
  709.     PRTCoefficient[0][2] = 0.000000;
  710.     PRTCoefficient[0][3] = 0.000000;
  711.     PRTCoefficient[0][4] = 0.000000;
  712.     PRTWeight[0] = 0.000000;
  713.     PRTCoefficient[1][0] = 276.596985;
  714.     PRTCoefficient[1][1] = 0.051275;
  715.     PRTCoefficient[1][2] = 0.000001;
  716.     PRTCoefficient[1][3] = 0.000000;
  717.     PRTCoefficient[1][4] = 0.000000;
  718.     PRTWeight[1] = 0.250000;
  719.     PRTCoefficient[2][0] = 276.596985;
  720.     PRTCoefficient[2][1] = 0.051275;
  721.     PRTCoefficient[2][2] = 0.000001;
  722.     PRTCoefficient[2][3] = 0.000000;
  723.     PRTCoefficient[2][4] = 0.000000;
  724.     PRTWeight[2] = 0.250000;
  725.     PRTCoefficient[3][0] = 276.596985;
  726.     PRTCoefficient[3][1] = 0.051275;
  727.     PRTCoefficient[3][2] = 0.000001;
  728.     PRTCoefficient[3][3] = 0.000000;
  729.     PRTCoefficient[3][4] = 0.000000;
  730.     PRTWeight[3] = 0.250000;
  731.     PRTCoefficient[4][0] = 276.596985;
  732.     PRTCoefficient[4][1] = 0.051275;
  733.     PRTCoefficient[4][2] = 0.000001;
  734.     PRTCoefficient[4][3] = 0.000000;
  735.     PRTCoefficient[4][4] = 0.000000;
  736.     PRTWeight[4] = 0.250000;
  737.     RFuncWave[0] = 0.000000;
  738.     RFuncCenterWave[0] = 0.000000;
  739.     RFuncStep[0] = 0.000000;
  740.     RFuncCount[0] = 0.000000;
  741.     RFuncSum[0] = 0.000000;
  742.     RFuncWidth[0] = 0.000000;
  743.     SolarIrradiance[0] = 0.000000;
  744.     RFuncWave[1] = 0.000000;
  745.     RFuncCenterWave[1] = 0.000000;
  746.     RFuncStep[1] = 0.000000;
  747.     RFuncCount[1] = 0.000000;
  748.     RFuncSum[1] = 0.000000;
  749.     RFuncWidth[1] = 0.000000;
  750.     SolarIrradiance[1] = 0.000000;
  751.     RFuncWave[2] = 2439.023926;
  752.     RFuncCenterWave[2] = 2721.463867;
  753.     RFuncStep[2] = 9.414660;
  754.     RFuncCount[2] = 60.000000;
  755.     RFuncSum[2] = 1.042425;
  756.     RFuncWidth[2] = 0.000000;
  757.     SolarIrradiance[2] = 0.000000;
  758.     RFuncWave[3] = 847.457520;
  759.     RFuncCenterWave[3] = 931.583191;
  760.     RFuncStep[3] = 2.804190;
  761.     RFuncCount[3] = 60.000000;
  762.     RFuncSum[3] = 1.000085;
  763.     RFuncWidth[3] = 0.000000;
  764.     SolarIrradiance[3] = 0.000000;
  765.     RFuncWave[4] = 787.401367;
  766.     RFuncCenterWave[4] = 850.344055;
  767.     RFuncStep[4] = 2.098090;
  768.     RFuncCount[4] = 60.000000;
  769.     RFuncSum[4] = 0.992360;
  770.     RFuncWidth[4] = 0.000000;
  771.     SolarIrradiance[4] = 0.000000;
  772.     SpaceRad[2] = 0.000000;
  773.     SpaceRad[3] = 0.000000;
  774.     SpaceRad[4] = 0.000000;
  775.     AGIGain[0] = 0.104223;
  776.     AGIInter[0] = -4.449080;
  777.     AGIGain[1] = 0.101440;
  778.     AGIInter[1] = -3.399256;
  779.     RFunc[2][0] = 0.000044;
  780.     RFunc[2][1] = 0.000043;
  781.     RFunc[2][2] = 0.000056;
  782.     RFunc[2][3] = 0.000098;
  783.     RFunc[2][4] = 0.000189;
  784.     RFunc[2][5] = 0.000379;
  785.     RFunc[2][6] = 0.000757;
  786.     RFunc[2][7] = 0.001460;
  787.     RFunc[2][8] = 0.002308;
  788.     RFunc[2][9] = 0.002879;
  789.     RFunc[2][10] = 0.003148;
  790.     RFunc[2][11] = 0.003242;
  791.     RFunc[2][12] = 0.003271;
  792.     RFunc[2][13] = 0.003302;
  793.     RFunc[2][14] = 0.003357;
  794.     RFunc[2][15] = 0.003410;
  795.     RFunc[2][16] = 0.003427;
  796.     RFunc[2][17] = 0.003437;
  797.     RFunc[2][18] = 0.003466;
  798.     RFunc[2][19] = 0.003502;
  799.     RFunc[2][20] = 0.003518;
  800.     RFunc[2][21] = 0.003488;
  801.     RFunc[2][22] = 0.003430;
  802.     RFunc[2][23] = 0.003395;
  803.     RFunc[2][24] = 0.003397;
  804.     RFunc[2][25] = 0.003427;
  805.     RFunc[2][26] = 0.003477;
  806.     RFunc[2][27] = 0.003533;
  807.     RFunc[2][28] = 0.003548;
  808.     RFunc[2][29] = 0.003498;
  809.     RFunc[2][30] = 0.003495;
  810.     RFunc[2][31] = 0.003619;
  811.     RFunc[2][32] = 0.003470;
  812.     RFunc[2][33] = 0.003596;
  813.     RFunc[2][34] = 0.003442;
  814.     RFunc[2][35] = 0.003322;
  815.     RFunc[2][36] = 0.002956;
  816.     RFunc[2][37] = 0.002140;
  817.     RFunc[2][38] = 0.001237;
  818.     RFunc[2][39] = 0.000651;
  819.     RFunc[2][40] = 0.000356;
  820.     RFunc[2][41] = 0.000211;
  821.     RFunc[2][42] = 0.000141;
  822.     RFunc[2][43] = 0.000101;
  823.     RFunc[2][44] = 0.000074;
  824.     RFunc[2][45] = 0.000061;
  825.     RFunc[2][46] = 0.000057;
  826.     RFunc[2][47] = 0.000054;
  827.     RFunc[2][48] = 0.004781;
  828.     RFunc[2][49] = 0.000044;
  829.     RFunc[2][50] = 0.000045;
  830.     RFunc[2][51] = 0.000048;
  831.     RFunc[2][52] = 0.000049;
  832.     RFunc[2][53] = 0.000048;
  833.     RFunc[2][54] = 0.000048;
  834.     RFunc[2][55] = 0.000048;
  835.     RFunc[2][56] = 0.000048;
  836.     RFunc[2][57] = 0.000048;
  837.     RFunc[2][58] = 0.000048;
  838.     RFunc[2][59] = 0.000000;
  839.     RFunc[3][0] = 0.000235;
  840.     RFunc[3][1] = 0.000250;
  841.     RFunc[3][2] = 0.000249;
  842.     RFunc[3][3] = 0.000219;
  843.     RFunc[3][4] = 0.000209;
  844.     RFunc[3][5] = 0.000348;
  845.     RFunc[3][6] = 0.000760;
  846.     RFunc[3][7] = 0.001508;
  847.     RFunc[3][8] = 0.002627;
  848.     RFunc[3][9] = 0.004074;
  849.     RFunc[3][10] = 0.005591;
  850.     RFunc[3][11] = 0.006882;
  851.     RFunc[3][12] = 0.007820;
  852.     RFunc[3][13] = 0.008498;
  853.     RFunc[3][14] = 0.009021;
  854.     RFunc[3][15] = 0.009461;
  855.     RFunc[3][16] = 0.009867;
  856.     RFunc[3][17] = 0.010275;
  857.     RFunc[3][18] = 0.010653;
  858.     RFunc[3][19] = 0.010935;
  859.     RFunc[3][20] = 0.011072;
  860.     RFunc[3][21] = 0.011104;
  861.     RFunc[3][22] = 0.011095;
  862.     RFunc[3][23] = 0.011102;
  863.     RFunc[3][24] = 0.011148;
  864.     RFunc[3][25] = 0.011250;
  865.     RFunc[3][26] = 0.011415;
  866.     RFunc[3][27] = 0.011628;
  867.     RFunc[3][28] = 0.011867;
  868.     RFunc[3][29] = 0.012104;
  869.     RFunc[3][30] = 0.012293;
  870.     RFunc[3][31] = 0.012382;
  871.     RFunc[3][32] = 0.012336;
  872.     RFunc[3][33] = 0.012206;
  873.     RFunc[3][34] = 0.012071;
  874.     RFunc[3][35] = 0.011983;
  875.     RFunc[3][36] = 0.011819;
  876.     RFunc[3][37] = 0.011368;
  877.     RFunc[3][38] = 0.010435;
  878.     RFunc[3][39] = 0.009049;
  879.     RFunc[3][40] = 0.007414;
  880.     RFunc[3][41] = 0.005743;
  881.     RFunc[3][42] = 0.004198;
  882.     RFunc[3][43] = 0.002867;
  883.     RFunc[3][44] = 0.001830;
  884.     RFunc[3][45] = 0.001138;
  885.     RFunc[3][46] = 0.000730;
  886.     RFunc[3][47] = 0.000513;
  887.     RFunc[3][48] = 0.000397;
  888.     RFunc[3][49] = 0.000332;
  889.     RFunc[3][50] = 0.000300;
  890.     RFunc[3][51] = 0.000283;
  891.     RFunc[3][52] = 0.000266;
  892.     RFunc[3][53] = 0.000251;
  893.     RFunc[3][54] = 0.000239;
  894.     RFunc[3][55] = 0.000232;
  895.     RFunc[3][56] = 0.000230;
  896.     RFunc[3][57] = 0.000232;
  897.     RFunc[3][58] = 0.000235;
  898.     RFunc[3][59] = 0.000000;
  899.     RFunc[4][0] = 0.000343;
  900.     RFunc[4][1] = 0.000081;
  901.     RFunc[4][2] = 0.000075;
  902.     RFunc[4][3] = 0.000579;
  903.     RFunc[4][4] = 0.001763;
  904.     RFunc[4][5] = 0.003479;
  905.     RFunc[4][6] = 0.005505;
  906.     RFunc[4][7] = 0.007609;
  907.     RFunc[4][8] = 0.009513;
  908.     RFunc[4][9] = 0.010932;
  909.     RFunc[4][10] = 0.011658;
  910.     RFunc[4][11] = 0.011897;
  911.     RFunc[4][12] = 0.011984;
  912.     RFunc[4][13] = 0.012216;
  913.     RFunc[4][14] = 0.012624;
  914.     RFunc[4][15] = 0.013116;
  915.     RFunc[4][16] = 0.013603;
  916.     RFunc[4][17] = 0.014051;
  917.     RFunc[4][18] = 0.014467;
  918.     RFunc[4][19] = 0.014858;
  919.     RFunc[4][20] = 0.011520;
  920.     RFunc[4][21] = 0.015466;
  921.     RFunc[4][22] = 0.015585;
  922.     RFunc[4][23] = 0.015547;
  923.     RFunc[4][24] = 0.015433;
  924.     RFunc[4][25] = 0.015340;
  925.     RFunc[4][26] = 0.015351;
  926.     RFunc[4][27] = 0.015442;
  927.     RFunc[4][28] = 0.015531;
  928.     RFunc[4][29] = 0.015538;
  929.     RFunc[4][30] = 0.015451;
  930.     RFunc[4][31] = 0.015348;
  931.     RFunc[4][32] = 0.015313;
  932.     RFunc[4][33] = 0.015387;
  933.     RFunc[4][34] = 0.015383;
  934.     RFunc[4][35] = 0.015044;
  935.     RFunc[4][36] = 0.014117;
  936.     RFunc[4][37] = 0.012541;
  937.     RFunc[4][38] = 0.010500;
  938.     RFunc[4][39] = 0.008190;
  939.     RFunc[4][40] = 0.005812;
  940.     RFunc[4][41] = 0.003586;
  941.     RFunc[4][42] = 0.001743;
  942.     RFunc[4][43] = 0.000511;
  943.     RFunc[4][44] = 0.000000;
  944.     RFunc[4][45] = 0.000000;
  945.     RFunc[4][46] = 0.000121;
  946.     RFunc[4][47] = 0.000285;
  947.     RFunc[4][48] = 0.000326;
  948.     RFunc[4][49] = 0.000291;
  949.     RFunc[4][50] = 0.000231;
  950.     RFunc[4][51] = 0.000195;
  951.     RFunc[4][52] = 0.000189;
  952.     RFunc[4][53] = 0.000200;
  953.     RFunc[4][54] = 0.000217;
  954.     RFunc[4][55] = 0.000226;
  955.     RFunc[4][56] = 0.000228;
  956.     RFunc[4][57] = 0.000224;
  957.     RFunc[4][58] = 0.000218;
  958.     RFunc[4][59] = 0.000000;
  959.   }
  960.   else
  961.   if (satellite == 14)
  962.   {
  963.     PRTCoefficient[0][0] = 0.000000;
  964.     PRTCoefficient[0][1] = 0.000000;
  965.     PRTCoefficient[0][2] = 0.000000;
  966.     PRTCoefficient[0][3] = 0.000000;
  967.     PRTCoefficient[0][4] = 0.000000;
  968.     PRTWeight[0] = 0.000000;
  969.     PRTCoefficient[1][0] = 276.596985;
  970.     PRTCoefficient[1][1] = 0.051275;
  971.     PRTCoefficient[1][2] = 0.000001;
  972.     PRTCoefficient[1][3] = 0.000000;
  973.     PRTCoefficient[1][4] = 0.000000;
  974.     PRTWeight[1] = 0.250000;
  975.     PRTCoefficient[2][0] = 276.596985;
  976.     PRTCoefficient[2][1] = 0.051275;
  977.     PRTCoefficient[2][2] = 0.000001;
  978.     PRTCoefficient[2][3] = 0.000000;
  979.     PRTCoefficient[2][4] = 0.000000;
  980.     PRTWeight[2] = 0.250000;
  981.     PRTCoefficient[3][0] = 276.596985;
  982.     PRTCoefficient[3][1] = 0.051275;
  983.     PRTCoefficient[3][2] = 0.000001;
  984.     PRTCoefficient[3][3] = 0.000000;
  985.     PRTCoefficient[3][4] = 0.000000;
  986.     PRTWeight[3] = 0.250000;
  987.     PRTCoefficient[4][0] = 276.596985;
  988.     PRTCoefficient[4][1] = 0.051275;
  989.     PRTCoefficient[4][2] = 0.000001;
  990.     PRTCoefficient[4][3] = 0.000000;
  991.     PRTCoefficient[4][4] = 0.000000;
  992.     PRTWeight[4] = 0.250000;
  993.     RFuncWave[0] = 0.000000;
  994.     RFuncCenterWave[0] = 0.000000;
  995.     RFuncStep[0] = 0.000000;
  996.     RFuncCount[0] = 0.000000;
  997.     RFuncSum[0] = 0.000000;
  998.     RFuncWidth[0] = 0.136000;
  999.     SolarIrradiance[0] = 221.419998;
  1000.     RFuncWave[1] = 0.000000;
  1001.     RFuncCenterWave[1] = 0.000000;
  1002.     RFuncStep[1] = 0.000000;
  1003.     RFuncCount[1] = 0.000000;
  1004.     RFuncSum[1] = 0.000000;
  1005.     RFuncWidth[1] = 0.245000;
  1006.     SolarIrradiance[1] = 252.289993;
  1007.     RFuncWave[2] = 2439.023926;
  1008.     RFuncCenterWave[2] = 2676.710449;
  1009.     RFuncStep[2] = 7.922880;
  1010.     RFuncCount[2] = 60.000000;
  1011.     RFuncSum[2] = 1.000013;
  1012.     RFuncWidth[2] = 0.000000;
  1013.     SolarIrradiance[2] = 0.000000;
  1014.     RFuncWave[3] = 854.700684;
  1015.     RFuncCenterWave[3] = 935.079956;
  1016.     RFuncStep[3] = 2.679310;
  1017.     RFuncCount[3] = 60.000000;
  1018.     RFuncSum[3] = 1.000042;
  1019.     RFuncWidth[3] = 0.000000;
  1020.     SolarIrradiance[3] = 0.000000;
  1021.     RFuncWave[4] = 787.401367;
  1022.     RFuncCenterWave[4] = 839.894775;
  1023.     RFuncStep[4] = 1.749780;
  1024.     RFuncCount[4] = 60.000000;
  1025.     RFuncSum[4] = 1.000023;
  1026.     RFuncWidth[4] = 0.000000;
  1027.     SolarIrradiance[4] = 0.000000;
  1028.     SpaceRad[2] = 0.006900;
  1029.     SpaceRad[3] = -4.050000;
  1030.     SpaceRad[4] = -2.290000;
  1031.     AGIGain[0] = 0.108100;
  1032.     AGIInter[0] = -3.864800;
  1033.     AGIGain[1] = 0.109000;
  1034.     AGIInter[1] = -3.674900;
  1035.     RFunc[2][0] = 0.000046;
  1036.     RFunc[2][1] = 0.000052;
  1037.     RFunc[2][2] = 0.000061;
  1038.     RFunc[2][3] = 0.000078;
  1039.     RFunc[2][4] = 0.000116;
  1040.     RFunc[2][5] = 0.000193;
  1041.     RFunc[2][6] = 0.000329;
  1042.     RFunc[2][7] = 0.000555;
  1043.     RFunc[2][8] = 0.000918;
  1044.     RFunc[2][9] = 0.001429;
  1045.     RFunc[2][10] = 0.002041;
  1046.     RFunc[2][11] = 0.002652;
  1047.     RFunc[2][12] = 0.003088;
  1048.     RFunc[2][13] = 0.003271;
  1049.     RFunc[2][14] = 0.003303;
  1050.     RFunc[2][15] = 0.003281;
  1051.     RFunc[2][16] = 0.003269;
  1052.     RFunc[2][17] = 0.003299;
  1053.     RFunc[2][18] = 0.003348;
  1054.     RFunc[2][19] = 0.003395;
  1055.     RFunc[2][20] = 0.003445;
  1056.     RFunc[2][21] = 0.003499;
  1057.     RFunc[2][22] = 0.003529;
  1058.     RFunc[2][23] = 0.003505;
  1059.     RFunc[2][24] = 0.003463;
  1060.     RFunc[2][25] = 0.003453;
  1061.     RFunc[2][26] = 0.003467;
  1062.     RFunc[2][27] = 0.003462;
  1063.     RFunc[2][28] = 0.003431;
  1064.     RFunc[2][29] = 0.003403;
  1065.     RFunc[2][30] = 0.003394;
  1066.     RFunc[2][31] = 0.003384;
  1067.     RFunc[2][32] = 0.003358;
  1068.     RFunc[2][33] = 0.003330;
  1069.     RFunc[2][34] = 0.003315;
  1070.     RFunc[2][35] = 0.003305;
  1071.     RFunc[2][36] = 0.003278;
  1072.     RFunc[2][37] = 0.003226;
  1073.     RFunc[2][38] = 0.003166;
  1074.     RFunc[2][39] = 0.003120;
  1075.     RFunc[2][40] = 0.003133;
  1076.     RFunc[2][41] = 0.003240;
  1077.     RFunc[2][42] = 0.003358;
  1078.     RFunc[2][43] = 0.003320;
  1079.     RFunc[2][44] = 0.003032;
  1080.     RFunc[2][45] = 0.002535;
  1081.     RFunc[2][46] = 0.001894;
  1082.     RFunc[2][47] = 0.001239;
  1083.     RFunc[2][48] = 0.000729;
  1084.     RFunc[2][49] = 0.000435;
  1085.     RFunc[2][50] = 0.000286;
  1086.     RFunc[2][51] = 0.000202;
  1087.     RFunc[2][52] = 0.000149;
  1088.     RFunc[2][53] = 0.000113;
  1089.     RFunc[2][54] = 0.000085;
  1090.     RFunc[2][55] = 0.000065;
  1091.     RFunc[2][56] = 0.000053;
  1092.     RFunc[2][57] = 0.000048;
  1093.     RFunc[2][58] = 0.000046;
  1094.     RFunc[2][59] = 0.000000;
  1095.     RFunc[3][0] = 0.000230;
  1096.     RFunc[3][1] = 0.000274;
  1097.     RFunc[3][2] = 0.000343;
  1098.     RFunc[3][3] = 0.000461;
  1099.     RFunc[3][4] = 0.000645;
  1100.     RFunc[3][5] = 0.000900;
  1101.     RFunc[3][6] = 0.001228;
  1102.     RFunc[3][7] = 0.001651;
  1103.     RFunc[3][8] = 0.002214;
  1104.     RFunc[3][9] = 0.002960;
  1105.     RFunc[3][10] = 0.003882;
  1106.     RFunc[3][11] = 0.004934;
  1107.     RFunc[3][12] = 0.006068;
  1108.     RFunc[3][13] = 0.007221;
  1109.     RFunc[3][14] = 0.008315;
  1110.     RFunc[3][15] = 0.009278;
  1111.     RFunc[3][16] = 0.010070;
  1112.     RFunc[3][17] = 0.010669;
  1113.     RFunc[3][18] = 0.011054;
  1114.     RFunc[3][19] = 0.011248;
  1115.     RFunc[3][20] = 0.011288;
  1116.     RFunc[3][21] = 0.011218;
  1117.     RFunc[3][22] = 0.011087;
  1118.     RFunc[3][23] = 0.010952;
  1119.     RFunc[3][24] = 0.010867;
  1120.     RFunc[3][25] = 0.010831;
  1121.     RFunc[3][26] = 0.010802;
  1122.     RFunc[3][27] = 0.010738;
  1123.     RFunc[3][28] = 0.010648;
  1124.     RFunc[3][29] = 0.010604;
  1125.     RFunc[3][30] = 0.010683;
  1126.     RFunc[3][31] = 0.010912;
  1127.     RFunc[3][32] = 0.011206;
  1128.     RFunc[3][33] = 0.011463;
  1129.     RFunc[3][34] = 0.011600;
  1130.     RFunc[3][35] = 0.011619;
  1131.     RFunc[3][36] = 0.011555;
  1132.     RFunc[3][37] = 0.011438;
  1133.     RFunc[3][38] = 0.011251;
  1134.     RFunc[3][39] = 0.010932;
  1135.     RFunc[3][40] = 0.010417;
  1136.     RFunc[3][41] = 0.009664;
  1137.     RFunc[3][42] = 0.008684;
  1138.     RFunc[3][43] = 0.007501;
  1139.     RFunc[3][44] = 0.006143;
  1140.     RFunc[3][45] = 0.004714;
  1141.     RFunc[3][46] = 0.003372;
  1142.     RFunc[3][47] = 0.002276;
  1143.     RFunc[3][48] = 0.001530;
  1144.     RFunc[3][49] = 0.001064;
  1145.     RFunc[3][50] = 0.000780;
  1146.     RFunc[3][51] = 0.000578;
  1147.     RFunc[3][52] = 0.000407;
  1148.     RFunc[3][53] = 0.000268;
  1149.     RFunc[3][54] = 0.000167;
  1150.     RFunc[3][55] = 0.000106;
  1151.     RFunc[3][56] = 0.000079;
  1152.     RFunc[3][57] = 0.000074;
  1153.     RFunc[3][58] = 0.000081;
  1154.     RFunc[3][59] = 0.000000;
  1155.     RFunc[4][0] = 0.000106;
  1156.     RFunc[4][1] = 0.000149;
  1157.     RFunc[4][2] = 0.000260;
  1158.     RFunc[4][3] = 0.000499;
  1159.     RFunc[4][4] = 0.000917;
  1160.     RFunc[4][5] = 0.001849;
  1161.     RFunc[4][6] = 0.003915;
  1162.     RFunc[4][7] = 0.006900;
  1163.     RFunc[4][8] = 0.009963;
  1164.     RFunc[4][9] = 0.012261;
  1165.     RFunc[4][10] = 0.013237;
  1166.     RFunc[4][11] = 0.013583;
  1167.     RFunc[4][12] = 0.014026;
  1168.     RFunc[4][13] = 0.014395;
  1169.     RFunc[4][14] = 0.014533;
  1170.     RFunc[4][15] = 0.014626;
  1171.     RFunc[4][16] = 0.014784;
  1172.     RFunc[4][17] = 0.014956;
  1173.     RFunc[4][18] = 0.015077;
  1174.     RFunc[4][19] = 0.015088;
  1175.     RFunc[4][20] = 0.014993;
  1176.     RFunc[4][21] = 0.014853;
  1177.     RFunc[4][22] = 0.014730;
  1178.     RFunc[4][23] = 0.014682;
  1179.     RFunc[4][24] = 0.014698;
  1180.     RFunc[4][25] = 0.014731;
  1181.     RFunc[4][26] = 0.014731;
  1182.     RFunc[4][27] = 0.014655;
  1183.     RFunc[4][28] = 0.014508;
  1184.     RFunc[4][29] = 0.014323;
  1185.     RFunc[4][30] = 0.014132;
  1186.     RFunc[4][31] = 0.013963;
  1187.     RFunc[4][32] = 0.013806;
  1188.     RFunc[4][33] = 0.013634;
  1189.     RFunc[4][34] = 0.013416;
  1190.     RFunc[4][35] = 0.013131;
  1191.     RFunc[4][36] = 0.012838;
  1192.     RFunc[4][37] = 0.012645;
  1193.     RFunc[4][38] = 0.012663;
  1194.     RFunc[4][39] = 0.012985;
  1195.     RFunc[4][40] = 0.013384;
  1196.     RFunc[4][41] = 0.013364;
  1197.     RFunc[4][42] = 0.012859;
  1198.     RFunc[4][43] = 0.012317;
  1199.     RFunc[4][44] = 0.011981;
  1200.     RFunc[4][45] = 0.011725;
  1201.     RFunc[4][46] = 0.011199;
  1202.     RFunc[4][47] = 0.009622;
  1203.     RFunc[4][48] = 0.006625;
  1204.     RFunc[4][49] = 0.003506;
  1205.     RFunc[4][50] = 0.001657;
  1206.     RFunc[4][51] = 0.000902;
  1207.     RFunc[4][52] = 0.000530;
  1208.     RFunc[4][53] = 0.000271;
  1209.     RFunc[4][54] = 0.000126;
  1210.     RFunc[4][55] = 0.000073;
  1211.     RFunc[4][56] = 0.000057;
  1212.     RFunc[4][57] = 0.000044;
  1213.     RFunc[4][58] = 0.000030;
  1214.     RFunc[4][59] = 0.000000;
  1215.   }
  1216.   else
  1217.   {
  1218.     return -1;
  1219.   }
  1220.   return 0;
  1221. }
  1222.  
  1223. int grabPRTlevels(unsigned short *aux)
  1224. {
  1225.    static int PRTDecomIndex;
  1226.    int j, PRTIndex;
  1227.  
  1228.    if (PRTDecomIndex == 0)
  1229.      PRTDecomIndex = -1;
  1230.    if (PRTDecomIndex == -1)
  1231.      if (aux[6+5]<10)
  1232.        PRTDecomIndex = 0;
  1233.    if (PRTDecomIndex >= 0)
  1234.    {
  1235.      PRTRead[0][PRTDecomIndex] = aux[6+5];
  1236.      PRTRead[1][PRTDecomIndex] = aux[6+6];
  1237.      PRTRead[2][PRTDecomIndex] = aux[6+7];
  1238.      if (PRTDecomIndex == 4)
  1239.      {
  1240.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1241.        {
  1242.          PRTTotal[0][PRTIndex] = PRTRead[0][PRTIndex];
  1243.          PRTTotal[1][PRTIndex] = PRTRead[1][PRTIndex];
  1244.          PRTTotal[2][PRTIndex] = PRTRead[2][PRTIndex];
  1245.        }
  1246.        PRTDecomIndex = -2;
  1247.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1248.        {
  1249.          PRTAverage[0][PRTIndex] = (float) PRTTotal[0][PRTIndex];
  1250.          PRTAverage[1][PRTIndex] = (float) PRTTotal[1][PRTIndex];
  1251.          PRTAverage[2][PRTIndex] = (float) PRTTotal[2][PRTIndex];
  1252.          PRT[PRTIndex] = PRTAverage[1][PRTIndex];
  1253.          PRT[PRTIndex] = ( PRTAverage[0][PRTIndex] +
  1254.                            PRTAverage[1][PRTIndex] +
  1255.                            PRTAverage[2][PRTIndex]) / 3;
  1256.        }
  1257.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1258.        {
  1259.          PRTTemperature[PRTIndex] = 0.0;
  1260.          for (j=0;j<5;j+=1)
  1261.          {
  1262.            PRTTemperature[PRTIndex] += PRTCoefficient[PRTIndex][j] * pow(PRT[PRTIndex], (float) j);
  1263.          }
  1264.        }
  1265.        InternalTargetTemperature = 0.0;
  1266.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1267.        {
  1268.          InternalTargetTemperature += PRTWeight[PRTIndex] * PRTTemperature[PRTIndex];
  1269.        }
  1270.        PRTDecomIndex = 0;
  1271.        return 0;
  1272.      }
  1273.      else
  1274.        PRTDecomIndex += 1;
  1275.    }
  1276.    return -1;
  1277. }
  1278.  
  1279. int grabPRTgains(unsigned short *aux, float gain[5], float inter[5])
  1280. {
  1281.    static int PRTDecomIndex;
  1282.    int PRTIndex, SSDIndex, ITDIndex, RFuncIndex;
  1283.    int i, j;
  1284.    float Wave;
  1285.  
  1286.    if (PRTDecomIndex == 0)
  1287.      PRTDecomIndex = -1;
  1288.    if (PRTDecomIndex < 0)
  1289.      if (aux[6+5]<10)
  1290.        PRTDecomIndex = 0;
  1291.    if (PRTDecomIndex >= 0)
  1292.    {
  1293.      PRTRead[0][PRTDecomIndex] = aux[6+5];
  1294.      PRTRead[1][PRTDecomIndex] = aux[6+6];
  1295.      PRTRead[2][PRTDecomIndex] = aux[6+7];
  1296.      if (PRTDecomIndex == 4)
  1297.      {
  1298.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1299.        {
  1300.          PRTTotal[0][PRTIndex] = PRTRead[0][PRTIndex];
  1301.          PRTTotal[1][PRTIndex] = PRTRead[1][PRTIndex];
  1302.          PRTTotal[2][PRTIndex] = PRTRead[2][PRTIndex];
  1303.        }
  1304.        PRTDecomIndex = 0;
  1305.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1306.        {
  1307.          PRTAverage[0][PRTIndex] = (float) PRTTotal[0][PRTIndex];
  1308.          PRTAverage[1][PRTIndex] = (float) PRTTotal[1][PRTIndex];
  1309.          PRTAverage[2][PRTIndex] = (float) PRTTotal[2][PRTIndex];
  1310.          PRT[PRTIndex] = PRTAverage[1][PRTIndex];
  1311.        }
  1312.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1313.        {
  1314.          PRTTemperature[PRTIndex] = 0.0;
  1315.          for (j=0;j<5;j+=1)
  1316.          {
  1317.            PRTTemperature[PRTIndex] += PRTCoefficient[PRTIndex][j] * pow(PRT[PRTIndex], (float) j);
  1318.          }
  1319.        }
  1320.        InternalTargetTemperature = 0.0;
  1321.        for (PRTIndex=0;PRTIndex<5;PRTIndex+=1)
  1322.        {
  1323.          InternalTargetTemperature += PRTWeight[PRTIndex] * PRTTemperature[PRTIndex];
  1324.        }
  1325.      }
  1326.      else
  1327.        PRTDecomIndex += 1;
  1328.    }
  1329.  
  1330.  
  1331.    for (ITDIndex=0;ITDIndex<10;ITDIndex+=1)
  1332.    {
  1333.      ITDTotal[ITDIndex][0] = 0;
  1334.      ITDTotal[ITDIndex][1] = 0;
  1335.      ITDTotal[ITDIndex][2] = aux[16+ITDIndex*3+0];
  1336.      ITDTotal[ITDIndex][3] = aux[16+ITDIndex*3+1];
  1337.      ITDTotal[ITDIndex][4] = aux[16+ITDIndex*3+2];
  1338.    }
  1339.    ITDCount = 1;
  1340.    for (SSDIndex=0;SSDIndex<10;SSDIndex+=1)
  1341.    {
  1342.      SSDTotal[SSDIndex][0] = aux[46+SSDIndex*5+0];
  1343.      SSDTotal[SSDIndex][1] = aux[46+SSDIndex*5+1];
  1344.      SSDTotal[SSDIndex][2] = aux[46+SSDIndex*5+2];
  1345.      SSDTotal[SSDIndex][3] = aux[46+SSDIndex*5+3];
  1346.      SSDTotal[SSDIndex][4] = aux[46+SSDIndex*5+4];
  1347.    }
  1348.    SSDCount = 1;
  1349.  
  1350.    for (RFuncIndex=2;RFuncIndex<5;RFuncIndex+=1)
  1351.    {
  1352.      RFuncRad[RFuncIndex] = 0.0;
  1353.      Wave = RFuncWave[RFuncIndex];
  1354.      for (i=0;i<RFuncCount[RFuncIndex];i+=1)
  1355.      {
  1356.        RFuncRad[RFuncIndex] += Planckf(Wave,
  1357.                    InternalTargetTemperature)*RFunc[RFuncIndex][i]*RFuncStep[RFuncIndex];
  1358.        Wave += RFuncStep[RFuncIndex];
  1359.      }
  1360.    }
  1361.  
  1362.    ITD[0] = 0.0;
  1363.    ITD[1] = 0.0;
  1364.    ITD[2] = 0.0;
  1365.    ITD[3] = 0.0;
  1366.    ITD[4] = 0.0;
  1367.    for (ITDIndex=0;ITDIndex<10;ITDIndex+=1)
  1368.    {
  1369.      ITDAverage[ITDIndex][0] = 0.0;
  1370.      ITDAverage[ITDIndex][1] = 0.0;
  1371.      ITDAverage[ITDIndex][2] = (float) ITDTotal[ITDIndex][2] / (float) ITDCount;
  1372.      ITDAverage[ITDIndex][3] = (float) ITDTotal[ITDIndex][3] / (float) ITDCount;
  1373.      ITDAverage[ITDIndex][4] = (float) ITDTotal[ITDIndex][4] / (float) ITDCount;
  1374.      ITD[0] += ITDAverage[ITDIndex][0];
  1375.      ITD[1] += ITDAverage[ITDIndex][1];
  1376.      ITD[2] += ITDAverage[ITDIndex][2];
  1377.      ITD[3] += ITDAverage[ITDIndex][3];
  1378.      ITD[4] += ITDAverage[ITDIndex][4];
  1379.    }
  1380.    ITD[0] /= 10.0;
  1381.    ITD[1] /= 10.0;
  1382.    ITD[2] /= 10.0;
  1383.    ITD[3] /= 10.0;
  1384.    ITD[4] /= 10.0;
  1385.  
  1386.    SSD[0] = 0.;
  1387.    SSD[1] = 0.;
  1388.    SSD[2] = 0.;
  1389.    SSD[3] = 0.;
  1390.    SSD[4] = 0.;
  1391.    for (SSDIndex=0;SSDIndex<10;SSDIndex+=1)
  1392.    {
  1393.      SSDAverage[SSDIndex][0] = (float) SSDTotal[SSDIndex][0] / (float) SSDCount;
  1394.      SSDAverage[SSDIndex][1] = (float) SSDTotal[SSDIndex][1] / (float) SSDCount;
  1395.      SSDAverage[SSDIndex][2] = (float) SSDTotal[SSDIndex][2] / (float) SSDCount;
  1396.      SSDAverage[SSDIndex][3] = (float) SSDTotal[SSDIndex][3] / (float) SSDCount;
  1397.      SSDAverage[SSDIndex][4] = (float) SSDTotal[SSDIndex][4] / (float) SSDCount;
  1398.      SSD[0] += SSDAverage[SSDIndex][0];
  1399.      SSD[1] += SSDAverage[SSDIndex][1];
  1400.      SSD[2] += SSDAverage[SSDIndex][2];
  1401.      SSD[3] += SSDAverage[SSDIndex][3];
  1402.      SSD[4] += SSDAverage[SSDIndex][4];
  1403.    }
  1404.    SSD[0] /= 10.0;
  1405.    SSD[1] /= 10.0;
  1406.    SSD[2] /= 10.0;
  1407.    SSD[3] /= 10.0;
  1408.    SSD[4] /= 10.0;
  1409.  
  1410.    for (i=0;i<2;i+=1)
  1411.    {
  1412.      gain[i] = AGIGain[i];
  1413.      inter[i] = AGIInter[i];
  1414.    }
  1415.    for (i=2;i<5;i+=1)
  1416.    {
  1417.      if ((SSD[i]-ITD[i]) != 0)
  1418.      {
  1419.        gain[i] = (SpaceRad[i] - RFuncRad[i])/(SSD[i]-ITD[i]);
  1420.        inter[i] = SpaceRad[i] - gain[i] * SSD[i];
  1421.      }
  1422.      else
  1423.      {
  1424.        gain[i] = -0.1;
  1425.        inter[i] = 0.5;
  1426.      }
  1427.    }
  1428.    return 0;
  1429. }
  1430.  
  1431. /*****************************************************************************
  1432. subroutine : swapint
  1433. input      : *tmpint -> pointer to the integer to be swapped
  1434. output     : *tmpint
  1435. description: swaps a 4-byte word to convert from a little endian to a big
  1436.              endian format
  1437. *****************************************************************************/
  1438.   
  1439. void swapint(int *tmpint) {
  1440.    unsigned short *ptr;
  1441.  
  1442.    ptr=(unsigned short *) tmpint;
  1443.    *ptr=(*ptr<<8)|((*ptr&0xff00)>>8);
  1444.    ptr++;
  1445.    *ptr=(*ptr<<8)|((*ptr&0xff00)>>8);
  1446.    *tmpint=(*tmpint<<16)|((*tmpint&0xffff0000)>>16);
  1447. }
  1448.