View Javadoc

1   /*
2    * $Id: AVODemoRunner.java,v 1.5 2004/09/29 19:47:26 pah Exp $
3    * 
4    * Created on 23-Jan-2004 by Paul Harrison (pah@jb.man.ac.uk)
5    *
6    * Copyright 2004 AstroGrid. All rights reserved.
7    *
8    * This software is published under the terms of the AstroGrid 
9    * Software License version 1.2, a copy of which has been included 
10   * with this distribution in the LICENSE.txt file.  
11   *
12   */
13  
14  package org.astrogrid.applications.avodemo;
15  
16  import java.io.StringWriter;
17  
18  import javax.mail.MessagingException;
19  import javax.mail.internet.AddressException;
20  import javax.mail.internet.InternetAddress;
21  
22  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
23  import org.astrogrid.community.beans.v1.Account;
24  import org.astrogrid.community.beans.v1.Credentials;
25  import org.astrogrid.community.beans.v1.Group;
26  import org.astrogrid.jes.types.v1.cea.axis.JobIdentifierType;
27  import org.astrogrid.store.Ivorn;
28  import org.astrogrid.store.VoSpaceClient;
29  import org.astrogrid.store.delegate.StoreFile;
30  import org.astrogrid.workflow.beans.v1.Input;
31  import org.astrogrid.workflow.beans.v1.Output;
32  import org.astrogrid.workflow.beans.v1.Sequence;
33  import org.astrogrid.workflow.beans.v1.Step;
34  import org.astrogrid.workflow.beans.v1.Tool;
35  import org.astrogrid.workflow.beans.v1.Workflow;
36  
37  /***
38   * @author Paul Harrison (pah@jb.man.ac.uk)
39   * @version $Name:  $
40   * @since iteration4.1
41   * @bugzilla 
42   */
43  public class AVODemoRunner implements Runnable {
44  
45  private JobIdentifierType jobstepid = new JobIdentifierType();
46     org.astrogrid.community.User fulluser =
47        new org.astrogrid.community.User(AVODemoConstants.ACCOUNT, AVODemoConstants.GROUP, AVODemoConstants.TOKEN);
48     private final String MYSPACEBASECONT = "ivo://"+AVODemoConstants.MYSPACE +"#" + fulluser.getUserId() + "/";
49     private String toAddress = "pah@jb.man.ac.uk";
50     private final String fromAddress = "pah@jb.man.ac.uk";
51     private Credentials credentials = new Credentials();
52     // TODO need to fill these in with something...
53  
54     private InternetAddress ccdests[];
55  
56     private String myspaceBaseRef;
57     private String sector = "sect23";
58     private String hemi = "s";
59     private String myspacebase;
60     private String username;
61     static private org.apache.commons.logging.Log logger =
62        org.apache.commons.logging.LogFactory.getLog(AVODemoRunner.class);
63  private String workflowIvorn;
64     
65     /***
66      * 
67      */
68     public AVODemoRunner() {
69  
70        myspacebase = MYSPACEBASECONT;
71        try {
72           ccdests = new InternetAddress[] { new InternetAddress("pah@jb.man.ac.uk")};
73        }
74        catch (AddressException e) {
75           // TODO Auto-generated catch block
76           e.printStackTrace();
77        }
78  
79     }
80     public static void main(String[] args) {
81  
82        AVODemoRunner demo = new AVODemoRunner();
83        demo.runit();
84  
85     }
86  
87     public void runit() {
88  
89        Thread runthread = new Thread(this);
90        runthread.start();
91  
92     }
93  
94     public void createWorkflow() throws Exception {
95        Group group = null;
96        VoSpaceClient myspacedel = new VoSpaceClient(fulluser);
97        StringBuffer msbr = new StringBuffer("ivo://");
98        msbr.append(AVODemoConstants.MYSPACE);
99        msbr.append("#");
100       msbr.append(username);
101       msbr.append("/");
102       myspacebase = msbr.toString(); // take the base from here...
103       msbr.append(sector);
104       msbr.append(hemi);
105       myspaceBaseRef = msbr.toString();
106       try {
107          myspacedel.newFolder(new Ivorn(myspaceBaseRef));
108       }
109       catch (Exception e2) {
110          // TODO Auto-generated catch block
111          e2.printStackTrace();
112       }
113       msbr.append("/");
114       myspaceBaseRef = msbr.toString();
115 
116       Workflow workflow = new Workflow();
117       workflow.setName("AVODEMO-" + sector + hemi);
118       workflow.setDescription(
119          "Workflow involving multi-band sextractor run followed by a data federation and hyperz run");
120          Credentials cred = new Credentials();
121          Account acc = new Account();
122          acc.setName(username); //FIXME this is a quick hack to get the username into the right place.
123          acc.setCommunity(fulluser.getCommunity());
124          
125         group = new Group();
126         group.setName("any");
127         group.setCommunity(AVODemoConstants.COMMUNITY);
128         cred.setAccount(acc);
129         cred.setGroup(group);
130         cred.setSecurityToken("notoken");
131         workflow.setCredentials(cred);
132        
133       Sequence sequence = new Sequence();
134       workflow.setSequence(sequence);
135       Step step = new Step();
136       populateSExtractorStep(step, "b");
137       sequence.addActivity(step);
138 
139       step = new Step();
140       populateSExtractorStep(step, "v");
141       sequence.addActivity(step);
142 
143       step = new Step();
144       populateSExtractorStep(step, "i");
145       sequence.addActivity(step);
146 
147       step = new Step();
148       populateSExtractorStep(step, "z");
149       sequence.addActivity(step);
150 
151       step = new Step();
152       populateDftStep(step);
153       sequence.addActivity(step);
154 
155       step = new Step();
156       populateHyperZStep(step);
157       sequence.addActivity(step);
158       
159      
160 
161       StringWriter outstr = new StringWriter();
162 
163       workflow.marshal(outstr);
164       byte[] bytes = outstr.toString().getBytes();
165       workflowIvorn = myspacebase + "workflow/" + workflow.getName();
166       myspacedel.putBytes(bytes, 0, bytes.length, new Ivorn(workflowIvorn), false);
167  
168    }
169    private void runworkflow() throws Exception {
170       VoSpaceClient myspacedel = new VoSpaceClient(fulluser);
171       myspaceBaseRef = myspacebase + sector + hemi;
172       myspacedel.newFolder(new Ivorn(myspaceBaseRef));
173 
174       String exid;
175       exid = runSExtractor("b");
176       waitForCompletion(exid);
177       exid = runSExtractor("v");
178       waitForCompletion(exid);
179       exid = runSExtractor("i");
180       waitForCompletion(exid);
181       exid = runSExtractor("z");
182       waitForCompletion(exid);
183       exid = runDft();
184       waitForCompletion(exid);
185       exid = runHyperZ();
186       waitForCompletion(exid);
187       String urlout = null;
188  
189           StoreFile fout = myspacedel.getFile(new Ivorn(
190             myspaceBaseRef + "hyperzout"));
191             
192       mailresult(fout.toString());
193    }
194 
195    private String runHyperZ() {
196        throw new UnsupportedOperationException("direct running not supported anymore");
197    }
198 
199    private void populateHyperZStep(Step step) {
200       Input input = new Input();
201       Output output = new Output();
202       String applicationid = AVODemoConstants.HYPERZ;
203       Tool tool = new Tool();
204       tool.setName(applicationid);
205       tool.setInput(input);
206       tool.setOutput(output);
207       tool.setInterface("simple");
208       ParameterValue param = new ParameterValue();
209       input.addParameter(param);
210       param.setName("config_file");
211 
212       param.setValue("file:///home/applications/demo/hyperz/zphot.param");
213       param.setIndirect(true);
214 
215       param = new ParameterValue();
216       input.addParameter(param);
217       param.setName("input_catalog");
218 
219       param.setValue(myspaceBaseRef + "merged");
220       param.setIndirect(true);
221 
222       param = new ParameterValue();
223       output.addParameter(param);
224       param.setName("output_catalog");
225 
226       param.setValue(myspaceBaseRef + "hyperzout");
227       param.setIndirect(true);
228       step.setTool(tool);
229       step.setName("hyperz");
230 
231    }
232 
233    private String runDft()  {
234        throw new UnsupportedOperationException("direct running not supported anymore");
235   }
236 
237    private void populateDftStep(Step step) {
238       Output output = new Output();
239       Input input = new Input();
240       String applicationid = AVODemoConstants.DFT;
241 
242       Tool tool = new Tool();
243       tool.setName(applicationid);
244       tool.setInput(input);
245       tool.setOutput(output);
246       tool.setInterface("simple");
247       ParameterValue param = new ParameterValue();
248       input.addParameter(param);
249       param.setName("targets");
250 
251       param.setValue(myspaceBaseRef + "sexout_z");
252       param.setIndirect(true);
253 
254       param = new ParameterValue();
255       param.setName("matches");
256       input.addParameter(param);
257       param.setValue(myspaceBaseRef + "sexout_b");
258       param.setIndirect(true);
259 
260       param = new ParameterValue();
261       input.addParameter(param);
262       param.setName("matches");
263 
264       param.setIndirect(true);
265       param.setValue(myspaceBaseRef + "sexout_v");
266 
267       param = new ParameterValue();
268       input.addParameter(param);
269       param.setName("matches");
270 
271       param.setIndirect(true);
272       param.setValue(myspaceBaseRef + "sexout_i");
273 
274       param = new ParameterValue();
275       param.setName("merged_output");
276       output.addParameter(param);
277       param.setIndirect(true);
278       param.setValue(myspaceBaseRef + "merged");
279       step.setTool(tool);
280       step.setName("dft");
281    }
282 
283    private String runSExtractor(String band)  {
284        throw new UnsupportedOperationException("direct running not supported anymore");
285 
286    }
287 
288    private void populateSExtractorStep(Step step, String band) {
289       Output output = new Output();
290       Input input = new Input();
291       String applicationid = AVODemoConstants.SEXTRACTOR;
292       Tool tool = new Tool();
293       tool.setName(applicationid);
294       tool.setInput(input);
295       tool.setOutput(output);
296       tool.setInterface("Simple");
297       ParameterValue param = new ParameterValue();
298       input.addParameter(param);
299       param.setName("DetectionImage");
300 
301       param.setIndirect(true);
302       param.setValue(
303          "file:///home/applications/data/GOODS/h_"
304             + hemi
305             + "z_"
306             + sector
307             + "_v1.0_drz_img.fits");
308 
309       param = new ParameterValue();
310       input.addParameter(param);
311       param.setName("PhotoImage");
312 
313       param.setIndirect(true);
314       param.setValue(
315          "file:///home/applications/data/GOODS/h_"
316             + hemi
317             + band
318             + "_"
319             + sector
320             + "_v1.0_drz_img.fits");
321 
322       param = new ParameterValue();
323       input.addParameter(param);
324       param.setName("config_file");
325 
326       param.setIndirect(true);
327       param.setValue(
328          "file:///home/applications/demo/h_goods_" + hemi + band + "_r1.0z_phot_sex.txt");
329 
330       param = new ParameterValue();
331       input.addParameter(param);
332       param.setName("PARAMETERS_NAME");
333 
334       param.setIndirect(true);
335       param.setValue("file:///home/applications/demo/std.param");
336 
337       param = new ParameterValue();
338       output.addParameter(param);
339       param.setName("CATALOG_NAME");
340 
341       param.setIndirect(true);
342       param.setValue(myspaceBaseRef + "sexout_" + band);
343       
344       param = new ParameterValue();
345       input.addParameter(param);
346       param.setName("FILTER_NAME");
347       param.setValue("file:///home/applications/demo/h_goods_r1.0z_detect_conv.txt");
348       param.setIndirect(true);
349 
350 
351       step.setTool(tool);
352       step.setName("sextractor - " + band);
353 
354    }
355 
356    private void waitForCompletion(String executionId) {
357 
358        //not doing this any more
359 //      if (executionId != null) {
360 //         try {
361 //            MessageType runStatus = controller.queryExecutionStatus(executionId);
362 //            try {
363 //               while (runStatus.getPhase() != ExecutionPhase.COMPLETED) {
364 //                  Thread.sleep(20000);
365 //                  runStatus = controller.queryExecutionStatus(executionId);
366 //
367 //               }
368 //
369 //            }
370 //            catch (InterruptedException e) {
371 //               // TODO Auto-generated catch block
372 //               e.printStackTrace();
373 //            }
374 //         }
375 //         catch (CEADelegateException e) {
376 //            // TODO Auto-generated catch block
377 //            e.printStackTrace();
378 //         }
379 //      }
380 //      else {
381 //         logger.error("the executionid is null - application error");
382 //      }
383 
384    }
385    private void mailresult(String url) throws MessagingException {
386        //this is not really necessary any longer....
387 //      Session session = config.mailSessionInstance();
388 //      Message message = new MimeMessage(session);
389 //
390 //      InternetAddress dests[] = new InternetAddress[] { new InternetAddress(toAddress)};
391 //      message.setFrom(new InternetAddress(fromAddress));
392 //      message.setRecipients(Message.RecipientType.TO, dests);
393 //      message.addRecipients(Message.RecipientType.BCC, ccdests);
394 //      message.setSubject("AVO Demo Results for " + sector + " " + hemi);
395 //      if (url != null) {
396 //
397 //         message.setContent(url, "text/plain");
398 //      }
399 //      else {
400 //         message.setContent("problem running job", "text/plain");
401 //      }
402 //
403 //      Transport.send(message);
404    }
405 
406    /***
407     * @return
408     */
409    public String getHemi() {
410       return hemi;
411    }
412 
413    /***
414     * @return
415     */
416    public String getSector() {
417       return sector;
418    }
419 
420    /***
421     * @return
422     */
423    public String getToAddress() {
424       return toAddress;
425    }
426 
427    /***
428     * @param string
429     */
430    public void setHemi(String string) {
431       hemi = string;
432    }
433 
434    /***
435     * @param string
436     */
437    public void setSector(String string) {
438       sector = string;
439    }
440 
441    /***
442     * @param string
443     */
444    public void setToAddress(String string) {
445       toAddress = string;
446    }
447 
448    /* (non-Javadoc)
449     * @see java.lang.Runnable#run()
450     */
451    public void run() {
452       try {
453          runworkflow();
454       }
455       catch (Exception e) {
456          throw new RuntimeException("problem running the job", e);
457       }
458    }
459 
460    /***
461     * @return
462     */
463    public String getMyspacebase() {
464       return myspacebase;
465    }
466 
467    /***
468     * @param string
469     */
470    public void setMyspacebase(String string) {
471       myspacebase = string;
472    }
473 
474    /***
475     * @return
476     */
477    public String getUsername() {
478       return username;
479    }
480 
481    /***
482     * @param string
483     */
484    public void setUsername(String string) {
485       username = string;
486    }
487 
488 public String getWorkflowIvorn() {
489     return workflowIvorn;
490 }
491 }