]> Pileus Git - ~andy/rsl/blobdiff - nsig.c
RSL v1.44
[~andy/rsl] / nsig.c
diff --git a/nsig.c b/nsig.c
index 038b7de6b16e79fb0056b61393fafd2a751c0128..e7454cac866cf384a4e5d45a4dfe883425d3d15b 100644 (file)
--- a/nsig.c
+++ b/nsig.c
@@ -69,11 +69,11 @@ FILE *nsig_open(char *file_name)
 
    /* Open input file */
   if (file_name == NULL) { /* Use stdin */
-       save_fd = dup(0);
-       fp = fdopen(save_fd, "r");
+    save_fd = dup(0);
+    fp = fdopen(save_fd, "r");
   } else if((fp = fopen(file_name,"r")) == NULL) {
-       perror(file_name);
-       return fp;
+    perror(file_name);
+    return fp;
   }
 
   fp = uncompress_pipe(fp); /* Transparently gunzip. */
@@ -98,8 +98,8 @@ int nsig_read_record(FILE *fp, char *nsig_rec)
 
    if (feof(fp)) return -1;
    nbytes = 0;
-   while((n = fread(&buf[nbytes], sizeof(char),NSIG_BLOCK-nbytes, fp)) > 0) {
-        nbytes += n;
+   while((n = fread(&buf[nbytes], sizeof(char), NSIG_BLOCK-nbytes, fp)) > 0) {
+     nbytes += n;
    }
    return nbytes;
 }
@@ -110,7 +110,7 @@ int nsig_read_record(FILE *fp, char *nsig_rec)
  *********************************************************/
 void nsig_close(FILE *fp)
    {
-        rsl_pclose(fp);
+     rsl_pclose(fp);
    }
 
 static int do_swap;
@@ -125,15 +125,15 @@ int nsig_endianess(NSIG_Record1 *rec1)
   printf("id = %d %d\n", (int)rec1->struct_head.id[0], (int)rec1->struct_head.id[1]);
   */
   if (rec1->struct_head.id[0] == 0) { /* Possible little-endian */
-       if (rec1->struct_head.id[1] >= 20)
-         /* This is a big-endian file. Typically, version 2. */
-         do_swap = little_endian();
-       else
-         do_swap = big_endian();
+    if (rec1->struct_head.id[1] >= 20)
+      /* This is a big-endian file. Typically, version 2. */
+      do_swap = little_endian();
+    else
+      do_swap = big_endian();
   } else if ((rec1->struct_head.id[1] == 0)) { /* Possible big-endian */
-       if (rec1->struct_head.id[0] <= 7)
-         /* This is a little-endian file.  Version 1. */
-         do_swap = big_endian();
+    if (rec1->struct_head.id[0] <= 7)
+      /* This is a little-endian file.  Version 1. */
+      do_swap = big_endian();
   }
   /*
   printf("DO SWAP = %d\n", do_swap);
@@ -170,16 +170,16 @@ void nsig_free_sweep(NSIG_Sweep **s)
   int i=0,itype;
   if (s == NULL) return;
   for (itype=0; itype<s[0]->nparams; itype++) {
-       if (s[itype] == NULL) continue;
-       
-       if (s[itype]->idh.data_type == NSIG_DTB_EXH)
-         free(s[itype]->ray[i]);
-       else {
-         for (i=0; i<NSIG_I2(s[itype]->idh.num_rays_act); i++)
-               nsig_free_ray(s[itype]->ray[i]);
-       }
-       free(s[itype]->ray);
-       free(s[itype]);
+    if (s[itype] == NULL) continue;
+    
+    if (s[itype]->idh.data_type == NSIG_DTB_EXH)
+      free(s[itype]->ray[i]);
+    else {
+      for (i=0; i<NSIG_I2(s[itype]->idh.num_rays_act); i++)
+        nsig_free_ray(s[itype]->ray[i]);
+    }
+    free(s[itype]->ray);
+    free(s[itype]);
   }
   free(s);
 }
@@ -207,102 +207,102 @@ int nsig_read_chunk(FILE *fp, char *chunk)
 #define Vprint
 #undef  Vprint
   while(the_code != 1) {
-       if (feof(fp)) return -1;
-       if (ipos == sizeof(data)) { /* the_code is in the next chunk */
+    if (feof(fp)) return -1;
+    if (ipos == sizeof(data)) { /* the_code is in the next chunk */
 #ifdef Vprint
-         printf("Exceeded block size looking for the_code. Get it from next buffer.\n");
+      printf("Exceeded block size looking for the_code. Get it from next buffer.\n");
 #endif
-         n = nsig_read_record(fp, (char *)data);
-         if (n <= 0) return n;  /* Problem. */
+      n = nsig_read_record(fp, (char *)data);
+      if (n <= 0) return n;  /* Problem. */
 
 #ifdef Vprint
-         printf("Read %d bytes.\n", n);
-         printf("Resetting ipos\n");
+      printf("Read %d bytes.\n", n);
+      printf("Resetting ipos\n");
 #endif
-         ipos = sizeof(NSIG_Raw_prod_bhdr);
-       }
-         
+      ipos = sizeof(NSIG_Raw_prod_bhdr);
+    }
+      
 #ifdef Vprint
