use of gaiasky.util.ucd.UCD in project gaiasky by langurmonkey.
the class EventScriptingInterface method getAttributeByName.
private IAttribute getAttributeByName(String name, CatalogInfo ci) {
try {
// One of the default attributes
Class<?> clazz = Class.forName("gaiasky.util.filter.attrib.Attribute" + name);
Constructor<?> ctor = clazz.getConstructor();
return (IAttribute) ctor.newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
// Try extra attributes
if (ci.object instanceof ParticleGroup) {
ParticleGroup pg = (ParticleGroup) ci.object;
ObjectDoubleMap.Keys<UCD> ucds = pg.get(0).extraKeys();
for (UCD ucd : ucds) if (ucd.colname.equalsIgnoreCase(name))
return new AttributeUCD(ucd);
}
}
return null;
}
use of gaiasky.util.ucd.UCD in project gaiasky by langurmonkey.
the class STILDataProvider method getAllStringsUcd.
private Pair<UCD, String>[] getAllStringsUcd(Array<UCD> UCDs, Object[] row) {
Array<Pair<UCD, String>> strings = new Array<>(false, 2);
for (UCD ucd : UCDs) {
try {
String str = row[ucd.index].toString().strip();
strings.add(new Pair<>(ucd, str));
} catch (Exception e) {
// not working, try next
}
}
Pair<UCD, String>[] result = new Pair[strings.size];
int i = 0;
for (Pair<UCD, String> value : strings) {
result[i++] = value;
}
return result;
}
use of gaiasky.util.ucd.UCD in project gaiasky by langurmonkey.
the class STILDataProvider method loadData.
/**
* @param ds The data source.
* @param factor Length factor.
* @param preCallback A function that runs before.
* @param updateCallback A function that runs after each object has loaded. Gets two longs, the first holds the current number of loaded objects and the
* second holds the total number of objects to load.
* @param postCallback A function that runs after the data has been loaded.
*
* @return The list of particle records.
*/
public List<IParticleRecord> loadData(DataSource ds, double factor, Runnable preCallback, RunnableLongLong updateCallback, Runnable postCallback) {
try {
if (factory != null) {
// RNG
final Random r = new Random(123L);
// Add extra builders
List<TableBuilder> builders = factory.getDefaultBuilders();
builders.add(new CsvTableBuilder());
builders.add(new AsciiTableBuilder());
if (preCallback != null)
preCallback.run();
// Try to load
StarTable table = factory.makeStarTable(ds);
long count = table.getRowCount();
initLists((int) count);
UCDParser ucdParser = new UCDParser();
ucdParser.parse(table);
int resampledLightCurves = 0;
int noPeriods = 0;
if (ucdParser.haspos) {
BVToTeff_ballesteros bvToTEff = new BVToTeff_ballesteros();
int nInvalidParallaxes = 0;
long i = 0L;
long step = Math.max(1L, Math.round(count / 100d));
RowSequence rs = table.getRowSequence();
while (rs.next()) {
Object[] row = rs.getRow();
try {
// POSITION
Pair<UCD, Double> a = getDoubleUcd(ucdParser.POS1, row);
Pair<UCD, Double> b = getDoubleUcd(ucdParser.POS2, row);
Pair<UCD, Double> c;
String unitC;
Pair<UCD, Double> pos3 = getDoubleUcd(ucdParser.POS3, row);
// Check missing pos3 -> Use default parallax
if (ucdParser.POS3.isEmpty() || pos3 == null || pos3.getSecond() == null || !Double.isFinite(pos3.getSecond())) {
c = new Pair<>(null, 0.04);
unitC = "mas";
nInvalidParallaxes++;
} else {
c = getDoubleUcd(ucdParser.POS3, row);
assert c != null;
unitC = c.getFirst().unit;
}
assert a != null;
assert b != null;
PositionType pt = ucdParser.getPositionType(a.getFirst(), b.getFirst(), c.getFirst());
// Check negative parallaxes -> Use default for consistency
if (pt.isParallax() && (c.getSecond() == null || c.getSecond().isNaN() || c.getSecond() <= 0)) {
c.setSecond(0.04);
unitC = "mas";
nInvalidParallaxes++;
}
Position p = new Position(a.getSecond(), a.getFirst().unit, b.getSecond(), b.getFirst().unit, c.getSecond(), unitC, pt);
double distPc = p.gsposition.len();
if ((pt.isParallax() && c.getSecond() <= 0) || !Double.isFinite(distPc) || distPc < 0) {
// Next
break;
}
p.gsposition.scl(Constants.PC_TO_U);
// Find out RA/DEC/Dist
Vector3d sph = new Vector3d();
Coordinates.cartesianToSpherical(p.gsposition, sph);
// PROPER MOTION
Vector3d pm;
double muAlphaStar = 0, muDelta = 0, radVel = 0;
// Only supported if position is equatorial spherical coordinates (ra/dec)
if (pt == PositionType.EQ_SPH_DIST || pt == PositionType.EQ_SPH_PLX) {
Pair<UCD, Double> pma = getDoubleUcd(ucdParser.PMRA, row);
Pair<UCD, Double> pmb = getDoubleUcd(ucdParser.PMDEC, row);
Pair<UCD, Double> pmc = getDoubleUcd(ucdParser.RADVEL, row);
muAlphaStar = pma != null ? pma.getSecond() : 0;
muDelta = pmb != null ? pmb.getSecond() : 0;
radVel = pmc != null ? pmc.getSecond() : 0;
double raRad = new Angle(a.getSecond(), a.getFirst().unit).get(AngleUnit.RAD);
double decRad = new Angle(b.getSecond(), b.getFirst().unit).get(AngleUnit.RAD);
pm = AstroUtils.properMotionsToCartesian(muAlphaStar, muDelta, radVel, raRad, decRad, distPc, new Vector3d());
} else {
pm = new Vector3d(Vector3d.Zero);
}
// MAGNITUDE
double appMag;
if (!ucdParser.MAG.isEmpty()) {
Pair<UCD, Double> appMagPair = getDoubleUcd(ucdParser.MAG, row);
assert appMagPair != null;
appMag = appMagPair.getSecond();
} else {
// Default magnitude
appMag = 15;
}
// Scale magnitude if needed
double magScl = (datasetOptions != null && datasetOptions.type == DatasetOptions.DatasetLoadType.STARS || (datasetOptions != null && datasetOptions.type == DatasetLoadType.VARIABLES)) ? datasetOptions.magnitudeScale : 0f;
appMag = appMag - magScl;
// Absolute magnitude to pseudo-size
final double absMag = AstroUtils.apparentToAbsoluteMagnitude(distPc, appMag);
final float size = (float) absoluteMagnitudeToPseudoSize(absMag);
// COLOR
float color;
if (!ucdParser.COL.isEmpty()) {
Pair<UCD, Double> colPair = getDoubleUcd(ucdParser.COL, row);
if (colPair == null) {
color = 0.656f;
} else {
color = colPair.getSecond().floatValue();
}
} else {
// Default color
color = 0.656f;
}
// VARIABILITY
float[] variMags = null;
double[] variTimes = null;
double pf = 0;
int nVari = 0;
if (ucdParser.hasvari) {
Pair<UCD, Double> period = getDoubleUcd(ucdParser.VARI_PERIOD, row);
if (!ucdParser.hasperiod || period == null || !Double.isFinite(period.getSecond())) {
// Skip stars without period
noPeriods++;
continue;
} else {
pf = period.getSecond();
}
Pair<UCD, double[]> variMagsPair = getDoubleArrayUcd(ucdParser.VARI_MAGS, row);
assert variMagsPair != null;
double[] variMagsDouble = variMagsPair.getSecond();
nVari = variMagsDouble.length;
variMags = new float[nVari];
Pair<UCD, double[]> variTimesPair = getDoubleArrayUcd(ucdParser.VARI_TIMES, row);
assert variTimesPair != null;
variTimes = variTimesPair.getSecond();
double[] auxMags = variMagsDouble;
double[] auxTimes = variTimes;
// SANITIZE (no NaNs)
List<Double> magnitudesList = new ArrayList<>();
List<Double> timesList = new ArrayList<>();
int idx = 0;
for (double mag : auxMags) {
if (Double.isFinite(mag)) {
magnitudesList.add(mag - magScl);
timesList.add(auxTimes[idx]);
}
idx++;
}
variMagsDouble = magnitudesList.stream().mapToDouble(Double::doubleValue).toArray();
variTimes = timesList.stream().mapToDouble(Double::doubleValue).toArray();
nVari = variMagsDouble.length;
// FOLD
List<Vector2d> list = new ArrayList<>(nVari);
for (int k = 0; k < nVari; k++) {
double phase = ((variTimes[k] - variTimes[0]) % pf);
list.add(new Vector2d(phase, variMagsDouble[k]));
}
list.sort(Comparator.comparingDouble(o -> o.x));
for (int k = 0; k < nVari; k++) {
Vector2d point = list.get(k);
variTimes[k] = point.x + variTimes[0];
variMagsDouble[k] = point.y;
}
// RESAMPLE (only if too many samples)
final int MAX_VARI = VariableGroupRenderSystem.MAX_VARI;
if (variMagsDouble.length > MAX_VARI) {
nVari = MAX_VARI;
double t0 = variTimes[0];
double tn = variTimes[variTimes.length - 1];
double tStep = (tn - t0) / (nVari - 1);
UnivariateInterpolator interp = new LinearInterpolator();
UnivariateFunction f = interp.interpolate(variTimes, variMagsDouble);
variMagsDouble = new double[nVari];
variTimes = new double[nVari];
for (idx = 0; idx < nVari; idx++) {
double t = t0 + tStep * idx;
variTimes[idx] = t;
variMagsDouble[idx] = f.value(t);
}
resampledLightCurves++;
}
// Convert magnitudes to sizes
assert variMags.length == variTimes.length;
for (int j = 0; j < variMagsDouble.length; j++) {
double variAbsoluteMag = AstroUtils.apparentToAbsoluteMagnitude(distPc, variMagsDouble[j]);
variMags[j] = (float) absoluteMagnitudeToPseudoSize(variAbsoluteMag);
}
}
// EFFECTIVE TEMPERATURE
float tEff;
if (!ucdParser.TEFF.isEmpty()) {
Pair<UCD, Double> tEffPair = getDoubleUcd(ucdParser.TEFF, row);
assert tEffPair != null;
tEff = tEffPair.getSecond().floatValue();
} else {
// Convert B-V to T_eff using Ballesteros 2012
tEff = (float) bvToTEff.bvToTeff(color);
}
// RGB
float[] rgb = ColorUtils.BVtoRGB(color);
// float[] rgb = ColorUtils.teffToRGB_harre(teff);
float col = Color.toFloatBits(rgb[0], rgb[1], rgb[2], 1.0f);
// IDENTIFIER AND NAME
String[] names;
long id = -1L;
int hip = -1;
if (ucdParser.NAME.isEmpty()) {
// Empty name
if (!ucdParser.ID.isEmpty()) {
// We have ID
Pair<UCD, String> namePair = getStringUcd(ucdParser.ID, row);
assert namePair != null;
names = new String[] { namePair.getSecond() };
if (namePair.getFirst().colname.equalsIgnoreCase("hip")) {
hip = Integer.parseInt(namePair.getSecond());
id = hip;
} else {
id = ++starId;
}
} else {
// Empty ID
id = ++starId;
names = new String[] { Long.toString(id) };
}
} else {
// We have a name
Pair<UCD, String>[] namePairs = getAllStringsUcd(ucdParser.NAME, row);
Array<String> namesArray = new Array<>(false, namePairs.length);
for (Pair<UCD, String> pair : namePairs) {
String[] currNames = pair.getSecond().split(Constants.nameSeparatorRegex);
for (String actualName : currNames) {
if (actualName != null && !actualName.isEmpty() && !TextUtils.contains(forbiddenNameValues, actualName, true)) {
namesArray.add(actualName);
}
}
}
names = new String[namesArray.size];
int k = 0;
for (String n : namesArray) {
names[k++] = n;
}
if (names.length == 0) {
names = new String[] { Long.toString(id) };
}
// Take care of HIP stars
if (!ucdParser.ID.isEmpty()) {
Pair<UCD, String> idPair = getStringUcd(ucdParser.ID, row);
assert idPair != null;
if (idPair.getFirst().colname.equalsIgnoreCase("hip")) {
hip = Integer.parseInt(idPair.getSecond());
id = hip;
} else {
id = ++starId;
}
} else {
id = ++starId;
}
}
// Populate provider lists
colors.put(id, rgb);
sphericalPositions.put(id, new double[] { sph.x, sph.y, sph.z });
if (datasetOptions == null || datasetOptions.type == DatasetOptions.DatasetLoadType.STARS || datasetOptions.type == DatasetOptions.DatasetLoadType.VARIABLES) {
double[] dataD = new double[ParticleRecord.STAR_SIZE_D];
float[] dataF = new float[ParticleRecord.STAR_SIZE_F];
dataD[ParticleRecord.I_X] = p.gsposition.x;
dataD[ParticleRecord.I_Y] = p.gsposition.y;
dataD[ParticleRecord.I_Z] = p.gsposition.z;
dataF[ParticleRecord.I_FPMX] = (float) pm.x;
dataF[ParticleRecord.I_FPMY] = (float) pm.y;
dataF[ParticleRecord.I_FPMZ] = (float) pm.z;
dataF[ParticleRecord.I_FMUALPHA] = (float) muAlphaStar;
dataF[ParticleRecord.I_FMUDELTA] = (float) muDelta;
dataF[ParticleRecord.I_FRADVEL] = (float) radVel;
dataF[ParticleRecord.I_FAPPMAG] = (float) appMag;
dataF[ParticleRecord.I_FABSMAG] = (float) absMag;
dataF[ParticleRecord.I_FCOL] = col;
dataF[ParticleRecord.I_FSIZE] = size;
dataF[ParticleRecord.I_FHIP] = hip;
// Extra
ObjectDoubleMap<UCD> extraAttributes = addExtraAttributes(ucdParser, row);
if (ucdParser.TEFF.isEmpty()) {
UCD tEffUCD = new UCD("phys.temperature.effective", "teff", "K", -1);
extraAttributes = initExtraAttributes(extraAttributes);
extraAttributes.put(tEffUCD, tEff);
} else {
extraAttributes = initExtraAttributes(extraAttributes);
extraAttributes.put(ucdParser.TEFF.first(), tEff);
}
final IParticleRecord sb;
if (datasetOptions != null && datasetOptions.type == DatasetLoadType.VARIABLES || variMags != null) {
sb = new VariableRecord(dataD, dataF, nVari, pf, variMags, variTimes, id, names, extraAttributes);
} else {
sb = new ParticleRecord(dataD, dataF, id, names, extraAttributes);
}
list.add(sb);
int appMagClamp = (int) MathUtilsd.clamp(appMag, 0, 21);
countsPerMag[appMagClamp] += 1;
} else if (datasetOptions.type == DatasetOptions.DatasetLoadType.PARTICLES) {
double[] point = new double[3];
point[ParticleRecord.I_X] = p.gsposition.x;
point[ParticleRecord.I_Y] = p.gsposition.y;
point[ParticleRecord.I_Z] = p.gsposition.z;
// Extra
ObjectDoubleMap<UCD> extraAttributes = addExtraAttributes(ucdParser, row);
IParticleRecord pb = new ParticleRecord(point, null, null, names, extraAttributes);
list.add(pb);
}
} catch (Exception e) {
logger.debug(e);
logger.debug("Exception parsing row " + i + ": skipping");
}
i++;
if (updateCallback != null && i % step == 0) {
updateCallback.run(i, count);
}
}
if (nInvalidParallaxes > 0) {
logger.warn("Found " + nInvalidParallaxes + " rows with nonexistent or negative parallax. Using the default 0.04 mas for them.");
}
if (resampledLightCurves > 0) {
logger.warn(resampledLightCurves + " light curves resampled to fit in default array size (=" + VariableGroupRenderSystem.MAX_VARI + ")");
}
if (noPeriods > 0) {
logger.warn("Skipped " + noPeriods + " variable stars without a period");
}
} else {
logger.error("Table not loaded: Position not found");
}
}
} catch (Exception e) {
logger.error(e);
} finally {
if (postCallback != null)
postCallback.run();
}
return list;
}
use of gaiasky.util.ucd.UCD in project gaiasky by langurmonkey.
the class STILDataProvider method addExtraAttributes.
private ObjectDoubleMap<UCD> addExtraAttributes(UCDParser ucdp, Object[] row) {
// Extra
ObjectDoubleMap<UCD> extraAttributes = null;
for (UCD extra : ucdp.extra) {
double val = Double.NaN;
try {
val = ((Number) row[extra.index]).doubleValue();
} catch (Exception e) {
Object o = row[extra.index];
if (o instanceof Character) {
Character c = (Character) o;
val = (double) c;
}
}
if (extraAttributes == null)
extraAttributes = new ObjectDoubleMap<>((int) (ucdp.extra.size * 1.25f), 0.8f);
extraAttributes.put(extra, val);
}
return extraAttributes;
}
use of gaiasky.util.ucd.UCD in project gaiasky by langurmonkey.
the class DatasetPreferencesWindow method generateFilterTable.
private void generateFilterTable(Filter filter) {
float minSelectWidth = 160f;
filterTable.clearChildren();
if (filter != null && filter.hasRules()) {
// Operation
OwnSelectBox<String> operation = new OwnSelectBox<>(skin);
operation.setWidth(minSelectWidth);
operation.setItems("and", "or", "xor");
operation.setSelected(filter.getOperationString().toLowerCase());
operation.addListener(event -> {
if (event instanceof ChangeEvent) {
String newOp = operation.getSelected();
filter.setOperation(newOp);
filterEdited = true;
return true;
}
return false;
});
filterTable.add(new OwnLabel(I18n.txt("gui.dataset.filter.operation"), skin)).left().padRight(pad10 * 2f).padBottom(pad10);
filterTable.add(operation).left().expandX().padBottom(pad10).row();
// Rules
Array<FilterRule> rules = filter.getRules();
Table rulesTable = new Table(skin);
filterTable.add(rulesTable).colspan(2);
for (FilterRule rule : rules) {
// UNIT
OwnLabel unit = new OwnLabel(rule.getAttribute().getUnit(), skin);
// ATTRIBUTE
boolean stars = ci.object instanceof StarGroup || ci.object instanceof OctreeWrapper;
Array<AttributeComboBoxBean> attrs = new Array<>(false, stars ? 12 : 7);
// Add particle attributes (dist, alpha, delta)
attrs.add(new AttributeComboBoxBean(new AttributeDistance()));
attrs.add(new AttributeComboBoxBean(new AttributeRA()));
attrs.add(new AttributeComboBoxBean(new AttributeDEC()));
attrs.add(new AttributeComboBoxBean(new AttributeEclLatitude()));
attrs.add(new AttributeComboBoxBean(new AttributeEclLongitude()));
attrs.add(new AttributeComboBoxBean(new AttributeGalLatitude()));
attrs.add(new AttributeComboBoxBean(new AttributeGalLongitude()));
if (stars) {
// Star-only attributes (appmag, absmag, mualpha, mudelta, radvel)
attrs.add(new AttributeComboBoxBean(new AttributeAppmag()));
attrs.add(new AttributeComboBoxBean(new AttributeAbsmag()));
attrs.add(new AttributeComboBoxBean(new AttributeMualpha()));
attrs.add(new AttributeComboBoxBean(new AttributeMudelta()));
attrs.add(new AttributeComboBoxBean(new AttributeRadvel()));
}
// Colors
attrs.add(new AttributeComboBoxBean(new AttributeColorRed()));
attrs.add(new AttributeComboBoxBean(new AttributeColorGreen()));
attrs.add(new AttributeComboBoxBean(new AttributeColorBlue()));
// Extra attributes
if (ci.object instanceof ParticleGroup) {
ParticleGroup pg = (ParticleGroup) ci.object;
if (pg.size() > 0) {
IParticleRecord first = pg.get(0);
if (first.hasExtra()) {
ObjectDoubleMap.Keys<UCD> ucds = first.extraKeys();
for (UCD ucd : ucds) attrs.add(new AttributeComboBoxBean(new AttributeUCD(ucd)));
}
}
}
OwnSelectBox<AttributeComboBoxBean> attribute = new OwnSelectBox<>(skin);
attribute.setItems(attrs);
attribute.setSelected(getAttributeBean(rule.getAttribute(), attrs));
attribute.addListener(event -> {
if (event instanceof ChangeEvent) {
IAttribute newAttr = attribute.getSelected().attr;
rule.setAttribute(newAttr);
// Update unit
unit.setText(newAttr.getUnit());
filterEdited = true;
return true;
}
return false;
});
rulesTable.add(attribute).left().padRight(pad10).padBottom(pad5);
// COMPARATOR
String[] cmps = new String[] { "<", "<=", ">", ">=", "==", "!=" };
OwnSelectBox<String> comparator = new OwnSelectBox<>(skin);
comparator.setWidth(minSelectWidth);
comparator.setItems(cmps);
comparator.setSelected(rule.getComparator().toString());
comparator.addListener(event -> {
if (event instanceof ChangeEvent) {
IComparator newComp = rule.getComparatorFromString(comparator.getSelected());
rule.setComparator(newComp);
filterEdited = true;
return true;
}
return false;
});
rulesTable.add(comparator).left().padRight(pad10).padBottom(pad5);
// VALUE
FloatValidator fval = new FloatValidator(-Float.MAX_VALUE, Float.MAX_VALUE);
OwnTextField value = new OwnTextField(Double.toString(rule.getValue()), skin, fval);
value.addListener(event -> {
if (event instanceof ChangeEvent) {
if (value.isValid()) {
try {
rule.setValue(Float.parseFloat(value.getText()));
filterEdited = true;
return true;
} catch (Exception e) {
logger.error(e);
return false;
}
}
return false;
}
return false;
});
rulesTable.add(value).left().padRight(pad10).padBottom(pad5);
// UNIT
rulesTable.add(unit).left().padRight(pad10 * 3f).padBottom(pad5);
// RUBBISH
OwnTextIconButton rubbish = new OwnTextIconButton("", skin, "rubbish");
rubbish.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.dataset.filter.rule.remove"), skin));
rubbish.addListener(event -> {
if (event instanceof ChangeEvent) {
deleteRule(filter, rule);
filterEdited = true;
return true;
}
return false;
});
rulesTable.add(rubbish).left().padBottom(pad5).row();
}
// New rule button
OwnTextIconButton addRule = new OwnTextIconButton(I18n.txt("gui.dataset.filter.rule.add"), skin, "add");
addRule.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.dataset.filter.rule.add"), skin));
addRule.pad(pad10);
rulesTable.add(addRule).left().padTop(pad10).row();
addRule.addListener(event -> {
if (event instanceof ChangeEvent) {
dpw.addRule(filter);
filterEdited = true;
return true;
}
return false;
});
} else {
// Add
filterTable.add(new OwnLabel(I18n.txt("gui.dataset.filter.nofilters"), skin)).left().padBottom(pad10).row();
OwnTextIconButton addFilter = new OwnTextIconButton(I18n.txt("gui.dataset.filter.add"), skin, "add");
addFilter.addListener(new OwnTextTooltip(I18n.txt("gui.tooltip.dataset.filter.add"), skin));
addFilter.pad(pad10);
filterTable.add(addFilter).left().padBottom(pad5).row();
addFilter.addListener(event -> {
if (event instanceof ChangeEvent) {
dpw.addFilter();
filterEdited = true;
return true;
}
return false;
});
}
pack();
}
Aggregations