Skip to content
seqIDataMgr.cpp 156 KiB
Newer Older
//    fits_write_key(fptr, TLOGICAL,"TTHISTORYENABLED", &intValue, "Number of images save", &FitStatus);
Fulvio Laudisio's avatar
Fulvio Laudisio committed
//    fits_write_key(fptr, TSTRING, "TTTIMEHISTORYFILE", (char *)"", "Time History Filename", &FitStatus);
//    fits_write_key(fptr, TINT,    "TTTIMEHISTLEN", &intValue, "Number of history lines if history is enabled", &FitStatus);
    floatValue = su::convertStringToType<float>(m_rtcKeywords["TTDMMAXPOWER"]);
    fits_write_key(fptr, TFLOAT,  "TTDMMAXPOWER", &floatValue, "Max Power allowed to DM", &FitStatus);
    fits_write_key(fptr, TSTRING, "TTBIASFILE", su::get_char_star(m_rtcKeywords["TTBIASFILE"]), "Bias File", &FitStatus);
    fits_write_key(fptr, TSTRING, "TTDMFLATFILE", su::get_char_star(m_rtcKeywords["TTDMFLATFILE"]), "Flat File", &FitStatus);
    if(m_pGetRtcStatus && !m_bFastSavingMode){m_rtcKeywords["TTSENSORTEMP"] = m_pGetRtcStatus("TTSENSORTEMP");}
    floatValue = su::convertStringToType<float>(m_rtcKeywords["TTSENSORTEMP"]);
    fits_write_key(fptr, TFLOAT,  "TTSENSORTEMP", &floatValue, "sensor temperature in degree C", &FitStatus);
    floatValue = su::convertStringToType<float>(m_rtcKeywords["TTCENTROIDGAINX"]);
    fits_write_key(fptr, TFLOAT,  "TTCENTROIDGAINX", &floatValue, "TipTilt gain on centroid X", &FitStatus);
    floatValue = su::convertStringToType<float>(m_rtcKeywords["TTCENTROIDGAINY"]);
    fits_write_key(fptr, TFLOAT,  "TTCENTROIDGAINY", &floatValue, "TipTilt gain on centroid Y", &FitStatus);
    floatValue = su::convertStringToType<float>(m_rtcKeywords["TTCENTROIDORIGX"]);
    fits_write_key(fptr, TFLOAT,  "TTCENTROIDORIGX", &floatValue, "X Origin position in [-1,1] coordinates", &FitStatus);
    floatValue = su::convertStringToType<float>(m_rtcKeywords["TTCENTROIDORIGY"]);
    fits_write_key(fptr, TFLOAT,  "TTCENTROIDORIGY", &floatValue, "Y Origin position in [-1,1] coordinates", &FitStatus);
    intValue = su::convertStringToType<int>(m_rtcKeywords["TTDMMODESNUM"]);
    fits_write_key(fptr, TINT,    "TTDMMODESNUM", &intValue, "number of modes in zonal influence matrix", &FitStatus);
    fits_write_key(fptr, TSTRING, "TTPIXELGAINFILE", su::get_char_star(m_rtcKeywords["TTPIXELGAINFILE"]), "Gain factor for each pixel in which the centroid is calculated", &FitStatus);
    intValue = su::convertStringToType<int>(m_rtcKeywords["TTPIXELENABLED"]);
    fits_write_key(fptr, TLOGICAL,"TTPIXELENABLED", &intValue, "Enable saving frames on memory", &FitStatus);
    intValue = su::convertStringToType<int>(m_rtcKeywords["TTPIXELDECIMATION"]);
    fits_write_key(fptr, TINT,    "TTPIXELDECIMATION", &intValue,   "Frame saving decimation factor", &FitStatus);
    intValue = su::convertStringToType<int>(m_rtcKeywords["TTDIAGENABLED"]);
    fits_write_key(fptr, TLOGICAL,"TTDIAGENABLED", &intValue, "Diagnostic enabled", &FitStatus);
    intValue = su::convertStringToType<int>(m_rtcKeywords["TTDIAGDECIMATION"]);
    fits_write_key(fptr, TINT,    "TTDIAGDECIMATION", &intValue,   "TipTilt diagnostic decimation factor", &FitStatus);
    fits_write_record(fptr, "        ------------------------------------------------------------------------", &FitStatus);
    int WCSAXES = 2;
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    if(m_sashaSetup[suc::InstrumentMode] == "LSS") { WCSAXES = 3;}
    fits_write_key(fptr, TINT,    "WCSAXES   ", &WCSAXES,     "Dimensionality of WCS: 2 for image, 3 for spectrum", &FitStatus);
    if(WCSAXES == 2)
        fits_write_key(fptr, TSTRING, "CTYPE1   ", (char *)"RA---TAN",    "Pixel coordinate system", &FitStatus);
        fits_write_key(fptr, TSTRING, "CTYPE2   ", (char *)"DEC--TAN",    "Pixel coordinate system", &FitStatus);
        fits_write_key(fptr, TSTRING, "RADESYS  ", (char *)"ICRS",        "reference system", &FitStatus);
        fits_write_key(fptr, TSTRING, "CUNIT1   ", (char *)"deg",         "Unit of coordinate transformation", &FitStatus);
        fits_write_key(fptr, TSTRING, "CUNIT2   ", (char *)"deg",         "Unit of coordinate transformation", &FitStatus);

        floatValue2 = 0.0000040277777777777;
        fits_write_key(fptr, TFLOAT,  "CDELT1   ", &floatValue2,"Increment along axis 1 as [CUNIT1]/pxl", &FitStatus);
        fits_write_key(fptr, TFLOAT,  "CDELT2   ", &floatValue2,"Increment along axis 2 as [CUNIT2]/pxl", &FitStatus);
        float ra_tel_deg = tenv.achieved_ra/suc::DEGREE, ra_point_offset = 0.f, ra_guide_offset = 0.f, ra_shark = 0.f;
        float dec_tel_deg = tenv.achieved_dec/suc::DEGREE, dec_point_offset = 0.f, dec_guide_offset = 0.f, dec_shark = 0.f;
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        floatValue = ra_tel_deg + ra_point_offset + ra_guide_offset + ra_shark;
        fits_write_key(fptr, TFLOAT,  "CRVAL1   ", &floatValue, "RA at CRPIX1 in units CUNIT1", &FitStatus);
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        floatValue = dec_tel_deg + dec_point_offset + dec_guide_offset + dec_shark;
        fits_write_key(fptr, TFLOAT,  "CRVAL2   ", &floatValue, "DEC at CRPIX2 in units CUNIT2", &FitStatus);

        floatValue = 58.1f + 885.f - xmin;
        fits_write_key(fptr, TFLOAT,  "CRPIX1   ", &floatValue, "Value of X Ref. pixel", &FitStatus);
        floatValue = 51.5f + 1020.f - ymin;
        fits_write_key(fptr, TFLOAT,  "CRPIX2   ", &floatValue, "Value of Y Ref. pixel", &FitStatus);

