Project

General

Profile

Download (15.9 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / resources / spectrofits / GetFitsDataSpectro.java @ 779bac69

1
/*******************************************************************************
2
 * Copyright 2010-2013 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
3
 *
4
 * This file is part of SITools2.
5
 *
6
 * SITools2 is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * SITools2 is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
18
 ******************************************************************************/
19
package fr.ias.sitools.resources.spectrofits;
20

    
21
import java.io.IOException;
22
import java.util.ArrayList;
23
import java.util.HashMap;
24
import java.util.List;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
27

    
28
import org.restlet.data.MediaType;
29
import org.restlet.ext.json.JsonRepresentation;
30
import org.restlet.ext.wadl.MethodInfo;
31
import org.restlet.representation.Representation;
32
import org.restlet.representation.Variant;
33
import org.restlet.resource.Get;
34
 
35
import fr.cnes.sitools.common.exception.SitoolsException;
36
import fr.cnes.sitools.common.resource.SitoolsParameterizedResource;
37
import fr.cnes.sitools.dataset.DataSetApplication;
38
import fr.cnes.sitools.dataset.database.DatabaseRequestFactory;
39
import fr.cnes.sitools.dataset.database.DatabaseRequestParameters;
40
import fr.cnes.sitools.dataset.database.common.DataSetExplorerUtil;
41
import fr.cnes.sitools.plugins.resources.model.ResourceParameter;
42
import fr.cnes.sitools.resources.order.utils.OrderResourceUtils;
43
import fr.cnes.sitools.dataset.database.DatabaseRequest;
44
import fr.cnes.sitools.datasource.jdbc.model.AttributeValue;
45
import fr.cnes.sitools.datasource.jdbc.model.Record;
46

    
47
import nom.tam.fits.*;
48

    
49
import org.json.JSONArray;
50
import org.json.JSONException;
51
import org.json.JSONObject;
52

    
53
/**
54
 * HTML resource
55
 * 
56
 * 
57
 * @author b.hasnoun
58
 */
59
public class GetFitsDataSpectro extends SitoolsParameterizedResource {
60

    
61
  @Override
62
  public void sitoolsDescribe() {
63
    setName("GetFitsDataSpectro");
64
    setDescription("Get cube fits file data");
65
  }
66

    
67
  @Override
68
  public void doInit() {
69
    super.doInit();
70
  }
71

    
72
  /**
73
   * Get HTML
74
   * 
75
   * @return Representation the HTML result
76
   */
77
  @Get
78
  public Representation getCsv() {
79
    return execute();
80
  }
81
 
82
  @Override
83
  protected void describeGet(MethodInfo info) {
84
    this.addInfo(info);
85
    info.setIdentifier("retrieve records and extract data and metadata from fits file");
86
    info.setDocumentation("Method to get the data and metadata from a fits file");
87
    addStandardGetRequestInfo(info);
88
    DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info);
89
    DataSetApplication application = (DataSetApplication) getApplication();
90
    DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained());
91
    addStandardResponseInfo(info);
92
    addStandardInternalServerErrorInfo(info);
93
  }
94

    
95
  @Override
96
  protected Representation head(Variant variant) {
97
    Representation repr = super.head();
98
    repr.setMediaType(MediaType.APPLICATION_JSON);
99
    return repr;
100
  }
101

    
102
  /**
103
   * Execute the request and return a Representation
104
   * 
105
   * @return the HTML representation
106
   */
107
  private Representation execute() {
108
    
109
    Representation repr = null;
110
    
111
    // generate the DatabaseRequest
112
    DataSetApplication datasetApp = (DataSetApplication) getApplication();
113
    DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext());
114

    
115
    // Get request parameters
116
    if (datasetApp.getConverterChained() != null) {
117
      datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest());
118
    }
119
    // Get DatabaseRequestParameters
120
    DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams();
121

    
122
    DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params);
123
    