-       printf("ipos = %d -- ", ipos);
+    printf("ipos = %d -- ", ipos);
 #endif
-       the_code = NSIG_I2(&data[ipos]);
+    the_code = NSIG_I2(&data[ipos]);
 #ifdef Vprint
-       printf("the_code = %d (%d) -- ", the_code, (unsigned short)the_code);
+    printf("the_code = %d (%d) -- ", the_code, (unsigned short)the_code);
 #endif
-       ipos += sizeof(twob);
-       
-       if (the_code < 0) {  /* THIS IS DATA */
-         nwords = the_code & 0x7fff;
+    ipos += sizeof(twob);
+    
+    if (the_code < 0) {  /* THIS IS DATA */
+      nwords = the_code & 0x7fff;
 #ifdef Vprint
-         printf("#data words (2-bytes) is %d\n", nwords);
+      printf("#data words (2-bytes) is %d\n", nwords);
 #endif
-         if (ipos + sizeof(twob)*nwords > sizeof(data)) {
+      if (ipos + sizeof(twob)*nwords > sizeof(data)) {
 #ifdef Vprint
-               printf("Exceeded block size... transferring and reading new (i=%d).\n", i);
+        printf("Exceeded block size... transferring and reading new (i=%d).\n", i);
 #endif
-               /* Need another phyical block. */
-                       
-               /* But, first transfer the remainder to the output chunk. */
-               /* And, transfer begining of next block */
-               /* Transfer end of current buffer. */
-               end_nwords = (NSIG_BLOCK - ipos)/sizeof(twob);
-               memmove(&chunk[i], &data[ipos], sizeof(twob)*end_nwords);
-           i += end_nwords * sizeof(twob);
-
-               n = nsig_read_record(fp, (char *)data);
-               if (n <= 0) return n;  /* Problem. */
-               /* New ipos */
-               nwords -= end_nwords;
-               ipos = sizeof(NSIG_Raw_prod_bhdr);
-                       
-               /* Transfer beginning of new buffer */
-               if (i+nwords * sizeof(twob) > NSIG_BLOCK) return -1;
-               memmove(&chunk[i], &data[ipos], sizeof(twob) * nwords);
-               i += nwords * sizeof(twob);
+        /* Need another phyical block. */
+            
+        /* But, first transfer the remainder to the output chunk. */
+        /* And, transfer begining of next block */
+        /* Transfer end of current buffer. */
+        end_nwords = (NSIG_BLOCK - ipos)/sizeof(twob);
+        memmove(&chunk[i], &data[ipos], sizeof(twob)*end_nwords);
+        i += end_nwords * sizeof(twob);
+
+        n = nsig_read_record(fp, (char *)data);
+        if (n <= 0) return n;  /* Problem. */
+        /* New ipos */
+        nwords -= end_nwords;
+        ipos = sizeof(NSIG_Raw_prod_bhdr);
+            
+        /* Transfer beginning of new buffer */
+        if (i+nwords * sizeof(twob) > NSIG_BLOCK) return -1;
+        memmove(&chunk[i], &data[ipos], sizeof(twob) * nwords);
+        i += nwords * sizeof(twob);
         ipos += nwords * sizeof(twob);
-               
+        
 #ifdef Vprint
-               printf("Words to transfer (at end of block) is %d\n", end_nwords);
-               printf("Transfer %d words from beginning of next buffer.\n", nwords);
-               printf("ipos in new buffer is %d\n", ipos);
+        printf("Words to transfer (at end of block) is %d\n", end_nwords);
+        printf("Transfer %d words from beginning of next buffer.\n", nwords);
+        printf("ipos in new buffer is %d\n", ipos);
 #endif
-         } else { /* Normal situation.  Position to end of data.
-                               * But, first transfer it to the chunk.
-                         */
-               if (i+nwords * sizeof(twob) > NSIG_BLOCK) return -1;
-               memmove(&chunk[i], &data[ipos], sizeof(twob) * nwords);
-               i += nwords * sizeof(twob);
-               ipos += sizeof(twob) * nwords;
-         }
-                 
-       } else if (the_code == 1) {  /* END OF THE RAY. */
+      } else { /* Normal situation.  Position to end of data.
+                * But, first transfer it to the chunk.
+              */
+        if (i+nwords * sizeof(twob) > NSIG_BLOCK) return -1;
+        memmove(&chunk[i], &data[ipos], sizeof(twob) * nwords);
+        i += nwords * sizeof(twob);
+        ipos += sizeof(twob) * nwords;
+      }
+          
+    } else if (the_code == 1) {  /* END OF THE RAY. */
 #ifdef Vprint
-         printf("------------------------------> Reached end of ray.\n");
+      printf("------------------------------> Reached end of ray.\n");
 #endif
-         break; /* or continue; */
+      break; /* or continue; */
 
-       } else if (the_code == 0) {  /* UNKNOWN */
-         break;
-       } else {  /* NUMBER OF ZERO's */
+    } else if (the_code == 0) {  /* UNKNOWN */
+      break;
+    } else {  /* NUMBER OF ZERO's */
 #ifdef Vprint
-         printf("#000000000000 to skip is %d (i=%d)\n", the_code, i);
+      printf("#000000000000 to skip is %d (i=%d)\n", the_code, i);
 #endif
-         if (i+the_code * sizeof(twob) > NSIG_BLOCK) return -1;
-         memset(&chunk[i], 0, the_code*sizeof(twob));
-         i += the_code * sizeof(twob);
-       }
-               
-       if (ipos >= sizeof(data)) {
+      if (i+the_code * sizeof(twob) > NSIG_BLOCK) return -1;
+      memset(&chunk[i], 0, the_code*sizeof(twob));
+      i += the_code * sizeof(twob);
+    }
+        
+    if (ipos >= sizeof(data)) {
 #ifdef Vprint
-         printf("Exceeded block size ... ipos = %d\n", ipos);
-         printf("This should be right at the end of the block.\n");
+      printf("Exceeded block size ... ipos = %d\n", ipos);
+      printf("This should be right at the end of the block.\n");
 #endif
-         n = nsig_read_record(fp, (char *)data);
-         if (n <= 0) return n; /* Problem. */
-         ipos = sizeof(NSIG_Raw_prod_bhdr);
-       }
-       
-  } /* End while. */                                                    
+      n = nsig_read_record(fp, (char *)data);
+      if (n <= 0) return n; /* Problem. */
+      ipos = sizeof(NSIG_Raw_prod_bhdr);
+    }
+    
+  } /* End while. */                             
   return i;
 }
 
