1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/*** File tmcat.c
*** July 1, 2003
*** By Jessica Mink, SAO
Copyright (C) 2006
Smithsonian Astrophysical Observatory, Cambridge, MA USA
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define MAXTOKENS 100 /* Maximum number of tokens to parse */
#define MAXWHITE 20 /* Maximum number of whitespace characters */
struct Tokens {
char *line; /* Line which has been parsed */
int lline; /* Number of characters in line */
int ntok; /* Number of tokens on line */
int nwhite; /* Number of whitespace characters */
char white[MAXWHITE]; /* Whitespace (separator) characters */
char *tok1[MAXTOKENS]; /* Pointers to start of tokens */
int ltok[MAXTOKENS]; /* Lengths of tokens */
int itok; /* Current token number */
};
int setoken(); /* Tokenize a string for easy decoding */
int nextoken(); /* Get next token from tokenized string */
int getoken(); /* Get specified token from tokenized string */
main (ac, av)
int ac;
char **av;
{
double ra, dec, spd, mj, mh, mk;
int i, j;
char root[] = "/data/astrocat/tmc1";
char dir[256];
char file[256], file0[256];
char line[512];
char token[64];
char f1[8], f2[8];
char id[32];
int use_src;
int dup_src;
int n = 0;
int ni = 0;
int nt = 0;
int nz = 0;
int z;
FILE *outfile = NULL;
FILE *sortfile = NULL;
struct Tokens tokens; /* Token structure */
for (j = 0; j < 180; j = j + 1) {
sprintf (dir,"%s/%03d", root, j);
if (access (dir, F_OK&W_OK)) {
if (mkdir (dir, 00777))
fprintf (stderr, "TMCAT: Cannot make directory %s\n", dir);
}
}
/* Initialize file name to all zeroes */
for (i = 0; i < 256; i++)
file[i] = (char) 0;
strcpy (file0, file);
/* Read through standard input until no more data can be read */
while (fgets (line, 512, stdin)) {
setoken (&tokens, line, "bar");
/* Compute South Polar Distance from declination */
getoken (&tokens, 2, token, 16);
dec = atof (token);
spd = dec + 90.0;
ni = ni + 1;
/* Compute zone number and skip if out of range */
z = (int) spd;
if (z > 180)
continue;
/* Set output directory name */
sprintf (dir,"%s/%03d", root, z);
/* Set output file name */
i = (int) (((spd + 0.0000001) * 10.0) - (z * 10));
sprintf (file, "%s/t%04d.cat", dir, i);
/* If not the same file as last, close old file; open new */
if (strcmp (file, file0)) {
/* if (nz > 1)
printf (" TMCAT: %7d stars written to %s\n", nz, file0); */
if (nz == 1) {
/* printf (" TMCAT: %7d stars written to %s", nz, file0);
printf (" %10.6f %10.6f %17s\n", ra, dec, id); */
sortfile = fopen ("resort", "a");
if (sortfile == NULL)
sortfile = fopen ("resort", "w");
if (sortfile != NULL) {
fprintf (sortfile,"%s\n", file0);
fclose (sortfile);
}
}
if (outfile != NULL)
fclose (outfile);
outfile = fopen (file, "a");
if (outfile == NULL) {
outfile = fopen (file, "w");
if (outfile == NULL) {
printf ("TMCAT: Cannot write to file %s\n", file);
continue;
}
}
strcpy (file0, file);
nz = 0;
}
/* Right ascension */
getoken (&tokens, 1, token, 16);
ra = atof (token);
/* ID */
getoken (&tokens, 6, id, 32);
/* J, H, K magnitudes */
getoken (&tokens, 7, token, 16);
mj = atof (token);
getoken (&tokens, 11, token, 16);
mh = atof (token);
getoken (&tokens, 15, token, 16);
mk = atof (token);
/* Photometric quality flag */
getoken (&tokens, 19, token, 4);
strncpy (f1, token, 3);
f1[3] = (char) 0;
/* Read flag */
getoken (&tokens, 20, token, 4);
strncpy (f2, token, 3);
f2[3] = (char) 0;
/* Duplicate source flag */
getoken (&tokens, 49, token, 4);
dup_src = atoi (token);
/* Use source flag */
getoken (&tokens, 50, token, 4);
use_src = atoi (token);
if (use_src) {
nz = nz + 1;
/* Append position and magnitudes from this line to current file */
fprintf (outfile,"%10.6f %10.6f %17s %6.3f %6.3f %6.3f %3s %3s\n",
ra, dec, id, mj, mh, mk, f1, f2);
nt = nt + 1;
if (nt % 100000 == 0)
fprintf (stderr, "%10d stars read to %10.6f %10.6f (%s)\n",
nt, ra, dec, file);
}
}
/* Close output file */
fclose (outfile);
}
/* -- SETOKEN -- tokenize a string for easy decoding */
int
setoken (tokens, string, cwhite)
struct Tokens *tokens; /* Token structure returned */
char *string; /* character string to tokenize */
char *cwhite; /* additional whitespace characters
* if = tab, disallow spaces and commas */
{
char squote, dquote, jch, newline;
char *iq, *stri, *wtype, *str0, *inew;
int i,j,naddw;
newline = (char) 10;
squote = (char) 39;
dquote = (char) 34;
if (string == NULL)
return (0);
/* Line is terminated by newline or NULL */
inew = strchr (string, newline);
if (inew != NULL)
tokens->lline = inew - string - 1;
else
tokens->lline = strlen (string);
/* Save current line in structure */
tokens->line = string;
/* Add extra whitespace characters */
if (cwhite == NULL)
naddw = 0;
else
naddw = strlen (cwhite);
/* if character is tab, allow only tabs and nulls as separators */
if (naddw > 0 && !strncmp (cwhite, "tab", 3)) {
tokens->white[0] = (char) 9; /* Tab */
tokens->white[1] = (char) 0; /* NULL (end of string) */
tokens->nwhite = 2;
}
/* if character is bar, allow only bars and nulls as separators */
else if (naddw > 0 && !strncmp (cwhite, "bar", 3)) {
tokens->white[0] = '|'; /* Bar */
tokens->white[1] = (char) 0; /* NULL (end of string) */
tokens->nwhite = 2;
}
/* otherwise, allow spaces, tabs, commas, nulls, and cwhite */
else {
tokens->nwhite = 4 + naddw;;
tokens->white[0] = ' '; /* Space */
tokens->white[1] = (char) 9; /* Tab */
tokens->white[2] = ','; /* Comma */
tokens->white[3] = (char) 124; /* Vertical bar */
tokens->white[4] = (char) 0; /* Null (end of string) */
if (tokens->nwhite > 20)
tokens->nwhite = 20;
if (naddw > 0) {
i = 0;
for (j = 4; j < tokens->nwhite; j++) {
tokens->white[j] = cwhite[i];
i++;
}
}
}
tokens->white[tokens->nwhite] = (char) 0;
tokens->ntok = 0;
tokens->itok = 0;
iq = string - 1;
for (i = 0; i < MAXTOKENS; i++) {
tokens->tok1[i] = NULL;
tokens->ltok[i] = 0;
}
/* Process string one character at a time */
stri = string;
str0 = string;
while (stri < string+tokens->lline) {
/* Keep stuff between quotes in one token */
if (stri <= iq)
continue;
jch = *stri;
/* Handle quoted strings */
if (jch == squote)
iq = strchr (stri+1, squote);
else if (jch == dquote)
iq = strchr (stri+1, dquote);
else
iq = stri;
if (iq > stri) {
tokens->ntok = tokens->ntok + 1;
if (tokens->ntok > MAXTOKENS) return (MAXTOKENS);
tokens->tok1[tokens->ntok] = stri + 1;
tokens->ltok[tokens->ntok] = (iq - stri) - 1;
stri = iq + 1;
str0 = iq + 1;
continue;
}
/* Search for unquoted tokens */
wtype = strchr (tokens->white, jch);
/* If this is one of the additional whitespace characters,
* pass as a separate token */
if (wtype > tokens->white + 3) {
/* Terminate token before whitespace */
if (stri > str0) {
tokens->ntok = tokens->ntok + 1;
if (tokens->ntok > MAXTOKENS) return (MAXTOKENS);
tokens->tok1[tokens->ntok] = str0;
tokens->ltok[tokens->ntok] = stri - str0;
}
/* Make whitespace character next token; start new one */
tokens->ntok = tokens->ntok + 1;
if (tokens->ntok > MAXTOKENS) return (MAXTOKENS);
tokens->tok1[tokens->ntok] = stri;
tokens->ltok[tokens->ntok] = 1;
stri++;
str0 = stri;
}
/* Pass previous token if regular whitespace or NULL */
else if (wtype != NULL || jch == (char) 0) {
/* Ignore leading whitespace */
if (stri == str0) {
stri++;
str0 = stri;
}
/* terminate token before whitespace; start new one */
else {
tokens->ntok = tokens->ntok + 1;
if (tokens->ntok > MAXTOKENS) return (MAXTOKENS);
tokens->tok1[tokens->ntok] = str0;
tokens->ltok[tokens->ntok] = stri - str0;
stri++;
str0 = stri;
}
}
/* Keep going if not whitespace */
else
stri++;
}
/* Add token terminated by end of line */
if (str0 < stri) {
tokens->ntok = tokens->ntok + 1;
if (tokens->ntok > MAXTOKENS)
return (MAXTOKENS);
tokens->tok1[tokens->ntok] = str0;
tokens->ltok[tokens->ntok] = stri - str0 + 1;
}
tokens->itok = 0;
return (tokens->ntok);
}
/* NEXTOKEN -- get next token from tokenized string */
int
nextoken (tokens, token, maxchars)
struct Tokens *tokens; /* Token structure returned */
char *token; /* token (returned) */
int maxchars; /* Maximum length of token */
{
int ltok; /* length of token string (returned) */
int it, i;
int maxc = maxchars - 1;
tokens->itok = tokens->itok + 1;
it = tokens->itok;
if (it > tokens->ntok)
it = tokens->ntok;
else if (it < 1)
it = 1;
ltok = tokens->ltok[it];
if (ltok > maxc)
ltok = maxc;
strncpy (token, tokens->tok1[it], ltok);
for (i = ltok; i < maxc; i++)
token[i] = (char) 0;
return (ltok);
}
/* GETOKEN -- get specified token from tokenized string */
int
getoken (tokens, itok, token, maxchars)
struct Tokens *tokens; /* Token structure returned */
int itok; /* token sequence number of token
* if <0, get whole string after token -itok
* if =0, get whole string */
char *token; /* token (returned) */
int maxchars; /* Maximum length of token */
{
int ltok; /* length of token string (returned) */
int it, i;
int maxc = maxchars - 1;
it = itok;
if (it > 0 ) {
if (it > tokens->ntok)
it = tokens->ntok;
ltok = tokens->ltok[it];
if (ltok > maxc)
ltok = maxc;
strncpy (token, tokens->tok1[it], ltok);
}
else if (it < 0) {
if (it < -tokens->ntok)
it = -tokens->ntok;
ltok = tokens->line + tokens->lline - tokens->tok1[-it];
if (ltok > maxc)
ltok = maxc;
strncpy (token, tokens->tok1[-it], ltok);
}
else {
ltok = tokens->lline;
if (ltok > maxc)
ltok = maxc;
strncpy (token, tokens->tok1[1], ltok);
}
for (i = ltok; i < maxc; i++)
token[i] = (char) 0;
return (ltok);
}
/* May 16 2003 New program
* Jun 25 2003 Add filter to keep stars only if use_src is 1
* Jun 25 2003 Log every 100,000 sources instead of every 10,000
* Jul 1 2003 Fix bug writing flags
*/
|