Fulvio Laudisio's avatar
Fulvio Laudisio committed
        double sign = (m_bTest01 ? 1. : -1.);
        floatValue = std::cos((ih.derotator.position1 - gDrotRef + sign*tenv.parallactic_angle)*suc::DEGREE);
        fits_write_key(fptr, TFLOAT,  "PC1_1    ", &floatValue, "Scale in [CUNIT1]/pixel", &FitStatus);
        int flip = -1;
        floatValue *= flip;
        fits_write_key(fptr, TFLOAT,  "PC2_2    ", &floatValue, "Scale in [CUNIT2]/pixel", &FitStatus);
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        floatValue = std::sin((ih.derotator.position1 - gDrotRef + sign*tenv.parallactic_angle)*suc::DEGREE);
        fits_write_key(fptr, TFLOAT,  "PC1_2    ", &floatValue, "Rotation and skew", &FitStatus);
        fits_write_key(fptr, TFLOAT,  "PC2_1    ", &floatValue, "Rotation and skew", &FitStatus);
    else if(WCSAXES == 3)
    {
        fits_write_key(fptr, TSTRING, "CTYPE1   ", (char *)"WAVE",    "Pixel coordinate system", &FitStatus);
Davide Ricci's avatar
Davide Ricci committed
        fits_write_key(fptr, TSTRING, "CTYPE2   ", (char *)"DIST",    "Pixel coordinate system", &FitStatus);
        // fits_write_key(fptr, TSTRING, "CTYPE3   ", strValue,    "Pixel coordinate system", &FitStatus);
        fits_write_key(fptr, TSTRING, "CUNIT1   ", (char *)"mas",    "Unit of coordinate transformation", &FitStatus);
        fits_write_key(fptr, TSTRING, "CUNIT2   ", (char *)"nm",    "Unit of coordinate transformation", &FitStatus);
        // fits_write_key(fptr, TSTRING, "CUNIT3   ", strValue,    "Unit of coordinate transformation", &FitStatus);
        float crval_1 = 0.0;
        float crval_2 = 800;
        fits_write_key(fptr, TFLOAT,  "CRVAL1   ", &crval_1, "Wavelength at CRPIX1", &FitStatus);
        fits_write_key(fptr, TFLOAT,  "CRVAL2   ", &crval_2, "RA at CRPIX1 in units CUNIT1", &FitStatus);
        float cdelt_1 = pixscale;
        float cdelt_2 = (2000-800)/2048;
        fits_write_key(fptr, TFLOAT,  "CDELT1   ", &cdelt_1,"Increment along axis 1 as [CUNIT1]/pxl", &FitStatus);
        fits_write_key(fptr, TFLOAT,  "CDELT2   ", &cdelt_2,"Increment along axis 2 as [CUNIT2]/pxl", &FitStatus);
        float crpix_1 = 0;
        float crpix_2 = 0;
        fits_write_key(fptr, TFLOAT,  "CRPIX1   ", &crpix_1, "Value of X Ref. pixel", &FitStatus);
        fits_write_key(fptr, TFLOAT,  "CRPIX2   ", &crpix_2, "Value of Y Ref. pixel", &FitStatus);
        // fits_write_key(fptr, TFLOAT,  "CRVAL3   ", &floatValue, "DEC at CRPIX2 in units CUNIT2", &FitStatus);
        // fits_write_key(fptr, TFLOAT,  "CD1_1    ", &floatValue, "Increment for CTYPE1", &FitStatus);
        // fits_write_key(fptr, TFLOAT,  "CD2_2    ", &floatValue, "Increment for CTYPE2", &FitStatus);
        // fits_write_key(fptr, TFLOAT,  "CD3_3    ", &floatValue, "Increment for CTYPE3", &FitStatus);
        int dispAxis = 1;
        fits_write_key(fptr, TINT,    "DISPAXIS ", &dispAxis,       "Dispersion axis (1=rows; 2=columns)", &FitStatus);
        fits_write_key(fptr, TSTRING, "DISPUNIT ", (char *)"Pixel", "Units for coordinate along DISPAXIS", &FitStatus);
        fits_write_key(fptr, TSTRING, "DISPWC   ", strValue,        "Approximate central dispersion coordinate on detector [DISPAXIS]", &FitStatus);
        fits_write_key(fptr, TSTRING, "DISPDW   ", strValue,        "Approximate central dispersion per pixel on the detector", &FitStatus);
   // TODO : Table 15 Enclosure, environment, and weather specific keywords
    fits_write_record(fptr, "        ------------------------------------------------------------------------", &FitStatus);
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    floatValue = 0.f;
//    fits_write_key(fptr, TFLOAT,  "DOMEPAN  ", &floatValue, "Dome position angle [degrees E of N]", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "DOMTEM   ", &floatValue, "Dome temperature [degrees C]", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "DOMHUM   ", &floatValue, "Dome relative humidity [%]", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "DOMDEWPT ", &floatValue, "Dome dew point [degrees C]", &FitStatus);

    fits_write_record(fptr, "        ------------------------------------------------------------------------", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "ENVTEM   ", &tenv.lbt_temp, "Outside temperature, C, at MJD-OBS", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "ENVPRE   ", &tenv.lbt_pressure, "Atmospheric pressure [mbar]", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "ENVWIN   ", &tenv.smt_wind_speed, "Outside wind speed (m/s) at MJD-OBS", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "ENVDIR   ", &tenv.smt_wind_dir, "(degrees) Direction of wind: E from N", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "ENVHUM   ", &tenv.lbt_humidity, "Outside relative humidity % at MJD-OBS", &FitStatus);
    //   fits_write_key(fptr, TFLOAT,  "ENVDEWPT ", &tenv.lbt_dewpoint, "Outside dew point (degrees C) at MJD-OBS", &FitStatus);
Fulvio Laudisio's avatar
Fulvio Laudisio committed
//    fits_write_key(fptr, TFLOAT,  "ENVTAU   ", &tenv.smt_tau, "Opacity at 225 GHz, from SMT", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "SEEING   ", &tenv.dimm_seeing, "Seeing estimate from S-H WFS", &FitStatus);
Fulvio Laudisio's avatar
Fulvio Laudisio committed
//    std::string operator_comment = " no comment by operator ";
//    fits_write_key(fptr, TSTRING, "WEATHER  ", su::get_char_star(operator_comment), "Comment by telescope operator or observer", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "SEEWAV   ", &floatValue, "Wavelength for seeing estimate", &FitStatus);
//    fits_write_key(fptr, TFLOAT,  "SEECAL   ", &floatValue, "Seeing: S-H to seeing calibration curve", &FitStatus);
void SeqDataMgr::getTelescopeEnv(STelescopeEnv & env)
{

    // DIMMFWHM                 DIMM seeing, [arcsecs]                      DIMMSeeing (DIMMSeeingZenith, DIMMSeeingElevation)
    // DIMMFLUX                 DIMM mean centroid flux                     DIMMMeanFlux
    // LBTWLINK                 Weather Station Link State                  LBTWeatherAlive (LBTWeatherAliveFront)
    // LBTPRES                  Ambient Pressure [hPa]                      LBTPressure
    // LBTTEMP                  Ambient Temperature [deg C]                 LBTTemp
    // LBTHUM                   LBT Relative Humidity [percent]             LBTHumidity
    // LBTDWPT                  LBT Dew Point [deg C]                       LBTDewPoint
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    static Nice::Date lastCall;
    Nice::Date currentCall = Nice::Date::now();
    if(currentCall - lastCall < Nice::Time::seconds(5))return;

    lbto::result res;
    lbto::DDstruct ddt;
    lbto::SeqDD dd;
    ddt.DDname = "DIMMSeeingZenith";                  //float         //1
    ddt.DDname = "DIMMSeeingElevation";               //float         //2
    dd.push_back(ddt);
    ddt.DDname = "LBTWeatherAlive";                   //bool          //3
    dd.push_back(ddt);
    ddt.DDname = "LBTPressure";                       //float         //4
    dd.push_back(ddt);
    ddt.DDname = "LBTTemp";                           //float         //5
    dd.push_back(ddt);
    ddt.DDname = "LBTHumidity";                       //float         //6
    dd.push_back(ddt);
    ddt.DDname = "LBTDewPoint";                       //float         //7
    dd.push_back(ddt);
    ddt.DDname = "LBTWindSpeed";                      //float         //8
    dd.push_back(ddt);
    ddt.DDname = "LBTWindDir";                        //float         //9
Davide Ricci's avatar
Davide Ricci committed
    dd.push_back(ddt);
    ddt.DDname = "LBTWindDirRaw";                     //float         //10
Davide Ricci's avatar
Davide Ricci committed
    dd.push_back(ddt);
    ddt.DDname = "LBTWindSpeedFront";                 //float         //11
Davide Ricci's avatar
Davide Ricci committed
    dd.push_back(ddt);
    ddt.DDname = "LBTWindDirFront";                   //float         //12
    dd.push_back(ddt);
    ddt.DDname = "LBTWindDirFrontRaw";                //float         //13
    dd.push_back(ddt);

    ddt.DDname = "AirMass";                           //float         //14
    dd.push_back(ddt);

    ddt.DDname = "ELPosition";                        //float         //15
    dd.push_back(ddt);
    ddt.DDname = "AZPosition";                        //float         //16
    dd.push_back(ddt);
    ddt.DDname = "L_AchievedALT";                     //float         //17
    dd.push_back(ddt);
    ddt.DDname = "L_AchievedAZ";                      //float         //18
    dd.push_back(ddt);
    ddt.DDname = "AZTrackingState";                   //String        //19
    dd.push_back(ddt);
    ddt.DDname = "ELTrackingState";                   //String        //20
    dd.push_back(ddt);
Fulvio Laudisio's avatar
Fulvio Laudisio committed

    ddt.DDname = "L_PresetRotMode";                   //String        //21
    dd.push_back(ddt);
    ddt.DDname = "L_AOmode";                          //String        //22
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "Epoch";                             //float         //23
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_TargetRA";                        //float         //24
    dd.push_back(ddt);
    ddt.DDname = "L_TargetDEC";                       //float         //25
    dd.push_back(ddt);
    ddt.DDname = "L_AchievedRA";                      //float         //26
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AchievedDEC";                     //float         //27
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AchievedRAHMS";                   //String        //28
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AchievedDECDMS";                  //String        //29
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AOReferenceRA";                   //float         //30
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AOReferenceDEC";                  //float         //31
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AORefMag";                        //float         //32
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_GuideRA";                         //float         //33
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_GuideDEC";                        //float         //34
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_DECOffset";                       //float         //35
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_RAOffset";                        //float         //36
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_POXOffset";                       //float         //37
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_POYOffset";                       //float         //38
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);

    ddt.DDname = "L_AOOffsetX";                       //float         //39  // WFS_LBTI_SX_baysidex_L_PSO_CUR 1; WFS_LBTI_SX_auxloops_L_modalplotTask_MAG
    ddt.DDname = "L_AOOffsetY";                       //float         //40
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_AOOffsetZ";                       //float         //41
    dd.push_back(ddt);

    ddt.DDname = "L_NSRATRKRate";                     //float         //42
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "L_NSDECTRKRate";                    //float         //43
    dd.push_back(ddt);

    ddt.DDname = "MoonAlt";                           //float         //44
    dd.push_back(ddt);
    ddt.DDname = "MoonAz";                            //float         //45
    ddt.DDname = "SunAlt";                            //float         //46
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    ddt.DDname = "SunAz";                             //float         //47
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);

    ddt.DDname = "LST";                               //String        //48
    dd.push_back(ddt);
    ddt.DDname = "ParAngle";                          //float         //49
    dd.push_back(ddt);
    ddt.DDname = "L_PointingModel";                  //String         //50
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    dd.push_back(ddt);
    //ddt.DDname = "UTC";
    //dd.push_back(ddt);
