Plan 9 from Bell Labs’s /usr/web/sources/contrib/pdt/sky/share/bsc2sky.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>

enum
{
	BUFSIZE=512,
};

enum
{
	Dnil = 0,
	Dint,
	Ddouble,
	Dstr,
};

struct record
{
	char ads[6+1];				//  [meta.id.cross] (index)  ADS Catalog Designation
	char ads_comp[2+1];		//  [meta.id.cross] (index)  ADS Letter Identification
	char alt_name[16+1];		//  [meta.id] (index)  Alternate Name (usually Bayer and/or Flamsteed)
	double bii;				// float4:.5f_degree [pos.galactic.lat] (index)  Galactic Latitude
	double bv_color;			// float4:.2f  [phot.color;em.opt.B;em.opt.V] (index)  B-V Color Magnitude
	char bv_uncert[2+1];		//  [meta.code.error;phot.color;em.opt.B;em.opt.V] (index)  B-V Uncertainty Flag (:=uncertain; ?=questionable)
	char cdec[8+1];			//  [pos.eq.dec]   Declination, Sexagesimal, J2000
	char cdec_1950[8+1];		//  [pos.eq.dec]   Declination, Sexagesimal, B1950
	int class;					// int  [src.class] (index)  Browse Classification
	char cra[8+1];				//  [pos.eq.ra]   Right Ascension, Sexagesimal, J2000
	char cra_1950[8+1];			//  [pos.eq.ra]   Right Ascension, Sexagesimal, B1950
	double dec;				// float8:.5f_degree [pos.eq.dec;meta.main] (key)  Declination, J2000
	double dec_1900;			// float8:.5f_degree [pos.eq.dec]   Declination, B1900
	double dec_1950;			// float8:.5f_degree [pos.eq.dec] (index)  Declination, B1950
	char dm_cat[2+1];			//  [meta.id.cross]   Durchmusterung Catalog ID [BD/CD/CP]
	char dm_num[10+1];		//  [meta.id.cross] (index)  Durchmusterung Number
	int fk5;					// int  [meta.id.cross] (index)  FK5 Star Number
	double glat;				// float4:.2f_degree [pos.galactic.lat]   Original Galactic Latitude
	double glon;				// float4:.2f_degree [pos.galactic.lon]   Original Galactic Longitude
	int hd;					// int  [meta.id.cross] (index)  Henry Draper Catalog Number
	int hr;					// int  [meta.id.cross] (index)  Harvard Revised BS Number
	char ir_flag[2+1];			//  [meta.ref] (index)  Infrared Source Flag [I]
	double lii;					// float4:.5f_degree [pos.galactic.lon] (index)  Galactic Longitude
	int m_cnt;					// int  [meta.number;meta.code.multip]   Number of Components in Multiple System
	char m_id[4+1];			//  [meta.code]   Identification of Double or the Brightest in Multiple System
	double m_mdiff;			// float4:.1f  [phot.mag;arith.diff]   Magnitude Difference of Double or the Brightest in Multiple System
	double m_sep;				//  [pos.angDistance]   Separation of Components of Double or the Brightest in Multiple System
	char multiple[2+1];			//  [meta.code.multip] (index)  Double or Multiple System Flag
	char name[12+1];			//  [meta.id;meta.main] (index)  Source Designation
	char note[2+1];				//  [meta.note]   Notation Flag [*]
	char par_code[2+1];			//  [meta.code;pos.parallax.trig] (index)  Parallax Code (D=Dynamical; Otherwise, Trigonometric)
	double parallax;			// float4:.3f_arcsec [pos.parallax.trig] (index)  Parallax (arcsec)
	double pmdec;				// float4:.3f_arcsecyr [pos.pm;pos.eq.dec] (index)  Proper Motion for Dec J2000 (arcsec/yr)
	double pmra;				// float4:.3f_arcsecyr [pos.pm;pos.eq.ra] (index)  Proper Motion for RA J2000 (arcsec/yr)
	double ra;					// float8:.5f_degree [pos.eq.ra;meta.main] (key)  Right Ascension, J2000
	double ra_1900;			// float8:.5f_degree [pos.eq.ra]   Right Ascension, B1900
	double ra_1950;			// float8:.5f_degree [pos.eq.ra] (index)  Right Ascension, B1950
	int radvel;					// int2_kms [spect.dopplerVeloc] (index)  Radial Velocity (Line of Sight, Heliocentric)
	char radvel_comm[4+1];		//  [meta.note;spect.dopplerVeloc]   Radial Velocity Comments
	char ri_code[2+1];			//  [meta.code;phot.color;em.opt.R;em.opt.I] (index)  R-I Systems Codes
	double ri_color;			// float4:.2f  [phot.color;em.opt.R;em.opt.I] (index)  R-I in Systems Specified
	int rotvel;					// int2_kms [phys.veloc.rotat] (index)  Rotational Velocity
	char rotvel_comm[2+1];		//  [meta.code.error;phys.veloc.rotat]   Rotational Velocity Comments
	char rotvel_uncert[2+1];		//  [meta.code.error]   Rotational Velocity Uncertainty Flag (:=uncertain; ?=questionable)
	int sao;					// int  [meta.id.cross] (index)  SAO Catalog Number
	char spect_code[2+1];		//  [meta.code;src.spType] (index)  Spectral Type Code
	char spect_type[20+1];		//  [src.spType] (index)  Spectral Type of Source
	double ub_color;			// float4:.2f  [phot.color;em.opt.U;em.opt.B] (index)  U-B Color Magnitude
	char ub_uncert[2+1];		//  [meta.code.error;phot.color;em.opt.U;em.opt.B] (index)  U-B Uncertainty Flag (:=uncertain; ?=questionable)
	char var_id[10+1];			//  [meta.id] (index)  Variable Star Identification
	double vmag;				// float4:5.2f  [phot.mag;em.opt.V] (index)  Photographic Magnitude
	char vmag_code[2+1];		//  [meta.code;phot.mag;em.opt.V] (index)  Photographic Magnitude Code
	char vmag_uncert[2+1];		//  [meta.code;stat.error;phot.mag;em.opt.V]  
} rec;
	
