Project

General

Profile

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

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

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6

    
7
package fr.ias.sitools.resources.fits;
8

    
9
import fr.cnes.sitools.common.exception.SitoolsException;
10
import fr.cnes.sitools.common.resource.SitoolsParameterizedResource;
11
import fr.cnes.sitools.dataset.DataSetApplication;
12
import fr.cnes.sitools.dataset.database.DatabaseRequest;
13
import fr.cnes.sitools.dataset.database.DatabaseRequestFactory;
14
import fr.cnes.sitools.dataset.database.DatabaseRequestParameters;
15
import fr.cnes.sitools.dataset.database.common.DataSetExplorerUtil;
16
import fr.cnes.sitools.datasource.jdbc.model.AttributeValue;
17
import fr.cnes.sitools.datasource.jdbc.model.Record;
18
import fr.cnes.sitools.plugins.resources.model.ResourceParameter;
19
import fr.cnes.sitools.resources.order.utils.OrderResourceUtils;
20
import java.io.IOException;
21
import java.util.ArrayList;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.logging.Level;
25
import java.util.logging.Logger;
26
import java.util.regex.Matcher;
27
import java.util.regex.Pattern;
28
import nom.tam.fits.Fits;
29
import nom.tam.fits.FitsException;
30
import nom.tam.fits.FitsFactory;
31
import nom.tam.fits.Header;
32
import nom.tam.fits.HeaderCard;
33
import org.json.JSONException;
34
import org.json.JSONObject;
35
import org.restlet.data.MediaType;
36
import org.restlet.ext.json.JsonRepresentation;
37
import org.restlet.ext.wadl.MethodInfo;
38
import org.restlet.representation.Representation;
39
import org.restlet.representation.Variant;
40
import org.restlet.resource.Get;
41

    
42
/**
43
 *
44
 * @author mnicolas
45
 */
46
public class ShowHeader extends SitoolsParameterizedResource {
47
    
48
    @Override
49
    public void sitoolsDescribe() {
50
        setName("GetFitsDataSpectro");
51
        setDescription("Get cube fits file data");
52
    }
53

    
54
    @Override
55
    public void doInit() {
56
        super.doInit();
57
    }
58

    
59
    /**
60
    * Get HTML
61
    * 
62
    * @return Representation the HTML result
63
    */
64
    @Get
65
    public Representation get() {
66
        return execute();
67
    }
68

    
69
    @Override
70
    protected void describeGet(MethodInfo info) {
71
        this.addInfo(info);
72
        info.setIdentifier("retrieve records and extract data and metadata from fits file");
73
        info.setDocumentation("Method to get the data and metadata from a fits file");
74
        addStandardGetRequestInfo(info);
75
        DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info);
76
        DataSetApplication application = (DataSetApplication) getApplication();
77
        DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained());
78
        addStandardResponseInfo(info);
79
        addStandardInternalServerErrorInfo(info);
80
    }
81

    
82
    @Override
83
    protected Representation head(Variant variant) {
84
        Representation repr = super.head();
85
        repr.setMediaType(MediaType.APPLICATION_JSON);
86
        return repr;
87
    }
88

    
89
    private Representation execute() {
90
        Representation repr = null;
91
    
92
        // generate the DatabaseRequest
93
        DataSetApplication datasetApp = (DataSetApplication) getApplication();
94
        DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext());
95

    
96
        // Get request parameters
97
        if (datasetApp.getConverterChained() != null) {
98
        datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest());
99
        }
100
        // Get DatabaseRequestParameters
101
        DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams();
102

    
103
        DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params);
104
    
105
        if (params.getDistinct()) {
106
            try {
107
                databaseRequest.createDistinctRequest();
108
            }catch (SitoolsException e) {
109
                e.printStackTrace();
110
            }
111
        } else {
112
            try {
113
                databaseRequest.createRequest();
114
            }catch (SitoolsException e) {
115
                e.printStackTrace();
116
            }
117
        }
118
    
119
        ResourceParameter urlName = this.getModel().getParameterByName("colUrl");
120
    
