diff --git a/src/firefly/js/astro/conv/__test__/CoordConv-test.js b/src/firefly/js/astro/conv/__test__/CoordConv-test.js new file mode 100644 index 0000000000..7d5cf74fb8 --- /dev/null +++ b/src/firefly/js/astro/conv/__test__/CoordConv-test.js @@ -0,0 +1,790 @@ +/*eslint-env node, mocha */ +import {expect,assert} from 'chai'; +import {doConv, doConvPM} from '../CoordConv.js'; +const EQUATORIAL_J = 0; +const EQUATORIAL_B = 1; +const GALACTIC = 2; +const ECLIPTIC_B = 3; +const SUPERGALACTIC = 4; +const ECLIPTIC_J = 13; +const EQINOX2000=2000.0; +const EQINOX1950=1950.0; +const precision=10; +describe('A test suite for CoordConv.js', function () { + + + it('should be equal when convert from inCoord = EQUATORIAL_J, to outCoord = coordianteSys where inEquirox=outEquinox=2000', function () { + const coordianteSys = [ + //EQUATORIAL_J, + EQUATORIAL_B, + GALACTIC, + ECLIPTIC_B, + SUPERGALACTIC + ]; + const expectedLons = [ + 124.53450295504638, + 254.14759117810968, + 140.5026330845168, + 167.8940515712723 + ]; + const expectedLats =[ + -35.95580982205028, + -0.0946854772394013, + -53.509857120978175, + -62.562888161876515 + ]; + const inLon = 124.534666; + const inLat = -35.955853; + var lons = []; + var lats = []; + var ret; + const tobs=0.0; + const inCoord = EQUATORIAL_J; + for (let i = 0; i < coordianteSys.length; i++){ + ret = doConv(inCoord, EQINOX2000, + inLon, inLat, + coordianteSys[i], EQINOX2000, tobs); + lons[i]=ret.lon; + lats[i]=ret.lat; + } + for (let i=0; i { - request = makeTblRequest('IpacTableFromSource'); + // request = makeTblRequest('IpacTableFromSource'); } ); /* run once testing is done */ diff --git a/src/firefly/js/visualize/projection/Projection.js b/src/firefly/js/visualize/projection/Projection.js index 9e14d1a1f5..6798ab6d6c 100644 --- a/src/firefly/js/visualize/projection/Projection.js +++ b/src/firefly/js/visualize/projection/Projection.js @@ -121,13 +121,10 @@ const projTypes= { } }; -const getProjTypeInfo= (header) => projTypes[get(header,'maptype', UNRECOGNIZED)]; - - -const translateProjectionName= (header) => getProjTypeInfo(header).name; -const isImplemented= (header) => getProjTypeInfo(header).implemented; -const isWrappingProjection= (header) => getProjTypeInfo(header).wrapping; +const translateProjectionName= (maptype) => get(projTypes, [maptype,'name'],'UNRECOGNIZED'); +const isImplemented= (header) => get(header, ['maptype.implemented'],false); +const isWrappingProjection= (header) => get(header, ['maptype.wrapping'],false); @@ -156,7 +153,11 @@ function getWorldCoordsInternal(x, y, header, coordSys) { * @return ImagePt with X,Y in 'Skyview Screen' coordinates */ function getImageCoordsInternal(ra, dec, header) { + //console.log('in Project.js'); + //console.log(header); + //console.log(header.maptype); if (!projTypes[header.maptype]) return null; + //console.log(projTypes[header.maptype]); const image_pt = projTypes[header.maptype].revProject( ra, dec, header); return image_pt; } diff --git a/src/firefly/js/visualize/projection/__test__/ProjectTest.js b/src/firefly/js/visualize/projection/__test__/ProjectTest.js deleted file mode 100644 index 096242f804..0000000000 --- a/src/firefly/js/visualize/projection/__test__/ProjectTest.js +++ /dev/null @@ -1,24 +0,0 @@ - -import {describe, expect, it, assert} from 'chai'; -import {Projection, makeProjection} from '../Projection.js'; - -import {Projection} from '../Projection.js'; - -describe('A test suite for projection.js', function () { - /* run once before testing */ - var jsonObj; - before(() => { - //jsonObj = loadJsonObj(); - - } - ); - //TODO add test below - it('should have a header and a coordinate parameters', function() { - // Test implementation goes here - var gwtProject = 'load gwt project here'; - - var jsProject = makeProjection(jsonObj); - assert.equal(jsProject, gwtProject); - }); - -}); \ No newline at end of file diff --git a/src/firefly/js/visualize/projection/__test__/Projection-test.js b/src/firefly/js/visualize/projection/__test__/Projection-test.js new file mode 100644 index 0000000000..718ad521c8 --- /dev/null +++ b/src/firefly/js/visualize/projection/__test__/Projection-test.js @@ -0,0 +1,124 @@ +/*eslint-env node, mocha */ +import {expect,assert} from 'chai'; +import {Projection, makeProjection} from '../Projection.js'; +import CoordinateSys from '../../CoordSys.js'; + +/** + * LZ finalized on 8/30/16 + * The javascript unit test uses the same data in the java unit test tree. + * + */ +var fs = require('fs'); + +const JAVA_TEST_DATA_PATH='firefly_test_data/edu/caltech/ipac/visualize/plot/projection/'; + +const precision=10; + +var projectionJson={}; + + +const projTypes = { + GNOMONIC : 1001, + ORTHOGRAPHIC : 1002, + NCP : 1003, + AITOFF : 1004, + CAR : 1005, + LINEAR : 1006, + PLATE : 1007, + ARC : 1008, + SFL : 1009, + CEA : 1010, + UNSPECIFIED : 1998, + UNRECOGNIZED : 1999 +}; + +function getJsonFiles(dir){ + var fileList = []; + + var files = fs.readdirSync(dir); + for(var i in files){ + if (!files.hasOwnProperty(i)) continue; + var name = dir+'/'+files[i]; + if (!fs.statSync(name).isDirectory() && name.endsWith('json') ){ + fileList.push(name); + } + } + return fileList; +} + +describe('A test suite for projection.js', function () { + + + + describe('This is to test all json files stored in the testing directory', function () { + + + var path = require('path'); + //__filename returns absolute path to file where it is placed + var scriptDirString = path.dirname(fs.realpathSync(__filename)); + + //the abosolute root path to js/ not include js + // var rootPath = scriptDirString.split('js')[0]; + var rootPath = scriptDirString.split('firefly')[0]; + var dataPath = rootPath+JAVA_TEST_DATA_PATH; + + var jsonFiles = getJsonFiles(dataPath); + console.log(dataPath); + + var imageHeader; + var expectedImagePt; + var expectedWorldPt; + var imagePt, worldPt; + + + for (let i=0; i objClass = obj.getClass(); + //array containing Field objects reflecting all the accessible public fields of the + //class or interface represented by this Class object + Field[] fields = objClass.getFields(); + //process the field's name and value pair and save to JSONObject + for (Field field : fields) { + if (field == null) continue; + String name = field.getName(); + Object value = field.get(obj); + if (value != null) { + if (value.getClass().isArray() ) { + jsonObj.put(name, objetArrayToJsonSring(value)); + } + else { + if (name.equalsIgnoreCase("bunit")){ + + jsonObj.put(name, value.toString()); + } + else { + jsonObj.put(name, value); + } + + } + + } + } + return jsonObj; + } + + private static ImageHeader getImageHeader(Fits fits)throws FitsException { + BasicHDU[] hdus = fits.read(); + + if (hdus == null){ + + throw new FitsException (" hdu is null"); + } + Header header = hdus[0].getHeader(); + ImageHeader imageHeader= new ImageHeader(header); + + return imageHeader; + } + + + public static void writeToJson(String inputFitsFile) throws Exception { + + Fits fits = new Fits(inputFitsFile); + JSONObject obj = new JSONObject(); + String outJsonFile = inputFitsFile.substring(0, inputFitsFile.length()-5 ) + "Header.json"; + + obj.put("headerFileName", outJsonFile ); + ImageHeader imageHeader = getImageHeader(fits); + + //convert the ImageHeader object to jsonString + JSONObject imageHeaderObj = ImageHeaderToJson(imageHeader); + obj.put("header",imageHeaderObj); + + /*the expected value is achieved when the test is written. If the Java code changes and + the Assert is falling, the changes introduce the problem. + */ + Projection projection = imageHeader.createProjection(CoordinateSys.EQ_J2000); + + ProjectionPt imagePt = projection.getImageCoords( imageHeader.crval1, imageHeader.crval2);//RA and DEC at the center of the image + JSONObject imagePtJson = new JSONObject(); + imagePtJson.put("x",imagePt.getX() ); + imagePtJson.put("y",imagePt.getY() ); + obj.put("expectedImagePt", imagePtJson); + + WorldPt worldPt = projection.getWorldCoords(imagePt.getX(), imagePt.getY()); + JSONObject worldPJson = new JSONObject(); + worldPJson.put("x",worldPt.getX() ); + worldPJson.put("y",worldPt.getY() ); + obj.put("expectedImagePt", imagePtJson); + obj.put("expectedWorldPt",worldPJson); + + try { + FileWriter file = new FileWriter(outJsonFile); + file.write(obj.toJSONString()); + file.flush(); + file.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + + public static void main(String[] args) throws Exception { + + if (args.length<1){ + System.out.println("usage FitsHeaderToJson inputFile"); + } + + + + String path="/Users/zhang/lsstDev/testingData/projectionTestingData/"; + String[] fNames = { + "1904-66_SFL.fits", //SFL or GLS SANSON-FLAMSTEED + "f3.fits", //GNOMONIC + "DssImage-10.68470841.26902815.015.0poss2ukstu_red.fits", //PLATE + "GLM_01050+0000_mosaic_I2.fits", //CAR CARTESIAN + "m51.car.fits", //CAR CARTESIAN + "field1.fits", //SIN ORTHOGRAPHIC + "m51.sin.fits", //SIN ORTHOGRAPHIC + // "file7.fits", //Not FITS format + //"file7Downloaded.fits", + "SIP.fits", //TAN--SIP GNOMONIC + //"lsstsample1.fits", //CEA CYLINDRICAL EQUAL AREA + "allsky.fits", //AIT (ATF is deprecated) AITOFF + "bird_2ha3-Booth.fits", //---- LINEAR + "NED_M31.fits", //ARC ZENITH EQUIDISTANT + "cong_12_smo.fits" //ARC ZENITH EQUIDISTANT + }; + + for (int i=0; i fArray = new ArrayList(); + String[] fileNames = folder.list(); + + for (int i = 0; i < fileNames .length; i++) { + if (fileNames[i].endsWith(".json")) { + fArray.add(path+fileNames[i]); + } + } + return fArray.toArray(new String[0]); + } + + /** + * Thie method pass a JSONObject that contains two fields into a Java HashMap + * @param jsonObject + * @return + */ + private HashMap jsonObjectToMap(JSONObject jsonObject){ + HashMap map = new HashMap(); + + String[] keys = (String[]) jsonObject.keySet().toArray(new String[0]); + for (int i=0; i objClass = imageHeader.getClass(); + + //array containing Field objects reflecting all the accessible public fields of the + //class or interface represented by this Class object + Field[] fields = objClass.getFields(); + for (Field field : fields) { + + if (field == null || field.toString().contains("final")) continue; + String name = field.getName(); + Object value = jsonObject.get(name); + + if (value != null) { + if (value instanceof JSONArray){ //test if the value is an array + JSONArray jArray= (JSONArray) value; + if (jArray.get(0) instanceof JSONArray) { //test if it is a two dimensional array + int len = jArray.size(); + double[][] double2d = new double[len][((JSONArray) jArray.get(0)).size()]; + for (int i=0; i expectedImagePtMap = jsonObjectToMap((JSONObject) obj); + + + Assert.assertEquals( expectedImagePtMap.get("x").doubleValue(),image_pt.getX(), delta ); + Assert.assertEquals( expectedImagePtMap.get("y").doubleValue(),image_pt.getY(), delta ); + + /*the expected value is achieved when the test is written. If the Java code changes and + the Assert is falling, the changes introduce the problem. + */ + WorldPt world_pt = projection.getWorldCoords(image_pt.getX(), image_pt.getY()); + + obj = jsonObject.get("expectedWorldPt"); + HashMap expectedWorldPtMap = jsonObjectToMap((JSONObject) obj); + Assert.assertEquals(expectedWorldPtMap.get("x").doubleValue(), world_pt.getX(), delta ); + Assert.assertEquals(expectedWorldPtMap.get("y").doubleValue(), world_pt.getY(), delta ); + + + }catch (Exception e) { + System.out.println(jsonFileName + " has exception"); + } + + } + @Test + public void testAllProjections() throws IOException, ParseException, FitsException, ProjectionException, IllegalAccessException, URISyntaxException { + + + + //This is to run unit test under command line + String testTreePath = ProjectionTest.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + String rootPath = testTreePath.split("firefly")[0]; + String dataPath = rootPath+TEST_DATA_PATH; + String[] jsonHeaderFileNames = getJsonFiles(dataPath); + + + JSONParser parser = new JSONParser(); + + + for (int i=0; i