void SPECTRA::run( Session* session, const std::string& argument ){

  /* The argument should consist of 2 comma separated values:
     1) resolution
     2) tile number
     3) pixel index in x direction
     4) pixel index in y direction
  */

  if( session->loglevel >= 3 ) (*session->logfile) << "SPECTRA handler reached" << endl;

  int resolution, tile, x, y;


  // Time this command
  if( session->loglevel >= 2 ) command_timer.start();


  // Parse the argument list
  string arg = argument;
  int delimitter = arg.find( "," );
  resolution = atoi( arg.substr(0,delimitter).c_str() );

  arg = arg.substr( delimitter + 1, arg.length() );
  delimitter = arg.find( "," );
  tile = atoi( arg.substr(0,delimitter).c_str() );

  arg = arg.substr( delimitter + 1, arg.length() );
  delimitter = arg.find( "," );
  x = atoi( arg.substr(0,delimitter).c_str() );

  arg = arg.substr( delimitter + 1, arg.length() );
  delimitter = arg.find( "," );
  y = atoi( arg.substr(0,arg.length()).c_str() );

  if( session->loglevel >= 5 ){ 
    (*session->logfile) << "SPECTRA :: resolution: " << resolution
			<< ", tile: " << tile
			<< ", x: " << x
			<< ", y: " << y << endl;
  }

  // Make sure our x,y coordinates are within the tile dimensions
  if( x < 0 || x >= (int)(*session->image)->getTileWidth() ||
      y < 0 || y >= (int)(*session->image)->getTileHeight() ){
    throw invalid_argument( "SPECTRA :: Error: x,y coordinates outside of tile boundaries" );
  }
  

  TileManager tilemanager( session->tileCache, *session->image, session->watermark, session->jpeg, session->logfile, session->loglevel );

  // Use our horizontal views function to get a list of available spectral images
  list <int> views = (*session->image)->getHorizontalViewsList();
  list <int> :: const_iterator i;

  // Our list of spectral reflectance values for the requested point
  list <float> spectrum;


#ifndef DEBUG
  // Output our HTTP header
  stringstream header;
  header << session->response->createHTTPHeader( "xml", (*session->image)->getTimestamp() );
  session->out->putStr( (const char*) header.str().c_str(), header.tellp() );
  session->out->flush();
#endif

  session->out->putS( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
  session->out->putS( "<spectra>\n" );
  session->out->flush();

  for( i = views.begin(); i != views.end(); i++ ){

    int n = *i;

    RawTile rawtile = tilemanager.getTile( resolution, tile, n, session->view->yangle, session->view->getLayers(), UNCOMPRESSED );

    // Make sure our x,y coordinates are within the tile dimensions
    if( x >= (int)rawtile.width || y >= (int)rawtile.height ){
      if( session->loglevel >= 1 ){
	(*session->logfile) << "SPECTRA :: Error: x,y coordinates outside of tile boundaries" << endl;
      }
      break;
    }


    unsigned int tw = (*session->image)->getTileWidth();
    unsigned int index = y*tw + x;

    void *ptr;
    float reflectance = 0.0;

    if( session->loglevel >= 5 ) (*session->logfile) << "SPECTRA :: " << rawtile.bpc << " bits per channel data" << endl;

    // Handle depending on bit depth
    if( rawtile.bpc == 8 ){
      ptr = (unsigned char*) (rawtile.data);
      reflectance = static_cast<float>((float)((unsigned char*)ptr)[index]) / 255.0;
    }
    else if( rawtile.bpc == 16 ){
      ptr = (unsigned short*) (rawtile.data);
      reflectance = static_cast<float>((float)((unsigned short*)ptr)[index]) / 65535.0;
    }
    else if( rawtile.bpc == 32 ){
      if( rawtile.sampleType == FIXEDPOINT ) {
        ptr = (unsigned int*) rawtile.data;
        reflectance = static_cast<float>((float)((unsigned int*)ptr)[index]);
      }
      else {
        ptr = (float*) rawtile.data;
        reflectance = static_cast<float>((float)((float*)ptr)[index]);
      }
    }

    spectrum.push_front( reflectance );

    string metadata = (*session->image)->getMetadata( "subject" );

    char tmp[1024];
    snprintf( tmp, 1024, "\t<point>\n\t\t<wavelength>%d</wavelength>\n\t\t<reflectance>%f</reflectance>\n\t</point>\n", n, reflectance );
    session->out->putS( tmp );
    session->out->flush();

    if( session->loglevel >= 3 ) (*session->logfile) << "SPECTRA :: Band: " << n << ", reflectance: " << reflectance << endl;
  }


  session->out->putS( "</spectra>" );

  if( session->out->flush() == -1 ) {
    if( session->loglevel >= 1 ){
      *(session->logfile) << "SPECTRA :: Error flushing XML" << endl;
    }
  }


  // Inform our response object that we have sent something to the client
  session->response->setImageSent();

  // Total SPECTRA response time
  if( session->loglevel >= 2 ){
    *(session->logfile) << "SPECTRA :: Total command time " << command_timer.getTime() << " microseconds" << endl;
  }

}