@@ -354,14 +354,14 @@ NSIG_Ray *nsig_read_ray(FILE *fp)
   if (n == 0) return NULL; /* Silent error. */
 
   if (n < 0) {
-       fprintf(stderr, "nsig_read_ray: chunk return code = %d.\n", n);
-       return NULL;
+    fprintf(stderr, "nsig_read_ray: chunk return code = %d.\n", n);
+    return NULL;
   }
 
   if (n > NSIG_BLOCK) { /* Whoa! */
-       fprintf(stderr, "nsig_read_ray: chunk bigger than buffer. n = %d,\
+    fprintf(stderr, "nsig_read_ray: chunk bigger than buffer. n = %d,\
  maximum block size allowed is %d\n", n, NSIG_BLOCK);
-       return NULL;
+    return NULL;
   }
 
   ray = (NSIG_Ray *) calloc(1, sizeof(NSIG_Ray));
@@ -376,8 +376,13 @@ NSIG_Ray *nsig_read_ray(FILE *fp)
 #ifdef Vprint
   printf("               rayh.num_bins = %d (nbins %d, n %d)\n", NSIG_I2(rayh.num_bins), nbins, n);
 #endif
+  ray->range = (unsigned char *)calloc(n, sizeof(unsigned char));
+  /* Changed calloc nbins to calloc n for 2-byte data.
   ray->range = (unsigned char *)calloc(nbins, sizeof(unsigned char));
   memmove(ray->range, &chunk[sizeof(NSIG_Ray_header)], nbins);
+     Can remove this commented-out code once we know changes work.
+  */
+  memmove(ray->range, &chunk[sizeof(NSIG_Ray_header)], n);
   
   return ray;
 }