121
        if (databaseRequest != null) {
122

    
123
        // Next for reading first record
124
        try {
125
            databaseRequest.nextResult();
126
            Record rec = databaseRequest.getRecord();
127
        
128
            AttributeValue attributeValueURL = OrderResourceUtils.getInParam(urlName, rec);
129
        
130
            if (attributeValueURL.getValue() != null){
131
                // get the file path
132
                String filePath = (String) attributeValueURL.getValue();
133
                Fits fits = new Fits(filePath);
134
                FitsFactory.setUseHierarch(true);
135
                Header.setLongStringsEnabled(true);
136
              
137
                JSONObject jSON = new JSONObject();
138
              
139
                int waveHDU = -1;
140
              
141
                int nberHDUs = fits.size();
142
                System.out.println("Number of HDUs: "+nberHDUs);
143
                jSON.put("HDUs", nberHDUs);
144
              
145
                // Headers from fits
146
                System.out.println("Getting Headers...");
147
                List<String> headers = new ArrayList<String>(nberHDUs);
148
                List<List<List<String>>> headersList = new ArrayList<List<List<String>>>(nberHDUs);
149
              
150
                for (int i=0;i<nberHDUs;i++){
151
                
152
                    Header header = fits.getHDU(i).getHeader();
153

    
154
                    int nbreCards = header.getNumberOfCards();
155
                    String headerStr = "";
156
                
157
                    //making HIERARCH mapping
158
                    HashMap<String,String> mapHierarch = new HashMap<String,String>();                
159
                    for (int j=0; j<nbreCards; j++){
160
                        String cardStr = header.getCard(j);
161
                        if (cardStr.matches("HIERARCH\\s.*\\s(META.*)\\s*=\\s*(.*)")) {
162
                            //System.out.println(cardStr);
163
                            Pattern p = Pattern.compile("HIERARCH\\s.*\\s(META.*)\\s*=\\s*(.*)");
164
                            Matcher m = p.matcher(cardStr);
165
                            if (m.find()) {
166
                                //System.out.println(m.group(1)+" "+m.group(2));
167
                                mapHierarch.put(m.group(1), m.group(2));
168
                            }
169
                        }
170
                        headerStr = headerStr+cardStr+"<br>";
171
                    }
172
                
173
                    //List of extensions -- List of List of Cards
174
                    List<List<String>> cardList = new ArrayList<List<String>>(nbreCards);
175
                    for (int j=0; j<nbreCards; j++){
176
                  
177
                        List<String> keyValueComment = new ArrayList<String>(nbreCards); 
178
                        HeaderCard card = header.findCard(header.getKey(j));
179
                  
180
                        if ( card!=null ) {
181
                            //key
182
                            if (mapHierarch.containsKey(card.getKey())) { 
183
                                keyValueComment.add( "HIERARCH "+mapHierarch.get(card.getKey()) );
184
                            }else {
185
                                keyValueComment.add(card.getKey());
186
                            }
187
                    
188
                            //value
189
                            keyValueComment.add(card.getValue());
190
                    
191
                            if ( card.getKey().contains("EXTNAME") && card.getValue().contains("ImageIndex") ) { 
192
                                System.out.println("ImageIndex in HDU "+i);
193
                                waveHDU=i;
194
                            }
195
                    
196
                            //comment
197
                            String commentCard = card.getComment();
198
                            if (commentCard!=null) {
199

    
200
                                if (commentCard.contains("&")) {
201
                                    //System.out.println(j+" "+commentCard);
202
                                    commentCard = commentCard.substring(0, commentCard.length()-1);
203
                                    int jTmp = j;
204
                                    while(header.getCard(jTmp+1).startsWith("COMMENT")){
205
                                        String commentCardNext = header.getCard(jTmp+1);
206
                                        if (commentCardNext.contains("&")) { commentCardNext = commentCardNext.substring(0, commentCardNext.length()-1); }
207
                                        commentCard += commentCardNext.split("COMMENT ")[1];
208
                                        jTmp++;
209
                                    } 
210
                                    keyValueComment.add(commentCard);
211
                                }else {
212
                                    keyValueComment.add(commentCard); 
213
                                }
214
                            } else {
215
                                keyValueComment.add("");
216
                            }
217
                            cardList.add(keyValueComment);
218
                        } 
219
                    }
220
                    headersList.add(cardList);
221
                    headers.add(headerStr);
222
                }
223
                jSON.put("HEADERSGRID", headersList);
224
                getContext().getLogger().log(Level.INFO,"In Show Header Resources : Headers - Done !");
225
                repr = new JsonRepresentation(jSON);
226
            }
227
        } catch (FitsException ex) {
228
            Logger.getLogger(ShowHeader.class.getName()).log(Level.SEVERE, null, ex);
229
        } catch (JSONException ex) {
230
            Logger.getLogger(ShowHeader.class.getName()).log(Level.SEVERE, null, ex);
231
        }catch (SitoolsException ex) {
232
            Logger.getLogger(ShowHeader.class.getName()).log(Level.SEVERE, null, ex);
233
        } catch (IOException ex) {
234
            Logger.getLogger(ShowHeader.class.getName()).log(Level.SEVERE, null, ex);
235
        }
236
    }
237
    return repr;
238
    }
239
    
240
}