View Javadoc

1   /*$Id: SiapImageFetchApplication.java,v 1.2 2004/11/29 20:00:56 clq2 Exp $
2    * Created on 23-Nov-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.apps;
12  
13  import org.astrogrid.applications.AbstractApplication;
14  import org.astrogrid.applications.CeaException;
15  import org.astrogrid.applications.Status;
16  import org.astrogrid.applications.AbstractApplication.IDs;
17  import org.astrogrid.applications.apps.CatApplicationDescription.StreamParameterAdapter;
18  import org.astrogrid.applications.apps.SiapImageFetchDescription.ParameterAdapterDataSource;
19  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
20  import org.astrogrid.applications.description.ApplicationInterface;
21  import org.astrogrid.applications.description.ParameterDescription;
22  import org.astrogrid.applications.parameter.ParameterAdapter;
23  import org.astrogrid.applications.parameter.protocol.ExternalValue;
24  import org.astrogrid.applications.parameter.protocol.ProtocolLibrary;
25  import org.astrogrid.community.User;
26  import org.astrogrid.io.Piper;
27  import org.astrogrid.store.Ivorn;
28  import org.astrogrid.store.VoSpaceClient;
29  import org.astrogrid.workflow.beans.v1.Tool;
30  
31  import uk.ac.starlink.table.ColumnInfo;
32  import uk.ac.starlink.table.RowSequence;
33  import uk.ac.starlink.table.StarTable;
34  import uk.ac.starlink.table.StarTableFactory;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  import java.net.URISyntaxException;
40  import java.net.URL;
41  import java.text.SimpleDateFormat;
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  
48  /***
49   * @author Noel Winstanley nw@jb.man.ac.uk 19-Nov-2004
50   *
51   */
52  public class SiapImageFetchApplication extends AbstractApplication {
53  
54      /*** Construct a new SiapImageFetchApplication
55       * @param ids
56       * @param tool
57       * @param applicationInterface
58       * @param lib
59       */
60      public SiapImageFetchApplication(IDs ids, Tool tool, ApplicationInterface applicationInterface, ProtocolLibrary lib) {
61          super(ids, tool, applicationInterface, lib);
62      }
63      /*** create streaming parameter adapter for table (as may be large). 
64       * standard strings for the other ones.
65       * @see org.astrogrid.applications.AbstractApplication#instantiateAdapter(org.astrogrid.applications.beans.v1.parameters.ParameterValue, org.astrogrid.applications.description.ParameterDescription, org.astrogrid.applications.parameter.protocol.ExternalValue)
66       */
67      protected ParameterAdapter instantiateAdapter(ParameterValue pval,
68              ParameterDescription descr, ExternalValue indirectVal) {
69          if (descr.getName().equals(SiapImageFetchDescription.TABLE)) {
70              return new CatApplicationDescription.StreamParameterAdapter(pval,descr,indirectVal);
71          } else {
72              return super.instantiateAdapter(pval, descr, indirectVal);
73          }
74      }
75      public Runnable createExecutionTask() throws CeaException {
76          createAdapters();
77          setStatus(Status.INITIALIZED);
78          Runnable r = new ImageFetchRunnable();
79          return r;
80      }
81      
82  /*** runnable object that actually does the work */
83      final class ImageFetchRunnable implements Runnable {
84          private static final String REFERENCE_UCD = "VOX:Image_AccessReference";
85          protected final StarTableFactory factory = new StarTableFactory();
86          protected final VoSpaceClient vospace = new VoSpaceClient(new User()); // default user - should be sufficient for now.
87  
88  
89  
90          public void run() {
91              try {
92                  setStatus(Status.RUNNING);
93                  StreamParameterAdapter tableStream = null;
94                  String baseDir = null;
95                  // extract and process parameters
96                  for (Iterator i = inputParameterAdapters(); i.hasNext(); ) {
97                      ParameterAdapter input = (ParameterAdapter)i.next();
98                      String name = input.getWrappedParameter().getName();
99                      if (SiapImageFetchDescription.TABLE.equalsIgnoreCase(name)) {
100                         tableStream = (CatApplicationDescription.StreamParameterAdapter) input;
101                     } else if (SiapImageFetchDescription.BASEIVORN.equalsIgnoreCase(name)) {
102                         baseDir = (String)input.process();
103                         if (baseDir == null || baseDir.trim().length() ==0) {
104                             reportError("Empty BASEDIR parameter");
105                         }
106                     } else {
107                         reportError("Unknown input parameter " + name);
108                     }
109                 }
110                 // set up output folder.
111                 Date now = new Date();
112                 SimpleDateFormat df = new SimpleDateFormat("dd-MM-yy--HHmm");
113                 String unq = df.format(now);
114                                         
115                 Ivorn dirIvorn = new Ivorn(baseDir + (baseDir.endsWith("/") ? "" : "/") + unq);
116                 reportMessage("Images will be saved in folder " + dirIvorn);
117                 vospace.newFolder(dirIvorn);
118                 
119                 // save the votable itself.
120                 Ivorn votable = new Ivorn(dirIvorn.toString() + "/" + "votable.vot");
121                 InputStream vin = (InputStream)tableStream.process();                        
122                 OutputStream vout = vospace.putStream(votable);
123                 Piper.pipe(vin,vout);
124                 vin.close();
125                 vout.close();
126                 reportMessage("Saved source votable as " + votable);
127                 
128                 //now do the work on the table..
129                 StarTable table = factory.makeStarTable(
130                         new ParameterAdapterDataSource(tableStream),"votable");
131                 // first find correct metadata column
132                 int referenceCol = -1;
133                 for (int col = 0; col < table.getColumnCount(); col++) {
134                     ColumnInfo colInfo = table.getColumnInfo(col);
135                     if (REFERENCE_UCD.equals(colInfo.getUCD())){
136                         referenceCol = col;
137                     }
138                 }
139                 if (referenceCol == -1) {
140                     reportError("Couldn't find column with UCD :" + REFERENCE_UCD);
141                 }
142                 
143                 setStatus(Status.WRITINGBACK);
144 
145                 // iterate round, fetching and saving files.                        
146                 // process each row in turn.
147                 List urls = new ArrayList();
148                 List ivorns = new ArrayList();                        
149                 int count = 0;
150                 for(RowSequence rs = table.getRowSequence(); rs.hasNext(); ) {
151                     rs.next();
152                     URL url = new URL(rs.getCell(referenceCol).toString());
153                     Ivorn target = new Ivorn(dirIvorn.toString() + "/" + (count++) + ".fits");                            
154                     reportMessage("Saving " + url  + " to " + target);
155                     vospace.putUrl(url,target,false);
156                     urls.add(url);
157                     ivorns.add(target);
158                 }                        
159                 
160                 //return output parameters.
161                 for (Iterator i = outputParameterAdapters(); i.hasNext(); ) {
162                     ParameterAdapter p = (ParameterAdapter)i.next();
163                     String outputName = p.getWrappedParameter().getName();
164                     if (SiapImageFetchDescription.URLS.equalsIgnoreCase(outputName)) {
165                         p.writeBack(listToString(urls));
166                     } else if (SiapImageFetchDescription.IVORNS.equalsIgnoreCase(outputName)) {
167                         p.writeBack(listToString(ivorns));
168                     } else {
169                         reportError("Unknown output parameter " + outputName);
170                     }                            
171                 }
172                 
173                 setStatus(Status.COMPLETED);
174             } catch (URISyntaxException e) {
175                 reportError("Storage Ivorn not in correct format",e);
176             } catch (CeaException e) {
177                 reportError("something failed",e);
178             } catch (IOException e) {
179                 reportError("something failed",e);
180             }
181         }
182 
183         private Object listToString(List items) {
184               StringBuffer buff = new StringBuffer();
185               buff.append("[");
186               boolean isFirst = true;
187               for (Iterator i = items.iterator(); i.hasNext(); ) {
188                   String value = i.next().toString();
189                   if (!isFirst) {
190                       buff.append(",");
191                   }
192                   buff.append("'");
193                   buff.append(value);
194                   buff.append("'");
195                   isFirst = false;
196               }
197               buff.append("]");
198               return buff.toString();
199         }
200     }    
201 }
202 
203 /* 
204 $Log: SiapImageFetchApplication.java,v $
205 Revision 1.2  2004/11/29 20:00:56  clq2
206 nww-itn07-684
207 
208 Revision 1.1.2.2  2004/11/26 15:17:28  nw
209 some extra error-checking logic.
210 
211 Revision 1.1.2.1  2004/11/24 00:14:58  nw
212 factored the application class out of the descripiton - makes for
213 more manageable code.
214  
215 */