Fulvio Laudisio's avatar
Fulvio Laudisio committed

    bool get_parameters = true;
    int retry_counter = 0;
    if(!m_iif)
    {
        W_LLOG("Tcs Interface not connected. Skipping getParameters");
        return;
    }
    while(get_parameters)
    {
        get_parameters = false;
        try
        {
            res = m_iif->iifGetParameter(dd);
        }
        catch(Basda::ServiceIsBusyException)
        {
Fulvio Laudisio's avatar
Fulvio Laudisio committed
            if(retry_counter && retry_counter % 10 == 0)
            {
                W_LLOG("Busy : retrying " << retry_counter);
            }
            retry_counter++;
            get_parameters = true;
            usleep(50000);
            continue;
        }
        catch(const std::exception& e)
        {
            W_LLOG(e.what());
            return;
        }
        catch(...)
        {
            std::string what = boost::current_exception_diagnostic_information();
            std::string toErase = "std::exception::what :";
            size_t pos = what.find(toErase);
            if(pos != std::string::npos)what.erase(pos, toErase.length());
            what.erase(std::remove(what.begin(), what.end(), '\n'), what.end());
            std::stringstream log_msg;
            log_msg << "Error trying getting DD parmateres : " << what;
            W_LLOG(log_msg.str());
            return;
        }
    }
    if(res.rescode == EXIT_SUCCESS)
    {
        double temp;
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        // std::vector<std::string>::const_iterator it;
        // int counter = 0;
        // for(it = res.resmsg.begin(); it < res.resmsg.end(); it++)
        // {
        //     counter++;
        //     U9_LLOG(counter << " ~ " << *it);
        // }
        env.dimm_seeing_zenith = atof(res.resmsg[1].c_str());          //f   //1
        env.dimm_seeing_elevation = atof(res.resmsg[2].c_str());       //f   //2
        env.lbt_weather_alive = (bool)atoi(res.resmsg[3].c_str());     //b   //3
        env.lbt_pressure = atof(res.resmsg[4].c_str());                //f   //4
        env.lbt_temp = atof(res.resmsg[5].c_str());                    //f   //5
//        U9_LLOG(env.lbt_temp);
        env.lbt_humidity = atof(res.resmsg[6].c_str());                //f   //6
        env.lbt_dewpoint = atof(res.resmsg[7].c_str());                //f   //7
        env.lbt_wind_speed = atof(res.resmsg[8].c_str());              //f   //8
        env.lbt_wind_dir = atof(res.resmsg[9].c_str());                //f   //9
        env.lbt_wind_dir_raw = atof(res.resmsg[10].c_str());           //f   //10
        env.lbt_wind_speed_front = atof(res.resmsg[11].c_str());       //f   //11
        env.lbt_wind_dir_front = atof(res.resmsg[12].c_str());         //f   //12
        env.lbt_wind_dir_raw_front = atof(res.resmsg[13].c_str());     //f   //13

        env.air_mass = atof(res.resmsg[14].c_str());                   //f   //14
        env.mcs_elevation = atof(res.resmsg[15].c_str())/3600;         //f   //15
        env.mcs_azimuth = atof(res.resmsg[16].c_str())/3600;           //f   //16
        env.achieved_alt = atof(res.resmsg[17].c_str());               //f   //17
        env.achieved_az = atof(res.resmsg[18].c_str());                //f   //18
        env.az_tracking_state = res.resmsg[19];                        //S   //19
        env.el_tracking_state = res.resmsg[20];                        //S   //20

        env.telescope_mode = res.resmsg[21];                           //S   //21
        env.ao_mode = res.resmsg[22];                                  //S   //22
        env.epoch = atof(res.resmsg[23].c_str());                      //f   //23
        env.target_ra = atof(res.resmsg[24].c_str());                  //f   //24
        env.target_dec = atof(res.resmsg[25].c_str());                 //f   //25
        env.achieved_ra = atof(res.resmsg[26].c_str());                //f   //26 ?
        env.achieved_dec = atof(res.resmsg[27].c_str());               //f   //27 ?
        env.tcs_ra = res.resmsg[28];                                   //S   //28
        env.tcs_dec = res.resmsg[29];                                  //S   //29
        env.ao_ref_ra = atof(res.resmsg[30].c_str());                  //f   //30
        env.ao_ref_dec = atof(res.resmsg[31].c_str());                 //f   //31

        if( res.resmsg[32] == "nan")
        {
            env.ao_ref_mag = -999.0f;                                  //f   //32
        }
        else
        {
            env.ao_ref_mag = atof(res.resmsg[32].c_str());             //f   //32
        }
        env.guide_star_ra = atof(res.resmsg[33].c_str());              //f   //33
        env.guide_star_dec = atof(res.resmsg[34].c_str());             //f   //34
        env.target_decoffset = atof(res.resmsg[37].c_str());           //f   //37  (or 35?)
        env.target_raoffset = atof(res.resmsg[38].c_str());            //f   //38  (or 36?)

        env.ao_offsetx = atof(res.resmsg[39].c_str());                 //f   //39
        env.ao_offsety = atof(res.resmsg[40].c_str());                 //f   //40
        env.ao_offsetz = atof(res.resmsg[41].c_str());                 //f   //41

        env.tel_trk_RA = atof(res.resmsg[42].c_str());                 //f   //42
        env.tel_trk_DEC = atof(res.resmsg[43].c_str());                //f   //43

        env.moon_alt = atof(res.resmsg[44].c_str());                   //f   //44
        env.moon_az = atof(res.resmsg[45].c_str());                    //f   //45
        env.sun_alt = atof(res.resmsg[45].c_str());                    //f   //46
        env.sun_az = atof(res.resmsg[47].c_str());                     //f   //47

        env.lst = res.resmsg[48];                                      //S   //48

        env.parallactic_angle = atof(res.resmsg[49].c_str());          //f   //49
        env.pointing_model = res.resmsg[50];                           //S   //50
//        env.utc = atof(res.resmsg[40].c_str());

        //Converting string to int arr
        float rad2deg = 180/M_PI;
        float sec2rad = M_PI/(12*3600);
        int lst_arr[4];
        lst_arr[0] = atoi(env.lst.substr(0,2).c_str());
        lst_arr[1] = atoi(env.lst.substr(3,2).c_str());
        lst_arr[2] = atoi(env.lst.substr(6,2).c_str());
        lst_arr[3] = atoi(env.lst.substr(9,5).c_str()); //5 decimal digits after ','
        float lst = lst_arr[0]*3600 + lst_arr[1]*60 + lst_arr[2] + (float)lst_arr[3]/100000; //division by 100000 due to the choicheq
        //U9_LLOG("____________ LST destruct: "<<lst_arr[0]<<" : "<<lst_arr[1]<<" : "<<lst_arr[2]<<" . "<<lst_arr[3]);

        //Converting string to int arr
        int ra_arr[4];
        ra_arr[0] = atoi(env.tcs_ra.substr(0,2).c_str());
        ra_arr[1] = atoi(env.tcs_ra.substr(3,2).c_str());
        ra_arr[2] = atoi(env.tcs_ra.substr(6,2).c_str());
        ra_arr[3] = atoi(env.tcs_ra.substr(9,5).c_str()); //5 decimal digits after ','
        float ra = ra_arr[0]*3600 + ra_arr[1]*60 + ra_arr[2] + (float)ra_arr[3]/100000; //division by 100000 due to the choicheq
        //U9_LLOG("____________ RA destruct: "<<ra_arr[0]<<" : "<<ra_arr[1]<<" : "<<ra_arr[2]<<" . "<<ra_arr[3]);

        //Converting float val to int arr in format hh:mm:ss.sss
        float ha = ra-lst;
        if(ha<0) {ha = ha+24*3600;}
        //U9_LLOG("____________ ha in seconds: "<<ha);
        int ha_arr[4];
        ha_arr[0] = ha/3600;
        ha_arr[1] = ((int)ha%3600)/60;
        ha_arr[2] = ((int)ha%3600)%60;
        ha_arr[3] = (ha-(int)ha)*1000;
        //U9_LLOG("____________ HA destruct: "<<ha_arr[0]<<" : "<<ha_arr[1]<<" : "<<ha_arr[2]<<" . "<<ha_arr[3]);

        std::stringstream hour_angle;
        if(ha_arr[0]<10) {hour_angle<<"0";}
        hour_angle << ha_arr[0] << ":";
        if(ha_arr[1]<10) {hour_angle<<"0";}
        hour_angle << ha_arr[1] << ":";
        if(ha_arr[2]<10) {hour_angle<<"0";}
        hour_angle << ha_arr[2] << "." <<ha_arr[4];
        //U9_LLOG("Hour Angle =  "<<hour_angle.str());
        env.hour_angle = hour_angle.str();

        //Converting string to int arr
        int dec_arr[4];
        dec_arr[0] = atoi(env.tcs_dec.substr(0,2).c_str());
        dec_arr[1] = atoi(env.tcs_dec.substr(3,2).c_str());
        dec_arr[2] = atoi(env.tcs_dec.substr(6,2).c_str());
        dec_arr[3] = atoi(env.tcs_dec.substr(9,2).c_str()); //2 decimal digits after ','
        float dec = dec_arr[0] + dec_arr[1]/60 + dec_arr[2]/3600 + (float)dec_arr[3]/(3600*100); //division by 100 due to the choiche
        //U9_LLOG("____________ DEC destruct: "<<dec_arr[0]<<" : "<<dec_arr[1]<<" : "<<dec_arr[2]<<" . "<<dec_arr[3]);

        //Converting all values in rad
        ha  = ha*sec2rad;
        dec = dec/rad2deg;
        float lat = 32.701309/rad2deg;
        float h = asin(cos(ha)*cos(dec)*cos(lat)+sin(dec)*sin(lat));
        //U9_LLOG("_______________________ h = "<<h<<" rad ,h = "<<h*rad2deg<<" deg. _______ Zen Dist = "<<90-h*rad2deg<<" deg.");
        env.zenith_dist = ceil((90-h*rad2deg)*100)/100;
    }
    else
    {
        for(unsigned int i = 0; i < res.resmsg.size(); i++)
        {
            U9_LLOG(res.resmsg[i]);
        }
    }
}

