10 #include <gsl/gsl_math.h>
20 #define OFFSET(x,f) ((short)(((int)&x->f - ((int)x))/8 + 1))
21 #define MAX_SCANS 16384
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);
39 static float lp_findNextGoodPoint(
float *d,
int indx,
int l);
40 static float lp_findPrevGoodPoint(
float *d,
int indx,
int l);
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);
56 static int lp_close(fd)
75 if(!strstr(h->obsmode,
"CAL"))
77 if(!strncmp(h->typecal,
"CONST", 5))
79 if(h->stsys > 2000.0 || h->stsys < 50.0)
81 h->
stsys = h->tcal / 2.0;
90 static int lp_pdopen(name, mode)
97 if( (fd = open(name, mode, 0666 ))<0)
99 st_fail(
"lp_pdopen(): Error opening: '%s'", name );
111 st_report(
"Opening: %s, fd = %d", name, fd);
123 if( (fd = lp_pdopen(env->filename, O_RDONLY))<0)
128 if( read( fd, (
char *)&boot,
sizeof(
struct BOOTSTRAP))<0)
138 st_fail(
"File not of correct vintage; Consider using 'sdd convert'");
152 if( (sa = (
struct HEADER *)calloc(n,
sizeof(
struct HEADER))) == NULL)
154 fprintf(stderr,
"Error in findSize( malloc() )\n");
163 static int lp_findNumOfSources()
199 return((
struct SOURCES *)NULL);
203 static int lp_findFreqInSource(p, f)
213 if(fabs(f - p->freqs[i].freq) < 0.1)
224 static int lp_addFreqToSource(p, f)
230 found = lp_findFreqInSource(p, f);
234 p->freqs[found].count++;
241 if(!p->freqs[i].used)
243 p->freqs[i].used = 1;
244 p->freqs[i].count = 1;
245 p->freqs[i].freq = f;
258 static int lp_addSourceToList(name, mode, freq)
272 lp_addFreqToSource(p, freq);
283 strcpy(p->
name, name);
289 lp_addFreqToSource(p, freq);
315 st_print(
" Source Num-of-Scans\n\n");
333 static int lp_RfreqSort(a, b)
351 static int lp_freqSort(a, b)
371 int i, n=0, j, k, found,
max;
374 qsort(
sources, MAX_SOURCE_LIST,
sizeof(
struct SOURCES), (__compar_fn_t)lp_freqSort);
433 int i, n=0, k, found = 0;
437 qsort(
sources, MAX_SOURCE_LIST,
sizeof(
struct SOURCES), (__compar_fn_t)lp_freqSort);
443 p = &
sources[MAX_SOURCE_LIST-1];
498 int fd,
max, dirl, bytesinindex, bytesused,
num, j = 0;
499 int sizeofDIR,
first = 1;
508 if( (fd = lp_pdopen(env->filename, O_RDONLY))<0)
513 if(read( fd, (
char *)&boot,
sizeof(
struct BOOTSTRAP))<0)
525 fprintf(stderr,
"directory sizes dont match\n");
534 if( bytesinindex < bytesused+
sizeof(
struct DIRECTORY))
536 fprintf(stderr,
"Directory structure of %s is full.\n", env->filename );
544 fprintf(stderr,
"SDD version or type of %s is invalid.\n", env->filename );
552 fprintf(stderr,
"File %s has faulty direcory size\n", env->filename );
560 fprintf(stderr,
"File %s is empty\n", env->filename);
570 lseek( fd, (off_t)dirl, SEEK_SET );
572 if(read( fd, (
char *)&dir, sizeofDIR ) != sizeofDIR)
613 lseek( fd, (off_t)max, SEEK_SET );
615 if(read(fd, (
char *)&head,
sizeof(
struct HEADER) )<0)
627 if(!strncmp(head.
obsmode,
"LINE", 4))
636 if(!strncmp(head.
obsmode,
"CONT", 4))
643 if(!strncmp(head.
obsmode,
"LINETPMF", 8))
645 if(fabs(head.
openpar[5] - 6666.0) < 1.0)
655 if(!strncmp(head.
obsmode,
"CONT", 4))
663 strncpy(
object, head.
object, 16);
667 if(!strncmp(head.
obsmode,
"CONT", 4))
685 if(!strncmp(head.
telescop,
"ARO-HHT", 7))
691 if(!strncmp(head.
telescop,
"NRAO 12M", 8))
705 head.
align3[0] = (double)num;
707 bcopy((
char *)&head, (
char *)&sa[j],
sizeof(
struct HEADER));
714 fprintf(stderr,
"File %s contains too many scans\n", env->filename);
716 lp_findNumOfSources();
724 lp_findNumOfSources();
731 static float lp_findNextGoodPoint(d, indx, l)
740 if(d[i] > 1e-20 || d[i] < 1e-21)
752 static float lp_findPrevGoodPoint(d, indx, l)
761 if(d[i] > 1e-20 || d[i] < 1e-21)
789 if(gsl_isnan( (
double)d[i]))
794 if(gsl_isinf( (
double)d[i]))
800 if(fabs((
double)d[i]) > 1e9)
817 if(gsl_isnan( (
double)d[i]))
823 if(gsl_isinf( (
double)d[i]))
829 if(fabs((
double)d[i]) > 1e9)
837 if(autobad && (d[i] < 1e-20 && d[i] > 1e-21))
841 st_print(
"Smoothing Bad Channel %d\n", i);
846 next = lp_findNextGoodPoint(d, i, l);
853 prev = lp_findPrevGoodPoint(d, i, l);
859 next = lp_findNextGoodPoint(d, i, l);
860 prev = lp_findPrevGoodPoint(d, i, l);
862 d[i] = (prev + next) / 2.0;
879 int fd, dirl, bytesinindex, bytesused, num,
max;
886 if( (fd = lp_pdopen(filename, O_RDONLY))<0)
891 if( read( fd, (
char *)&boot,
sizeof(
struct BOOTSTRAP))<0)
903 fprintf(stderr,
"directory sizes dont match\n");
912 if( bytesinindex < bytesused+
sizeof(
struct DIRECTORY))
914 fprintf(stderr,
"Directory structure of %s is full.\n", filename );
922 fprintf(stderr,
"SDD version or type of %s is invalid.\n", filename );
930 fprintf(stderr,
"File %s has faulty direcory size\n", filename );
938 fprintf(stderr,
"File %s is empty\n", filename);
947 lseek( fd, (off_t)dirl, SEEK_SET );
949 if(read( fd, (
char *)&dir, sizeofDIR ) != sizeofDIR)
961 st_fail(
"Directory Structure Empty for scan: %.2f", scan);
968 lseek( fd, (off_t)dirl, SEEK_SET );
970 if(read( fd, (
char *)&dir, sizeofDIR ) != sizeofDIR)
990 lseek( fd, (off_t)dirl, SEEK_SET );
992 if(read( fd, (
char *)&dir, sizeofDIR ) != sizeofDIR)
1008 if(fabs(scan-dir.
scan) < 0.005)
1018 fprintf(stderr,
"Scan %7.2f NOT Found\n", scan);
1032 lseek( fd, (off_t)dirl, SEEK_SET );
1034 if(read( fd, (
char *)&dir, sizeofDIR ) != sizeofDIR )
1049 if(fabs(scan-dir.
scan) < 0.005)
1059 fprintf(stderr,
"Scan %7.2f NOT Found\n", scan);
1068 lseek( fd, (off_t)max, SEEK_SET );
1070 if(read(fd, (
char *)head,
sizeof(
struct HEADER) )<0)
1078 head->
align3[0] = (double)num;
1086 return((
int)head->
scan);
1093 return((
int)head->
scan);
1099 if(!strncmp(head->
obsmode,
"LINEOTF", 7))
1101 st_report(
"Scan is of OTF type: Using Internal data array");
1110 fprintf(stderr,
"Error in otfData( malloc() )\n");
1122 fprintf(stderr,
"Error reading scan %7.2f data\n", scan);
1131 bzero( (
char *)data,
MAX_CHANS *
sizeof(
float) );
1133 if( read( fd, (
char *)data, (
unsigned)head->
datalen) <0)
1136 fprintf(stderr,
"Error reading scan %7.2f data\n", scan);
1142 l = (unsigned)head->
datalen /
sizeof(
float);
1149 return((
int)head->
scan);
1160 int fd, dirl, bytesinindex, bytesused, num,
max;
1166 if( (fd = lp_pdopen(filename, O_RDONLY))<0)
1171 if( read( fd, (
char *)&boot,
sizeof(
struct BOOTSTRAP))<0)
1183 fprintf(stderr,
"directory sizes dont match\n");
1192 if( bytesinindex < bytesused+
sizeof(
struct DIRECTORY))
1194 fprintf(stderr,
"Directory structure of %s is full.\n", filename );
1202 fprintf(stderr,
"SDD version or type of %s is invalid.\n", filename );
1210 fprintf(stderr,
"File %s has faulty direcory size\n", filename );
1218 fprintf(stderr,
"File %s is empty\n", filename);
1231 lseek( fd, (off_t)dirl, SEEK_SET );
1233 if(read( fd, (
char *)&dir, sizeofDIR ) != sizeofDIR)
1259 st_fail(
"NSAVE %d NOT Found", nsave);
1267 lseek( fd, (off_t)max, SEEK_SET );
1269 if(read(fd, (
char *)head,
sizeof(
struct HEADER) )<0)
1279 bzero( (
char *)data,
MAX_CHANS *
sizeof(
float) );
1281 if( read( fd, (
char *)data, (
unsigned)head->
datalen) <0)
1284 st_fail(
"Error reading nsave %d data\n", nsave);
1290 head->
align3[0] = (double)num;
1293 l = (unsigned)head->
datalen /
sizeof(
float);
1299 return((
int)head->
scan);
1309 tmp = scan - floor(scan);
1317 feed = (int)(tmp + 0.25);
1329 long headlen,
datalen, dirl, start;
1330 long datab, maxb, bytesinindex, bytesused;
1335 if((outFD = lp_pdopen(name, O_RDWR)) < 0)
1347 if( read( outFD, &boot,
sizeof(
struct BOOTSTRAP))<0 )
1364 st_report(
"directory sizes dont match");
1375 if( bytesinindex < bytesused+
sizeof(
struct DIRECTORY))
1377 st_fail(
"Directory structure of %s is full.\n", name );
1385 st_fail(
"SDD version or type of %s is invalid.\n", name );
1391 headlen =
sizeof(
struct HEADER);
1395 datab = lp_blocks( headlen + datalen );
1401 st_fail(
"Data file %s is full.\n", name );
1407 if( maxb >= 1717986918 )
1409 st_fail(
"Data file is getting full. %d blocks left\n", 2147483647-maxb);
1415 if( lseek( outFD, 0l, SEEK_SET)<0)
1435 if( lseek( outFD, dirl, SEEK_SET ) < 0) {
1442 bzero( &dir,
sizeof(
struct DIRECTORY) );
1453 dir.
freq_res = lp_index_freqres(h);
1461 if( lp_write_dir( outFD, &dir, boot.
bytperent)<0)
1488 lp_padd(h->
obsid, 8);
1501 lp_padd(h->
frame, 8);
1511 if( lseek(outFD, start, SEEK_SET)<0)
1519 if( write( outFD, h, headlen)<0)
1527 if(write( outFD, d, datalen)<0)
1541 z = (
char *)alloca(dirl);
1545 if( write( outFD, z, dirl)<0)
1570 long datab, maxb, bytesinindex, bytesused;
1575 if( (fd = lp_pdopen(name, O_RDWR))<0)
1579 if( read( fd, &boot,
sizeof(
struct BOOTSTRAP))<0 ) {
1588 st_fail(
"directory sizes dont match\n");
1598 if( bytesinindex < bytesused+
sizeof(
struct DIRECTORY) ) {
1599 st_fail(
"Directory structure of %s is full.\n", name );
1605 st_fail(
"SDD version or type of %s is invalid.\n", name );
1610 headlen =
sizeof(
struct HEADER);
1615 datab = lp_blocks( headlen + datalen );
1621 st_fail(
"Data file %s is full.\n", name );
1626 if( maxb >= 1717986918 )
1627 st_fail(
"Data file is getting full. %d blocks left\n", 2147483647-maxb);
1632 if( lseek( fd, 0l, SEEK_SET ) <0 ) {
1647 if( lseek( fd, dirl, SEEK_SET ) < 0) {
1652 bzero( &dir,
sizeof(
struct DIRECTORY) );
1661 dir.
freq_res = lp_index_freqres(h);
1669 if( lp_write_dir( fd, &dir, boot.
bytperent ) <0 ) {
1693 lp_padd(h->
obsid, 8);
1714 lp_padd(h->
frame,8);
1727 if( lseek(fd, start, SEEK_SET ) <0 ) {
1732 if( write( fd, h, headlen) <0 ) {
1737 if( write( fd, d, datalen ) <0 ) {
1746 z = (
char *)alloca(dirl);
1748 if( write( fd, z, dirl ) <0 ) {
1767 if( (fd = open(name, O_CREAT | O_RDWR | O_EXCL, 0644 ))<0)
1776 st_report(
"new_sdd(): Creating data file: %s; FD = %d", name, fd);
1779 bzero((
char *)&boot,
sizeof(
struct BOOTSTRAP) );
1791 if(write(fd, &boot, BLOCK) != BLOCK)
1806 static int lp_blocks(b)
1811 return(b/BLOCK + 1);
1821 static int lp_padd(s,n)
1827 for( i=0; i<n; i++ )
1829 if( !flag && s[i] ==
'\0' )
1844 static double lp_index_freqres(h)
1847 double ret, sq, cos(), sqrt();
1849 if(!strncmp(h->obsmode,
"CONT", 4))
1851 sq = h->deltaxr * cos(h->ysource * M_PI / 180.0);
1852 ret = sqrt( sq * sq + h->deltayr * h->deltayr );
1862 static double lp_index_restfreq(h)
1867 if( strncmp(h->obsmode,
"CONT", 4 ) == 0 )
1880 static int lp_obsmode(o)
1885 if( strncasecmp(o,
"LINE", 4 )== 0 )
1890 if( strncasecmp(o,
"CONT", 4 )== 0 )
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;
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;
1959 static int lp_pos_code(s)
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;
1980 static int lp_write_boot( fd, b, len )
1988 bcopy( b, &bb,
sizeof(
struct BOOTSTRAP));
1992 st_report(
"lp_writeboot(): fd = %d, len = %d", fd, len);
1995 ret = write(fd, &bb, len);
2001 static int lp_write_dir( fd, d, len )
2009 bcopy( d, &dd,
sizeof(
struct DIRECTORY));
2011 ret = write(fd, &dd, len);
char * strcmprs(char *str)
int doSummaryOfFreqs2Buf()
struct SOURCES * findSourceInList()
unsigned int num_data_rec
struct FREQS freqs[MAX_FREQS]
struct SOURCES sources[MAX_SOURCE_LIST]
unsigned int num_entries_used
unsigned int num_index_rec