Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
read_scan.c
Go to the documentation of this file.
1 #include "ds_extern.h"
2 #include "modes.h"
3 
4 #define SIZEOFDIR (sizeof(struct DIRECTORY))
5 
6 #define MAX_SCANS 16384
7 
8 static int pdopen();
9 
10 extern int datalen;
11 extern char errBuf[];
13 
14 
15 int checkBoot(b, fn)
16 struct BOOTSTRAP *b;
17 char *fn;
18 {
19  int bytesinindex, bytesused;
20 
21  if( SIZEOFDIR != b->bytperent ) {
22  log_msg("directory sizes dont match");
23  return(1);
24  }
25 
26  bytesinindex = b->num_index_rec * b->bytperrec;
27  bytesused = b->num_entries_used * b->bytperent;
28 
29  if( bytesinindex < bytesused + SIZEOFDIR ) {
30  log_msg("Dir struct of %s, full.", fn );
31  /*
32  return(1);
33  */
34  }
35 
36  if( b->typesdd || !b->version ) {
37  log_msg("SDD version or type of %s is invalid.", fn );
38  return(1);
39  }
40 
41  if( SIZEOFDIR != b->bytperent ) {
42  log_msg( "File %s has faulty direcory size, can't verify", fn );
43  return(1);
44  }
45 
46  if( b->num_entries_used <= 0 ) {
47 /*
48  log_msg( "File %s is empty", fn );
49  */
50  return(1);
51  }
52  return(0);
53 }
54 
55 
56 int fixVel(head)
57 struct HEADER *head;
58 {
59  if(head->refpt_vel == 0.0)
60  head->refpt_vel = head->velocity;
61 
62  return(0);
63 }
64 
65 
67 float scan;
68 char *filename;
69 float **data;
70 struct HEADER *head;
71 {
72  int fd, dirl, num, max;
73  struct BOOTSTRAP boot;
74  struct DIRECTORY dir;
75  int i, l;
76  extern int checkIgnoreScans(int v, double s);
77 
78  if(checkIgnoreScans(version, scan))
79  {
80  log_msg("READ_SCAN: Scan %.2f, in version %d Ignored", scan, version);
81 
82  return(-2);
83  }
84 
85  if(*data) {
86  free((char *)*data);
87  *data = NULL;
88  }
89 
90  if( (fd = pdopen(filename))<0)
91  return(-1);
92 
93  if( read( fd, (char *)&boot, sizeof(struct BOOTSTRAP))<0 ) {
94  log_perror("read boot");
95  close(fd);
96  return(-1);
97  }
98 
99  if( checkBoot(&boot, filename)) {
100  close(fd);
101  return(-1);
102  }
103 
104  if( scan < 0.0) {
105  dirl = (boot.num_entries_used-1) * boot.bytperent + boot.bytperrec;
106  lseek( fd, (off_t)dirl, SEEK_SET );
107  if(read( fd, (char *)&dir, SIZEOFDIR )!= SIZEOFDIR ){
108  log_perror("readdir");
109  close(fd);
110  return(-1);
111  }
112  close(fd);
113  return((int)dir.scan);
114  }
115 
116  num = boot.num_entries_used;
117 
118  while(num >= 0)
119  {
120  dirl = num * boot.bytperent + boot.bytperrec;
121  lseek( fd, (off_t)dirl, SEEK_SET );
122 
123  if(read( fd, (char *)&dir, SIZEOFDIR ) != SIZEOFDIR )
124  {
125  log_perror("readdir");
126  close(fd);
127 
128  return(-1);
129  }
130  if( scan == dir.scan )
131  {
132  break;
133  }
134 
135  num--;
136  }
137 
138  if( num < 0)
139  {
140 // log_msg("READ_SCAN: Scan %.2f NOT Found in %s", scan, filename);
141  close(fd);
142 
143  return(-1);
144  }
145 
146  max = boot.bytperrec * (dir.start_rec-1);
147  lseek( fd, (off_t)max, SEEK_SET );
148 
149  if(read(fd, (char *)head, sizeof(struct HEADER) )<0)
150  {
151  perror("read header");
152  close(fd);
153 
154  return(-1);
155  }
156 
157  fixVel(head);
158 
159  datalen = (int)head->datalen;
160  *data = (float *)malloc( (unsigned int)datalen );
161  bzero((char *)*data, datalen );
162 
163  if( read( fd, (char *)*data, datalen) <0 ) {
164  perror("read data");
165  log_msg("Error reading scan %7.2f data", scan);
166  close(fd);
167  return(-1);
168  }
169 
170  if(head->tcal < 0)
171  ignoreThisChan = 1;
172  else
173  ignoreThisChan = 0;
174 
175  /* now check for abnormal float in data array */
176  l = datalen / sizeof(float);
177 
178  for(i=0;i<l;i++)
179  {
180  if(isnan( (double)(*data)[i]))
181  {
182  (*data)[i] = 0.0;
183  }
184  }
185 
186  close(fd);
187 
188  return(0);
189 }
190 
191 
192 static int pdopen(name)
193 char *name;
194 {
195  int fd;
196 
197  if( (fd = open(name, O_RDONLY, 0666 ))<0)
198  {
199  log_msg("Unable to open: %s", name );
200 
201  return(-1);
202  }
203 
204  return(fd);
205 }
206 
207 
208 /* given an id, return the field associated with it */
209 int modefield(id, field)
210 short int id;
211 char *field;
212 {
213  if(id >= 0 || id < NUMMODES)
214  {
215  strcpy(field, modes[id].field);
216  return(0);
217  }
218  else
219  {
220  strcpy(field, modes[0].field);
221  return(1);
222  }
223 }
224 
225 
226 
227 int peekScan(scan, filename, be, modeid, head )
228 float scan;
229 char *filename;
230 int *be, *modeid;
231 struct HEADER *head;
232 {
233  int fd, dirl, bytesinindex, bytesused, num, max;
234  struct BOOTSTRAP boot;
235  struct DIRECTORY dir;
236  char tmode[20];
237 
238  if( (fd = pdopen(filename))<0)
239  return(-1);
240 
241 // log_msg("PEEK: Looking for scan %.2f in %s", scan, filename);
242 
243  if( read( fd, (char *)&boot, sizeof(struct BOOTSTRAP))<0 ) {
244  log_perror("read boot");
245  close(fd);
246  return(-1);
247  }
248 
249  /* check bootstrap block */
250 
251  if( SIZEOFDIR != boot.bytperent ) {
252  log_msg("directory sizes dont match");
253  close(fd);
254  return(-1);
255  }
256 
257  bytesinindex = boot.num_index_rec * boot.bytperrec;
258  bytesused = boot.num_entries_used * boot.bytperent;
259 
260  if( bytesinindex < bytesused + SIZEOFDIR ) {
261  log_msg("Dir struct of %s, full.", filename );
262  }
263 
264  if( boot.typesdd || !boot.version ) {
265  log_msg("SDD ver or type of %s, invalid.", filename );
266  close(fd);
267  return(-1);
268  }
269 
270  if( SIZEOFDIR != boot.bytperent ) {
271  log_msg( "File %s has faulty dir size", filename );
272  close(fd);
273  return(-1);
274  }
275 
276  if( boot.num_entries_used <= 0 ) {
277 // log_msg( "File %s is empty", filename );
278  close(fd);
279 
280  return(-1);
281  }
282 
283  if( scan < 0.0)
284  {
285  dirl = (boot.num_entries_used-1) * boot.bytperent + boot.bytperrec;
286  lseek( fd, (off_t)dirl, SEEK_SET );
287  if(read( fd, (char *)&dir, SIZEOFDIR ) != SIZEOFDIR ){
288  log_perror("readdir");
289  close(fd);
290  return(-1);
291  }
292  }
293  else
294  {
295  num = boot.num_entries_used;
296  while(num >= 0) {
297  dirl = num * boot.bytperent + boot.bytperrec;
298  lseek( fd, (off_t)dirl, SEEK_SET );
299  if(read( fd, (char *)&dir, SIZEOFDIR ) != SIZEOFDIR ){
300  log_perror("readdir");
301  close(fd);
302  return(-1);
303  }
304  if( scan == dir.scan )
305  break;
306  num--;
307  }
308 
309  if( num < 0)
310  {
311  close(fd);
312  return(-1);
313  }
314  }
315 
316  max = boot.bytperrec * (dir.start_rec-1);
317  lseek( fd, (off_t)max, SEEK_SET );
318 
319  if(read(fd, (char *)head, sizeof(struct HEADER) )<0)
320  {
321  perror("read header");
322  close(fd);
323 
324  return(-1);
325  }
326 
327  fixVel(head);
328 
329  strncpy( tmode, head->obsmode, 4);
330  tmode[4] = '\0';
331 
332  if(strstr(tmode, "CONT"))
333  *be = DIGITAL_BE;
334  else
335  if(strstr(tmode, "LINE"))
336  *be = SPECTRAL_BE;
337  else
338  *be = MYSTERY_BE;
339 
340  dir.obsmode &= 0x7f;
341  *modeid = dir.obsmode;
342 
343  close(fd);
344 
345 // log_msg("PEEK: Found scan %.2f in %s", scan, filename);
346 
347  return(0);
348 }
double datalen
Definition: header.h:34
int scan
int datalen
Definition: dataserv.c:33
int log_perror(char *msg)
int read_scan()
char filename[256]
Definition: dataserv.c:49
#define max(a, b)
Definition: cactus.h:115
Definition: header.h:13
double tcal
Definition: header.h:202
int version
Definition: dataserv.c:41
#define SIZEOFDIR
Definition: read_scan.c:4
int modefield(int id, char *field)
int log_msg(const char *fmt,...)
double refpt_vel
Definition: header.h:175
unsigned int version
Definition: sdd.h:24
char obsmode[8]
Definition: header.h:41
int checkIgnoreScans(int v, double s)
unsigned int num_entries_used
Definition: sdd.h:9
char errBuf[512]
Definition: dataserv.c:52
#define MYSTERY_BE
Definition: observing_par.h:11
unsigned int bytperrec
Definition: sdd.h:7
int ignoreThisChan
Definition: read_scan.c:12
int fixVel(struct HEADER *head)
Definition: read_scan.c:56
short obsmode
Definition: sdd.h:56
unsigned int typesdd
Definition: sdd.h:23
#define NUMMODES
Definition: modes.h:72
Definition: sdd.h:4
int checkBoot(struct BOOTSTRAP *b, char *fn)
Definition: read_scan.c:15
int peekScan(double scan, char *filename, int *be, int *modeid, struct HEADER *head)
struct MODE modes[]
Definition: modes.h:15
unsigned int start_rec
Definition: sdd.h:33
unsigned int bytperent
Definition: sdd.h:8
float scan
Definition: sdd.h:42
unsigned int num_index_rec
Definition: sdd.h:5
Definition: sdd.h:31
#define SPECTRAL_BE
Definition: defines.h:17
#define DIGITAL_BE
Definition: defines.h:16