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

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