124
    if (params.getDistinct()) {
125
      try {
126
        databaseRequest.createDistinctRequest();
127
      }
128
      catch (SitoolsException e) {
129
        
130
        e.printStackTrace();
131
      }
132
    } else {
133
      try {
134
        databaseRequest.createRequest();
135
      }
136
      catch (SitoolsException e) {
137
       
138
        e.printStackTrace();
139
      }
140
    }
141
    
142
    ResourceParameter urlName = this.getModel().getParameterByName("colUrl");
143
    int hdu = Integer.parseInt(this.getModel().getParameterByName("hduCube").getValue());
144
    String waveLocation = this.getModel().getParameterByName("wave").getValue();
145
    
146
    if (databaseRequest != null) {
147

    
148
      // Next for reading first record
149
      try {
150
        databaseRequest.nextResult();
151
        Record rec = databaseRequest.getRecord();
152
        
153
        AttributeValue attributeValueURL = OrderResourceUtils.getInParam(urlName, rec);
154
        
155
        if (attributeValueURL.getValue() != null){
156
            // get the file path
157
            String filePath = (String) attributeValueURL.getValue();
158
            try {
159
              
160
              Fits fits = new Fits(filePath);
161
              FitsFactory.setUseHierarch(true);
162
              Header.setLongStringsEnabled(true);
163
              
164
              JSONObject jSON = new JSONObject();
165
              
166
              int waveHDU = -1;
167
              
168
              int nberHDUs = fits.size();
169
              System.out.println("Number of HDUs: "+nberHDUs);
170
              jSON.put("HDUs", nberHDUs);
171
              
172
              // Headers from fits
173
              System.out.println("Getting Headers...");
174
              List<String> headers = new ArrayList<String>(nberHDUs);
175
              List<List<List<String>>> headersList = new ArrayList<List<List<String>>>(nberHDUs);
176
              
177
              for (int i=0;i<nberHDUs;i++){
178
                
179
                Header header = fits.getHDU(i).getHeader();
180

    
181
                int nbreCards = header.getNumberOfCards();
182
                String headerStr = "";
183
                
184
                //making HIERARCH mapping
185
                HashMap<String,String> mapHierarch = new HashMap<String,String>();                
186
                for (int j=0; j<nbreCards; j++){
187
                  String cardStr = header.getCard(j);
188
                  if (cardStr.matches("HIERARCH\\s.*\\s(META.*)\\s*=\\s*(.*)")) {
189
                    //System.out.println(cardStr);
190
                    Pattern p = Pattern.compile("HIERARCH\\s.*\\s(META.*)\\s*=\\s*(.*)");
191
                    Matcher m = p.matcher(cardStr);
192
                    if (m.find()) {
193
                      //System.out.println(m.group(1)+" "+m.group(2));
194
                      mapHierarch.put(m.group(1), m.group(2));
195
                    }
196
                  }
197
                  headerStr = headerStr+cardStr+"<br>";
198
                }
199
                
200
                //List of extensions -- List of List of Cards
201
                List<List<String>> cardList = new ArrayList<List<String>>(nbreCards);
202
                for (int j=0; j<nbreCards; j++){
203
                  
204
                  List<String> keyValueComment = new ArrayList<String>(nbreCards); 
205
                  HeaderCard card = header.findCard(header.getKey(j));
206
                  
207
                  if ( card!=null ) {
208
                    
209
                    //key
210
                    if (mapHierarch.containsKey(card.getKey())) { 
211
                      keyValueComment.add( "HIERARCH "+mapHierarch.get(card.getKey()) );
212
                      }
213
                    else {
214
                      keyValueComment.add(card.getKey());
215
                    }
216
                    
217
                    //value
218
                    keyValueComment.add(card.getValue());
219
                    
220
                    if ( card.getKey().contains("EXTNAME") && card.getValue().contains("ImageIndex") ) { 
221
                      System.out.println("ImageIndex in HDU "+i);
222
                      waveHDU=i;
223
                      }
224
                    
225
                    //comment
226
                    String commentCard = card.getComment();
227
                    if (commentCard!=null) {
228

    
229
                      if (commentCard.contains("&")) {
230
                        //System.out.println(j+" "+commentCard);
231
                        commentCard = commentCard.substring(0, commentCard.length()-1);
232
                        int jTmp = j;
233
                        while(header.getCard(jTmp+1).startsWith("COMMENT")){
234
                          String commentCardNext = header.getCard(jTmp+1);
235
                          if (commentCardNext.contains("&")) { commentCardNext = commentCardNext.substring(0, commentCardNext.length()-1); }
236
                          commentCard += commentCardNext.split("COMMENT ")[1];
237
                          jTmp++;
238
                          }
239
                        keyValueComment.add(commentCard);
240
                        }
241
                      else { keyValueComment.add(commentCard); }
242
                      
243
                    } else {keyValueComment.add("");}
244
                    
245
                    cardList.add(keyValueComment);
246
                    
247
                  }
248
                  
249
                }
250
                
251
                headersList.add(cardList);
252
                
253
                headers.add(headerStr);
254
                
255
              }
256

    
257
    
258
              jSON.put("HEADERSGRID", headersList);
259
              System.out.println("Headers - Done !");
260
              
261
              
262
              System.out.println("Getting Wave...");
263
              int hhdduu = -1;
264
              for (int i=0;i<nberHDUs;i++){
265
                Header header = fits.getHDU(i).getHeader();
266
                String a = header.findKey("CDELT3");
267
                if(a != null){
268
                    System.out.println("CDELT3 find in "+i);
269
                    hhdduu = i;
270
                }else{
271
                    System.out.println("CDELT3 not found in "+i);
272
                }
273
              }
274
              
275
              // Wave from fits
276
              //if (fits.getHDU(hdu).getHeader().findKey("CDELT3")!=null) {
277
              if(hhdduu != -1){
278
                System.out.println("... from WCS");
279

    
280
                double naxis3 = fits.getHDU(hdu).getHeader().getDoubleValue("NAXIS3");
281
                double crval3 = fits.getHDU(hdu).getHeader().getDoubleValue("CRVAL3");
282
                double cdelt3 = fits.getHDU(hdu).getHeader().getDoubleValue("CDELT3");
283
                System.out.println(naxis3+" "+crval3+" "+cdelt3);
284
                float[] wave = new float[(int)naxis3];
285
                for (int i = 0 ; i < wave.length; i++) { wave[i] = (float) (crval3 + i * cdelt3); }
286
                jSON.put("WAVE", wave);
287
                
288
              }else {
289
                System.out.println("not from WCS");
290
                if (waveHDU!=-1) {
291
                  System.out.println("No wave data in the cube WCS - Using wave array from ImageIndex HDU table");
292
                  TableHDU cols = (TableHDU) fits.getHDU(waveHDU);
293
                  double[] waveD = (double[]) cols.getColumn(0);
294
                  float[] wave = new float[waveD.length];
295
                  for (int i = 0 ; i < waveD.length; i++) { wave[i] = (float) waveD[i]; }
296
                  jSON.put("WAVE", wave);
297
                } else {
298
                  System.out.println("No wave data in the cube WCS - Using wave array from specific HDU table - "+waveLocation);
299
                  if (waveLocation.matches("(\\d+)-(\\d+)")){
300
                    int waveTable = Integer.parseInt(waveLocation.split("-")[0]);
301
                    int waveColumn = Integer.parseInt(waveLocation.split("-")[1]);
302
                    TableHDU cols = (TableHDU) fits.getHDU(waveTable);
303
                    
304
                    double[] waveD = (double[]) cols.getColumn(waveColumn);
305
                    float[] wave = new float[waveD.length];
306
                    for (int i = 0 ; i < waveD.length; i++) { wave[i] = (float) waveD[i]; }
307
                    jSON.put("WAVE", wave);
308
                  } else {
309
                    System.out.println("No wave data in "+waveLocation);
310
                  }                  
311
                } 
312
              }
313
              System.out.println(" Wave - Done !");
314
              
315
              System.out.println("Getting Cube Data...");
316
              // Spectrum from fits
317
              for (int m=1; m<=2; m++) {
318
                jSON.put("NAXIS"+m, fits.getHDU(hdu).getHeader().getDoubleValue("NAXIS"+m));
319
                jSON.put("CRPIX"+m, fits.getHDU(hdu).getHeader().getDoubleValue("CRPIX"+m));
320
                jSON.put("CRVAL"+m, fits.getHDU(hdu).getHeader().getDoubleValue("CRVAL"+m));
321
                jSON.put("CDELT"+m, fits.getHDU(hdu).getHeader().getDoubleValue("CDELT"+m));
322
                jSON.put("CTYPE"+m, fits.getHDU(hdu).getHeader().getStringValue("CTYPE"+m));
323
              }
324
              
325
              jSON.put("NAXIS3", fits.getHDU(hdu).getHeader().getDoubleValue("NAXIS3"));
326
              
327
              jSON.put("INFO_QTTY", fits.getHDU(hdu).getHeader().getStringValue("INFO____") );
328
              jSON.put("UNIT_QTTY", fits.getHDU(hdu).getHeader().getStringValue("QTTY____") );
329
              jSON.put("INFO_WAVE", fits.getHDU(hdu).getHeader().getStringValue("CTYPE3") );
330
              jSON.put("UNIT_WAVE", fits.getHDU(hdu).getHeader().getStringValue("CUNIT3") );
331
              
332
              double[][][] cubeFits = ((double[][][]) fits.getHDU(hdu).getData().getData());
333
              int naxis1 = cubeFits[0][0].length;
334
              int naxis2 = cubeFits[0].length;
335
              int naxis3 = cubeFits.length;
336
              
337
              //Reorganizing cube
338
              List<List<List<Float>>> cube3DL = new ArrayList<List<List<Float>>>(naxis1);  
339
              //float cube[][][] = new float[naxis1][naxis2][naxis3];
340
              for (int x=0;x<naxis1;x++){
341
                List<List<Float>> list2 = new ArrayList<List<Float>>(naxis2);  
342
                for (int y=0;y<naxis2;y++){
343
                  List<Float> list3 = new ArrayList<Float>(naxis3);  
344
                  for (int z=0;z<naxis3;z++){
345
                    //cube[x][y][z] = (float) cubeFits[z][y][x];
346
                    if (cubeFits[z][y][x] != Double.NaN ) { list3.add( (float) cubeFits[z][y][x] ); }
347
                    else { list3.add(null); }
348
                  }
349
                  list2.add(list3);    
350
                }
351
                cube3DL.add(list2);  
352
              }
353
              
354
              /*
355
              List<List<List<Float>>> cube3DL = new ArrayList<List<List<Float>>>(naxis1);  
356
              for (int i = 0; i < naxis1; i++) {  
357
                  List<List<Float>> list2 = new ArrayList<List<Float>>(naxis2);  
358
                  for (int j = 0; j < naxis2; j++) {  
359
                      List<Float> list3 = new ArrayList<Float>(naxis3);  
360
                      for (int k = 0; k < naxis3; k++) { 
361
                        list3.add(cube[i][j][k]);
362
                        }  
363
                      list2.add(list3);  
364
                      }  
365
                  cube3DL.add(list2);  
366
                  } 
367
              */
368
              
369
              JSONArray cubeJL= new JSONArray(cube3DL.toString());
370
              jSON.put("SPECTRUM", cubeJL);
371
              
372
              System.out.println(" Cube - Done !");
373
             
374
              repr = new JsonRepresentation(jSON);
375
              
376
              
377
            }
378
            catch (FitsException e) {
379
              // TODO Auto-generated catch block
380
              e.printStackTrace();
381
            }
382
            catch (IOException e) {
383
              // TODO Auto-generated catch block
384
              e.printStackTrace();
385
            }
386
            
387
            catch (JSONException e) {
388
              // TODO Auto-generated catch block
389
              e.printStackTrace();
390
            }
391
            
392
        }
393
        databaseRequest.close();
394
        
395
      }
396
      catch (SitoolsException e) {
397
        // TODO Auto-generated catch block
398
        e.printStackTrace();
399
      }
400
      
401
    }
402

    
403
    return repr;
404
    
405
  }
406

    
407
}