22
33import edu .caltech .ipac .firefly .data .ServerParams ;
44import edu .caltech .ipac .firefly .data .TableServerRequest ;
5+ import edu .caltech .ipac .firefly .data .table .MetaConst ;
6+ import edu .caltech .ipac .firefly .server .ServerContext ;
57import edu .caltech .ipac .firefly .server .SrvParam ;
8+ import edu .caltech .ipac .firefly .server .servlets .HiPSRetrieve ;
69import edu .caltech .ipac .table .DataGroup ;
710import edu .caltech .ipac .table .io .FITSExtractToTable ;
811import edu .caltech .ipac .visualize .plot .ImagePt ;
912import edu .caltech .ipac .visualize .plot .WorldPt ;
1013import edu .caltech .ipac .visualize .plot .plotdata .FitsExtract ;
1114import nom .tam .fits .FitsException ;
1215
16+ import java .io .File ;
1317import java .io .IOException ;
1418import java .util .ArrayList ;
1519import java .util .HashMap ;
1620import java .util .List ;
1721import java .util .Map ;
1822
19- import static edu .caltech .ipac .firefly .core .Util .Try ;
20-
21- ;
23+ import static edu .caltech .ipac .util .StringUtils .isEmpty ;
2224
2325/**
2426 * Extract data from an image and make a table
3234 @ ParamDoc (name ="wptAry" , desc ="for point selection, added to the created table" ),
3335 @ ParamDoc (name ="wlAry[i]" , desc ="an array of wavelength to add to the table, added to the created table" ),
3436 @ ParamDoc (name ="wlUnit[i]" , desc ="wavelength units. if defined" ),
35- @ ParamDoc (name ="fluxUnit[i]" , desc ="flux units. if defined" ),
3637 @ ParamDoc (name ="filename[i]" , desc ="filename on the server" ),
38+ @ ParamDoc (name ="hipsTileUrl" , desc ="z-axis only, url of a HiPS FITS cube tile, used instead of filename" ),
39+ @ ParamDoc (name =ServerParams .HIPS_TILE_URL_ARY , desc ="points or line, url of the HiPS FITS tile of each point, used with ptAry, wptAry and plane instead of the indexed parameters" ),
3740 @ ParamDoc (name ="refHDUNum[i]" , desc ="hdu number to extract" ),
3841 @ ParamDoc (name ="extractionSizeX" , desc ="number of the x size of the extract" ),
3942 @ ParamDoc (name ="extractionSizeY" , desc ="number of the y size of the extract" ),
@@ -46,6 +49,7 @@ public class ExtractFromImage extends EmbeddedDbProcessor {
4649 public static final String ID = "ExtractFromImage" ;
4750 public static final String EXTRACTION_TYPE = "extractionType" ;
4851 public static final String FILENAME = "filename" ;
52+ public static final String HIPS_TILE_URL = "hipsTileUrl" ;
4953 public static final String REF_HDU_NUM = "refHDUNum" ;
5054 public static final String EXTRACTION_SIZE_X = "extractionSizeX" ;
5155 public static final String EXTRACTION_SIZE_Y = "extractionSizeY" ;
@@ -54,44 +58,77 @@ public class ExtractFromImage extends EmbeddedDbProcessor {
5458 @ Override
5559 public DataGroup fetchDataGroup (TableServerRequest req ) throws DataAccessException {
5660
57- String extType = req .getParam (EXTRACTION_TYPE );
61+ String extType = req .getParam (EXTRACTION_TYPE , "" );
5862 int ptSizeX = req .getIntParam (EXTRACTION_SIZE_X , 1 );
5963 int ptSizeY = req .getIntParam (EXTRACTION_SIZE_Y , 1 );
6064 FitsExtract .CombineType ct = Enum .valueOf (FitsExtract .CombineType .class ,req .getParam (ServerParams .COMBINE_OP ,"AVG" ));
6165 FitsExtract .SecondaryHduCombine secondCombine = Enum .valueOf (FitsExtract .SecondaryHduCombine .class ,req .getParam ("secondaryCombine" ,"AUTO" ));
6266 boolean allMatchingHDUs = req .getBooleanParam (ALL_MATCHING_HDUS , true );
67+ var isLine = extType .equals ("line" );
68+ var ptOrLine = extType .equals ("points" ) || isLine ;
69+ var hipsTiles = req .getParam (ServerParams .HIPS_TILE_URL_ARY )!=null ;
70+ WorldPt [] wptAry = ptOrLine ? SrvParam .getWorldPtAryFromJson (req .getParam (ServerParams .WPT_ARY )) : null ;
6371 try {
64- if (extType == null || extType .equals ("z-axis" )) {
65- String filename = req .getParam (FILENAME );
66- int refHduNum = req .getIntParam (REF_HDU_NUM , -1 );
72+ if (extType .equals ("z-axis" ) || isEmpty (extType )) {
73+ String hipsTileUrl = req .getParam (HIPS_TILE_URL );
74+ File fitsFile = hipsTileUrl !=null
75+ ? HiPSRetrieve .getHiPSFitsTileFile (hipsTileUrl )
76+ : ServerContext .convertToFile (req .getParam (FILENAME ));
77+ int refHduNum = hipsTileUrl !=null ? FitsExtract .FIRST_IMAGE_HDU : req .getIntParam (REF_HDU_NUM , FitsExtract .NOT_SPECIFIED );
6778 ImagePt pt = ImagePt .parse (req .getParam (ServerParams .PT ));
6879 WorldPt wpt = WorldPt .parse (req .getParam (ServerParams .WPT ));
6980 String wlUnit = req .getParam (ServerParams .WL_UNIT );
7081 double [] wlAry = SrvParam .getDoubleAryFromJson (req .getParam (ServerParams .WL_ARY ));
71- checkZAxisParams (pt , filename , refHduNum );
72- Map <Integer ,String > fluxUnit = makeMapOfUnitsFromParam (req );
73- return FITSExtractToTable .getCubeZaxisAsTable (pt , wpt , filename , refHduNum , allMatchingHDUs ,
74- ptSizeX , ct , secondCombine , wlAry ,wlUnit ,fluxUnit );
82+ checkZAxisParams (pt , fitsFile , refHduNum );
83+ DataGroup dg = FITSExtractToTable .getCubeZaxisAsTable (pt , wpt , fitsFile , refHduNum , allMatchingHDUs ,
84+ ptSizeX , ct , secondCombine , wlAry ,wlUnit );
85+ if (hipsTileUrl !=null ) dg .getTableMeta ().addKeyword (MetaConst .HIPS_TILE_URL , hipsTileUrl );
86+ return dg ;
87+ }
88+ else if (ptOrLine && hipsTiles ) {
89+ String [] tileUrlAry = SrvParam .getStringAryFromJson (req .getParam (ServerParams .HIPS_TILE_URL_ARY ));
90+ return FITSExtractToTable .getHiPSPointsAsTable (
91+ tileUrlAry ,
92+ getHiPSTileFiles (tileUrlAry ),
93+ SrvParam .getImagePtAryFromJson (req .getParam (ServerParams .PTARY )),
94+ wptAry ,
95+ FitsExtract .FIRST_IMAGE_HDU ,
96+ req .getIntParam (ServerParams .PLANE , 0 ),
97+ allMatchingHDUs , ptSizeX , ptSizeY , ct , secondCombine , isLine );
7598 }
76- else if (extType . equals ( "line" ) || extType . equals ( "points" ) ) {
99+ else if (ptOrLine ) {
77100 var extractParamsList = getDataExtractParams (req );
78- WorldPt [] wptAry = SrvParam .getWorldPtAryFromJson (req .getParam (ServerParams .WPT_ARY ));
79- return FITSExtractToTable .getDataSelectAsTable (extractParamsList , wptAry , allMatchingHDUs ,
80- ptSizeX , ptSizeY , ct , secondCombine , extType .equals ("line" ));
101+ return FITSExtractToTable .getDataSelectAsTable (extractParamsList , wptAry ,
102+ allMatchingHDUs , ptSizeX , ptSizeY , ct , secondCombine , isLine );
81103 }
82104 } catch (IOException | FitsException e ) {
83105 throw new IllegalArgumentException ("Could not make a table from extracted data" );
84106 }
85107 throw new IllegalArgumentException ("extractionType must be z-axis, line, or points" );
86108 }
87109
110+ /**
111+ * retrieve the HiPS FITS tiles, each url is retrieved once
112+ * @param tileUrlAry the url of the tile for each point
113+ * @return the file for each url, null for a tile that could not be retrieved
114+ */
115+ private static File [] getHiPSTileFiles (String [] tileUrlAry ) {
116+ if (tileUrlAry ==null ) throw new IllegalArgumentException (ServerParams .HIPS_TILE_URL_ARY +" is required" );
117+ Map <String ,File > fileByUrl = new HashMap <>();
118+ File [] fileAry = new File [tileUrlAry .length ];
119+ for (int i =0 ; i <tileUrlAry .length ; i ++) {
120+ fileAry [i ]= fileByUrl .computeIfAbsent (tileUrlAry [i ], HiPSRetrieve ::getHiPSFitsTileFile );
121+ }
122+ return fileAry ;
123+ }
124+
88125 private static List <FITSExtractToTable .DataExtractParams > getDataExtractParams (TableServerRequest req ) {
89126 var eList = new ArrayList <FITSExtractToTable .DataExtractParams >();
90127 String [] titles = SrvParam .getStringAryFromJson ( req .getParam ("titleAry" ));
91128 if (titles ==null ) titles = new String [] {"" };
92129 for (int i =0 ; i <titles .length ; i ++) {
93130 String filename = req .getParam (FILENAME +i );
94- int refHduNum = req .getIntParam (REF_HDU_NUM +i , - 1 );
131+ int refHduNum = req .getIntParam (REF_HDU_NUM +i , FitsExtract . NOT_SPECIFIED );
95132 int plane = req .getIntParam (ServerParams .PLANE +i , -1 );
96133 double [] wlAry = SrvParam .getDoubleAryFromJson (req .getParam (ServerParams .WL_ARY +i ));
97134 String wlUnit = req .getParam (ServerParams .WL_UNIT +i );
@@ -102,30 +139,21 @@ private static List<FITSExtractToTable.DataExtractParams> getDataExtractParams(T
102139 return eList ;
103140 }
104141
105- private static Map <Integer ,String > makeMapOfUnitsFromParam (TableServerRequest req ) {
106- String [] sAry = SrvParam .getStringAryFromJson (req .getParam (ServerParams .FLUX_UNIT ));
107- Map <Integer ,String > fluxUnit = new HashMap <>();
108- if (sAry ==null ) return fluxUnit ;
109- for (String entry : sAry ) {
110- String [] s = entry .split ("=" );
111- if (s .length ==2 ) {
112- Try .it (() -> fluxUnit .put (Integer .parseInt (s [0 ]),s [1 ]));
113- }
114- }
115- return fluxUnit ;
116- }
117-
118- private static void checkZAxisParams (ImagePt pt , String filename , int refHduNum ) {
142+ private static void checkZAxisParams (ImagePt pt , File fitsFile , int refHduNum ) {
119143 if (pt ==null ) throw new IllegalArgumentException ("Point is require for z-axis extraction" );
120- if (filename ==null ) throw new IllegalArgumentException ("filename is require for z-axis extraction" );
121- if (refHduNum <0 ) throw new IllegalArgumentException ("refHduNum is require for z-axis extraction" );
144+ if (fitsFile ==null ) throw new IllegalArgumentException ("filename or a retrievable hipsTileUrl is require for z-axis extraction" );
145+ if (refHduNum ==FitsExtract .NOT_SPECIFIED || (refHduNum <0 && refHduNum !=FitsExtract .FIRST_IMAGE_HDU )) {
146+ throw new IllegalArgumentException ("refHduNum is require for z-axis extraction" );
147+ }
122148 }
123149
124150 private static void checkPointParams (ImagePt [] ptAry , int plane , String filename , int refHduNum ) {
125151 if (ptAry ==null || ptAry .length ==0 ) throw new IllegalArgumentException ("ptAry is require for points extraction" );
126152 if (filename ==null ) throw new IllegalArgumentException ("filename is require for points extraction" );
127- if (refHduNum <0 ) throw new IllegalArgumentException ("refHduNum is require for points extraction" );
128- if (plane <0 ) throw new IllegalArgumentException ("refHduNum is require for points extraction" );
153+ if (refHduNum ==FitsExtract .NOT_SPECIFIED || refHduNum <0 ) {
154+ throw new IllegalArgumentException ("refHduNum is require for points extraction" );
155+ }
156+ if (plane <0 ) throw new IllegalArgumentException ("plane is require for points extraction" );
129157 }
130158
131159}
0 commit comments