void SeqDataMgr::getMaskCombo(SInstrumentHeader & ih)
{
    int coro_delimiter = ih.coro_slit_w.type.find(":");
    int coro_word_len = ih.coro_slit_w.type.length()-coro_delimiter;
    std::string coro_named_pos= ih.coro_slit_w.type.substr(coro_delimiter+1,coro_word_len);

    int lyot_delimiter = ih.lyot_grism_w.type.find(":");
    int lyot_word_len = ih.lyot_grism_w.type.length()-lyot_delimiter;
    std::string lyot_named_pos= ih.lyot_grism_w.type.substr(lyot_delimiter+1,lyot_word_len);

    ih.mask_comb.type = coro_named_pos+"+"+lyot_named_pos;

    //U9_LLOG("Prova funzione, IH.Mask_Comb : " << ih.mask_comb.type);
std::string SeqDataMgr::GetReadoutRegion(int & xmin, int & xmax, int & ymin, int & ymax)
Davide Ricci's avatar
Davide Ricci committed
{
    xmin = xmax = ymin = ymax = 0;
    std::map<std::string, indi::IElement>::const_iterator itRegion;
    const std::map<std::string, indi::IElement> & elements = sashaReadoutRegion.getElements();
    itRegion = elements.find("x1");
Davide Ricci's avatar
Davide Ricci committed
    if(itRegion != elements.end())
Davide Ricci's avatar
Davide Ricci committed
        if(!su::getValue<int>(itRegion->second.get(), xmin)){ W_LLOG("Could not get readout region XMIN value");}
Davide Ricci's avatar
Davide Ricci committed
    else { W_LLOG("Could not find readout region XMIN value");}
    itRegion = elements.find("x2");
Davide Ricci's avatar
Davide Ricci committed
    if(itRegion != elements.end())
    {
        if(!su::getValue<int>(itRegion->second.get(), xmax)){ W_LLOG("Could not get readout region XMAX value");}
    }
Davide Ricci's avatar
Davide Ricci committed
    else { W_LLOG("Could not find readout region XMAX value");}
    itRegion = elements.find("y1");
Davide Ricci's avatar
Davide Ricci committed
    if(itRegion != elements.end())
    {
        if(!su::getValue<int>(itRegion->second.get(), ymin)){ W_LLOG("Could not get readout region YMIN value");}
    }
Davide Ricci's avatar
Davide Ricci committed
    else { W_LLOG("Could not find readout region YMIN value");}
    itRegion = elements.find("y2");
Davide Ricci's avatar
Davide Ricci committed
    if(itRegion != elements.end())
    {
        if(!su::getValue<int>(itRegion->second.get(), ymax)){ W_LLOG("Could not get readout region YMAX value");}
    }
Davide Ricci's avatar
Davide Ricci committed
    else { W_LLOG("Could not find readout region YMAX value");}
    
    std::stringstream activeRegion;
    activeRegion << "["<< xmin <<":"<< xmax <<", "<< ymin <<":"<< ymax << "]";
    return activeRegion.str();
int SeqDataMgr::sashaEnableSave(const std::map<std::string, std::string> & _dSashaSetup)
{
    // To be Implemented
    U6_LLOG(__FUNCTION__ << " ~ DEPRECATED");
    Ice::Int status = EXIT_SUCCESS;
#ifdef UNDEFINED
    IProperty sashaSave(IProperty::Switch), recvProp;
    std::map<std::string, std::string>::const_iterator itFind;
    itFind = _dSashaSetup.find("SAVE");

    // m_ptrIc->connect();
    // NOTE while alive, indiserver does not close the connection to the client.
    // U1_LLOG("Command sasha to save to file");
    U1_LLOG("Command sasha to save to file");

    sashaSave.setDevice("sasha_save");
    sashaSave.setName("enable_save");
    sashaSave.setPerm(IProperty::ReadWrite);
    sashaSave.setState(IProperty::Busy);
    sashaSave.add(IElement("value"));
    // Switch properties can have value On or Off
    sashaSave["value"].setSwitchState(IElement::Off);
    U1_LLOG(sashaSave.getType());
Davide Ricci's avatar
Davide Ricci committed
    if(!(itFind == _dSashaSetup.end()))
Davide Ricci's avatar
Davide Ricci committed
        if((su::str_tolower(itFind->second) == "true") || (itFind->second == "1"))
        {
            sashaSave["value"].setSwitchState(IElement::On);
        }
    }

    //
    m_ptrIc2->send_new_property_wait2(sashaSave, recvProp);
#endif
    // status = m_ptrIc->close();
    U8_LLOG(__FUNCTION__);
    return status;
}


Davide Ricci's avatar
Davide Ricci committed
//  Patch to get rid of the timeout problem of sasha driver. NREAD is sent whenever NDIT changes and no other parameters are sent
int SeqDataMgr::sashaEndSetup(std::map<std::string, std::string> & _dTimeoutSetup, bool ndit_changed)
{
    U6_LLOG(__FUNCTION__);
Davide Ricci's avatar
Davide Ricci committed
    int status = EXIT_SUCCESS;
    if(ndit_changed && _dTimeoutSetup.empty())
    {
        IProperty & prop = m_sashaProps.find("NREADS")->second;
        const std::map<std::string, indi::IElement> elements = prop.getElements();
        if(elements.find("value") != elements.end())
        {
            _dTimeoutSetup["NREADS"] = prop["value"].getValue();
        }
        else
        {
            std::stringstream log_msg;
            log_msg << "Error : IProperty \"" << prop.getDevice() << "." << prop.getName() << "\" from INDI Server has changed";
            E_LLOG(log_msg.str());
            throw(std::runtime_error(log_msg.str()));
        }
    }
    std::map<std::string, std::string>::iterator itSetup;
    int num_properties = _dTimeoutSetup.size();
    int counter = 0, sent_properties = 0;
    try
    {
        for(itSetup = _dTimeoutSetup.begin(); itSetup != _dTimeoutSetup.end(); itSetup++)
        {
            if(m_getFitsAbortFlag)break;
            counter++;
            IProperty & prop = m_sashaProps.find(itSetup->first)->second;
            const std::map<std::string, indi::IElement> elements = prop.getElements();
            bool bPropError = false, bSetProperty = false;
            if(elements.find("value") != elements.end())
            {
                if(m_bForcePropertySet || itSetup->second != prop["value"].getValue())
                {
                    prop["value"].setValue<int>(su::convertStringToType<int>(itSetup->second));
                    bSetProperty = true;
                }
            }
            else
                bPropError = true;
            if(bPropError)
            {
                std::stringstream log_msg;
                log_msg << "Error : IProperty \"" << prop.getDevice() << "." << prop.getName() << "\" from INDI Server has changed";
                E_LLOG(log_msg.str());
                throw(std::runtime_error(log_msg.str()));
            }
            else if(bSetProperty || (ndit_changed && sent_properties == 0 && counter == num_properties))
                if(!bSetProperty && (ndit_changed && sent_properties == 0 && counter == num_properties))
                {
                    U9_LLOG("Sending property " << itSetup->first << " to make camera driver recalculate exposure duration");
                }
                m_sashaPropsSet[itSetup->first] = false;
                SendNewProperty(prop);
                sent_properties++;
            }
        }
        bool wait_result = false;
        wait_result = WaitSetMessages(60000000);
        if(!wait_result)status = EXIT_FAILURE;
    }
    catch(...)
    {
        E_LLOG(__FUNCTION__);
        U8_LLOG("");
        throw;
    }
    
    U8_LLOG("");
    return status;
}
int SeqDataMgr::sashaSetup(const std::map<std::string, std::string> & _dSashaExpose)
{
    U6_LLOG(__FUNCTION__);
    int status = EXIT_SUCCESS;
    m_exposeMutex.lock();
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    m_getFitsAbortFlag = 0;
Fulvio Laudisio's avatar
Fulvio Laudisio committed
    bool bObjectNamesSet = false;
Davide Ricci's avatar
Davide Ricci committed
        std::map<std::string, std::string>::iterator itSashaSetup;
        for(itSashaSetup = m_sashaSetup.begin(); itSashaSetup != m_sashaSetup.end(); itSashaSetup++)
        {
Fulvio Laudisio's avatar
Fulvio Laudisio committed
            if(itSashaSetup->first == suc::InstrumentMode)
            {
                itSashaSetup->second = "GEN";
            }
Davide Ricci's avatar
Davide Ricci committed
            else
                itSashaSetup->second.clear();
        }
        std::map<std::string, std::string>::const_iterator itSetup;
        for(itSetup = _dSashaExpose.begin(); itSetup != _dSashaExpose.end(); itSetup++)
        {
            U9_LLOG("Keyword : " << itSetup->first << "; Value : " << itSetup->second);
Davide Ricci's avatar
Davide Ricci committed
            if(m_sashaSetup.find(itSetup->first) == m_sashaSetup.end())
            {
                std::stringstream log_msg;
                log_msg << "Keyword not allowed in sashaExpose : " << itSetup->first;
                E_LLOG(log_msg.str());
                throw(std::invalid_argument(log_msg.str()));
            }
Davide Ricci's avatar
Davide Ricci committed
            m_sashaSetup[itSetup->first] = itSetup->second;
        bool bForceTimeoutRefresh = false;
        bool ndit_changed = false;
        std::map<std::string, std::string> incomingSetup = _dSashaExpose;
        std::map<std::string, std::string> timeoutSetup;
        timeoutSetup = su::extract_setup(m_timeoutSetup, incomingSetup);
        for(itSetup = incomingSetup.begin(); itSetup != incomingSetup.end(); itSetup++)
        {
            if(m_getFitsAbortFlag)break;
            if(itSetup->first == "DIT" || itSetup->first == "InstrumentMode")
            {
            }
            else
            {
    //            U9_LLOG("setup : " << itSetup->first << ":" << itSetup->second);
                IProperty & prop = m_sashaProps.find(itSetup->first)->second;
                const std::map<std::string, indi::IElement> elements = prop.getElements();
                bool bPropError = false, bSetProperty = false;
                if(itSetup->first == "SAVE")
                {
                    bool bSave = true;
                    su::get_bool_from_setup(_dSashaExpose,  "SAVE",  bSave);
                    std::map<std::string, indi::IElement>::const_iterator itEl;
                    if(elements.find("value") != elements.end())
                    {
                        indi::IElement::SwitchStateType switch_state = (bSave ? IElement::On : IElement::Off);
                        if(m_bForcePropertySet || prop["value"].getSwitchState() != switch_state)
                        {
                            prop["value"].setSwitchState(switch_state);
Davide Ricci's avatar
Davide Ricci committed
//                            U9_LLOG("Setting : " << "value" << " to " << bSave);
                    else
                        bPropError = true;
                }
                else if(itSetup->first == "OBJECTNAME")
                {
                    if(elements.find("value") != elements.end())
                    {
                        std::string current_name = prop["value"].getValue();
                        if(m_bForcePropertySet || current_name != itSetup->second)
                        {
                            prop["value"].setValue(itSetup->second);
                            bSetProperty = true;
Fulvio Laudisio's avatar
Fulvio Laudisio committed
                            bObjectNamesSet = true;
                    else
                        bPropError = true;
                }
    //            else if(itSetup->first == "NCOADDS")
    //            {
    //                if(elements.find("num_coadds") != elements.end())
    //                    prop["num_coadds"].setValue<int>(su::convertStringToType<int>(itSetup->second));
    //                else
    //                    bPropError = true;
    //            }
                else if(itSetup->first == "READOUT")
                {
                    if(elements.find("value") != elements.end())
                        if(m_bForcePropertySet || prop["value"].getValue() != itSetup->second)
                        {
                            prop["value"].setValue(itSetup->second);
                            bSetProperty = true;
                        }
                    }
                    else
                        bPropError = true;
                }
                else
                {
                    if(elements.find("value") != elements.end())
                    {
//                        int value = su::convertStringToType<int>(prop["value"].getValue());
                        if(m_bForcePropertySet || itSetup->second != prop["value"].getValue())
                            if(itSetup->first == "NDIT")ndit_changed = true;
                            prop["value"].setValue<int>(su::convertStringToType<int>(itSetup->second));
                            bSetProperty = true;
                        }
                    }
                    else
                        bPropError = true;
                }
                if(bPropError)
                {
                    std::stringstream log_msg;
                    log_msg << "Error : IProperty \"" << prop.getDevice() << "." << prop.getName() << "\" from INDI Server has changed";
                    E_LLOG(log_msg.str());
                    throw(std::runtime_error(log_msg.str()));
                }
                else if(bSetProperty)
                {
                    m_sashaPropsSet[itSetup->first] = false;
                    SendNewProperty(prop);
                }
            }
        }
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        updateInstrumentHeader();
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        if(false && !bObjectNamesSet)
Fulvio Laudisio's avatar
Fulvio Laudisio committed
        {
            const std::map<std::string, indi::IElement> elements = sashaObjName.getElements();
            if(elements.find("value") != elements.end())
            {
                m_sashaPropsSet["OBJECTNAME"] = false;
                sashaObjName["value"].setValue(m_TcsPreset.object_name);
                SendNewProperty(sashaObjName);
            }
        }
        bool wait_result = false;
        wait_result = WaitSetMessages(60000000);
        if(!wait_result)status = EXIT_FAILURE;
Davide Ricci's avatar
Davide Ricci committed
        int status2 = sashaEndSetup(timeoutSetup, ndit_changed);
        if(status == EXIT_FAILURE || status2 == EXIT_FAILURE)status = EXIT_FAILURE;
    }
    catch(...)
    {
        m_exposeMutex.unlock();
        U8_LLOG("");
        throw;
    }
    m_exposeMutex.unlock();
    U8_LLOG(__FUNCTION__ << " exiting");
    return status;
}


int SeqDataMgr::sashaSetFrequency(const std::map<std::string, std::string> & _dSashaExpose)
{
    U6_LLOG(__FUNCTION__);
    int status = EXIT_SUCCESS;
    m_exposeMutex.lock();
    try
    {
        std::map<std::string, std::string>::const_iterator itSetup;
        itSetup = _dSashaExpose.find("FREQUENCY");
        if(itSetup != _dSashaExpose.end())
        {
            IProperty & prop = m_sashaProps.find(itSetup->first)->second;
            const std::map<std::string, indi::IElement> elements = prop.getElements();
            if(elements.find("value") != elements.end())
            {
                if(m_bForcePropertySet || prop["value"].getValue() != itSetup->second)
                {
                    prop["value"].setValue(itSetup->second);
                    m_sashaPropsSet[itSetup->first] = false;
                    SendNewProperty(prop);
                    //camera reinitializes and readout region is reset
                    m_sashaPropsSet["READOUT"] = false;
                    WaitSetMessages(60000000);
                }
            }
            else
            {
                U9_LLOG("Error: frequency property from INDI server has changed");
            }
        }
    }
    catch(...)
    {
        m_exposeMutex.unlock();
        U8_LLOG("");
        throw;
    }
    m_exposeMutex.unlock();
    U8_LLOG(__FUNCTION__ << " exiting");
    return status;
}

int SeqDataMgr::checkContinuousFlag()
{
    int status = EXIT_SUCCESS;
    if(!m_bFastSavingMode)
    {
    if(!(sashaExposeProp["value"].getSwitchState() == IElement::Off))
    {
        m_sashaPropsSet["STOP"] = false;
        sashaStop["value"].setSwitchState(IElement::On);
        SendNewProperty(sashaStop);
        m_sashaPropsSet["EXPOSE"] = false;
        U6_LLOG(__FUNCTION__ << " ~ Clearing Expose Property");
        WaitSetMessages(10000000);
        m_sashaPropsSet["EXPOSE"] = false;
        WaitSetMessages(10000000);
        U8_LLOG("");
    }
//    if(!(sashaEnableCont["value"].getSwitchState() == IElement::Off))
//  enable_cont property is resetted every time because is sometimes found misaligned with the one on the indi server
    {
        m_sashaPropsSet["CONTINUOUS"] = false;
        sashaEnableCont["value"].setSwitchState(IElement::Off);
        SendNewProperty(sashaEnableCont);
        WaitSetMessages(10000000);
    }
Fulvio Laudisio's avatar
Fulvio Laudisio committed
int SeqDataMgr::sashaExpose(std::vector<std::string> & out_files)
{
    U6_LLOG(__FUNCTION__);
    int status = EXIT_SUCCESS;
    m_exposeMutex.lock();
        m_last_generated_files.clear();
        if(m_bSaveOnlyExposedFiles && !m_getFitsAbortFlag)
        {
            sashaBlob.setBLOBEnable(IProperty::Also);
            SendNewProperty(sashaBlob);
        }
        bool wait_result = false;
        wait_result = WaitSetMessages(60000000);
        if(!m_getFitsAbortFlag)
        {
            m_sashaPropsSet["EXPOSE"] = false;
            sashaExposeProp["value"].setSwitchState(IElement::On);
            SendNewProperty(sashaExposeProp);
            m_ExpectedBlobs = su::convertStringToType<int>(sashaNumSeqs["value"].getValue());
            m_bInstrumentSetupUpdated = false;
            updateInstrumentHeader();
            wait_result = WaitSetMessages(30000000);
        }
        if(!m_getFitsAbortFlag)
        {
            wait_result = WaitStartAcquireOff(600000000);//ExtimateTiming());
            m_bBlobArrived = false;
        }
        if(m_bSaveOnlyExposedFiles)
        {
Fulvio Laudisio's avatar
Fulvio Laudisio committed
            if(m_ExpectedBlobs > 0)WaitBLOB(20000);
            sashaBlob.setBLOBEnable(IProperty::Never);
            SendNewProperty(sashaBlob);
        }
        if(!wait_result)status = EXIT_FAILURE;
        if(m_getFitsAbortFlag)
        {
            throw(std::runtime_error("Error acquisition aborted by user"));
        }
    }
    catch(...)
    {
        m_exposeMutex.unlock();
        if(m_getFitsAbortFlag)m_getFitsAbortFlag--;
        U8_LLOG("");
        throw;
    }
    if(m_getFitsAbortFlag)m_getFitsAbortFlag--;
    m_exposeMutex.unlock();
    out_files = m_last_generated_files;
    m_last_generated_files.clear();
    U8_LLOG(__FUNCTION__ << " exiting");
    return status;
}


void SeqDataMgr::abortExposure()
{
    m_getFitsAbortFlag = 1;
    SendNewProperty(sashaStop);
}

bool SeqDataMgr::WaitSetMessages(int timeout)
{
    bool wait_sets = true;
    int waited_time = 0;
Davide Ricci's avatar
Davide Ricci committed
    int sleep_time = 1000;
    std::stringstream log_msg;
Davide Ricci's avatar
Davide Ricci committed
        usleep(sleep_time);
        waited_time += sleep_time;
        sleep_time = 49000*(waited_time == 1000) + 50000*(waited_time > 1000);
        std::map<std::string, bool>::iterator itSet;
        for(itSet = m_sashaPropsSet.begin(); itSet != m_sashaPropsSet.end(); itSet++)
        {
            if(!itSet->second)break;
        }
        wait_sets = itSet != m_sashaPropsSet.end();
        if(waited_time >= timeout)
        {
            log_msg << __FUNCTION__ << " ~ Timed Out waiting for : ";
            for(itSet = m_sashaPropsSet.begin(); itSet != m_sashaPropsSet.end(); itSet++)
            {
                if(!itSet->second)
                    log_msg << itSet->first << " ~ ";
            }
Davide Ricci's avatar
Davide Ricci committed
            U6_LLOG(__FUNCTION__);
            E_LLOG("Received EOF");
            U8_LLOG("");
            throw(std::runtime_error("Indi Server signaled EoF"));
        }
        if(m_getFitsAbortFlag)break;
    }
Davide Ricci's avatar
Davide Ricci committed
    if(waited_time > 1000)
    {
        U6_LLOG(__FUNCTION__);
        if(!log_msg.str().empty()){E_LLOG(log_msg.str());};
        U8_LLOG(__FUNCTION__ << " ~ Exiting; waited " << waited_time/1000.f << "ms");
    }
    return !wait_sets;
}

bool SeqDataMgr::WaitStartAcquireOff(unsigned int timeout)
{
    U6_LLOG(__FUNCTION__);
    bool wait_result = true;
    unsigned int waited_time = 0;
    while(wait_result)
    {
        usleep(200000);
        waited_time += 200;
        wait_result = !(sashaExposeProp["value"].getSwitchState() == IElement::Off);
        if(waited_time >= timeout)
        {
            E_LLOG(__FUNCTION__ << " ~ Timed Out");
            break;
        }
        if((waited_time % 300000) == 0)
        {
            U9_LLOG(__FUNCTION__ << " ~ Waiting " << waited_time/1000.f << "/" << timeout/1000.f << "s");
        }
        if(m_getFitsAbortFlag)break;
        if(m_bReceivedEof)
        {
            E_LLOG("Received EOF");
            U8_LLOG("");
            throw(std::runtime_error("Indi Server signaled EoF"));
        }
    }
    U8_LLOG(__FUNCTION__ << " ~ Exiting; waited " << waited_time << "ms");
    return !wait_result;
}

bool SeqDataMgr::WaitBLOB(unsigned int timeout)
{
    U6_LLOG(__FUNCTION__);
    bool wait_result = true;
    unsigned int waited_time = 0;
    while(wait_result)
    {
        usleep(200000);
        waited_time += 200;
        wait_result = !(m_bBlobArrived == true);
        if(wait_result && waited_time >= timeout)
        {
            E_LLOG(__FUNCTION__ << " ~ Timed Out");