Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sdd.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <gsl/gsl_math.h>
11 
12 #include "observing_par.h"
13 
14 #include "scans.h"
15 #include "global.h"
16 
17 #include "linuxlib_proto.h"
18 
19 #define BLOCK 512
20 #define OFFSET(x,f) ((short)(((int)&x->f - ((int)x))/8 + 1))
21 #define MAX_SCANS 16384 /* number of directory entrys, for new file */
22 
25 extern int verboseLevel;
26 
28 static struct BOOTSTRAP boot;
29 
30 extern struct HEADER *sa;
31 
32 static int lp_pdopen(char *name, int mode);
33 static int lp_findNumOfSources(void);
34 static int lp_findFreqInSource(struct SOURCES *p, double f);
35 static int lp_addFreqToSource(struct SOURCES *p, double f);
36 static int lp_addSourceToList(char *name, int mode, double freq);
37 static int lp_RfreqSort(struct SOURCES *a, struct SOURCES *b);
38 static int lp_freqSort(struct SOURCES *a, struct SOURCES *b);
39 static float lp_findNextGoodPoint(float *d, int indx, int l);
40 static float lp_findPrevGoodPoint(float *d, int indx, int l);
41 //static int lp_checkBounds(float *d, int l, int autobad);
42 static int lp_blocks(int b);
43 static int lp_padd(char *s, int n);
44 static double lp_index_freqres(struct HEADER *h);
45 static double lp_index_restfreq(struct HEADER *h);
46 static int lp_obsmode(char *o);
47 static int lp_pos_code(char *s);
48 static int lp_write_dir(int fd, struct DIRECTORY *d, int len);
49 static int lp_write_boot(int fd, struct BOOTSTRAP *b, int len);
50 static int lp_close(int *fd);
51 
52 
53 float *otfData = NULL;
54 
55 
56 static int lp_close(fd)
57 int *fd;
58 {
60  {
61  st_report("Closing: fd = %d", *fd);
62  }
63 
64  close(*fd);
65 
66  *fd = 0;
67 
68  return(0);
69 }
70 
72 struct HEADER *h;
73 {
74  /* fix the 99999 tsys when in CONST cal mode */
75  if(!strstr(h->obsmode, "CAL"))
76  {
77  if(!strncmp(h->typecal, "CONST", 5))
78  {
79  if(h->stsys > 2000.0 || h->stsys < 50.0)
80  {
81  h->stsys = h->tcal / 2.0;
82  }
83  }
84  }
85 
86  return(0);
87 }
88 
89 
90 static int lp_pdopen(name, mode)
91 char *name;
92 int mode;
93 {
94  int fd;
95  struct stat statb;
96 
97  if( (fd = open(name, mode, 0666 ))<0)
98  {
99  st_fail("lp_pdopen(): Error opening: '%s'", name );
100  return(-1);
101  }
102 
103  if(mode == O_RDONLY) /* Is this the online file ?? */
104  {
105  fstat(fd, &statb);
106  lastFileSize = statb.st_size;
107  }
108 
110  {
111  st_report("Opening: %s, fd = %d", name, fd);
112  }
113 
114  return(fd);
115 }
116 
117 
119 struct ENVIRONMENT *env;
120 {
121  int fd, n;
122 
123  if( (fd = lp_pdopen(env->filename, O_RDONLY))<0)
124  {
125  return(-1);
126  }
127 
128  if( read( fd, (char *)&boot, sizeof(struct BOOTSTRAP))<0)
129  {
130  st_perror("read boot");
131  lp_close(&fd);
132 
133  return(-1);
134  }
135 
136  if(boot.version != 1)
137  {
138  st_fail("File not of correct vintage; Consider using 'sdd convert'");
139 
140  return(-1);
141  }
142 
143  lp_close(&fd);
144 
145  n = boot.num_entries_used+1;
146 
147  if(sa)
148  {
149  free(sa);
150  }
151 
152  if( (sa = (struct HEADER *)calloc(n, sizeof(struct HEADER))) == NULL)
153  {
154  fprintf(stderr,"Error in findSize( malloc() )\n");
155 
156  return(-1);
157  }
158 
159  return(0);
160 }
161 
162 
163 static int lp_findNumOfSources()
164 {
165  int i;
166  static struct SOURCES *p;
167 
168  numOfSources = 0;
169 
170  p = &sources[0];
171  for(i=0;i<MAX_SOURCE_LIST;i++,p++)
172  {
173  if(p->used)
174  {
175  numOfSources++;
176  }
177  }
178 
179  return(0);
180 }
181 
182 
184 char *name;
185 {
186  int i;
187  static struct SOURCES *p;
188 
189  p = &sources[0];
190 
191  for(i=0;i<MAX_SOURCE_LIST;i++,p++)
192  {
193  if(p->used && !strcmp(name, p->name))
194  {
195  return(p);
196  }
197  }
198 
199  return((struct SOURCES *)NULL);
200 }
201 
202 
203 static int lp_findFreqInSource(p, f)
204 struct SOURCES *p;
205 double f;
206 {
207  int i;
208 
209  for(i=1;i<MAX_FREQS;i++)
210  {
211  if(p->freqs[i].used)
212  {
213  if(fabs(f - p->freqs[i].freq) < 0.1) /* Compare in GHz */
214  {
215  return(i);
216  }
217  }
218  }
219 
220  return(0);
221 }
222 
223 
224 static int lp_addFreqToSource(p, f)
225 struct SOURCES *p;
226 double f;
227 {
228  int found, i;
229 
230  found = lp_findFreqInSource(p, f);
231 
232  if(found)
233  {
234  p->freqs[found].count++;
235 
236  return(0);
237  }
238 
239  for(i=1;i<MAX_FREQS;i++)
240  {
241  if(!p->freqs[i].used)
242  {
243  p->freqs[i].used = 1;
244  p->freqs[i].count = 1;
245  p->freqs[i].freq = f;
246 
247  p->nfreqs++;
248 
249  return(0);
250  }
251  }
252 
253  return(0);
254 }
255 
256 
257 
258 static int lp_addSourceToList(name, mode, freq)
259 char *name;
260 int mode;
261 double freq;
262 {
263  int i;
264  struct SOURCES *p;
265 
266  p = findSourceInList(name);
267 
268  if(p) /* Already have a source; Bump it's count */
269  {
270  p->num++;
271 
272  lp_addFreqToSource(p, freq);
273 
274  return(0);
275  }
276 
277  /* Look for an empty one */
278  p = &sources[0];
279  for(i=0;i<MAX_SOURCE_LIST;i++,p++)
280  {
281  if(!p->used)
282  {
283  strcpy(p->name, name);
284 
285  p->num++;
286  p->used = 1;
287  p->mode = mode;
288 
289  lp_addFreqToSource(p, freq);
290 
291  return(0);
292  }
293  }
294 
295 
296  return(1);
297 }
298 
299 
301 {
302  bzero((char *)&sources, sizeof(sources));
303 
304  return(0);
305 }
306 
307 
309 {
310  int i, n=0;
311  struct SOURCES *p;
312 
313  p = &sources[0];
314 
315  st_print(" Source Num-of-Scans\n\n");
316 
317  for(i=0;i<MAX_SOURCE_LIST;i++,p++)
318  {
319  if(p->used)
320  {
321  st_print("%16.16s %4d\n", p->name, p->num);
322  n += p->num;
323  }
324  }
325 
326  st_print("\n Total -> %5d\n", n);
327 
328  return(0);
329 }
330 
331 
332 /* Reverse sort */
333 static int lp_RfreqSort(a, b)
334 struct SOURCES *a;
335 struct SOURCES *b;
336 {
337  if(a->nfreqs > b->nfreqs)
338  {
339  return(1);
340  }
341  else
342  if(a->nfreqs < b->nfreqs)
343  {
344  return(-1);
345  }
346 
347  return(0);
348 }
349 
350 
351 static int lp_freqSort(a, b)
352 struct SOURCES *a;
353 struct SOURCES *b;
354 {
355  if(a->nfreqs > b->nfreqs)
356  {
357  return(-1);
358  }
359  else
360  if(a->nfreqs < b->nfreqs)
361  {
362  return(1);
363  }
364 
365  return(0);
366 }
367 
368 
370 {
371  int i, n=0, j, k, found, max;
372  struct SOURCES *p;
373 
374  qsort(sources, MAX_SOURCE_LIST, sizeof(struct SOURCES), (__compar_fn_t)lp_freqSort);
375 
376  p = &sources[0];
377 
378  max = p->nfreqs;
379 
380  st_print(" Source Scans ");
381 
382  for(i=0;i<5;i++)
383  {
384  st_print(" Freq Scans");
385  }
386 
387  st_print("\n\n");
388 
389  for(i=0;i<MAX_SOURCE_LIST;i++,p++)
390  {
391  if(p->used)
392  {
393  found = 0;
394 
395  st_print("%16.16s %4d", p->name, p->num-1); /* -1, I don't know why?? */
396 
397  n += p->num;
398 
399  j = 0;
400  for(k=0;k<MAX_FREQS;k++)
401  {
402  if(p->freqs[k].used)
403  {
404  if(j>0 && !(j%5))
405  {
406  st_print("\n ");
407  }
408 
409  found = 1;
410  st_print(" %10.6f %4d", p->freqs[k].freq / 1000.0, p->freqs[k].count);
411  j++;
412  }
413  }
414 
415  if(found)
416  {
417  st_print("\n");
418  }
419  }
420  }
421 
422  st_print("\n Total -> %6d\n", n);
423 
424  return(0);
425 }
426 
427 
429 char *source;
430 char *buf;
431 int rev;
432 {
433  int i, n=0, k, found = 0;
434  struct SOURCES *p;
435  char tbuf[256];
436 
437  qsort(sources, MAX_SOURCE_LIST, sizeof(struct SOURCES), (__compar_fn_t)lp_freqSort);
438 
439  buf[0] = '\0';
440 
441  if(rev) /* Show it in reverse order */
442  {
443  p = &sources[MAX_SOURCE_LIST-1];
444 
445  for(i=0;i<MAX_SOURCE_LIST;i++, p--) /* Same number of interations */
446  {
447  if(p->used)
448  {
449  n += p->num;
450 
451  for(k=0;k<MAX_FREQS;k++)
452  {
453  if(p->freqs[k].used)
454  {
455  /* Leave the space before name so sources with a '-' in the front of their name
456  don't get interpeted as a switch option by zenity */
457  sprintf(tbuf, "' %s' '%10.6f' '%4d' ", p->name, p->freqs[k].freq / 1000.0, p->freqs[k].count);
458  strcat(buf, tbuf);
459  found++;
460  }
461  }
462  }
463  }
464  }
465  else
466  {
467  p = &sources[0];
468 
469  for(i=0;i<MAX_SOURCE_LIST;i++,p++)
470  {
471  if(p->used)
472  {
473  n += p->num;
474 
475  for(k=0;k<MAX_FREQS;k++)
476  {
477  if(p->freqs[k].used)
478  {
479  /* Leave the space before name so sources with a '-' in the front of their name
480  don't get interpeted as a switch option by zenity */
481  sprintf(tbuf, "' %s' '%10.6f' '%4d' ", p->name, p->freqs[k].freq / 1000.0, p->freqs[k].count);
482  strcat(buf, tbuf);
483  found++;
484  }
485  }
486  }
487  }
488  }
489 
490  return(found);
491 }
492 
493 
494 
496 struct ENVIRONMENT *env;
497 {
498  int fd, max, dirl, bytesinindex, bytesused, num, j = 0;
499  int sizeofDIR, first = 1;
500  struct DIRECTORY dir;
501  struct HEADER head;
502  char object[80];
503 
504  clearSourceList();
505 
506  sizeofDIR = sizeof(struct DIRECTORY);
507 
508  if( (fd = lp_pdopen(env->filename, O_RDONLY))<0)
509  {
510  return(-1);
511  }
512 
513  if(read( fd, (char *)&boot, sizeof(struct BOOTSTRAP))<0)
514  {
515  st_perror("read boot");
516  lp_close(&fd);
517 
518  return(-1);
519  }
520 
521  /* check bootstrap block */
522 
523  if( sizeof(struct DIRECTORY) != boot.bytperent)
524  {
525  fprintf(stderr,"directory sizes dont match\n");
526  lp_close(&fd);
527 
528  return(-1);
529  }
530 
531  bytesinindex = boot.num_index_rec * boot.bytperrec;
532  bytesused = boot.num_entries_used * boot.bytperent;
533 
534  if( bytesinindex < bytesused+sizeof(struct DIRECTORY))
535  {
536  fprintf(stderr,"Directory structure of %s is full.\n", env->filename );
537  lp_close(&fd);
538 
539  return(-1);
540  }
541 
542  if( boot.typesdd || !boot.version)
543  {
544  fprintf(stderr, "SDD version or type of %s is invalid.\n", env->filename );
545  lp_close(&fd);
546 
547  return(-1);
548  }
549 
550  if(boot.bytperent != sizeof(struct DIRECTORY))
551  {
552  fprintf(stderr,"File %s has faulty direcory size\n", env->filename );
553  lp_close(&fd);
554 
555  return(-1);
556  }
557 
558  if( boot.num_entries_used <= 0)
559  {
560  fprintf(stderr,"File %s is empty\n", env->filename);
561  lp_close(&fd);
562 
563  return(-1);
564  }
565 
566  num = 0;
567  while(num < boot.num_entries_used)
568  {
569  dirl = num * boot.bytperent + boot.bytperrec;
570  lseek( fd, (off_t)dirl, SEEK_SET );
571 
572  if(read( fd, (char *)&dir, sizeofDIR ) != sizeofDIR)
573  {
574  st_perror("readdir");
575  lp_close(&fd);
576 
577  return(-1);
578  }
579 
580  /* It is possible that some files have holes in them; i.e. save files.
581  Check here */
582  if(dir.start_rec == 0 || dir.end_rec == 0)
583  {
584  num++;
585  continue;
586  }
587 
588  /* Check that this scan is of the correct type before
589  wasting anymore time on this one */
590  if(env->mode == MODE_CONT)
591  {
592  if((dir.obsmode & 0x01ff) == OTF)
593  {
594  num++;
595  continue;
596  }
597  }
598  else
599  if(env->mode == MODE_SPEC)
600  {
601  if((dir.obsmode & 0x01ff) == OTF)
602  {
603  num++;
604  continue;
605  }
606  }
607  else
608  if(env->mode == MODE_OTF)
609  {
610  }
611 
612  max = boot.bytperrec * (dir.start_rec-1);
613  lseek( fd, (off_t)max, SEEK_SET );
614 
615  if(read(fd, (char *)&head, sizeof(struct HEADER) )<0)
616  {
617  st_perror("read header");
618  lp_close(&fd);
619 
620  return(-1);
621  }
622 
623  checkConstCal(&head);
624 
625  if(env->mode == MODE_CONT)
626  {
627  if(!strncmp(head.obsmode, "LINE", 4))
628  {
629  num++;
630  continue;
631  }
632  }
633  else
634  if(env->mode == MODE_SPEC)
635  {
636  if(!strncmp(head.obsmode, "CONT", 4))
637  {
638  num++;
639  continue;
640  }
641 
642  /* Check if this is a TP OFF associated with a OTF Map */
643  if(!strncmp(head.obsmode, "LINETPMF", 8))
644  {
645  if(fabs(head.openpar[5] - 6666.0) < 1.0) /* Check for marker */
646  {
647  num++;
648  continue;
649  }
650  }
651  }
652  else
653  if(env->mode == MODE_OTF)
654  {
655  if(!strncmp(head.obsmode, "CONT", 4))
656  {
657  num++;
658  continue;
659  }
660  }
661 
662 
663  strncpy(object, head.object, 16);
664  object[16] = '\0';
665  strcmprs(object);
666 
667  if(!strncmp(head.obsmode, "CONT", 4)) /* Look in obsfreq for continuum */
668  {
669  if(head.restfreq < 11000.0)
670  {
671  lp_addSourceToList(object, dir.obsmode, head.obsfreq);
672  }
673  else
674  {
675  lp_addSourceToList(object, dir.obsmode, head.restfreq);
676  }
677  }
678  else
679  {
680  lp_addSourceToList(object, dir.obsmode, head.restfreq);
681  }
682 
683  if(first)
684  {
685  if(!strncmp(head.telescop, "ARO-HHT", 7))
686  {
687  st_report("Data File is SMT Data");
688  env->issmt = 1;
689  }
690  else
691  if(!strncmp(head.telescop, "NRAO 12M", 8))
692  {
693  st_report("Data File is 12M Data");
694  env->issmt = 0;
695  }
696  else
697  {
698  st_warn("Data File is Unknown Data: %8.8s", head.telescop);
699  env->issmt = 0;
700  }
701 
702  first = 0;
703  }
704 
705  head.align3[0] = (double)num; /* Save the directory entry here */
706 
707  bcopy((char *)&head, (char *)&sa[j], sizeof(struct HEADER));
708 
709  num++;
710  j++;
711 
712  if(num > MAX_SCANS)
713  {
714  fprintf(stderr, "File %s contains too many scans\n", env->filename);
715 
716  lp_findNumOfSources();
717 
718  return(num);
719  }
720  }
721 
722  lp_close(&fd);
723 
724  lp_findNumOfSources();
725 
726  return(j);
727 }
728 
729 
730 
731 static float lp_findNextGoodPoint(d, indx, l)
732 float *d;
733 int indx, l;
734 {
735  int i;
736  float good=0.0;
737 
738  for(i=indx;i<l;i++)
739  {
740  if(d[i] > 1e-20 || d[i] < 1e-21)
741  {
742  good = d[i];
743  break;
744  }
745  }
746 
747  return(good);
748 }
749 
750 
751 
752 static float lp_findPrevGoodPoint(d, indx, l)
753 float *d;
754 int indx, l;
755 {
756  int i;
757  float good=0.0;
758 
759  for(i=indx;i>0;i--)
760  {
761  if(d[i] > 1e-20 || d[i] < 1e-21)
762  {
763  good = d[i];
764  break;
765  }
766  }
767 
768  return(good);
769 }
770 
771 
772 int lp_checkBounds(d, l, autobad)
773 float *d;
774 int l, autobad;
775 {
776  int i, j = 0;
777  float next, prev;
778  double avg = 0.0;
779  extern struct ENVIRONMENT *env;
780 
781  if(env->oob_val)
782  {
783  avg = env->oob_val;
784  }
785  else
786  {
787  for(i=0;i<l;i++)
788  {
789  if(gsl_isnan( (double)d[i]))
790  {
791  continue;
792  }
793 
794  if(gsl_isinf( (double)d[i]))
795  {
796  d[i] = 0.0;
797  continue;
798  }
799 
800  if(fabs((double)d[i]) > 1e9)
801  {
802  continue;
803  }
804 
805  avg += (double)d[i];
806  j++;
807  }
808 
809  if(j)
810  {
811  avg /= (double)j;
812  }
813  }
814 
815  for(i=0;i<l;i++)
816  {
817  if(gsl_isnan( (double)d[i]))
818  {
819  st_print("Chan %d was NAN\n", i);
820  d[i] = avg;
821  }
822 
823  if(gsl_isinf( (double)d[i]))
824  {
825  st_print("Chan %d was INF\n", i);
826  d[i] = avg;
827  }
828 
829  if(fabs((double)d[i]) > 1e9)
830  {
831  st_print("Chan %d was OOB\n", i);
832  d[i] = avg;
833  }
834 
835  /* Automatically remove bad channels */
836  /* I readly need a better look up for good channels */
837  if(autobad && (d[i] < 1e-20 && d[i] > 1e-21))
838  {
839  if(autobad > 1) /* Verbose ? */
840  {
841  st_print("Smoothing Bad Channel %d\n", i);
842  }
843 
844  if(i == 0) /* First channel ?? */
845  {
846  next = lp_findNextGoodPoint(d, i, l);
847 
848  d[i] = next;
849  }
850  else
851  if(i == l-1) /* Last channel ?? */
852  {
853  prev = lp_findPrevGoodPoint(d, i, l);
854 
855  d[i] = prev;
856  }
857  else
858  {
859  next = lp_findNextGoodPoint(d, i, l);
860  prev = lp_findPrevGoodPoint(d, i, l);
861 
862  d[i] = (prev + next) / 2.0; /* Average them */
863  }
864  }
865 
866  }
867 
868  return(0);
869 }
870 
871 
872 int read_scan(scan, filename, data, head, chk )
873 float scan;
874 char *filename;
875 float *data;
876 struct HEADER *head;
877 int chk;
878 {
879  int fd, dirl, bytesinindex, bytesused, num, max;
880  int l, sizeofDIR;
881  struct DIRECTORY dir;
882  extern struct ENVIRONMENT *env;
883 
884  sizeofDIR = sizeof(struct DIRECTORY);
885 
886  if( (fd = lp_pdopen(filename, O_RDONLY))<0)
887  {
888  return(-1);
889  }
890 
891  if( read( fd, (char *)&boot, sizeof(struct BOOTSTRAP))<0)
892  {
893  st_perror("read boot");
894  lp_close(&fd);
895 
896  return(-1);
897  }
898 
899  /* check bootstrap block */
900 
901  if( sizeof(struct DIRECTORY) != boot.bytperent)
902  {
903  fprintf(stderr,"directory sizes dont match\n");
904  lp_close(&fd);
905 
906  return(-1);
907  }
908 
909  bytesinindex = boot.num_index_rec * boot.bytperrec;
910  bytesused = boot.num_entries_used * boot.bytperent;
911 
912  if( bytesinindex < bytesused+sizeof(struct DIRECTORY))
913  {
914  fprintf(stderr,"Directory structure of %s is full.\n", filename );
915  lp_close(&fd);
916 
917  return(-1);
918  }
919 
920  if( boot.typesdd || !boot.version)
921  {
922  fprintf(stderr, "SDD version or type of %s is invalid.\n", filename );
923  lp_close(&fd);
924 
925  return(-1);
926  }
927 
928  if(boot.bytperent != sizeof(struct DIRECTORY))
929  {
930  fprintf(stderr,"File %s has faulty direcory size\n", filename );
931  lp_close(&fd);
932 
933  return(-1);
934  }
935 
936  if(boot.num_entries_used <= 0)
937  {
938  fprintf(stderr,"File %s is empty\n", filename);
939  lp_close(&fd);
940 
941  return(-1);
942  }
943 
944  if(scan < 0.0)
945  {
946  dirl = boot.bytperrec;
947  lseek( fd, (off_t)dirl, SEEK_SET );
948 
949  if(read( fd, (char *)&dir, sizeofDIR ) != sizeofDIR)
950  {
951  st_perror("readdir");
952  lp_close(&fd);
953 
954  return(-1);
955  }
956 
957  /* It is possible that some files have holes in them; i.e. save files.
958  Check here */
959  if(dir.start_rec == 0 || dir.end_rec == 0)
960  {
961  st_fail("Directory Structure Empty for scan: %.2f", scan);
962  lp_close(&fd);
963 
964  return(-1);
965  }
966 
967  dirl = (boot.num_entries_used-1) * boot.bytperent + boot.bytperrec;
968  lseek( fd, (off_t)dirl, SEEK_SET );
969 
970  if(read( fd, (char *)&dir, sizeofDIR ) != sizeofDIR)
971  {
972  st_perror("readdir");
973  lp_close(&fd);
974 
975  return(-1);
976  }
977  }
978  else
979  if(scan > 0.0)
980  {
981  if(env->read_dir == READ_REVERSE) /* default way */
982  {
983  /* CHANGE: Start at the end and read backward looking for scan */
984  /* That way, you find the most recent scan */
985  num = boot.num_entries_used;
986 
987  while(num >= 0)
988  {
989  dirl = num * boot.bytperent + boot.bytperrec;
990  lseek( fd, (off_t)dirl, SEEK_SET );
991 
992  if(read( fd, (char *)&dir, sizeofDIR ) != sizeofDIR)
993  {
994  st_perror("readdir");
995  lp_close(&fd);
996 
997  return(-1);
998  }
999 
1000  /* It is possible that some files have holes in them; i.e. save files.
1001  Check here */
1002  if(dir.start_rec == 0 || dir.end_rec == 0)
1003  {
1004  num--;
1005  continue;
1006  }
1007 
1008  if(fabs(scan-dir.scan) < 0.005)
1009  {
1010  break;
1011  }
1012 
1013  num--;
1014  }
1015 
1016  if(num == -1)
1017  {
1018  fprintf(stderr,"Scan %7.2f NOT Found\n", scan);
1019  lp_close(&fd);
1020 
1021  return(-1);
1022  }
1023  }
1024  else
1025  {
1026  num = 0;
1027 
1028  while(num < boot.num_entries_used)
1029  {
1030  dirl = num * boot.bytperent + boot.bytperrec;
1031 
1032  lseek( fd, (off_t)dirl, SEEK_SET );
1033 
1034  if(read( fd, (char *)&dir, sizeofDIR ) != sizeofDIR )
1035  {
1036  st_perror("readdir");
1037  lp_close(&fd);
1038  return(-1);
1039  }
1040 
1041  /* It is possible that some files have holes in them; i.e. save files.
1042  Check here */
1043  if(dir.start_rec == 0 || dir.end_rec == 0)
1044  {
1045  num++;
1046  continue;
1047  }
1048 
1049  if(fabs(scan-dir.scan) < 0.005)
1050  {
1051  break;
1052  }
1053 
1054  num++;
1055  }
1056 
1057  if(num >= boot.num_entries_used)
1058  {
1059  fprintf(stderr,"Scan %7.2f NOT Found\n", scan);
1060  lp_close(&fd);
1061 
1062  return(-1);
1063  }
1064  }
1065  }
1066 
1067  max = boot.bytperrec * (dir.start_rec-1);
1068  lseek( fd, (off_t)max, SEEK_SET );
1069 
1070  if(read(fd, (char *)head, sizeof(struct HEADER) )<0)
1071  {
1072  st_perror("read header");
1073  lp_close(&fd);
1074 
1075  return(-1);
1076  }
1077 
1078  head->align3[0] = (double)num; /* Save the directory entry here */
1079 
1080  checkConstCal(head);
1081 
1082  if(scan < 0.0)
1083  {
1084  lp_close(&fd);
1085 
1086  return((int)head->scan);
1087  }
1088 
1089  if(chk == -1) /* caller just wanted headers, no data */
1090  {
1091  lp_close(&fd);
1092 
1093  return((int)head->scan);
1094  }
1095 
1096  /* Do NOT read OTF data into these passed arrays;
1097  They are TOO small for OTF data,
1098  use malloc() instead. */
1099  if(!strncmp(head->obsmode, "LINEOTF", 7))
1100  {
1101  st_report("Scan is of OTF type: Using Internal data array");
1102 
1103  if(otfData)
1104  {
1105  free(otfData);
1106  }
1107 
1108  if( (otfData = (float *)malloc(head->datalen)) == NULL)
1109  {
1110  fprintf(stderr, "Error in otfData( malloc() )\n");
1111 
1112  lp_close(&fd);
1113 
1114  return(-1);
1115  }
1116 
1117  bzero( (char *)otfData, head->datalen);
1118 
1119  if( read( fd, (char *)otfData, (unsigned)head->datalen) <0)
1120  {
1121  st_perror("read data");
1122  fprintf(stderr,"Error reading scan %7.2f data\n", scan);
1123 
1124  lp_close(&fd);
1125 
1126  return(-1);
1127  }
1128  }
1129  else
1130  {
1131  bzero( (char *)data, MAX_CHANS * sizeof(float) );
1132 
1133  if( read( fd, (char *)data, (unsigned)head->datalen) <0)
1134  {
1135  st_perror("read data");
1136  fprintf(stderr,"Error reading scan %7.2f data\n", scan);
1137  lp_close(&fd);
1138 
1139  return(-1);
1140  }
1141  /* now check for abnormal float in data array */
1142  l = (unsigned)head->datalen / sizeof(float);
1143 
1144  lp_checkBounds(data, l, chk);
1145  }
1146 
1147  lp_close(&fd);
1148 
1149  return((int)head->scan);
1150 }
1151 
1152 
1153 int read_nsave(nsave, filename, data, head, chk )
1154 int nsave;
1155 char *filename;
1156 float *data;
1157 struct HEADER *head;
1158 int chk;
1159 {
1160  int fd, dirl, bytesinindex, bytesused, num, max;
1161  int l, sizeofDIR;
1162  struct DIRECTORY dir;
1163 
1164  sizeofDIR = sizeof(struct DIRECTORY);
1165 
1166  if( (fd = lp_pdopen(filename, O_RDONLY))<0)
1167  {
1168  return(-1);
1169  }
1170 
1171  if( read( fd, (char *)&boot, sizeof(struct BOOTSTRAP))<0)
1172  {
1173  st_perror("read boot");
1174  lp_close(&fd);
1175 
1176  return(-1);
1177  }
1178 
1179  /* check bootstrap block */
1180 
1181  if( sizeof(struct DIRECTORY) != boot.bytperent)
1182  {
1183  fprintf(stderr,"directory sizes dont match\n");
1184  lp_close(&fd);
1185 
1186  return(-1);
1187  }
1188 
1189  bytesinindex = boot.num_index_rec * boot.bytperrec;
1190  bytesused = boot.num_entries_used * boot.bytperent;
1191 
1192  if( bytesinindex < bytesused+sizeof(struct DIRECTORY))
1193  {
1194  fprintf(stderr,"Directory structure of %s is full.\n", filename );
1195  lp_close(&fd);
1196 
1197  return(-1);
1198  }
1199 
1200  if( boot.typesdd || !boot.version)
1201  {
1202  fprintf(stderr, "SDD version or type of %s is invalid.\n", filename );
1203  lp_close(&fd);
1204 
1205  return(-1);
1206  }
1207 
1208  if(boot.bytperent != sizeof(struct DIRECTORY))
1209  {
1210  fprintf(stderr,"File %s has faulty direcory size\n", filename );
1211  lp_close(&fd);
1212 
1213  return(-1);
1214  }
1215 
1216  if(boot.num_entries_used <= 0)
1217  {
1218  fprintf(stderr,"File %s is empty\n", filename);
1219  lp_close(&fd);
1220 
1221  return(-1);
1222  }
1223 
1224  if(nsave >= 0)
1225  {
1226  num = 0;
1227 
1228  while(num < boot.num_entries_used)
1229  {
1230  dirl = num * boot.bytperent + boot.bytperrec;
1231  lseek( fd, (off_t)dirl, SEEK_SET );
1232 
1233  if(read( fd, (char *)&dir, sizeofDIR ) != sizeofDIR)
1234  {
1235  st_perror("readdir");
1236  lp_close(&fd);
1237 
1238  return(-1);
1239  }
1240 
1241  /* It is possible that some files have holes in them; i.e. save files.
1242  Check here */
1243  if(dir.start_rec == 0 || dir.end_rec == 0)
1244  {
1245  num++;
1246  continue;
1247  }
1248 
1249  if( num == nsave )
1250  {
1251  break;
1252  }
1253 
1254  num++;
1255  }
1256 
1257  if( num >= boot.num_entries_used)
1258  {
1259  st_fail("NSAVE %d NOT Found", nsave);
1260  lp_close(&fd);
1261 
1262  return(-1);
1263  }
1264  }
1265 
1266  max = boot.bytperrec * (dir.start_rec-1);
1267  lseek( fd, (off_t)max, SEEK_SET );
1268 
1269  if(read(fd, (char *)head, sizeof(struct HEADER) )<0)
1270  {
1271  st_perror("read header");
1272  lp_close(&fd);
1273 
1274  return(-1);
1275  }
1276 
1277  checkConstCal(head);
1278 
1279  bzero( (char *)data, MAX_CHANS * sizeof(float) );
1280 
1281  if( read( fd, (char *)data, (unsigned)head->datalen) <0)
1282  {
1283  st_perror("read data");
1284  st_fail("Error reading nsave %d data\n", nsave);
1285  lp_close(&fd);
1286 
1287  return(-1);
1288  }
1289 
1290  head->align3[0] = (double)num; /* Save the directory entry here */
1291 
1292  /* now check for abnormal float in data array */
1293  l = (unsigned)head->datalen / sizeof(float);
1294 
1295  lp_checkBounds(data, l, chk);
1296 
1297  lp_close(&fd);
1298 
1299  return((int)head->scan);
1300 }
1301 
1302 
1304 double scan;
1305 {
1306  int feed;
1307  double tmp;
1308 
1309  tmp = scan - floor(scan);
1310 
1311  if(tmp > 0.1) /* is this the MAC data; .11 .12 */
1312  {
1313  tmp -= 0.1;
1314  }
1315 
1316  tmp *= 100.0;
1317  feed = (int)(tmp + 0.25);
1318 
1319  return(feed);
1320 }
1321 
1322 
1323 int lp_write_sdd(name, h, d)
1324 char *name;
1325 struct HEADER *h;
1326 char *d;
1327 {
1328  int outFD;
1329  long headlen, datalen, dirl, start;
1330  long datab, maxb, bytesinindex, bytesused;
1331  struct DIRECTORY dir;
1332  char *z;
1333  time_t itime;
1334 
1335  if((outFD = lp_pdopen(name, O_RDWR)) < 0)
1336  {
1337  return(-1);
1338  }
1339 
1340  if(verboseLevel & VERBOSE_FILEOPS)
1341  {
1342  st_report("FD = %d", outFD);
1343  }
1344 
1345  bzero(&boot, sizeof(struct BOOTSTRAP));
1346 
1347  if( read( outFD, &boot, sizeof(struct BOOTSTRAP))<0 )
1348  {
1349  st_perror("read boot");
1350  lp_close(&outFD);
1351 
1352  return(-2);
1353  }
1354 
1355  if(verboseLevel & VERBOSE_FILEOPS)
1356  {
1357  printLastBoot();
1358  }
1359 
1360  /* update bootstrap block */
1361 
1362  if( sizeof(struct DIRECTORY) != boot.bytperent)
1363  {
1364  st_report("directory sizes dont match");
1365  lp_close(&outFD);
1366 
1367  return(-4);
1368  }
1369 
1370 /* There could be holes but I'm not going to check for that */
1371 
1372  bytesinindex = boot.num_index_rec * boot.bytperrec;
1373  bytesused = boot.num_entries_used * boot.bytperent;
1374 
1375  if( bytesinindex < bytesused+sizeof(struct DIRECTORY))
1376  {
1377  st_fail("Directory structure of %s is full.\n", name );
1378  lp_close(&outFD);
1379 
1380  return(-5);
1381  }
1382 
1383  if( boot.typesdd || !boot.version)
1384  {
1385  st_fail("SDD version or type of %s is invalid.\n", name );
1386  lp_close(&outFD);
1387 
1388  return(-5);
1389  }
1390 
1391  headlen = sizeof(struct HEADER);
1392  h->headlen = (double)headlen;
1393  h->headlen = h->headlen;
1394  datalen = (long)h->datalen;
1395  datab = lp_blocks( headlen + datalen );
1396  boot.num_data_rec += datab;
1397  maxb = boot.num_data_rec + boot.num_index_rec;
1398 
1399  if( maxb < 0)
1400  {
1401  st_fail("Data file %s is full.\n", name );
1402  lp_close(&outFD);
1403 
1404  return(-5);
1405  }
1406 
1407  if( maxb >= 1717986918 )
1408  {
1409  st_fail("Data file is getting full. %d blocks left\n", 2147483647-maxb);
1410  }
1411 
1412  boot.counter++;
1413  boot.num_entries_used++;
1414 
1415  if( lseek( outFD, 0l, SEEK_SET)<0)
1416  {
1417  st_perror("lseek boot");
1418  lp_close(&outFD);
1419 
1420  return(-7);
1421  }
1422 
1423  if( write(outFD, &boot, sizeof(struct BOOTSTRAP)) != sizeof(struct BOOTSTRAP))
1424  {
1425  st_perror("write boot");
1426  lp_close(&outFD);
1427 
1428  return(-3);
1429  }
1430 
1431  /* update new directory */
1432 
1433  dirl = (boot.num_entries_used-1) * boot.bytperent + boot.bytperrec;
1434 
1435  if( lseek( outFD, dirl, SEEK_SET ) < 0) {
1436  st_perror("lseek dir");
1437  lp_close(&outFD);
1438 
1439  return(-7);
1440  }
1441 
1442  bzero( &dir, sizeof(struct DIRECTORY) );
1443 
1444  dir.start_rec = boot.num_index_rec + boot.num_data_rec - datab + 1;
1445  dir.end_rec = boot.num_index_rec + boot.num_data_rec;
1446  dir.h_coord = h->xsource;
1447  dir.v_coord = h->ysource;
1448 
1449  strncpy( dir.source, h->object, 16 );
1450  lp_padd(dir.source, 16);
1451 
1452  dir.scan = h->scan;
1453  dir.freq_res = lp_index_freqres(h);
1454  dir.rest_freq = lp_index_restfreq(h);
1455  dir.lst = h->lst;
1456  dir.ut = h->utdate;
1457  dir.obsmode = lp_obsmode(h->obsmode);
1458  dir.phase_rec = -1;
1459  dir.pos_code = lp_pos_code(h->coordcd);
1460 
1461  if( lp_write_dir( outFD, &dir, boot.bytperent)<0)
1462  {
1463  st_perror("write dir");
1464  lp_close(&outFD);
1465 
1466  return(-3);
1467  }
1468 
1469  /* set header offsets */
1470 
1471  h->headcls = (short)13;
1472  h->oneptr = (short)OFFSET(h, headlen);
1473  h->twoptr = (short)OFFSET(h, xpoint);
1474  h->thrptr = (short)OFFSET(h, utdate);
1475  h->fourptr = (short)OFFSET(h, epoch);
1476  h->fiveptr = (short)OFFSET(h, tamb);
1477  h->sixptr = (short)OFFSET(h, scanang);
1478  h->sevptr = (short)OFFSET(h, bfwhm);
1479  h->eigptr = (short)OFFSET(h, appeff);
1480  h->nineptr = (short)OFFSET(h, synfreq);
1481  h->tenptr = (short)OFFSET(h, openpar[0]);
1482  h->elvptr = (short)OFFSET(h, current_disk);
1483  h->twlptr = (short)OFFSET(h, obsfreq);
1484  h->trnptr = (short)OFFSET(h, nostac);
1485 
1486 /* padd strings with spaces */
1487 
1488  lp_padd(h->obsid, 8);
1489  lp_padd(h->observer, 16);
1490  lp_padd(h->telescop, 8);
1491  lp_padd(h->projid, 8);
1492  lp_padd(h->object, 16);
1493  lp_padd(h->obsmode, 8);
1494  lp_padd(h->frontend, 8);
1495  lp_padd(h->backend, 8);
1496  lp_padd(h->precis, 8);
1497  lp_padd(h->pt_model, 8);
1498  lp_padd(h->cl11type, 8);
1499  lp_padd(h->rx_info, 16);
1500  lp_padd(h->coordcd, 8);
1501  lp_padd(h->frame, 8);
1502  lp_padd(h->veldef, 8);
1503  lp_padd(h->typecal, 8);
1504 
1505  /* fill the header stop "burn_time" with the time it was written. */
1506  itime = time(NULL);
1507  h->burn_time = (double)itime;
1508 
1509  /* write data */
1510  start = (dir.start_rec-1) * boot.bytperrec;
1511  if( lseek(outFD, start, SEEK_SET)<0)
1512  {
1513  st_perror("lseek data");
1514  lp_close(&outFD);
1515 
1516  return(-7);
1517  }
1518 
1519  if( write( outFD, h, headlen)<0)
1520  {
1521  st_perror("write head");
1522  lp_close(&outFD);
1523 
1524  return(-3);
1525  }
1526 
1527  if(write( outFD, d, datalen)<0)
1528  {
1529  st_perror("write data");
1530  lp_close(&outFD);
1531 
1532  return(-3);
1533  }
1534 
1535  /* complete block */
1536  dirl = (datalen + headlen) % boot.bytperrec;
1537 
1538  if( dirl > 0)
1539  {
1540  dirl = boot.bytperrec - dirl;
1541  z = (char *)alloca(dirl);
1542 
1543  bzero(z, dirl);
1544 
1545  if( write( outFD, z, dirl)<0)
1546  {
1547  st_perror("write block_fill");
1548  lp_close(&outFD);
1549 
1550  return(-3);
1551  }
1552  }
1553 
1554  lp_close(&outFD);
1555 
1556  return((boot.num_index_rec-1) * boot.bytperrec / boot.bytperent - boot.num_entries_used);
1557 }
1558 
1559 
1560 
1561 /* Here is my attempt to write scans to a named directory entry; i.e. nsaves */
1562 int lp_nwrite_sdd(name, nsave, h, d)
1563 char *name;
1564 int nsave;
1565 struct HEADER *h;
1566 char *d;
1567 {
1568  int fd;
1569  long headlen, datalen, dirl, start;
1570  long datab, maxb, bytesinindex, bytesused;
1571  struct DIRECTORY dir;
1572  char *z;
1573  time_t itime;
1574 
1575  if( (fd = lp_pdopen(name, O_RDWR))<0)
1576  return(-1);
1577 
1578  bzero(&boot, sizeof(struct BOOTSTRAP));
1579  if( read( fd, &boot, sizeof(struct BOOTSTRAP))<0 ) {
1580  st_perror("read boot");
1581  lp_close(&fd);
1582  return(-2);
1583  }
1584 
1585  /* update bootstrap block */
1586 
1587  if( sizeof(struct DIRECTORY) != boot.bytperent ) {
1588  st_fail("directory sizes dont match\n");
1589  lp_close(&fd);
1590  return(-4);
1591  }
1592 
1593 /* There could be holes but I'm not going to check for that */
1594 
1595  bytesinindex = boot.num_index_rec * boot.bytperrec;
1596  bytesused = boot.num_entries_used * boot.bytperent;
1597 
1598  if( bytesinindex < bytesused+sizeof(struct DIRECTORY) ) {
1599  st_fail("Directory structure of %s is full.\n", name );
1600  lp_close(&fd);
1601  return(-5);
1602  }
1603 
1604  if( boot.typesdd || !boot.version ) {
1605  st_fail("SDD version or type of %s is invalid.\n", name );
1606  lp_close(&fd);
1607  return(-5);
1608  }
1609 
1610  headlen = sizeof(struct HEADER);
1611  h->headlen = (double)headlen;
1612  h->headlen = h->headlen;
1613  datalen = (long)h->datalen;
1614 
1615  datab = lp_blocks( headlen + datalen );
1616  boot.num_data_rec += datab;
1617 
1618  maxb = boot.num_data_rec + boot.num_index_rec;
1619 
1620  if( maxb < 0 ) {
1621  st_fail("Data file %s is full.\n", name );
1622  lp_close(&fd);
1623  return(-5);
1624  }
1625 
1626  if( maxb >= 1717986918 )
1627  st_fail("Data file is getting full. %d blocks left\n", 2147483647-maxb);
1628 
1629  boot.counter++;
1630  boot.num_entries_used++;
1631 
1632  if( lseek( fd, 0l, SEEK_SET ) <0 ) {
1633  st_perror("lseek boot");
1634  return(-7);
1635  }
1636 
1637 // if( lp_write_boot( fd, &boot, sizeof(struct BOOTSTRAP)) <0 ) {
1638  if( write(fd, &boot, sizeof(struct BOOTSTRAP)) != sizeof(struct BOOTSTRAP))
1639  {
1640  st_perror("write boot");
1641  return(-3);
1642  }
1643 
1644  /* update new directory */
1645  dirl = (boot.num_entries_used-1) * boot.bytperent + boot.bytperrec;
1646 
1647  if( lseek( fd, dirl, SEEK_SET ) < 0) {
1648  st_perror("lseek dir");
1649  return(-7);
1650  }
1651 
1652  bzero( &dir, sizeof(struct DIRECTORY) );
1653 
1654  dir.start_rec = boot.num_index_rec + boot.num_data_rec - datab + 1;
1655  dir.end_rec = boot.num_index_rec + boot.num_data_rec;
1656  dir.h_coord = h->xsource;
1657  dir.v_coord = h->ysource;
1658  strncpy( dir.source, h->object, 16 );
1659  lp_padd(dir.source, 16);
1660  dir.scan = h->scan;
1661  dir.freq_res = lp_index_freqres(h);
1662  dir.rest_freq = lp_index_restfreq(h);
1663  dir.lst = h->lst;
1664  dir.ut = h->utdate;
1665  dir.obsmode = lp_obsmode(h->obsmode);
1666  dir.phase_rec = -1;
1667  dir.pos_code = lp_pos_code(h->coordcd);
1668 
1669  if( lp_write_dir( fd, &dir, boot.bytperent ) <0 ) {
1670  st_perror("write dir");
1671  return(-3);
1672  }
1673 
1674  /* set header offsets */
1675 
1676  h->headcls = (short)13;
1677  h->oneptr = (short)OFFSET(h, headlen);
1678  h->twoptr = (short)OFFSET(h, xpoint);
1679  h->thrptr = (short)OFFSET(h, utdate);
1680  h->fourptr = (short)OFFSET(h, epoch);
1681  h->fiveptr = (short)OFFSET(h, tamb);
1682  h->sixptr = (short)OFFSET(h, scanang);
1683  h->sevptr = (short)OFFSET(h, bfwhm);
1684  h->eigptr = (short)OFFSET(h, appeff);
1685  h->nineptr = (short)OFFSET(h, synfreq);
1686  h->tenptr = (short)OFFSET(h, openpar[0]);
1687  h->elvptr = (short)OFFSET(h, current_disk);
1688  h->twlptr = (short)OFFSET(h, obsfreq);
1689  h->trnptr = (short)OFFSET(h, nostac);
1690 
1691 /* padd strings with spaces */
1692 
1693  lp_padd(h->obsid, 8);
1694  lp_padd(h->observer, 16);
1695  lp_padd(h->telescop, 8);
1696  lp_padd(h->projid, 8);
1697  lp_padd(h->object, 16);
1698  lp_padd(h->obsmode, 8);
1699  lp_padd(h->frontend, 8);
1700  lp_padd(h->backend, 8);
1701  lp_padd(h->precis, 8);
1702  lp_padd(h->pt_model,8);
1703  lp_padd(h->cl11type,8);
1704 /*
1705  lp_padd(h->phastb01,32);
1706  lp_padd(h->phastb02,32);
1707  lp_padd(h->phastb03,32);
1708  lp_padd(h->phastb04,32);
1709  lp_padd(h->phastb05,32);
1710  */
1711  lp_padd(h->rx_info,16);
1712 
1713  lp_padd(h->coordcd,8);
1714  lp_padd(h->frame,8);
1715  lp_padd(h->veldef,8);
1716  lp_padd(h->typecal,8);
1717 
1718  /* fill the header stop "burn_time" with the time it was written. */
1719 
1720  itime = time(NULL);
1721  h->burn_time = (double)itime;
1722 
1723  /* write data */
1724 
1725  start = (dir.start_rec-1) * boot.bytperrec;
1726 
1727  if( lseek(fd, start, SEEK_SET ) <0 ) {
1728  st_perror("lseek data");
1729  return(-7);
1730  }
1731 
1732  if( write( fd, h, headlen) <0 ) {
1733  st_perror("write head");
1734  return(-3);
1735  }
1736 
1737  if( write( fd, d, datalen ) <0 ) {
1738  st_perror("write data");
1739  return(-3);
1740  }
1741 
1742  /* complete block */
1743  dirl = (datalen + headlen) % boot.bytperrec;
1744  if( dirl > 0 ) {
1745  dirl = boot.bytperrec - dirl;
1746  z = (char *)alloca(dirl);
1747  bzero(z,dirl);
1748  if( write( fd, z, dirl ) <0 ) {
1749  st_perror("write block_fill");
1750  return(-3);
1751  }
1752  }
1753 
1754  lp_close(&fd);
1755 
1756  return((boot.num_index_rec-1) * boot.bytperrec / boot.bytperent - boot.num_entries_used);
1757 }
1758 
1759 
1760 /* create new sdd file with (MAX_SCANS) 16384 possible scans (formerly 4096) */
1761 
1762 int lp_new_sdd(name)
1763 char *name;
1764 {
1765  int fd;
1766 
1767  if( (fd = open(name, O_CREAT | O_RDWR | O_EXCL, 0644 ))<0)
1768  {
1769  st_perror("open");
1770 
1771  return(1);
1772  }
1773 
1774  if(verboseLevel & VERBOSE_FILEOPS)
1775  {
1776  st_report("new_sdd(): Creating data file: %s; FD = %d", name, fd);
1777  }
1778 
1779  bzero((char *)&boot, sizeof(struct BOOTSTRAP) );
1780 
1781  boot.num_index_rec = MAX_SCANS * sizeof(struct DIRECTORY)/BLOCK+1;
1782  boot.num_data_rec = 0;
1783  boot.bytperrec = BLOCK;
1784  boot.bytperent = sizeof(struct DIRECTORY);
1785  boot.num_entries_used = 0;
1786  boot.counter = 0;
1787  boot.typesdd = 0;
1788  boot.version = 1;
1789 
1790 // if( lp_write_boot( fd, &boot, BLOCK)<0)
1791  if(write(fd, &boot, BLOCK) != BLOCK)
1792  {
1793  st_perror("write");
1794  lp_close(&fd);
1795 
1796  return(1);
1797  }
1798 
1799  lp_close(&fd);
1800 
1801  return(0);
1802 }
1803 
1804 
1805 /* return number of stupid fortran blocks given bytes */
1806 static int lp_blocks(b)
1807 int b;
1808 {
1809  if( b % BLOCK )
1810  {
1811  return(b/BLOCK + 1);
1812  }
1813  else
1814  {
1815  return(b/BLOCK);
1816  }
1817 }
1818 
1819 
1820 /* padd a string with spaces, fortran style */
1821 static int lp_padd(s,n)
1822 char *s;
1823 int n;
1824 {
1825  int i, flag = 0;
1826 
1827  for( i=0; i<n; i++ )
1828  {
1829  if( !flag && s[i] == '\0' )
1830  {
1831  flag = 1;
1832  }
1833 
1834  if( flag )
1835  {
1836  s[i] = ' ';
1837  }
1838  }
1839 
1840  return(0);
1841 }
1842 
1843 
1844 static double lp_index_freqres(h)
1845 struct HEADER *h;
1846 {
1847  double ret, sq, cos(), sqrt();
1848 
1849  if(!strncmp(h->obsmode, "CONT", 4))
1850  {
1851  sq = h->deltaxr * cos(h->ysource * M_PI / 180.0);
1852  ret = sqrt( sq * sq + h->deltayr * h->deltayr );
1853  }
1854  else
1855  {
1856  ret = h->freqres;
1857  }
1858 
1859  return(ret);
1860 }
1861 
1862 static double lp_index_restfreq(h)
1863 struct HEADER *h;
1864 {
1865  double ret, cos();
1866 
1867  if( strncmp(h->obsmode, "CONT", 4 ) == 0 )
1868  {
1869  ret = h->samprat;
1870  }
1871  else
1872  {
1873  ret = h->restfreq;
1874  }
1875 
1876  return(ret);
1877 }
1878 
1879 
1880 static int lp_obsmode(o)
1881 char *o;
1882 {
1883  int mask, mode;
1884 
1885  if( strncasecmp(o, "LINE", 4 )== 0 )
1886  {
1887  mask = 512;
1888  }
1889  else
1890  if( strncasecmp(o, "CONT", 4 )== 0 )
1891  {
1892  mask = 256;
1893  }
1894  else
1895  {
1896  mask = 0;
1897  }
1898 
1899  if( strncasecmp(&o[4], "PS ", 4)==0) mode = 1;
1900  else if( strncasecmp(&o[4], "APS ", 4)==0) mode = 2;
1901  else if( strncasecmp(&o[4], "FS ", 4)==0) mode = 3;
1902  else if( strncasecmp(&o[4], "BSP ", 4)==0) mode = 4;
1903  else if( strncasecmp(&o[4], "TPON", 4)==0) mode = 5;
1904  else if( strncasecmp(&o[4], "TPOF", 4)==0) mode = 6;
1905  else if( strncasecmp(&o[4], "ATP ", 4)==0) mode = 7;
1906  else if( strncasecmp(&o[4], "PSM ", 4)==0) mode = 8;
1907  else if( strncasecmp(&o[4], "APM ", 4)==0) mode = 9;
1908  else if( strncasecmp(&o[4], "FSM ", 4)==0) mode = 10;
1909  else if( strncasecmp(&o[4], "TPMO", 4)==0) mode = 11;
1910  else if( strncasecmp(&o[4], "TPMF", 4)==0) mode = 12;
1911  else if( strncasecmp(&o[4], "DRFT", 4)==0) mode = 13;
1912  else if( strncasecmp(&o[4], "PCAL", 4)==0) mode = 14;
1913  else if( strncasecmp(&o[4], "BCAL", 4)==0) mode = 15;
1914  else if( strncasecmp(&o[4], "BLNK", 4)==0) mode = 16;
1915  else if( strncasecmp(&o[4], "SEQ ", 4)==0) mode = 17;
1916  else if( strncasecmp(&o[4], "FIVE", 4)==0) mode = 18;
1917  else if( strncasecmp(&o[4], "MAP ", 4)==0) mode = 19;
1918  else if( strncasecmp(&o[4], "FOC ", 4)==0) mode = 20;
1919  else if( strncasecmp(&o[4], "NSFC", 4)==0) mode = 21;
1920  else if( strncasecmp(&o[4], "TTIP", 4)==0) mode = 22;
1921  else if( strncasecmp(&o[4], "STIP", 4)==0) mode = 23;
1922  else if( strncasecmp(&o[4], "D_ON", 4)==0) mode = 24;
1923  else if( strncasecmp(&o[4], "CAL ", 4)==0) mode = 25;
1924  else if( strncasecmp(&o[4], "FSPS", 4)==0) mode = 26;
1925  else if( strncasecmp(&o[4], "EWFC", 4)==0) mode = 27;
1926  else if( strncasecmp(&o[4], "ZERO", 4)==0) mode = 28;
1927 
1928  else if( strncasecmp(&o[4], "TLPW", 4)==0) mode = 29;
1929  else if( strncasecmp(&o[4], "FQSW", 4)==0) mode = 30;
1930  else if( strncasecmp(&o[4], "NOCL", 4)==0) mode = 31;
1931  else if( strncasecmp(&o[4], "PLCL", 4)==0) mode = 32;
1932  else if( strncasecmp(&o[4], "ONOF", 4)==0) mode = 33;
1933  else if( strncasecmp(&o[4], "BMSW", 4)==0) mode = 34;
1934  else if( strncasecmp(&o[4], "PSSW", 4)==0) mode = 35;
1935  else if( strncasecmp(&o[4], "DRFT", 4)==0) mode = 36;
1936  else if( strncasecmp(&o[4], "OTF ", 4)==0) mode = 37;
1937  else if( strncasecmp(&o[4], "S_ON", 4)==0) mode = 38;
1938  else if( strncasecmp(&o[4], "S_OF", 4)==0) mode = 39;
1939  else if( strncasecmp(&o[4], "QK5 ", 4)==0) mode = 40;
1940  else if( strncasecmp(&o[4], "QK5A", 4)==0) mode = 41;
1941  else if( strncasecmp(&o[4], "PS-1", 4)==0) mode = 42;
1942  else if( strncasecmp(&o[4], "VLBI", 4)==0) mode = 43;
1943  else if( strncasecmp(&o[4], "PZC ", 4)==0) mode = 44;
1944  else if( strncasecmp(&o[4], "CPZM", 4)==0) mode = 45;
1945  else if( strncasecmp(&o[4], "PSPZ", 4)==0) mode = 46;
1946  else if( strncasecmp(&o[4], "CPZ1", 4)==0) mode = 47;
1947  else if( strncasecmp(&o[4], "CPZ2", 4)==0) mode = 48;
1948  else if( strncasecmp(&o[4], "CCPZ", 4)==0) mode = 49;
1949  else if( strncasecmp(&o[4], "CCPM", 4)==0) mode = 50;
1950  else if( strncasecmp(&o[4], "TPPZ", 4)==0) mode = 51;
1951  else if( strncasecmp(&o[4], "OPT ", 4)==0) mode = 52;
1952  else if( strncasecmp(&o[4], "COLD", 4)==0) mode = 53;
1953  else mode = 0;
1954 
1955  return(mode+mask);
1956 }
1957 
1958 
1959 static int lp_pos_code(s)
1960 char *s;
1961 {
1962  if( strncmp( s, "GALACTIC", 8 )==0) return 1;
1963  else if( strncmp( s, "1950RADC", 8 )==0) return 2;
1964  else if( strncmp( s, "EPOCRADC", 8 )==0) return 3;
1965  else if( strncmp( s, "MEANRADC", 8 )==0) return 4;
1966  else if( strncmp( s, "APPRADC", 7 )==0) return 5;
1967  else if( strncmp( s, "APPHADC", 7 )==0) return 6;
1968  else if( strncmp( s, "1950ECL", 7 )==0) return 7;
1969  else if( strncmp( s, "EPOCECL", 7 )==0) return 8;
1970  else if( strncmp( s, "MEANECL", 7 )==0) return 9;
1971  else if( strncmp( s, "APPECL", 6 )==0) return 10;
1972  else if( strncmp( s, "AZEL", 4 )==0) return 11;
1973  else if( strncmp( s, "USERDEF", 7 )==0) return 12;
1974  else if( strncmp( s, "2000RADC", 8 )==0) return 13;
1975  else if( strncmp( s, "INDRADC", 7 )==0) return 14;
1976  else return 0;
1977 }
1978 
1979 
1980 static int lp_write_boot( fd, b, len )
1981 int fd;
1982 struct BOOTSTRAP *b;
1983 int len;
1984 {
1985  int ret;
1986  struct BOOTSTRAP bb;
1987 
1988  bcopy( b, &bb, sizeof(struct BOOTSTRAP));
1989 
1990  if(verboseLevel & VERBOSE_FILEOPS)
1991  {
1992  st_report("lp_writeboot(): fd = %d, len = %d", fd, len);
1993  }
1994 
1995  ret = write(fd, &bb, len);
1996 
1997  return(ret);
1998 }
1999 
2000 
2001 static int lp_write_dir( fd, d, len )
2002 int fd;
2003 struct DIRECTORY *d;
2004 int len;
2005 {
2006  int ret;
2007  struct DIRECTORY dd;
2008 
2009  bcopy( d, &dd, sizeof(struct DIRECTORY));
2010 
2011  ret = write(fd, &dd, len);
2012 
2013  return(ret);
2014 }
2015 
2016 
2018 {
2019  st_report("Current BOOT Struct:");
2020  st_report(" ");
2021  st_report("num_index_rec = %d", boot.num_index_rec);
2022  st_report("num_data_rec = %d", boot.num_data_rec);
2023  st_report("bytperrec = %d", boot.bytperrec);
2024  st_report("bytperent = %d", boot.bytperent);
2025  st_report("num_entries_used = %d", boot.num_entries_used);
2026  st_report("counter = %d", boot.counter);
2027  st_report("typesdd = %d", boot.typesdd);
2028  st_report("version = %d", boot.version);
2029  st_report(" ");
2030 
2031  return(0);
2032 }
int getFeed()
char frame[8]
Definition: header.h:110
double scan
Definition: header.h:35
int lastFileSize
Definition: sdd.c:23
char * strcmprs(char *str)
#define MODE_OTF
Definition: defines.h:88
int doSummaryOfFreqs2Buf()
int first
Definition: dbase.c:106
#define MAX_FREQS
Definition: global.h:11
char typecal[8]
Definition: header.h:120
float lst
Definition: sdd.h:54
char source[16]
Definition: sdd.h:41
double datalen
Definition: header.h:34
#define OTF
Definition: observing_par.h:63
char obsid[8]
Definition: header.h:36
#define MAX_SOURCE_LIST
Definition: defines.h:148
double utdate
Definition: header.h:61
double scanang
Definition: header.h:100
float * otfData
Definition: sdd.c:53
short elvptr
Definition: header.h:25
int scan
int datalen
Definition: dataserv.c:33
int lp_checkBounds()
short phase_rec
Definition: sdd.h:57
char source[SRCLEN+1]
Definition: dataserv.c:52
#define MAX_CHANS
Definition: defines.h:38
short trnptr
Definition: header.h:27
int read_scan()
#define READ_REVERSE
Definition: defines.h:152
double bfwhm
Definition: header.h:114
char filename[256]
Definition: dataserv.c:49
#define MODE_CONT
Definition: defines.h:86
struct SOURCES * findSourceInList()
char rx_info[16]
Definition: header.h:219
#define max(a, b)
Definition: cactus.h:115
unsigned int num_data_rec
Definition: sdd.h:6
Definition: header.h:13
double xsource
Definition: header.h:74
double ysource
Definition: header.h:75
char coordcd[8]
Definition: header.h:87
double nostac
Definition: header.h:223
double rest_freq
Definition: sdd.h:50
int mode
Definition: global.h:25
int lp_nwrite_sdd()
short pos_code
Definition: sdd.h:59
struct HEADER * sa
Definition: linuxpops.c:72
char telescop[8]
Definition: header.h:38
double openpar[10]
Definition: header.h:155
int numOfSources
Definition: sdd.c:24
#define OFFSET(x, f)
Definition: sdd.c:20
double align3[4]
Definition: header.h:230
Definition: global.h:21
int clearSourceList()
Definition: sdd.c:300
double synfreq
Definition: header.h:132
short nineptr
Definition: header.h:23
float ut
Definition: sdd.h:55
#define VERBOSE_FILEOPS
Definition: defines.h:76
struct ENVIRONMENT * env
Definition: linuxpops.c:71
char pt_model[8]
Definition: header.h:57
int used
Definition: global.h:24
char name[32]
Definition: global.h:22
unsigned int counter
Definition: sdd.h:15
float v_coord
Definition: sdd.h:39
double current_disk
Definition: header.h:161
int showSourceList()
Definition: sdd.c:308
struct FREQS freqs[MAX_FREQS]
Definition: global.h:28
double lst
Definition: header.h:63
struct SOURCES sources[MAX_SOURCE_LIST]
Definition: sdd.c:27
int lp_new_sdd()
unsigned int version
Definition: sdd.h:24
int read_nsave()
double epoch
Definition: header.h:73
double indx
Definition: header.h:84
int num
Definition: global.h:23
int findSize()
short twlptr
Definition: header.h:26
void st_warn()
int doSummary()
Definition: sdd.c:369
char obsmode[8]
Definition: header.h:41
double obsfreq
Definition: header.h:197
unsigned int end_rec
Definition: sdd.h:36
unsigned int num_entries_used
Definition: sdd.h:9
char backend[8]
Definition: header.h:43
void st_fail()
int strcmp()
int read_dir
Definition: env.h:132
short sixptr
Definition: header.h:20
double headlen
Definition: header.h:33
double xpoint
Definition: header.h:48
int feed
Definition: stats.c:66
short oneptr
Definition: header.h:15
double stsys
Definition: header.h:203
char frontend[8]
Definition: header.h:42
#define MODE_SPEC
Definition: defines.h:87
int used
Definition: global.h:15
unsigned int bytperrec
Definition: sdd.h:7
#define BLOCK
Definition: sdd.c:19
int findScans()
void st_report()
short sevptr
Definition: header.h:21
short obsmode
Definition: sdd.h:56
char projid[8]
Definition: header.h:39
float freq_res
Definition: sdd.h:43
int printLastBoot()
Definition: sdd.c:2017
int checkConstCal()
short eigptr
Definition: header.h:22
#define MAX_SCANS
Definition: sdd.c:21
short fiveptr
Definition: header.h:19
short twoptr
Definition: header.h:16
char precis[8]
Definition: header.h:44
unsigned int typesdd
Definition: sdd.h:23
short thrptr
Definition: header.h:17
char observer[16]
Definition: header.h:37
Definition: sdd.h:4
short tenptr
Definition: header.h:24
double burn_time
Definition: header.h:181
int count
Definition: global.h:16
char veldef[8]
Definition: header.h:119
double avg
Definition: beam.h:5
short headcls
Definition: header.h:14
float h_coord
Definition: sdd.h:37
double tamb
Definition: header.h:91
unsigned int start_rec
Definition: sdd.h:33
void st_print()
unsigned int bytperent
Definition: sdd.h:8
int nfreqs
Definition: global.h:26
double appeff
Definition: header.h:124
float scan
Definition: sdd.h:42
unsigned int num_index_rec
Definition: sdd.h:5
char object[16]
Definition: header.h:40
int st_perror()
double restfreq
Definition: header.h:198
int lp_write_sdd()
Definition: sdd.h:31
double freq
Definition: global.h:14
char cl11type[8]
Definition: header.h:69
double oob_val
Definition: env.h:232
short fourptr
Definition: header.h:18
Definition: env.h:7
int verboseLevel
Definition: linuxpops.c:80