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

Last change on this file since 744 was 740, checked in by tbretz, 24 years ago
*** empty log message ***
File size: 4.7 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 compressed = !strcasecmp(name+strlen(name)-4, ".cmp");
70
71 fEntries = file->Size()/(compressed?70-20:205);
72
73 cout << "Opening" << (compressed?" compressed":"") << " catalog '" << name << "'"
74 << " with " << fEntries << " entries" << endl;
75
76 delFlag = ' ';
77}
78
79SaoFile::~SaoFile()
80{
81 delete file;
82}
83
84int SaoFile::GetEntryNr() const
85{
86 return file->Tell()/(compressed?70-20:205);
87}
88
89int SaoFile::GetEntry(const int i)
90{
91 file->Seek(i*(compressed?70-20:205));
92 return GetNextEntry();
93}
94
95void SaoFile::Reset() const
96{
97 file->Seek(0);
98}
99
100int SaoFile::GetNextEntry()
101{
102 if (file->Eof())
103 return FALSE;
104
105 SAO = file->Geti(6);
106 if (!compressed)
107 {
108 delFlag = file->Getc();
109 file->Skip(77-8);
110 }
111 Pmag = file->Getf(4);
112 Vmag = file->Getf(4);
113 if (!compressed)
114 file->Skip(161/*151*/-85);
115 /*
116 RA2000h = file->geti(2);
117 RA2000m = file->geti(2);
118 RA2000s = file->getf(6);
119 */
120 pmRA2000 = file->Getf(7);
121 if (!compressed)
122 file->Skip(178-168);
123 /*
124 DE2000sgn = file->getc();
125 DE2000d = file->geti(2);
126 DE2000m = file->geti(2);
127 DE2000s = file->getf(5);
128 */
129 pmDE2000 = file->Getf(6);
130 RA2000rad = file->Getf(10);
131 DE2000rad = file->Getf(11);
132 file->Newline();
133
134 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",
135 Nr(), MagP(), MagV(), /*RaH(), RaM(), RaS(),*/ RaPm(),
136 /*DecSgn(), DecD(), DecM(), DecS(),*/ DecPm(), Ra(), Dec());
137
138 return !file->Eof();
139}
140
141void SaoFile::Print() const
142{
143 cout << SAO << " " << delFlag << " " << Pmag << " " << Vmag << " "
144 << /*RA2000h << "h" << RA2000m << "m" << RA2000s << "s " << */pmRA2000 << " "
145 << /*DE2000sgn << DE2000d << "d" << DE2000m << "m" << DE2000s << "s " << */pmDE2000 << " Ra:"
146 << RA2000rad << " Dec:" << DE2000rad << endl;
147}
148
149void SaoFile::Compress()
150{
151 FILE *f = fopen("sao.cmp", "w");
152
153 cout << endl << " Compressing catalog..." << flush;
154
155 const int entries = GetNrOfEntries();
156 const int step = entries/360;
157
158 for (int i=0; i<entries; i++)
159 {
160 GetNextEntry();
161
162 if (!DelFlag())
163 fprintf(f, String());
164
165 if (i%step==0)
166 cout << "." << flush;
167 }
168 cout << "done." << endl;
169
170 fclose(f);
171}
172
173void SaoFile::Sort()
174{
175 cout << endl << "Reading coordinates..." << flush;
176
177 const int entries=GetNrOfEntries();
178
179 sort_t **srt = new sort_t*[entries];
180 for (int i=0; i<entries; i++)
181 srt[i] = new sort_t;
182
183 int counter=0;
184 const int step = entries/360;
185 while(GetNextEntry())
186 {
187 srt[counter]->ra = (int)(360.0*(Ra()/D2PI));
188 srt[counter]->dec = (int)(360.0*(Dec()/D2PI));
189 srt[counter]->nr = counter;
190
191 if (counter%step==0)
192 cout << "." << flush;
193
194 counter++;
195 }
196 cout << "done." << endl << endl;
197
198 /* sort entries */
199 cout << "Sorting coordinates..." << flush;
200 rekMergesort(srt, entries, 0, entries-1);
201 cout << "done." << endl << endl;
202
203 cout << "Writing sorted catalogue..." << flush;
204 FILE *f = fopen("sao-sort.cmp", "w");
205 FILE *idx = fopen("sao-sort.idx", "w");
206
207 int ra=0, ral=0;
208 int dec=0, decl=0;
209 for (int i=0; i<entries; i++)
210 {
211 GetEntry(srt[i]->nr);
212
213 ra = (int)(360.0*Ra()/D2PI);
214 dec = (int)(360.0*Dec()/D2PI);
215
216 if (ra!=ral || dec!=decl)
217 {
218 if (ra!=ral)
219 cout << "." << flush;
220 fprintf(idx, "%3d %3d %10d\n", ra, dec, i);
221 }
222
223 ral = ra;
224 decl = dec;
225
226 fprintf(f, String());
227 if (i%5000==0)
228 cout << "." << flush;
229 }
230 fclose(idx);
231 fclose(f);
232 cout << "done." << endl;
233}
234
Note: See TracBrowser for help on using the repository browser.