source: trunk/MagicSoft/Cosy/catalog/SaoFile.cc@ 8149

Last change on this file since 8149 was 1109, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 4.8 KB
Line 
1#include "SaoFile.h"
2
3#include <iostream.h>
4#include <string.h>
5
6#include "slamac.h"
7
8#include "File.h"
9
10#define FALSE 0
11#define TRUE 1
12
13ClassImp(SaoFile);
14
15static int lt(sort_t *t1, sort_t *t2)
16{
17 // t1->ra < t2->ra
18 if (t1->ra < t2->ra)
19 return 1;
20
21 // t1->ra > t2->ra
22 if (t1->ra > t2->ra)
23 return 0;
24
25 // t1->ra == t2->ra
26 if (t1->dec < t2->dec)
27 return 1;
28 else
29 return 0;
30}
31
32
33static void rekMergesort(sort_t **a, int length, int l, int r)
34{
35 // a recursive merge sort algorithm
36 if ( r <= l )
37 return;
38
39 sort_t **b = new sort_t*[length];
40
41 int m = (r+l)/2;
42
43 rekMergesort(a, length, l, m);
44
45 rekMergesort(a, length, m+1, r);
46
47 int i;
48 for (i=m+1; i>l; i--)
49 b[i-1]=a[i-1];
50
51 int j;
52 for (j=m; j<r; j++)
53 b[r+m-j]=a[j+1];
54
55 int k;
56 for (k=l; k<=r; k++)
57 {
58 if (lt( b[i],b[j]) )
59 a[k]=b[i++];
60 else
61 a[k]=b[j--];
62 }
63
64 delete b;
65}
66
67SaoFile::SaoFile(const char *name)
68{
69 file = new File(name, "r");
70
71 if (!*file)
72 exit(0);
73
74 compressed = !strcasecmp(name+strlen(name)-4, ".cmp");
75
76 fEntries = file->Size()/(compressed?70-20:205);
77
78 cout << "Opening" << (compressed?" compressed":"") << " catalog '" << name << "'"
79 << " with " << fEntries << " entries" << endl;
80
81 delFlag = ' ';
82}
83
84SaoFile::~SaoFile()
85{
86 delete file;
87}
88
89int SaoFile::GetEntryNr() const
90{
91 return file->Tell()/(compressed?70-20:205);
92}
93
94int SaoFile::GetEntry(const int i)
95{
96 file->Seek(i*(compressed?70-20:205));
97 return GetNextEntry();
98}
99
100void SaoFile::Reset() const
101{
102 file->Seek(0);
103}
104
105int SaoFile::GetNextEntry()
106{
107 if (file->Eof())
108 return FALSE;
109
110 SAO = file->Geti(6);
111 if (!compressed)
112 {
113 delFlag = file->Getc();
114 file->Skip(77-8);
115 }
116 Pmag = file->Getf(4);
117 Vmag = file->Getf(4);
118 if (!compressed)
119 file->Skip(161/*151*/-85);
120 /*
121 RA2000h = file->geti(2);
122 RA2000m = file->geti(2);
123 RA2000s = file->getf(6);
124 */
125 pmRA2000 = file->Getf(7);
126 if (!compressed)
127 file->Skip(178-168);
128 /*
129 DE2000sgn = file->getc();
130 DE2000d = file->geti(2);
131 DE2000m = file->geti(2);
132 DE2000s = file->getf(5);
133 */
134 pmDE2000 = file->Getf(6);
135 RA2000rad = file->Getf(10);
136 DE2000rad = file->Getf(11);
137 file->Newline();
138
139 sprintf(data, "%6i%4.1f%4.1f"/*"%2i%2i%6.3f"*/"%7.4f"/*"%c%2i%2i%5.2f"*/"%6.3f%10.8f%11.8f\n",
140 Nr(), MagP(), MagV(), /*RaH(), RaM(), RaS(),*/ RaPm(),
141 /*DecSgn(), DecD(), DecM(), DecS(),*/ DecPm(), Ra(), Dec());
142
143 return !file->Eof();
144}
145
146void SaoFile::Print() const
147{
148 cout << SAO << " " << delFlag << " " << Pmag << " " << Vmag << " "
149 << /*RA2000h << "h" << RA2000m << "m" << RA2000s << "s " << */pmRA2000 << " "
150 << /*DE2000sgn << DE2000d << "d" << DE2000m << "m" << DE2000s << "s " << */pmDE2000 << " Ra:"
151 << RA2000rad << " Dec:" << DE2000rad << endl;
152}
153
154void SaoFile::Compress()
155{
156 FILE *f = fopen("sao.cmp", "w");
157
158 cout << endl << " Compressing catalog..." << flush;
159
160 const int entries = GetNrOfEntries();
161 const int step = entries/360;
162
163 for (int i=0; i<entries; i++)
164 {
165 GetNextEntry();
166
167 if (!DelFlag())
168 fprintf(f, String());
169
170 if (i%step==0)
171 cout << "." << flush;
172 }
173 cout << "done." << endl;
174
175 fclose(f);
176}
177
178void SaoFile::Sort()
179{
180 cout << endl << "Reading coordinates..." << flush;
181
182 const int entries=GetNrOfEntries();
183
184 sort_t **srt = new sort_t*[entries];
185 for (int i=0; i<entries; i++)
186 srt[i] = new sort_t;
187
188 int counter=0;
189 const int step = entries/360;
190 while(GetNextEntry())
191 {
192 srt[counter]->ra = (int)(360.0*(Ra()/D2PI));
193 srt[counter]->dec = (int)(360.0*(Dec()/D2PI));
194 srt[counter]->nr = counter;
195
196 if (counter%step==0)
197 cout << "." << flush;
198
199 counter++;
200 }
201 cout << "done." << endl << endl;
202
203 /* sort entries */
204 cout << "Sorting coordinates..." << flush;
205 rekMergesort(srt, entries, 0, entries-1);
206 cout << "done." << endl << endl;
207
208 cout << "Writing sorted catalogue..." << flush;
209 FILE *f = fopen("sao-sort.cmp", "w");
210 FILE *idx = fopen("sao-sort.idx", "w");
211
212 int ra=0, ral=0;
213 int dec=0, decl=0;
214 for (int i=0; i<entries; i++)
215 {
216 GetEntry(srt[i]->nr);
217
218 ra = (int)(360.0*Ra()/D2PI);
219 dec = (int)(360.0*Dec()/D2PI);
220
221 if (ra!=ral || dec!=decl)
222 {
223 if (ra!=ral)
224 cout << "." << flush;
225 fprintf(idx, "%3d %3d %10d\n", ra, dec, i);
226 }
227
228 ral = ra;
229 decl = dec;
230
231 fprintf(f, String());
232 if (i%5000==0)
233 cout << "." << flush;
234 }
235 fclose(idx);
236 fclose(f);
237 cout << "done." << endl;
238}
239
Note: See TracBrowser for help on using the repository browser.