@@ -386,11 +391,12 @@ NSIG_Ray *nsig_read_ray(FILE *fp)
 NSIG_Sweep **nsig_read_sweep(FILE *fp, NSIG_Product_file *prod_file)
 {
   NSIG_Sweep **s;
-  int i, n;
-  static NSIG_Ingest_data_header **idh = NULL;
-  static NSIG_Raw_prod_bhdr *bhdr = NULL;
+  int i, j, n;
+  NSIG_Ingest_data_header **idh = NULL;
+  NSIG_Raw_prod_bhdr *bhdr = NULL;
   NSIG_Ray *nsig_ray;
   int data_mask, iray, nrays[12], max_rays;
+  int masks[5];
   int nparams;
   int is_new_ray;
   int idtype[12];
@@ -441,12 +447,25 @@ NSIG_Sweep **nsig_read_sweep(FILE *fp, NSIG_Product_file *prod_file)
   (void)nsig_endianess(&prod_file->rec1);
   
   /* Setup the array of ingest data headers [0..nparams-1] */
+#ifdef NSIG_VER2
+  memmove(&masks[0], prod_file->rec2.task_config.dsp_info.data_mask_cur.mask_word_0,
+    sizeof(fourb));
+  memmove(&masks[1], &prod_file->rec2.task_config.dsp_info.data_mask_cur.mask_word_1,
+    4*sizeof(fourb));
+  nparams = 0;
+  for (j=0; j < 5; j++) {
+    data_mask = masks[j];
+    for (i=0; i<32; i++)
+      nparams += (data_mask >> i) & 0x1;
+  }
+#else
   memmove(&data_mask, prod_file->rec2.task_config.dsp_info.data_mask, sizeof(fourb));
+  for (nparams=i=0; i<32; i++)
+    nparams += (data_mask >> i) & 0x1;
 #ifdef Vprint
   printf("data_mask %x\n", data_mask);
 #endif
-  for (nparams=i=0; i<32; i++)
-       nparams += (data_mask >> i) & 0x1;
+#endif
   /* Number of sweeps */
 #ifdef Vprint
   {int nsweeps;
@@ -457,13 +476,13 @@ NSIG_Sweep **nsig_read_sweep(FILE *fp, NSIG_Product_file *prod_file)
 
 
   if (idh == NULL) {
-       
-       idh = (NSIG_Ingest_data_header **)calloc(nparams, sizeof(NSIG_Ingest_data_header *));
-       ipos = 0;
-       for (i=0; i<nparams; i++) {
-         idh[i] = (NSIG_Ingest_data_header *)&data[sizeof(NSIG_Raw_prod_bhdr) + i*sizeof(NSIG_Ingest_data_header)];
-       }
-       bhdr = (NSIG_Raw_prod_bhdr *)prod_file->data;
+    
+    idh = (NSIG_Ingest_data_header **)calloc(nparams, sizeof(NSIG_Ingest_data_header *));
+    ipos = 0;
+    for (i=0; i<nparams; i++) {
+      idh[i] = (NSIG_Ingest_data_header *)&data[sizeof(NSIG_Raw_prod_bhdr) + i*sizeof(NSIG_Ingest_data_header)];
+    }
+    bhdr = (NSIG_Raw_prod_bhdr *)prod_file->data;
   }
 
   xh_size = NSIG_I2(prod_file->rec2.ingest_head.size_ext_ray_headers);
@@ -503,32 +522,32 @@ NSIG_Sweep **nsig_read_sweep(FILE *fp, NSIG_Product_file *prod_file)
   /* Now pointers to all possible rays. */
   for (i=0; i<nparams; i++) {
   /* Load sweep headers */
-       s[i] = (NSIG_Sweep *) calloc (nparams, sizeof(NSIG_Sweep));
-       s[i]->nparams = nparams;
-       memmove(&s[i]->bhdr, &bhdr, sizeof(NSIG_Raw_prod_bhdr));
-       memmove(&s[i]->idh, idh[i], sizeof(NSIG_Ingest_data_header));
-       s[i]->ray = (NSIG_Ray **) calloc (max_rays, sizeof(NSIG_Ray *));
-  }    
+    s[i] = (NSIG_Sweep *) calloc (nparams, sizeof(NSIG_Sweep));
+    s[i]->nparams = nparams;
+    memmove(&s[i]->bhdr, &bhdr, sizeof(NSIG_Raw_prod_bhdr));
+    memmove(&s[i]->idh, idh[i], sizeof(NSIG_Ingest_data_header));
+    s[i]->ray = (NSIG_Ray **) calloc (max_rays, sizeof(NSIG_Ray *));
+  } 
 
   /* Process this sweep. Keep track of the end of the ray. */
   ipos = sizeof(NSIG_Raw_prod_bhdr); /* Position in the 'data' array */
 
   max_rays = 0;
   for (i=0; i<nparams; i++) {
-       idtype[i] = NSIG_I2(idh[i]->data_type);
-       nrays[i] = (int)NSIG_I2(idh[i]->num_rays_act);
-       if (nrays[i] > max_rays) max_rays = nrays[i];
+    idtype[i] = NSIG_I2(idh[i]->data_type);
+    nrays[i] = (int)NSIG_I2(idh[i]->num_rays_act);
+    if (nrays[i] > max_rays) max_rays = nrays[i];
 #ifdef Vprint
-       printf("New ray: parameter %d has idtype=%d\n", i, idtype[i]);
-       printf("Number of expected rays in sweep %d is %d\n", isweep, (int)NSIG_I2(idh[i]->num_rays_exp));
-       printf("Number of actual   rays in sweep %d is %d\n", isweep,  (int)NSIG_I2(idh[i]->num_rays_act));
+    printf("New ray: parameter %d has idtype=%d\n", i, idtype[i]);
+    printf("Number of expected rays in sweep %d is %d\n", isweep, (int)NSIG_I2(idh[i]->num_rays_exp));
+    printf("Number of actual   rays in sweep %d is %d\n", isweep,  (int)NSIG_I2(idh[i]->num_rays_act));
 #endif
 
   }
   if (is_new_sweep) 
-       ipos += nparams * sizeof(NSIG_Ingest_data_header);
+    ipos += nparams * sizeof(NSIG_Ingest_data_header);
   
-  /*   ipos = sizeof(NSIG_Raw_prod_bhdr) + nparams*sizeof(NSIG_Ingest_data_header); */
+  /* ipos = sizeof(NSIG_Raw_prod_bhdr) + nparams*sizeof(NSIG_Ingest_data_header); */
   /* 'iray' is the true ray index into 's', whereas, 'nsig_iray' is what
    * the NSIG file says it is.  I'll trust 'iray'
    *
@@ -564,44 +583,44 @@ NSIG_Sweep **nsig_read_sweep(FILE *fp, NSIG_Product_file *prod_file)
   
   do {
 #ifdef Vprint
-       printf("---------------------- New Ray <%d> --------------------\n", iray);
+    printf("---------------------- New Ray <%d> --------------------\n", iray);
 #endif
-       if (feof(fp)) { /* Premature eof */
-         return NULL; /* This will have to do. */
-       }
-       /* For all parameters present. */
-       is_new_ray = 0;
-       for (i=0; i<nparams; i++) {
-
-         /* Keep track of the cursor within the buffer, and, possibly
+    if (feof(fp)) { /* Premature eof */
+      return NULL; /* This will have to do. */
+    }
+    /* For all parameters present. */
+    is_new_ray = 0;
+    for (i=0; i<nparams; i++) {
+
+      /* Keep track of the cursor within the buffer, and, possibly
        * read another buffer when a ray is split across two NSIG blocks
-          */
-         nsig_ray = NULL;
-         if (idtype[i] != 0) { /* Not an extended header. */
-               nsig_ray = nsig_read_ray(fp);
-
-         } else { /* Check extended header version. */
-               if (xh_size <= 20) {
-                 exh0 = nsig_read_ext_header_ver0(fp);
-                 if (exh0) {
-                       nsig_ray = (NSIG_Ray *)calloc(1, sizeof(NSIG_Ray));
-                       nsig_ray->range = (unsigned char *)exh0;
-                 }
-               } else {
-                 exh1 = nsig_read_ext_header_ver1(fp);
-                 if (exh1) {
-                       nsig_ray = (NSIG_Ray *)calloc(1, sizeof(NSIG_Ray));
-                       nsig_ray->range = (unsigned char *)exh1;
-                 }
-               }
-         }
-         if (nsig_ray) is_new_ray = 1;
-         if (iray > nrays[i]) break;
-         s[i]->ray[iray] = nsig_ray;
-
-       } /* End for */
-       if (is_new_ray) iray++;
-       
+       */
+      nsig_ray = NULL;
+      if (idtype[i] != 0) { /* Not an extended header. */
+        nsig_ray = nsig_read_ray(fp);
+
+      } else { /* Check extended header version. */
+        if (xh_size <= 20) {
+          exh0 = nsig_read_ext_header_ver0(fp);
+          if (exh0) {
+            nsig_ray = (NSIG_Ray *)calloc(1, sizeof(NSIG_Ray));
+            nsig_ray->range = (unsigned char *)exh0;
+          }
+        } else {
+          exh1 = nsig_read_ext_header_ver1(fp);
+          if (exh1) {
+            nsig_ray = (NSIG_Ray *)calloc(1, sizeof(NSIG_Ray));
+            nsig_ray->range = (unsigned char *)exh1;
+          }
+        }
+      }
+      if (nsig_ray) is_new_ray = 1;
+      if (iray > nrays[i]) break;
+      s[i]->ray[iray] = nsig_ray;
+
+    } /* End for */
+    if (is_new_ray) iray++;
+    
   } while (iray < max_rays);
 #ifdef Vprint
   printf("iray = %d\n", iray);