bool NoiseAdjustGadget::saveNoiseCovariance()
  {
    char* buf = NULL;
    size_t len(0);
    
    //Do we have any noise?
    if (noise_covariance_matrixf_.get_number_of_elements() == 0) {
      return true;
    }

    //Scale the covariance matrix before saving
    hoNDArray< std::complex<float> > covf(noise_covariance_matrixf_);

    if (number_of_noise_samples_ > 1) {
      covf *= std::complex<float>(1.0/(float)(number_of_noise_samples_-1),0.0);
    }

    if ( !covf.serialize(buf, len) ) {
      GDEBUG("Noise covariance serialization failed ...\n");
      return false;
    }

    std::stringstream xml_ss;
    ISMRMRD::serialize(current_ismrmrd_header_, xml_ss);
    std::string xml_str = xml_ss.str();
    uint32_t xml_length = static_cast<uint32_t>(xml_str.size());

    std::ofstream outfile;
    std::string filename  = this->generateNoiseDependencyFilename(measurement_id_);
    outfile.open (filename.c_str(), std::ios::out|std::ios::binary);

    if (outfile.good())
      {
	GDEBUG("write out the noise dependency file : %s\n", filename.c_str());
	outfile.write( reinterpret_cast<char*>(&xml_length), 4);
	outfile.write( xml_str.c_str(), xml_length );
	outfile.write( reinterpret_cast<char*>(&noise_dwell_time_us_), sizeof(float));
	outfile.write( reinterpret_cast<char*>(&len), sizeof(size_t));
	outfile.write(buf, len);
	outfile.close();

	// set the permission for the noise file to be rewritable
#ifndef _WIN32
	int res = chmod(filename.c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
	if ( res != 0 ) {
	  GDEBUG("Changing noise prewhitener file permission failed ...\n");
	}
#endif // _WIN32
      } else {
      delete [] buf;
      GERROR_STREAM("Noise prewhitener file is not good for writing");
      return false;
    }

    delete [] buf;
    return true;
  }
gaussian_field::gaussian_field():GaussianProcess(2, "CovSum ( CovSEiso, CovNoise)")
{
    Eigen::VectorXd params(covf().get_param_dim());
    params << log(16), log(2), log(active_survey_param::gp_sigma);
    covf().set_loghyper(params);
}