struct annotation
{
	char *id;
	int dtyp;
	void *p;	
} note[]={
	{"ads", Dstr, rec.ads,},
	{"ads_comp", Dstr, rec.ads_comp,},
	{"alt_name", Dstr, rec.alt_name,},
	{"bii", Ddouble, &rec.bii,},
	{"bv_color", Ddouble, &rec.bv_color,},
	{"bv_uncert", Dstr, rec.bv_uncert,},
	{"cdec", Dstr, rec.cdec,},
	{"cdec_1950", Dstr, rec.cdec_1950,},
	{"class", Dint, &rec.class,},
	{"cra", Dstr, rec.cra,},
	{"cra_1950", Dstr, rec.cra_1950,},
	{"dec", Ddouble, &rec.dec,},
	{"dec_1900", Ddouble, &rec.dec_1900,},
	{"dec_1950", Ddouble, &rec.dec_1950,},
	{"dm_cat", Dstr, rec.dm_cat,},
	{"dm_num", Dstr, rec.dm_num,},
	{"fk5", Dint, &rec.fk5,},
	{"glat", Ddouble, &rec.glat,},
	{"glon", Ddouble, &rec.glon,},
	{"hd", Dint, &rec.hd,},
	{"hr", Dint, &rec.hr,},
	{"ir_flag", Dstr, rec.ir_flag,},
	{"lii", Ddouble, &rec.lii,},
	{"m_cnt", Dint, &rec.m_cnt,},
	{"m_id", Dstr, rec.m_id,},
	{"m_mdiff", Ddouble, &rec.m_mdiff,},
	{"m_sep", Ddouble, &rec.m_sep,},
	{"multiple", Dstr, rec.multiple,},
	{"name", Dstr, rec.name,},
	{"note", Dstr, rec.note,},
	{"par_code", Dstr, rec.par_code,},
	{"parallax", Ddouble, &rec.parallax,},
	{"pmdec", Ddouble, &rec.pmdec,},
	{"pmra", Ddouble, &rec.pmra,},
	{"ra", Ddouble, &rec.ra,},
	{"ra_1900", Ddouble, &rec.ra_1900,},
	{"ra_1950", Ddouble, &rec.ra_1950,},
	{"radvel", Dint, &rec.radvel,},
	{"radvel_comm", Dstr, rec.radvel_comm,},
	{"ri_code", Dstr, rec.ri_code,},
	{"ri_color", Ddouble, &rec.ri_color,},
	{"rotvel", Dint, &rec.rotvel,},
	{"rotvel_comm", Dstr, rec.rotvel_comm,},
	{"rotvel_uncert", Dstr, rec.rotvel_uncert,},
	{"sao", Dint, &rec.sao,},
	{"spect_code", Dstr, rec.spect_code,},
	{"spect_type", Dstr, rec.spect_type,},
	{"ub_color", Ddouble, &rec.ub_color,},
	{"ub_uncert", Dstr, rec.ub_uncert,},
	{"var_id", Dstr, rec.var_id,},
	{"vmag", Ddouble, &rec.vmag,},
	{"vmag_code", Dstr, rec.vmag_code,},
	{"vmag_uncert", Dstr, rec.vmag_uncert,},
};

