/***********************************************
 * gas_mix.c                 new version 6/5/93
 *
 ************************************************/

#include <module.h>
#include "gas.h"

main()
{
    int cpu_nr, tcp_ip_nr;
    char *hv_config[3];
    int gains_set[96], gains_read[96], data[96];
    float datv[96];

    float percent[3], flow[3], flow_percent[3];
    float flow_percent1[3];
    float iso_monitor, total_flow;
    float t_mon, t1_mon;
    float pressione_PD;

    unsigned base;
    int ndata, gain_mode, diff_mode, i;
    float vmin = -10.;   /* give range of the A/D : settable only hardware ! */
    float vmax =  10.;
    float nch = 4096;

    if (get_config( &tcp_ip_nr, &cpu_nr, hv_config) != 0){
       printf (" error in get_config \n");
       exit (0);
    }

    if (cpu_nr != 1){
       printf(" command not valid from this CPU \n");
       printf(" please login on the south balcony CPU \n");
       printf(" ( \"south\" or \"telnet 131.169.32.121\" )\n");
       exit(0);
    }

    base = (unsigned) BASE_ADDR;

    diff_mode = 1;    /* 1: 48 diff chan's;  0: 96 single ended chan's   */
    gain_mode = 1;    /* 1: common amplification for all chan's
                         0: individual amplification                 */

    gains_set[0] = gain_word(1);   /*   -> amplification = 1 */

    anl_adc_init(base, &gain_mode, gains_set, &diff_mode);

    anl_adc_read(base, data, &ndata, &gain_mode, gains_read, &diff_mode);

    for ( i = 0; i < 7; i++){

       datv[i] = (vmax - vmin)/(nch-1) * (float) data[i] + vmin;     
       if (i > 0 && i < 4){       
          percent[3 - i] = datv[i] / (IMAX * R0);
       }
       else if(i == 0)
          iso_monitor = datv[i];
       else if(i == 4)
          t_mon = datv[i];
       else if(i == 5)
          t1_mon = datv[i];
       else if(i == 6)
          pressione_PD = datv[i]; 
    } 
    
    total_flow = 0;
    for ( i = 0; i < 3; i++){
       flow[i] = percent[i] * full_scale[i];
       percent[i] *= 100.;
       total_flow += flow[i];
    }
    for ( i = 0; i < 3; i++)
       if (total_flow > 0.)
          flow_percent[i] = flow[i] / total_flow * 100.;
       else
          flow_percent[i] = 0.;
    if ( flow[1] + flow[2] > 0){
       flow_percent1[1] = flow[1] / ( flow[1] + flow[2] ) * 100.;
       flow_percent1[2] = flow[2] / ( flow[1] + flow[2] ) * 100.;
    }
    else{ 
       flow_percent1[1] = 0.;
       flow_percent1[2] = 0.;
    }
    printf("\n\n  B/RMUON  MASS FLOWMETERS \n");
    printf("   \n\n");
    printf("   gas      full scale %%     flow ( liter/h ) ");
    printf("     %%     rel. %%\n\n");
    for ( i = 0; i < 3; i++)
       if ( i == 0)  
          printf("  %6s       %4.1f          %5.0f     %13.1f  \n",
                 gas_name[i], percent[i], flow[i], flow_percent[i]);
       else  
          printf("  %6s       %4.1f          %5.0f     %13.1f   %5.0f  \n",
                 gas_name[i], percent[i], flow[i], flow_percent[i], 
                 flow_percent1[i]);
    printf("\n  %6s                      %5.0f \n",
        "total",  total_flow);

    printf("\n\n\n I.R. Iso monitor: %4.2f V  ( Hardware Alarm at +%4.2f V )\n",
        iso_monitor, alarm_thr);

    t_mon = t_mon / 0.01026 -273. - T_OFFSET;
    t1_mon = t1_mon / 0.01026 -273. - T_OFFSET;
    printf("\n\n Gas Room Temperature : %.0f C\n", t_mon); 
    printf("\n Iso Hut  Temperature : %.0f C\n", t1_mon); 

    /* taratura pressione:           25/6/96
       - sensibilita` = 0.0167 V/mb;
       - a 1018 mb segna 3.7143 V letti con AND_ADC;
       - nelle stesse condizioni il barometro CTD segna 1017;
       - Inoltre, agendo sul trimmer, l'indicazione era gia' stata
         fatta scendere da 1022 a 1018 (la taratura precedente era
         stata fatta a Pd da Pitacco    */
    pressione_PD = (pressione_PD-VREF)/PSENS + PREF;
    printf("\n Atmospheric pressure from BMU barometer: %.1f mb\n",
           pressione_PD);

    printf("\n Try also to read atmospheric pressure from CTD\n");
    printf("    ( it works only if communications with SCM are OK\n");
    system("pressione");
}