void
strip(char *b)
{
	while(*b){
		if((*b>='a' && *b<='z') ||
			(*b>='A' && *b <= 'Z') ||
			(*b>='0' && *b <= '9') ||
			(*b == '_')){
			b++;
			continue;
		}
		*b++ = '_';
	}
}

void
skiphdr(Biobuf *bp)
{
	char *l;
	int n;

	//fprint(2, "skiphdr\n");
	do{
		l=Brdline(bp, '\n');
		n=Blinelen(bp);
		//fprint(2, "skipping\n");
	}while(l && strncmp(l, "<DATA>\n", n));
}

int
rdline(Biobufhdr *bp, char *b)
{
	char *l;
	int n;

	if(!(l=Brdline(bp, '\n'))) return -1;
	if(!(n = Blinelen(bp))) return -1;
	strncpy(b, l, n-1);
	b[n-1] = '\0';
	if(!strncmp(b, "<END>", n-1)) return -1;

	return 0;
}

int
parse(char *b)
{
	int n, len;
	char *start, *pos;
	struct annotation *np;

	n = nelem(note);
	start = pos = b;
	np = note;
	while(n--){
		//fprint(2, "parse: %d(%s) ", np->dtyp, np->id);
eat:		switch(*pos){
		case '\0':
			//fprint(2, "<eol>\n");
			goto eol;
		case '|':
			//fprint(2, "<eos>\n");
			*pos = '\0';
			break;
		default:
			//fprint(2, "%c", *pos);
			pos++;
			goto eat;
		}
		switch(np->dtyp){
		case Dint:
			*(int*)(np->p) = atoi(start);
			break;
		case Ddouble:
			*(double*)(np->p) = atof(start);
			break;
		case Dstr:
			strncpy(np->p, start, pos-start+1);
			break;
		}
		np++;
		start = ++pos;
	}
eol:	return n+1; // number of fields left
}

void
fprintrec(int fd)
{
	char *id;
	int r,g,b;

	id = rec.name;
	strip(id);
	fprint(fd, "# bsc5p object %s\n", id);
	fprint(fd, "(insert %s)\n", id);
	fprint(fd, "(set %s.str \"%s\")\n", id, rec.alt_name);
	fprint(fd, "(set %s.eq.ra %f)\n", id, rec.ra * PI / 180.0);
	fprint(fd, "(set %s.eq.dec %f)\n", id, rec.dec * PI / 180.0);
	fprint(fd, "(set %s.bv %f)\n", id, rec.bv_color);
	fprint(fd, "(set %s.vmag %f)\n", id, rec.vmag);
	fprint(fd, "(set %s.size %d)\n", id, 1);
	fprint(fd, "\n");
}

void
main(int argc, char *argv[])
{
	Biobuf *bp;
	char b[BUFSIZE];

	if(!(bp=malloc(sizeof(Biobuf))))
		sysfatal("malloc");

	if(Binit(bp, 0, OREAD) == Beof)
		sysfatal("Binit");

	skiphdr(bp);
	while(rdline(bp, b) >= 0){
		if(parse(b)){
			fprint(2, "[warning] skipping incomplete line %s\n", b);
			continue;
		}
		fprintrec(1);
	}

	Bterm(bp);
	free(bp);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.