DAS  3.1.6 - 18/09/2017
Procedures.c
Go to the documentation of this file.
1 #include <windows.h>
2 #include <stdio.h>
3 #include <io.h>
4 #include <fcntl.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <direct.h>
10 #include <time.h>
11 #include <math.h>
12 #include <errno.h>
13 //#include <sys/timeb.h>
14 #include "mgui.h"
15 #include "DAS_Spatram.h"
16 #include "dcl.h"
17 #include "dil.h"
18 #include "bil.h"
19 #include "DOAS.h"
20 
26 //ccd cc;
27 
28 // CCD & Camera vars
29 ccd DC_CCD; // CCD settings
30 ptf DC_HP; // Platform settings
31 config Cfg; // Camera controls & Options
37 flag FLAG; //flags structure
38 //cpl CPL; //Control Panel
39 paramini PARAM; //Param.ini
40 //marc MARC; // Marconi struct
41 d_geo DGEO; // geographical coordinates structure
42 bil BIL; //Bortul Library Structure
43 //db_options DB_O;
46 
48 
49 int SaveCnt = 1; // Contatore acquisizioni in un file
50 int fftprh = 1; //flag first time program run here (for saving porpouses - Save sky data)
51 int fftsavecal = 1; //flag first calibration (for saving porpouses - Save calibration data)
52 extern int ExeREM;
53 
54 
55 /* Build spectrum Header
56  Param: int mod
57  mod = 0 --> ASCII Header
58  mod = 1 --> Binary Header
59  int s
60  s = server id
61 
62 */
63 void D_BuildH(int mod, int s)
64 {
65 
66  char buf[16];
67  // Image format
68  if(DAS.DOption.savingmode)
69  HDRSP.imgfmt=1;
70  else
71  HDRSP.imgfmt=0;
72 
73  HDRSP.ccdwx = DC_CCD.dx; //X Pixel Number
74  HDRSP.ccdwy = DC_CCD.dy; //Y Pixel Number
75 
76 
77  HDRSP.ccdxs = DAS.Paramini.ccdXoff; // window X Start
78  HDRSP.ccdys = DAS.Paramini.ccdYoff; // windowY Start
79  HDRSP.ccdxe = DAS.Paramini.ccdWx; // windowX End
80  HDRSP.ccdye = DAS.Paramini.ccdWy; // windowY End
81 
82  HDRSP.ccdvbin = DAS.Paramini.VBin; //Vertical Binning
83  HDRSP.ccdnr = (DAS.Paramini.ccdWy - DAS.Paramini.ccdYoff) / DAS.Paramini.VBin; //Row Number
84  HDRSP.DATE = DIL.Img.Mat[Server].DATE;
85  HDRSP.TIME = DIL.Img.Mat[Server].TIME;
86 
87 
88  if(mod) //binary
89  {
90  if (DAS.Paramini.EqType == TROPOGAS || DAS.Paramini.EqType == SPATRAM3 || DAS.Paramini.EqType == GASCODNG4)
91  sprintf(buf, "%s",Mir_CodeTropo[MIS.Pos]);
92  else
93  sprintf(buf, "%s",Mir_Code[MIS.Pos]);
94 
95 
96  sprintf(HDRSP.binmirpos, buf);
97  }
98  else
99  {
100  if (DAS.Paramini.EqType == TROPOGAS || DAS.Paramini.EqType == SPATRAM3 || DAS.Paramini.EqType == GASCODNG4)
101  sprintf(buf, "%s",Mir_CodeTropo[MIS.Pos]);
102  else
103  sprintf(buf, "%s",Mir_Code[MIS.Pos]);
104 
105 
106  sprintf(HDRSP.mirpos, buf);
107  }
108  HDRSP.lambda = wl[GRS.Wl];
109 
110  if(mod)
111  {
112  if(DAS.Paramini.EqType == LIS_)
113  sprintf(HDRSP.binfilpos, Fil_Code8H[FWS.Pos]);
114  else
115  {
116  if(DAS.Paramini.EqType == TROPOGAS)
117  sprintf(HDRSP.binfilpos, Fil_CodeTropo[FWS.Pos]);
118  else if (DAS.Paramini.EqType == GASCODNG2 || DAS.Paramini.EqType == GASCODNG3 || DAS.Paramini.EqType == GASCODNG4)
119  sprintf(HDRSP.binfilpos, Fil_CodeNG[FWS.Pos]);
120  else if (DAS.Paramini.EqType == GASCODNG1)
121  sprintf(HDRSP.binfilpos, Fil_CodeNG1[FWS.Pos]);
122  else
123  sprintf(HDRSP.binfilpos, Fil_Code[FWS.Pos]);
124  }
125  }
126  else
127  {
128  if(DAS.Paramini.EqType == LIS_)
129  HDRSP.filpos = Fil_Code8H[FWS.Pos];
130  else
131  {
132  if(DAS.Paramini.EqType == TROPOGAS)
133  HDRSP.filpos = Fil_CodeTropo[FWS.Pos];
134  else if (DAS.Paramini.EqType == GASCODNG2 || DAS.Paramini.EqType == GASCODNG3 || DAS.Paramini.EqType == GASCODNG4)
135  HDRSP.filpos = Fil_CodeNG[FWS.Pos];
136  else if (DAS.Paramini.EqType == GASCODNG1)
137  sprintf(HDRSP.binfilpos, Fil_CodeNG1[FWS.Pos]);
138  else
139  HDRSP.filpos = Fil_Code[FWS.Pos];
140  }
141  }
142 
143  HDRSP.ccdtemp = DAS.Marconi.CCDTemp;
144 
145  HDRSP.ccdtexp = (float) TExp;
146  HDRSP.imgmax = (unsigned int) DIL.Img.Mat[s].High;
147  HDRSP.imgmin = (unsigned int) DIL.Img.Mat[s].Low;
148 /* HDRSP.imgavg =
149  HDRSP.imgsig =
150 */
151  HDRSP.dummy1 = (int)(ZST.realzenpos * 100); //elevation angle
152  HDRSP.dummy2 = (int)(AST.realazpos * 100); //azimuth angle
153 
154 
155  if(DAS.DOption.filtertype == FDOUBLE)
156  HDRSP.dummy3 = NDFWS.Pos;
157  else if(DAS.DOption.filtertype == FPLUS)
158  HDRSP.dummy3 = TMFlg;
159  else
160  HDRSP.dummy3 = 0;
161 
162  if(DAS.DOption.measmode == 1) //measuring with average
163  {
164  if(DAS.DOption.avgmode == 0)
165  HDRSP.dummy4 = N_AVG; // number of measurements in fixed number of measurements
166  else
167  HDRSP.dummy4 = N_RM; // number of measurements in fixed exposure time
168  }
169  else
170  HDRSP.dummy4 = 0;
171 
172  if(mod) //binary
173  sprintf(HDRSP.bincomment, "Default Comment");
174  else
175  HDRSP.comment = "Default Comment ";
176 
177  sprintf(HDRSP.s_name, "%s", DAS.DGeo.s_name);
178  HDRSP.s_latitude = (float)DAS.DGeo.latitude;
179  HDRSP.s_longitude = (float)DAS.DGeo.longitude;
180  HDRSP.s_alt = (unsigned int) DAS.DGeo.alititude;
181  HDRSP.s_sza = (float)S_zenetr;
182 
183 
184  //Image Type, Camera Type, CCD Type, CCDWx, CCDWy, CCDXoff, CCDYoff, VBin
185  //Date, Time, MirrorPos, Lambda, Filter, TempCCD, T_Exp,
186  //Max, Min, Media, Deviazione, Dummy1, Dummy2, Dummy3, Dummy4
187  //Riga di commento
188 
189 }
190 
191 
192 void D_Plot(void)
193 {
194 
195 
196  ViewPlot(1);
197  //MDCreateSh_Plot();
198 
199 }
200 
201 void DB_MirrorX(int w)
202 {
203  int x, y, mx = DIL.Img.Idx[w] - 30;
204  U16 *m, t;
205  float *M, T;
206 
207  if(DAS.DOption.measmode == 0) //Single
208  D_VRange("Mirroring X axis..", 0, 0, 222, 235);
209 
210  if(DIL.Img.Mat[w].type == FLTIMG)
211  for(y = 0; y < DIL.Img.Idy[w]; y++)
212  {
213  M = &DIL.Img.Mat[w].Fmat[(long) y * (mx + 30)];
214  for(x = 0; x <= mx / 2; x++)
215  {
216  T = M[x];
217  M[x] = M[mx - x];
218  M[mx - x] = T;
219  }
220  if(DAS.DOption.measmode == 0) //Single
221  {
222  if((y % 20) == 0 && y > 0)
223  D_VRange(NULL, (long) y * 100 / DIL.Img.Idy[w],0, 222, 235);
224  }
225 
226  }
227  else
228  for(y = 0; y < DIL.Img.Idy[w]; y++)
229  {
230  m = &DIL.Img.Mat[w].Imat[(long) y * (mx + 30)];
231  for(x = 0; x <= mx / 2; x++)
232  {
233  t = m[x];
234  m[x] = m[mx - x];
235  m[mx - x] = t;
236  }
237  if(DAS.DOption.measmode == 0) //Single
238  {
239  if((y % 20) == 0 && y > 0)
240  D_VRange(NULL, (long) y * 100 / DIL.Img.Idy[w],0, 222, 235);
241  }
242  }
243  if(DAS.DOption.measmode == 0) //Single
244  D_VRange(NULL, 100, 0,0,0);
245 }
246 
247 
248 
249 
250 
251 
252 float maxar,maxtt, minar,mintt; //Max AutoRanging, Max Test Time, Min AutoRanging, Min Test Time
253 
254 // Shutter delay opening (be carefull it has to be < 1000 in AVG mode)
255 int ShuttDelay = 0;//10;
256 /* New Autoranging procedure */
257 
258 
259 
260 
262 {
263 
264  if(DAS.DOption.filtertype == FDOUBLE)
265  {
266  if(DAS.Paramini.EqType == SPATRAM2 || DAS.Paramini.EqType == SPATRAMPLUS || DAS.Paramini.EqType == SPATRAM3)
267  {
268  if (NDFWS.Pos == 0)
269  SD_NDFilterW(1); //ND1
270  else if (NDFWS.Pos == 1)
271  SD_NDFilterW(2); //ND2
272  else if (NDFWS.Pos == 2)
273  SD_NDFilterW(3); //ND3
274  else if (NDFWS.Pos == 3)
275  SD_NDFilterW(4); //ND4
276  }
277  }
278  if(DAS.DOption.filtertype == FPLUS)
279  {
280 // if(DAS.Paramini.EqType == GASCODNG1)
281 // {
282  SD_FNTorqueM(1);//ND1
283  delay(700);
284 // }
285  }
286  if(DAS.DOption.filtertype == FSIMPLE)
287  {
288  if (DAS.Paramini.EqType == GASCODNG3 )
289  {
290  //*Fil_CodeNG[] = {{"NULL"}, {"B12 "}, {"B12F"}, {"B39 "}, {"B39F"}, {"UG5 "}};
291  if ((wl[GRS.Wl] == 3850))
292  {
293  if ( FWS.Pos == 1); //B12
294  SD_FilterW(2); //B12F
295  if ( FWS.Pos == 3); //B39
296  SD_FilterW(4); //B39F
297  }
298  if (wl[GRS.Wl] == 4358)
299  {
300  //The Wl 4358 can be used with 2 different filters:BG12 and BG39, with the ND filters also
301  if (FWS.Pos == 0) //B12
302  SD_FilterW(2); //B12F
303  if (FWS.Pos == 1) //B12
304  SD_FilterW(2); //B12F
305  if (FWS.Pos == 3) //B39
306  SD_FilterW(4); //B39F
307  }
308  if (wl[GRS.Wl] == 4861)
309  {
310  if ((FWS.Pos == 1)) //B12
311  SD_FilterW(2); //B12F
312  if ((FWS.Pos == 3)) //B39
313  SD_FilterW(4); //B39F
314  }
315  if ((wl[GRS.Wl] == 5332) )
316  {
317  if ((FWS.Pos == 1)) //B12
318  SD_FilterW(2); //B12F
319  if ((FWS.Pos == 3)) //B39
320  SD_FilterW(4); //B39F
321  }
322  if ((wl[GRS.Wl] == 4610) )
323  {
324  if ((FWS.Pos == 1)) //B12
325  SD_FilterW(2); //B12F
326  if ((FWS.Pos == 3)) //B39
327  SD_FilterW(4); //B39F
328  }
329  }
330  }
331 }
332 
333 
334 double AutoR_TestMeas(int mod, double texpmin, double texpar, float level, float levelinf)
335 {
336 
337  char buf[80];
338  int k;
339  int FlgSat = 1;
340 
341  double FlagTExp = 2.0;
342 
343 // double TExpLast;
344  double temp = 0;
345  int Lim_supinf = 0;
346 
347  float treshold = 2;
348 
349  float lowval = 700;
350  float Offset = 0;
351  int numrip = 0;
352 
353 
354  /****************************************************************************************/
355  // To take into account the Blind Pixels
356  //RC = IDY/2;
357  if (DC_CCD.dx - (DAS.Paramini.ccdWx +1) != 0)
358  {
359  BlindPix = DC_CCD.dx - DAS.Paramini.ccdWx-1 ;
360  }
361  /*****************************************************************************************/
362 
363 
364  if (mod)
365  TExp = texpar;
366  else
367  TExp = texpmin;
368 
369  //delay(ShuttDelay);
370  DB_GetCCD(1);
371  //delay(ShuttDelay);
372  // Scan Max Min
376  // Regen the histogram
377  DI_Regen(Server);
378 
379 //Check the obtained signal
380  // Max value greater than the superior limit
381  if (DIL.Img.Mat[Server].High >= level)
382  {
383  // enter in the first do cicle where the exp. time is iteratively reduced
384  //(until TExpMin * treshold)
385  do
386  {
387  TExp = TExp / FlagTExp;
388 
389  sprintf(buf, "%.3lf", TExp);
390  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
391  Status("Autoranging >");
392 
393  //Measure
394  //delay(ShuttDelay);
395  DB_GetCCD(1);
396  //delay(ShuttDelay);
397  // Scan Max Min
401  // Regen the histogram
402  DI_Regen(Server);
403 
404  if(DIL.Img.Mat[Server].High > level && numrip >= 4)
405  {
406  //Exit DO loop and IF condition with TExp = TExpMin
407  //in order to execute the following IF
408  TExp = texpmin;
409  return TExp;
410  }
411  // Increse FlagTExp (in order to reduce the TExp)
412  FlagTExp = FlagTExp * 2.5;
413  numrip++;
414 
415  } while(DIL.Img.Mat[Server].High > level );
416 
417  }
418  // Max value lower than the inferior limit
419  else if (DIL.Img.Mat[Server].High < levelinf)
420  {
421  // enter in the second do cicle where the exp. time is iteratively increased
422  // the cycle is repeted at maximum 5 times
423  k=0;
424  do
425  {
426  TExp = TExp * FlagTExp;
427  k++;
428  sprintf(buf, "%.3lf", TExp);
429  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
430  Status("Autoranging >");
431  //Measure
432  DB_GetCCD(1);
433  // Scan Max Min
437  // Regen the histogram
438  DI_Regen(Server);
439  // Increase FlagTExp (in order to increase the TExp)
440  FlagTExp = FlagTExp * 2.5;
441  } while((DIL.Img.Mat[Server].High < levelinf) && k < 5);
442 
443  sprintf(buf, "%.3lf", TExp);
444  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
445  }
446  else //No needs for others attempts
447  {
448  if (mod)
449  TExp = TExp;
450  else
451  TExp = TExp;
452  sprintf(buf, "%.3lf", TExp);
453  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
454 
455  }
456  return TExp;
457 
458 }
459 
460 
461 
462 /*
463 double AutoR_No_Shutt(void)
464 {
465 
466 
467  */
468 double AutoR_No_Shutt(void)
469 {
470 
471  double TExpAutoR = 0.2;
472  double TExpMin=0.05;
473 // double TExpTest;
474  float treshold = 2;
475  float Level = 55000;
476  float LevelInf = 1000;
477  int numrip, maxrep;
478  struct time ACTime;
479  struct date ACDate;
480  struct tm today;
481  double TExpZero;
482  double TExpLast;
483 
484 
485  GetDateTime(&ACDate, &ACTime, &today);
486 
487 
488 /* if (S_zenetr>= 85 & S_zenetr<= 93) // & ACTime.ti_hour <12)
489  Level = 55000;
490  else
491  Level = 55000;
492 */
493 
494  /******************************************************************************************
495  If the Shutter Uniblitz VS25 is present the minimum Exposure time is 0.02 sec
496  otherwise (if the shutter is NOT installed) the minimum exposure time depends on the CCD
497  Reading Time. This last is a function of the binning parameters and it can be calculated
498  according to the formula (for HIRES_IV):
499 
500  RT (in ms)= (vp/vb)*Kv + (vp/vb)*(hp/hb)*Kh
501  where:
502  vp = vertical pixels number
503  vb = vertical binning
504  Kv = 0.024 for hires
505  hp = horizontal pixels number
506  hb = horizontal binning
507  Kh = 0.0125
508 
509  For High values of Vertical Binning the Reading Time (RT) is very short
510  (i.e for VertBin of41-the maximum supported by HiRESIV-RT is less than 0.09 sec)
511  therefore the excess of signal due to the over-exposition of the sensor
512  can be neglected.
513  ******************************************************************************************/
514  //Set Minimum exposure time
515  TExpMin = treshold * ((((float)DC_CCD.dy / (float)DAS.Paramini.VBin) * 0.024 + ((float)DC_CCD.dy /
516  (float)DAS.Paramini.VBin) * DC_CCD.dx * 0.0125) / 1000);
517 
518 
519 
520  if(DAS.DOption.filtertype == FDOUBLE)
521  maxrep = 6;
522  else
523  maxrep = 2;
524 
525  numrip = 1;
526 
527 
528  /*************************************************/
529  Level = (float) DAS.DOption.maxlvlar;
530  /*************************************************/
531  /* zero measurement: with TExp = Tmin (No necessity for Loop over the time
532  since this is the minimum time). The cycle is repeated since when the maximum
533  is at least half of the MAXIMUM set signal (LEVEL=60000)*/
534  TExp = TExpMin;
535 
536  do
537  {
538  //delay(ShuttDelay);
539  DB_GetCCD(1);
540  //delay(ShuttDelay);
541  // Scan Max Min
545  // Regen the histogram
546  DI_Regen(Server);
547 
548  if(DIL.Img.Mat[Server].IHigh >= Level/4)
549  {
551  numrip++;
552  }
553 
554  }
555  while ((DIL.Img.Mat[Server].IHigh >= Level/4) && numrip <= maxrep);
556 
557 // Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
559  mintt = DIL.Img.Mat[Server].ILow;
560  TExpZero = TExp;
561 
562 
563 
564  //The Zero point is calculated and the filter positioned
565  //Now the measure (It) for computation of TExp, with TExpAutoR.
566  // decrease the Exposure time (till when the time is the double of the Minimum Exposure time)
567  maxrep = 6;
568  numrip = 1;
569  TExpAutoR = 4*TExpMin;
570  TExp = TExpAutoR;
571 
572  do
573  {
574  //delay(ShuttDelay);
575  DB_GetCCD(1);
576  //delay(ShuttDelay);
577  // Scan Max Min
581  // Regen the histogram
582  DI_Regen(Server);
583 
584  if(DIL.Img.Mat[Server].IHigh >= Level)
585  {
586  TExp = TExp / 1.3;
587  numrip++;
588  }
589  if(DIL.Img.Mat[Server].IHigh - DIL.Img.Mat[Server].ILow <= 100)
590  {
591  TExp = TExp * 3;
592  numrip++;
593  }
594  }
595  while( (DIL.Img.Mat[Server].IHigh >= Level && numrip < maxrep)
596  || ((DIL.Img.Mat[Server].IHigh - DIL.Img.Mat[Server].ILow <= 100) && numrip < maxrep) );
597 
598 // Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
600  minar = DIL.Img.Mat[Server].ILow;
601  TExpLast = TExp;
602 
603 
604  TExp = ( Level * ((1000*(TExpLast- TExpZero))/( maxar - maxtt )))/ 1000;
605 
606  if(TExp <= TExpMin)
607  TExp = TExpMin;
608 
609  if( DAS.Paramini.maxtexp == 0.0 )
610  TExp = TExp;
611  else
612  {
613  if (TExp >= DAS.Paramini.maxtexp)
614  TExp = DAS.Paramini.maxtexp;
615  else
616  TExp= TExp;
617  }
618 
619 
620  return TExp;
621 
622 
623 
624 
625 }
626 
628 {
629 
630  double TExpAutoR = 0.2;
631  double TExpMin;
632  double TExpZero;
633  float Level = 62000;
634  float LevelInf = 1000;
635  int numrip, maxrep;
636  float lowval = 700;
637  float Offset = 0;
638  double TExpLast;
639 // double TExpTest;
640 
641  struct time ACTime;
642  struct date ACDate;
643  struct tm today;
644 
645  GetDateTime(&ACDate, &ACTime, &today);
646 
647 
648 // if ((S_zenetr>= 85 & S_zenetr<= 94) & ACTime.ti_hour <12)
649 // Level = 57000;
650 // else
651  Level = 58000;
652  if (DAS.Paramini.EqType == GASCODNG2 )
653  Level = 54000;
654  if (DAS.Paramini.EqType == TROPOGAS)
655  Level = 50000;
656 
657 
658  if (DAS.DOption.shuttertype == VS25)
659  TExpMin = 0.02;
660  if(DAS.DOption.shuttertype == ROTSHUT)
661  TExpMin = 0.045;
662  if (DAS.DOption.shuttertype == TORQUEM)
663  TExpMin = 0.08; //per NG2//0.1; //0.08;
664 
665  //depending on the installed filter module set the max number of repetition
666  if(DAS.DOption.filtertype == FDOUBLE)
667  maxrep = 6;
668  else
669  maxrep = 2;
670 
671  numrip = 1;
672 
673  Level = (float)DAS.DOption.maxlvlar;
674 
675 
676  /****************************************************************************************/
677  // To take into account the Blind Pixels
678  //RC = IDY/2;
679  if (DC_CCD.dx - (DAS.Paramini.ccdWx +1) != 0)
680  {
681  BlindPix = DC_CCD.dx - DAS.Paramini.ccdWx-1 ;
682  }
683  /*****************************************************************************************/
684 
685 
686  /* zero measurement: with TExp = Tmin (No necessity for Loop over the time
687  since this is the minimum time). The cycle is repeated since when the maximum
688  is at least half of the MAXIMUM set signal (LEVEL=60000)*/
689  TExp = TExpMin;
690 
691  do
692  {
693  //delay(ShuttDelay);
694  DB_GetCCD(1);
695  //delay(ShuttDelay);
696  // Scan Max Min
700  // Regen the histogram
701  DI_Regen(Server);
702 
703  if(DIL.Img.Mat[Server].IHigh >= Level/3)
704  {
706  numrip++;
707  }
708  }
709  while ((DIL.Img.Mat[Server].IHigh >= Level/3) && numrip <= maxrep);
710 
711  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
713  mintt = DIL.Img.Mat[Server].ILow;
714  TExpZero = TExp;
715 
716 
717 
718  //The Zero point is calculated and the filter positioned
719  //Now the measure (It) for computation of TExp, with TExpAutoR.
720  // decrease the Exposure time (till when the time is the double of the Minimum Exposure time)
721  maxrep = 6;
722  numrip = 1;
723 
724  TExpAutoR = 3*TExpMin;
725 
726  if (DAS.Paramini.EqType == GASCODNG2 ) //with the torquemotor shutter
727  TExpAutoR = 2*TExpMin;
728 
729  TExp = TExpAutoR;
730 
731  do
732  {
733  //delay(ShuttDelay);
734  DB_GetCCD(1);
735  //delay(ShuttDelay);
736  // Scan Max Min
740  // Regen the histogram
741  DI_Regen(Server);
742 
743  if(DIL.Img.Mat[Server].IHigh >= Level)
744  {
745  TExp = TExp/1.3;
746  numrip++;
747  }
748 // if(DIL.Img.Mat[Server].ILow <= LevelInf)
749 // TExp = TExp*1.1;
750  else if( (DIL.Img.Mat[Server].IHigh - maxtt) <= 800)
751  {
752  TExp = TExpMin * (4 + numrip);
753  numrip++;
754  }
755  }
756  while( ((DIL.Img.Mat[Server].IHigh >= Level || (DIL.Img.Mat[Server].IHigh - maxtt) <= 800 ) && (numrip < maxrep) ) );
757  //while(DIL.Img.Mat[Server].IHigh >= Level && numrip < maxrep && (DIL.Img.Mat[Server].IHigh - maxtt) <= 800 );
758 // while (TExp >= 2* TExpMin || numrip < maxrep);
759 
760  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
762  minar = DIL.Img.Mat[Server].ILow;
763  TExpLast = TExp;
764 
765  //Test 21/04/2015
766  if ( (maxar - maxtt) > 0)
767  {
768  TExp = (double)( Level * ((10000*(TExpLast- TExpZero))/( fabs(maxar - maxtt) )))/ 10000;
769  if( DAS.Paramini.maxtexp == 0.0 )
770  TExp = TExp;
771  else
772  {
773  if (TExp >= DAS.Paramini.maxtexp)
774  TExp = DAS.Paramini.maxtexp;
775  else
776  TExp= TExp;
777  }
778  }
779 // else if ( (maxar - maxtt) <=0)
780 // {
781 // TExp = TExpMin;
782 // }
783 
784  else
785  {
786  if( DAS.Paramini.maxtexp != 0.0 )
787  TExp = DAS.Paramini.maxtexp;
788  else
789  TExp = 1;
790 
791  //TExp = ( Level * ((10000*(TExpLast- TExpZero))/( maxar - maxtt+1 )))/ 10000;
792  }
793 
794 
795  if(TExp <= TExpMin )
796  TExp = TExpMin;
797 
798 
799 
800  return TExp;
801 
802 
803 
804 
805  /*****************************************************************************/
806  //do the First test Measurement to fix the zero point (with TExpMin)
807  TExpZero = AutoR_TestMeas(0, TExpMin, TExpAutoR, Level, LevelInf);
808  if (DIL.Img.Mat[Server].High >= Level)
809  {
810  //depending on the installed filter module set the max number of repetition
811  if(DAS.DOption.filtertype == FDOUBLE)
812  maxrep = 6;
813  else
814  maxrep = 2;
815 
816  numrip = 1;
817  do
818  {
820  TExpZero = AutoR_TestMeas(0, TExpMin, TExpAutoR, Level, LevelInf);
821  numrip++;
822 
823  if(DIL.Img.Mat[Server].High<= Level && TExpZero >= TExpMin)
824  break;
825  }while ((TExpZero <= TExpMin) && (numrip < maxrep));
826  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
828  mintt = DIL.Img.Mat[Server].ILow;
829  }
830  else
831  {
832  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
833  //TExpTest = TExp;
835  mintt = DIL.Img.Mat[Server].ILow;
836 
837  }
838  /*****************************************************************************/
839 
840  /*****************************************************************************/
841 
842 
843  /******************************************************************************/
844  /******************************************************************************/
845  //do the Test Measurement with TExpAutoR
846  //TExpAutoR = 2 * TExpMin;
847  if (TExpZero >=TExpAutoR)
848  TExpAutoR= TExpZero*1.1;
849  TExpLast = AutoR_TestMeas(1, TExpMin, TExpAutoR, Level, LevelInf);
850  if (DIL.Img.Mat[Server].High >= 65535 || TExpLast <= TExpZero)
851  {
852  //depending on the installed filter module set the max number of repetition
853  if(DAS.DOption.filtertype == FDOUBLE)
854  maxrep = 6;
855  else
856  maxrep = 2;
857 
858  numrip = 1;
859  do
860  {
862  TExpZero = AutoR_TestMeas(0, TExpMin, TExpAutoR, Level, LevelInf);
864  mintt = DIL.Img.Mat[Server].ILow;
865 
866  TExpLast = AutoR_TestMeas(1, TExpMin, TExpAutoR, Level, LevelInf);
867  numrip++;
868 
869  }while ((DIL.Img.Mat[Server].High >= Level || TExpLast <= TExpZero) && numrip < maxrep);
870  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
872  minar = DIL.Img.Mat[Server].ILow;
873  }
874  else
875  {
876  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
878  minar = DIL.Img.Mat[Server].ILow;
879 
880  }
881 
882  LevelInf = lowval + Offset;
883 
884 
885  /******************************************************************************/
886  /******************************************************************************/
887 
888  //Final TExp Computation
889  //TExpLast = TExp;
890 
891  TExp = ( Level * ((1000*(TExpLast- TExpZero))/( maxar - maxtt )))/ 1000;
892 
893  return TExp;
894 
895 }
896 
897 /* New Autoranging procedure */
898 
899 double AutoRange(int mod)
900 {
901  char buf[80];
902  int i;
903  int FlgSat = 1;
904  double FlagTExp = 2.0;
905  double TExpAutoR = 0.2;
906  //double TExpMin=0;
907 // double TExpLast;
908  double temp = 0;
909  int Lim_supinf = 0;
910 
911  float Level = 55000;
912 // float LevelInf;
913  float lowval = 700;
914  float Offset = 0;
915  static int numrip = 0;
916 
917  /*****************************************************************************************/
918  // For DEMO Mode
919  if(DAS.Flag.exemode == DBDEMO || DAS.Flag.exemode == TESTSZAMODE)
920  {
921  for(i=0;i<3; i++)
922  {
923  delay(ShuttDelay);
924  TExp =(float) (3 - i);
925  DB_GetCCD(1);
926 
927  }
928  TExp = 1.2;
929  return TExp;
930  }
931  /*****************************************************************************************/
932 
933 
934 
935  /******************************************************************************************
936  If the Shutter Uniblitz VS25 is present the minimum Exposure time is 0.02 sec
937  otherwise (if the shutter is NOT installed) the minimum exposure time depends on the CCD
938  Reading Time. This last is a function of the binning parameters and it can be calculated
939  according to the formula (for HIRES_IV):
940 
941  RT (in ms)= (vp/vb)*Kv + (vp/vb)*(hp/hb)*Kh
942  where:
943  vp = vertical pixels number
944  vb = vertical binning
945  Kv = 0.024 for hires
946  hp = horizontal pixels number
947  hb = horizontal binning
948  Kh = 0.0125
949 
950  For High values of Vertical Binning the Reading Time (RT) is very short
951  (i.e for VertBin of41-the maximum supported by HiRESIV-RT is less than 0.09 sec)
952  therefore the excess of signal due to the over-exposition of the sensor
953  can be neglected.
954  ******************************************************************************************/
955  Status("Autoranging");
956 if (S_zenetr>= 100 & DAS.Paramini.EqType == GASCODNG1 & MIS.Pos != 2)
957 {
958  TExp = 300.0;
959 }
960 else
961 {
962  if (DAS.DOption.emshutter)
963  {
965  }
966  else
967  {
968  TExp = AutoR_No_Shutt();
969  }
970 }
971  /*****************************************************************************************/
972 
973 
974  sprintf(buf, "%.3lf", TExp);
975  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
976 
977  Status("Autoranging DONE");
978  return TExp;
979 
980 
981 }
982 
983 
984 /*******************************/
985 /*double AutoRangeFeb2013(int mod)
986 {
987  char buf[80];
988  int RC, i, k;
989  int FlgSat = 1;
990  double FlagTExp = 2.0;
991  double TExpAutoR = 0.2;
992  double TExpMin=0;
993  double TExpLast;
994  double temp = 0;
995  int Lim_supinf = 0;
996 
997  float Level = 60000;
998  float LevelInf;
999  float lowval = 700;
1000  float Offset = 0;
1001 
1002  struct time ACTime;
1003  struct date ACDate;
1004  struct tm today;
1005 
1006  GetDateTime(&ACDate, &ACTime, &today);
1007 
1008  //gettime(&ACTime);
1009 
1010  if (S_zenetr>= 85 & S_zenetr<= 93) // & ACTime.ti_hour <12)
1011  Level = 55000;
1012  else
1013  Level = 55000;
1014 
1015  /******************************************************************************************
1016  If the Shutter Uniblitz VS25 is present the minimum Exposure time is 0.02 sec
1017  otherwise (if the shutter is NOT installed) the minimum exposure time depends on the CCD
1018  Reading Time. This last is a function of the binning parameters and it can be calculated
1019  according to the formula (for HIRES_IV):
1020 
1021  RT (in ms)= (vp/vb)*Kv + (vp/vb)*(hp/hb)*Kh
1022  where:
1023  vp = vertical pixels number
1024  vb = vertical binning
1025  Kv = 0.024 for hires
1026  hp = horizontal pixels number
1027  hb = horizontal binning
1028  Kh = 0.0125
1029 
1030  For High values of Vertical Binning the Reading Time (RT) is very short
1031  (i.e for VertBin of41-the maximum supported by HiRESIV-RT is less than 0.09 sec)
1032  therefore the excess of signal due to the over-exposition of the sensor
1033  can be neglected.
1034  ******************************************************************************************/
1035 /* Status("Autoranging");
1036  if (DAS.DOption.emshutter)
1037  {
1038  if (DAS.DOption.shuttertype == VS25)
1039  TExpMin = 0.02;
1040  if(DAS.DOption.shuttertype == ROTSHUT)
1041  TExpMin = 0.045;
1042  if (DAS.DOption.shuttertype == TORQUEM)
1043  {
1044  TExpMin = 0.08;
1045  TExpAutoR = 0.2;
1046  }
1047  }
1048  else
1049  {
1050  TExpAutoR = 0.3;
1051 
1052  TExpMin = ((((float)DC_CCD.dy / (float)DAS.Paramini.VBin) * 0.024 + ((float)DC_CCD.dy / (float)DAS.Paramini.VBin) * DC_CCD.dx * 0.0125) / 1000);
1053  if (TExpAutoR < TExpMin)
1054  TExpAutoR = TExpMin;
1055  }
1056  /****************************************************************************************/
1057  // To take into account the Blind Pixels
1058 /* RC = IDY/2;
1059  if (DC_CCD.dx - (DAS.Paramini.ccdWx +1) != 0)
1060  {
1061  BlindPix = DC_CCD.dx - DAS.Paramini.ccdWx-1 ;
1062  }
1063  /*****************************************************************************************/
1064  /*****************************************************************************************/
1065  // For DEMO Mode
1066 /* if(DAS.Flag.exemode == DBDEMO || DAS.Flag.exemode == TESTSZAMODE)
1067  {
1068  for(i=0;i<3; i++)
1069  {
1070  delay(ShuttDelay);
1071  TExp =(float) (3 - i);
1072  DB_GetCCD(1);
1073 
1074  }
1075  TExp = 1.2;
1076  return TExp;
1077  }
1078  /*****************************************************************************************/
1079  /*****************************************************************************************/
1080 /*if (S_zenetr>= 100 & DAS.Paramini.EqType == GASCODNG1 & MIS.Pos != 2)
1081 {
1082  TExp = 300.0;
1083 }
1084 else
1085 {
1086  /****** SHUTTER INSTALLED ******/
1087 /* if (DAS.DOption.emshutter)
1088  {
1089 
1090  //set offset
1091  if(DAS.Paramini.EqType == LIS_)
1092  Offset = 3000;
1093  else if (DAS.Paramini.EqType == TROPOGAS)
1094  Offset = 800;
1095  else if(DAS.Paramini.EqType == SPATRAM2 ||
1096  DAS.Paramini.EqType == GASCODNG1 ||
1097  DAS.Paramini.EqType == GASCODNG2 ||
1098  DAS.Paramini.EqType == GASCODNG3 ||
1099  DAS.Paramini.EqType == SPATRAMPLUS)
1100  {
1101  //First Test Measurement with minimum exposure time ( to set the zero point)
1102  //Measure
1103  TExp = TExpMin;
1104  DB_GetCCD(1);
1105  // Scan Max Min
1106  DB_ScanMaxMin(Server, &DIL.Img.Mat[Server].High, &DIL.Img.Mat[Server].Low, BlindPix);
1107  DIL.Img.Mat[Server].ILow = DIL.Img.Mat[Server].Low ;
1108  DIL.Img.Mat[Server].IHigh = DIL.Img.Mat[Server].High ;
1109  // Regen the histogram
1110  DI_Regen(Server);
1111  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
1112  maxtt = DIL.Img.Mat[Server].IHigh;
1113  mintt = DIL.Img.Mat[Server].ILow;
1114  //CCD saturated --> ND Filter
1115  if (DIL.Img.Mat[Server].IHigh >= 65535)
1116  {
1117  if(DAS.DOption.filtertype = FDOUBLE)
1118  {
1119  if(DAS.Paramini.EqType == SPATRAM2)
1120  SD_NDFilterW(2); //ND1
1121  }
1122  if(DAS.DOption.filtertype = FPLUS)
1123  {
1124  if(DAS.Paramini.EqType == GASCODNG1)
1125  {
1126  SD_FNTorqueM(1);//ND1
1127  delay(700);
1128  }
1129  //SD_FilterW(2); //ND1
1130  }
1131 
1132  //First Test Measurement with minimum exposure time
1133  //Measure
1134  TExp = TExpMin;
1135  DB_GetCCD(1);
1136  // Scan Max Min
1137  DB_ScanMaxMin(Server, &DIL.Img.Mat[Server].High, &DIL.Img.Mat[Server].Low, BlindPix);
1138  DIL.Img.Mat[Server].ILow = DIL.Img.Mat[Server].Low ;
1139  DIL.Img.Mat[Server].IHigh = DIL.Img.Mat[Server].High ;
1140  // Regen the histogram
1141  DI_Regen(Server);
1142 
1143  Offset = DIL.Img.Mat[Server].IHigh;// - DIL.Img.Mat[Server].ILow;
1144  maxtt = DIL.Img.Mat[Server].IHigh;
1145  mintt = DIL.Img.Mat[Server].ILow;
1146  }
1147  }
1148  else
1149  {
1150  Offset = 3000;
1151  }
1152  LevelInf = lowval + Offset;
1153 
1154  }
1155 
1156  //do the First test Measurement
1157  //Measure
1158  TExp = TExpAutoR;
1159  //delay(ShuttDelay);
1160  DB_GetCCD(1);
1161  //delay(ShuttDelay);
1162  // Scan Max Min
1163  DB_ScanMaxMin(Server, &DIL.Img.Mat[Server].High, &DIL.Img.Mat[Server].Low, BlindPix);
1164  DIL.Img.Mat[Server].ILow = DIL.Img.Mat[Server].Low ;
1165  DIL.Img.Mat[Server].IHigh = DIL.Img.Mat[Server].High ;
1166  // Regen the histogram
1167  DI_Regen(Server);
1168 
1169  //Check the obtained signal
1170 
1171  // Max value greater than the superior limit
1172  if (DIL.Img.Mat[Server].High >= Level)
1173  {
1174  // enter in the first do cicle where the exp. time is iteratively reduced (until TExpMin * 2)
1175  do
1176  {
1177  TExp = TExpAutoR / FlagTExp;
1178 
1179  sprintf(buf, "%.3lf", TExp);
1180  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
1181  Status("Autoranging >");
1182 
1183  //Measure
1184  //delay(ShuttDelay);
1185  DB_GetCCD(1);
1186  //delay(ShuttDelay);
1187  // Scan Max Min
1188  DB_ScanMaxMin(Server, &DIL.Img.Mat[Server].High, &DIL.Img.Mat[Server].Low, BlindPix);
1189  DIL.Img.Mat[Server].ILow = DIL.Img.Mat[Server].Low ;
1190  DIL.Img.Mat[Server].IHigh = DIL.Img.Mat[Server].High ;
1191  // Regen the histogram
1192  DI_Regen(Server);
1193 
1194  if(TExp <= TExpMin*2)
1195  {
1196  //Exit DO loop and IF condition with TExp = TExpMin
1197  //in order to execute the following IF
1198  TExp = TExpMin;
1199  break;
1200  }
1201  // Increse FlagTExp (in order to reduce the TExp)
1202  FlagTExp = FlagTExp * 2.5;
1203 
1204  } while(DIL.Img.Mat[Server].High > Level);
1205 
1206  }
1207  // Max value lower than the inferior limit
1208  else if (DIL.Img.Mat[Server].High < LevelInf)
1209  {
1210  // enter in the second do cicle where the exp. time is iteratively increased
1211  // the cycle is repeted at maximum 5 times
1212  k=0;
1213  do
1214  {
1215  TExp = TExpAutoR * FlagTExp;
1216  k++;
1217  sprintf(buf, "%.3lf", TExp);
1218  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
1219  Status("Autoranging >");
1220  //Measure
1221  DB_GetCCD(1);
1222  // Scan Max Min
1223  DB_ScanMaxMin(Server, &DIL.Img.Mat[Server].High, &DIL.Img.Mat[Server].Low, BlindPix);
1224  DIL.Img.Mat[Server].ILow = DIL.Img.Mat[Server].Low ;
1225  DIL.Img.Mat[Server].IHigh = DIL.Img.Mat[Server].High ;
1226  // Regen the histogram
1227  DI_Regen(Server);
1228  // Increase FlagTExp (in order to increase the TExp)
1229  FlagTExp = FlagTExp * 2.5;
1230  } while((DIL.Img.Mat[Server].High < LevelInf) && k < 5);
1231 
1232  sprintf(buf, "%.3lf", TExp);
1233  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
1234  }
1235  else
1236  {
1237  TExp = TExpAutoR;
1238  sprintf(buf, "%.3lf", TExp);
1239  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
1240 
1241  }
1242  /**********************************/
1243  //TO BE CHECKED
1244 /* if((mod == 0) & (TExp == TExpMin))
1245  {
1246  Status("3");
1247  if(DAS.Paramini.EqType == LIS_)
1248  SD_FilterW(5); //ND1.3
1249  else if(DAS.Paramini.EqType == SPATRAM2 || DAS.Paramini.EqType == SPATRAMPLUS)
1250  {
1251  if (NDFWS.Pos == 0)
1252  SD_NDFilterW(1); //ND1
1253  else if (NDFWS.Pos == 1)
1254  SD_NDFilterW(2); //ND2
1255  else if (NDFWS.Pos == 2)
1256  SD_NDFilterW(3); //ND3
1257  else if (NDFWS.Pos == 3)
1258  SD_NDFilterW(4); //ND4
1259  }
1260  else if (DAS.Paramini.EqType == GASCODNG1)
1261  {
1262  SD_FNTorqueM(1); //ND1
1263 
1264  }
1265  else if (DAS.Paramini.EqType == GASCODNG2 || DAS.Paramini.EqType == GASCODNG3)
1266  {
1267  //*Fil_CodeNG[] = {{"NULL"}, {"B12 "}, {"B12F"}, {"B39 "}, {"B39F"}, {"UG5 "}};
1268  if ((wl[GRS.Wl] == 3850))
1269  {
1270  if ( FWS.Pos == 1); //B12
1271  SD_FilterW(2); //B12F
1272  if ( FWS.Pos == 3); //B39
1273  SD_FilterW(4); //B39F
1274  }
1275 
1276  if (wl[GRS.Wl] == 4358)
1277  {
1278  //The Wl 4358 can be used with 2 different filters:BG12 and BG39, with the ND filters also
1279  if (FWS.Pos == 1) //B12
1280  SD_FilterW(2); //B12F
1281  if (FWS.Pos == 3) //B39
1282  SD_FilterW(4); //B39F
1283  }
1284  if (wl[GRS.Wl] == 4861)
1285  {
1286  if ((FWS.Pos == 1)) //B12
1287  SD_FilterW(2); //B12F
1288  if ((FWS.Pos == 3)) //B39
1289  SD_FilterW(4); //B39F
1290  }
1291  if ((wl[GRS.Wl] == 5332) )
1292  if ((FWS.Pos == 1)) //B12
1293  SD_FilterW(2); //B12F
1294  if ((FWS.Pos == 3)) //B39
1295  SD_FilterW(4); //B39F
1296 
1297  }
1298  else
1299  {
1300  if ((wl[GRS.Wl] == 3850) | (wl[GRS.Wl] == 4358))
1301  SD_FilterW(4); //BG23
1302  if ((wl[GRS.Wl] == 4861) )
1303  SD_FilterW(4); //BG23
1304  if ((wl[GRS.Wl] == 5332) )
1305  SD_FilterW(2); //UG5
1306  if ((wl[GRS.Wl] == 3337) )
1307  SD_FilterW(5); //LPAS
1308  }
1309  AutoRange(1);
1310 
1311  TExp = TExp;
1312  Status("4");
1313  return TExp;
1314 
1315  }
1316  //********************************************************************
1317  //Computation EIntegration time with the formula:
1318  // TExp= Level * (TExpAR - TExpMin) / (Max(TExpAR) - Max(TExpMin))
1319  if((TExp >= TExpMin) )
1320  {
1321  if (DAS.DOption.emshutter)
1322  {
1323  TExpLast = TExp;
1324  maxtar= DIL.Img.Mat[Server].High;
1325  mintar= DIL.Img.Mat[Server].Low;
1326 
1327  TExp = ( Level * ((1000*(TExpLast- TExpMin))/( maxtar - maxtt )))/ 1000;
1328  }
1329  else
1330  {
1331  //TExp = (( Level + DIL.Img.Mat[Server].Low) *TExp)/ (DIL.Img.Mat[Server].High - DIL.Img.Mat[Server].Low);
1332  TExp = ( Level * TExp)/ (DIL.Img.Mat[Server].High);
1333 
1334  }
1335  }
1336 
1337  Status("TExp Calculated");
1338 
1339  if(TExp <= TExpMin)
1340  TExp = TExpMin;
1341 
1342 
1343  if( DAS.Paramini.maxtexp == 0.0 )
1344  {
1345  TExp = TExp;
1346  Status("1");
1347  }
1348  else
1349  {
1350  Status("2");
1351  if (TExp >= DAS.Paramini.maxtexp)
1352  TExp = DAS.Paramini.maxtexp;
1353  else
1354  TExp= TExp;
1355  }
1356 }
1357  /*********************************/
1358 /* sprintf(buf, "%.3lf", TExp);
1359  MObjectSetText(DAS.Gui.ContrPanel.Albl_IE[0], buf);
1360 
1361 
1362  Status("Autoranging DONE");
1363  return TExp;
1364 
1365 
1366 }
1367 
1368 */
1369 void D_Saving(void)
1370 {
1371 
1372  char tmpbuf[128] ;
1373  static char tit[_MAX_PATH];
1374  char tit1[80];
1375  time_t ltime;
1376  struct tm *today;
1377  int fd, er = 0;
1378 
1379 
1380 
1381 
1382  if(fftprh) //flag first time program run here
1383  {
1384  /* Get UNIX-style time */
1385  time( &ltime );
1386  /* Use time structure to build a customized time string. */
1387  today = localtime( &ltime );
1388  /* Use strftime to build a customized time string. */
1389  strftime( tmpbuf, 7,"%y%m%d", today );
1390 
1391  sprintf(BIL.DFILEM.DAILYDATADIRECTORY,"%s\\%s",BIL.DFILEM.DATADIRECTORY,tmpbuf);
1392 
1393  //_chdir( BIL.DFILEM.DATADIRECTORY );
1394 
1395  er = _mkdir( BIL.DFILEM.DAILYDATADIRECTORY );
1396 /*
1397  if ( er ) //error in creating dir
1398  {
1399  if(EEXIST)
1400  _chdir( BIL.DFILEM.DAILYDATADIRECTORY );
1401  else if (ENOENT)
1402 
1403 
1404  }
1405  else
1406  {
1407  _chdir( BIL.DFILEM.DAILYDATADIRECTORY );
1408  }
1409 */
1410  fftprh=0;
1411  if(DAS.DOption.savingmode) // Binary saving
1412  {
1413  do
1414  {
1415  Total++;
1416  //sprintf(tit, "DATA\\%s%s%03d.bif", DGEO.s_name, tmpbuf, Total);
1417  sprintf(tit, "%s\\%s%s%03d.bif",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1418  fd = access(tit, 0);
1419  }while(!fd);
1420  DB_Save(tit, 1);
1421  }
1422  else //ASCII saving
1423  {
1424  do
1425  {
1426  Total++;
1427  //sprintf(tit, "DATA\\%s%s%03d.dat", DGEO.s_name, tmpbuf, Total);
1428  sprintf(tit, "%s\\%s%s%03d.dat",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1429  sprintf(tit1, "%s", tit+5);
1430  fd = access(tit, 0);
1431  }while(!fd);
1432  DB_Save(tit, 0);
1433  }
1434  sprintf(BIL.SPH.olddate, "%s",tmpbuf);
1435 
1436  }
1437  else
1438  {
1439 
1440 
1441  /* Get UNIX-style time */
1442  time( &ltime );
1443  /* Use time structure to build a customized time string. */
1444  today = localtime( &ltime );
1445  /* Use strftime to build a customized time string. */
1446  strftime( BIL.SPH.newdate, 7,"%y%m%d", today );
1447  /* Use strftime to build a customized time string. */
1448  strftime( tmpbuf, 7,"%y%m%d", today );
1449 
1450 
1451 
1452 
1453  // Se e' cambiata la data rimette a 1 il contatore del numero dei files
1454  if(strcmp(BIL.SPH.newdate, BIL.SPH.olddate) != 0)
1455  {
1456  sprintf(BIL.SPH.olddate, "%s",BIL.SPH.newdate);
1457  Total = 0;
1458 
1459  sprintf(BIL.DFILEM.DAILYDATADIRECTORY,"%s\\%s",BIL.DFILEM.DATADIRECTORY,tmpbuf);
1460  er = _mkdir( BIL.DFILEM.DAILYDATADIRECTORY );
1461 
1462  /*****************************/
1463  if(DAS.DOption.savingmode) // Binary saving
1464  {
1465  do
1466  {
1467  Total++;
1468 // sprintf(tit, "DATA\\%s%s%03d.bif", DGEO.s_name, tmpbuf, Total);
1469  sprintf(tit, "%s\\%s%s%03d.bif",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1470  fd = access(tit, 0);
1471  }while(!fd);
1472  DB_Save(tit, 1);
1473  }
1474  else //ASCII saving
1475  {
1476  do
1477  {
1478  Total++;
1479 // sprintf(tit, "DATA\\%s%s%03d.dat", DGEO.s_name, tmpbuf, Total);
1480  sprintf(tit, "%s\\%s%s%03d.dat",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1481  sprintf(tit1, "%s", tit+5);
1482  fd = access(tit, 0);
1483  }while(!fd);
1484  DB_Save(tit, 0);
1485  }
1486 /*****************************/
1487  }
1488  //Se ha raggiunto il numero di acquisizioni SAVECNT=50
1489  if((SaveCnt % SAVECNT) == 0)
1490  {
1491  if(DAS.DOption.savingmode)
1492  {
1493 // sprintf(tit, "DATA\\%s%s%03d.bif", DGEO.s_name, tmpbuf, Total);
1494  sprintf(tit, "%s\\%s%s%03d.bif",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1495  Total++;
1496  DB_Save(tit, 1);
1497  }
1498  else
1499  {
1500  //sprintf(tit, "DATA\\%s%s%03d.dat", DGEO.s_name, tmpbuf, Total);
1501  sprintf(tit, "%s\\%s%s%03d.dat",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1502  Total++;
1503  DB_Save(tit, 0);
1504  }
1505  }
1506  else
1507  {
1508  if(DAS.DOption.savingmode)
1509  {
1510  //sprintf(tit, "DATA\\%s%s%03d.bif", DGEO.s_name, tmpbuf, Total);
1511  sprintf(tit, "%s\\%s%s%03d.bif",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1512  DB_Save(tit, 1);
1513  }
1514  else
1515  {
1516  //sprintf(tit, "DATA\\%s%s%03d.dat", DGEO.s_name, tmpbuf, Total);
1517  sprintf(tit, "%s\\%s%s%03d.dat",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf, Total);
1518  DB_Save(tit, 0);
1519  }
1520  }
1521  }
1522 
1523 
1524 
1525  sprintf(tit1, "%s%s%03d.bif",DAS.DGeo.s_name, tmpbuf, Total);
1526  MObjectSetText(DAS.Gui.ContrPanel.Albl_CS[3], tit1);
1527  sprintf(tit1, "%05d", SaveCnt);
1528  MObjectSetText(DAS.Gui.ContrPanel.Albl_CS[2], tit1);
1529 
1530  SaveCnt++;
1531 
1532 
1533 
1534 }
1535 
1536 
1537 
1538 void MaskPix(int a, int b, int c, int d, int e, int f)
1539 {
1540 
1541  if (c!=0 && d!=0)
1542  DIL.Img.Mat[Server].Imat[a + b * IDX] = (int)(DIL.Img.Mat[Server].Imat[a + b * IDX - c] + DIL.Img.Mat[Server].Imat[a + b * IDX + d])/2;
1543  if (e!=0 && f!=0)
1544  DIL.Img.Mat[Server].Imat[a + b * IDX] = (int)(DIL.Img.Mat[Server].Imat[a + (b-e) * IDX ] + DIL.Img.Mat[Server].Imat[a + (b+f) * IDX ])/2;
1545 
1546 }
1547 
1548 void MaskCol(int a, int b, int c, int d, int e)
1549 {
1550  int k;
1551 
1552  for(k=b;k<c;k++)
1553  DIL.Img.Mat[Server].Imat[a + k * IDX] = (DIL.Img.Mat[Server].Imat[a + k * IDX - d] / 2) + (DIL.Img.Mat[Server].Imat[a + k * IDX + e] / 2);
1554 
1555 }
1556 
1557 void MaskRow(int a, int b, int c, int d, int e)
1558 {
1559 
1560  int k;
1561 
1562  for(k=a;k<b;k++)
1563  DIL.Img.Mat[Server].Imat[k + c * IDX] = (DIL.Img.Mat[Server].Imat[k + c * IDX - (IDX * d)] / 2) + (DIL.Img.Mat[Server].Imat[k + c * IDX + (IDX * e)] / 2);
1564 
1565 }
1566 
1567 
1569 {
1570  int i;
1571 
1572 
1573  for(i=0;i<PItem;i++)
1574  {
1575 
1576  MaskPix(MaskedPix[i][0], MaskedPix[i][1], MaskedPix[i][2], MaskedPix[i][3], MaskedPix[i][4], MaskedPix[i][5]);
1577 
1578  }
1579 
1580 
1581  for(i=0;i<CItem;i++)
1582  {
1583 
1584  MaskCol(MaskedCol[i][0], MaskedCol[i][1], MaskedCol[i][2], MaskedCol[i][3], MaskedCol[i][4]);
1585 
1586  }
1587 
1588 
1589  for(i=0;i<RItem;i++)
1590  {
1591 
1592  MaskRow(MaskedRow[i][0], MaskedRow[i][1], MaskedRow[i][2], MaskedRow[i][3], MaskedRow[i][4]);
1593 
1594 
1595  }
1596 
1597 }
1598 
1599 
1601 {
1602 
1603  double l = 0;
1604  int t, j;
1605 
1606  BlindPix = 0;
1607  if(IDX > 1024)
1608  BlindPix = IDX - 1024;
1609 
1610 
1611  for(t = 0; t < IDY; t++)
1612  {
1613  for(j = IDX - BlindPix + 1 ; j < IDX; j++)
1614  {
1615  l = l + (double) DIL.Img.Mat[Server].Imat[t * IDX + j];
1616  }
1617  }
1618  l = l / (IDY * (BlindPix));
1619 
1620  for(t = 0; t < IDY; t++)
1621  {
1622  for(j = 0; j < IDX ; j++)
1623  {
1624  DIL.Img.Mat[Server].Imat[t * IDX + j] = (unsigned short) (DIL.Img.Mat[Server].Imat[t * IDX + j] ) - (unsigned short) l;
1625  }
1626  }
1627 
1628 
1629 
1630 }
1631 
1632 double DB_ScanMaxMin(int w, float *mx, float *mi, int bp)
1633 {
1634  double l = 0;
1635  int x, y, xi = DIL.Img.Idx[w] - bp , yi = DIL.Img.Idy[w];
1636  U16 *m = DIL.Img.Mat[w].Imat;
1637  float *f = DIL.Img.Mat[w].Fmat;
1638 
1639 
1640  *mx = 0; *mi = DIL.Img.Mat[w].MaxNumbers;
1641  if(DAS.DOption.measmode == 0) //Single Measurement
1642  D_VRange("Find Max/Min..", 0, 192, 195, 192);
1643  //VRange("Find Max/Min", 0);
1644 
1645  if(DIL.Img.Mat[w].type == INTIMG || DIL.Img.Mat[w].type == RGBIMG)
1646  for(y = 0; y < yi-1; y++)
1647  {
1648  for(x = 2; x < xi; x++)
1649  {
1650  //l += (double) *m;
1651  *mi = min(*mi, m[y * DIL.Img.Idx[w] + x]);
1652  *mx = max(*mx, m[y * DIL.Img.Idx[w] + x]);
1653  //m++;
1654  }
1655  if(DAS.DOption.measmode == 0) //Single Measurement
1656  if((y % 32) == 0 && y > 0)
1657  D_VRange(NULL, (long) y * 100 / DIL.Img.Idy[w], 192, 195, 192);
1658  //VRange(NULL, (long) y * 100 / DIL.Img.Idy[w]);
1659  }
1660  if(DIL.Img.Mat[w].type == FLTIMG)
1661  for(y = 0; y < yi; y++)
1662  {
1663  for(x = 2; x < xi; x++)
1664  {
1665  l += (double) *f;
1666  *mi = min(*mi, *m);
1667  *mx = max(*mx, *m);
1668  f++;
1669  }
1670  if(DAS.DOption.measmode == 0) //Single Measurement
1671  if((y % 32) == 0 && y > 0)
1672  D_VRange(NULL, (long) y * 100 / DIL.Img.Idy[w], 192, 195, 192);
1673 
1674 // VRange(NULL, (long) y * 100 / DIL.Img.Idy[w]);
1675  }
1676  //VRange(NULL, 100);
1677  if(DAS.DOption.measmode == 0) //Single Measurement
1678  D_VRange(NULL, 100 , 192, 195, 192);
1679 
1680  return l;
1681 }
1682 
1683 // mod = 0 --> Read or create the file
1684 // mod = 1 --> write the number of shutter opening
1685 void ShutterOpening(int mod)
1686 {
1687 
1688  int fd, er;
1689  char buff[_MAX_PATH];
1690  FILE *fs;
1691 
1692 
1693  sprintf(buff,"%s\\Shutter.ini", BIL.DFILEM.CONFIGDIREQUIPMENT);
1694 
1695  if(mod == 0) //read or create file
1696  {
1697  fd = access(buff, 0);
1698  if (fd == 0) //file exist
1699  {
1700  fs = fopen(buff, "r");
1701  if(fs == NULL)
1702  {
1703  MMessageDialog("DAS information", "Problems in opening file", " Ok ", NULL);
1704  return ;
1705  }
1706  er = fscanf(fs, "%ul", &countsopsh);
1707 
1708  fclose(fs);
1709  }
1710  else
1711  {
1712  fs = fopen(buff, "w");
1713  if(fs == NULL)
1714  {
1715  MMessageDialog("DAS information", "Problems in opening file", " Ok ", NULL);
1716  return ;
1717  }
1718  fprintf(fs, "%u\n", 1);
1719  fclose(fs);
1720  }
1721 
1722  }
1723  else //mod == 1 write file
1724  {
1725  fs = fopen(buff, "w");
1726  if(fs == NULL)
1727  {
1728  MMessageDialog("DAS information", "Problems in opening file", " Ok ", NULL);
1729  return ;
1730  }
1731  fprintf(fs, "%u\n", countsopsh);
1732  fclose(fs);
1733  }
1734 
1735 
1736 
1737 
1738 
1739 }
1740 
1741 
1742 
1743 /********************************************
1744 int DB_GetCCD(int mod)
1745 mod = 0 --> Dark measurement
1746 mod = 1 --> Light measurement
1747 
1748 *********************************************/
1749 int DB_GetCCD(int mod)
1750 {
1751 
1752  char buf[80];
1753  MOBJECT sh;
1754  int t, j;
1755  unsigned long st, et, x;
1756  struct tm today;
1757 
1758 
1759 
1760 if(DAS.DOption.measmode == 0) //Single
1761 {
1762  if(TExp > .5)
1763  {
1764  sh = MDCreateExpo("EXPOSURE");
1765  sprintf(buf, "%6.1lf", TExp);
1766 
1767  DI_WriteDigit(DAS.Marconi.LblExpP, buf);
1768  DI_WriteDigit(DAS.Marconi.LblExpD, buf);
1769 
1770 
1771  }
1772 
1773 }
1774 /*************************************************************************/
1775  // DEMO mode
1776  if((DAS.Flag.exemode == DBDEMO) | (DAS.Flag.exemode == TESTSZAMODE))
1777  {
1778 
1779 
1780  GetDateTime(&DIL.Img.Mat[Server].DATE, &DIL.Img.Mat[Server].TIME, &today);
1781  //gettime( &DIL.Img.Mat[Server].TIME );
1782 
1783  st = GetTickCount();
1784  do
1785  {
1786  et = GetTickCount();
1787  x = labs(et - st);
1788  MLoopWhileEvents(1);
1789  //if((x % 10) == 0 && x > 0)
1790  if(x > 0)
1791  {
1792  sprintf(buf, "%6.1lf", TExp - x / 1000.0 );
1793  //sprintf(TTot, "%6.1lf", TExp );
1794  //ShowDown(buf, TTot);
1795 
1796  ShowDown(buf, 0);
1797 
1798 
1799  }
1800  }while(labs(et - st) < (TExp * 1000));
1801 
1802  if(DAS.DOption.measmode == 0) //Single Measurement
1803  {
1804  if(TExp > .5)
1805  {
1806  MShellUnrealize(sh);
1807  MShellDestroy(sh);
1808  }
1809  }
1810 
1811  return 0;
1812  }
1813 
1814  /*************************************************************************/
1815 
1816 /*
1817 void DC_GetWinCCD(int mode, double expos, int sx, int sy, int, dx, int dy, Unsigned *mat, int camera);
1818 Descrizione:
1819 Procedura che permette di leggere un’immagine selezionando un’area definita del ccd.
1820 I parametri che devono essere specificati sono:
1821 1. mode, Impostandolo a 0 si effettua una dark, con 1 si riprende l’immagine.
1822 2. expos, Tempo di esposizione espresso in secondi.
1823 3. sx, sy, Coordinate di origine della finestra.
1824 4. dx, dy Dimensione in pixel della finestra.
1825 5. mat, Array lineare dove viene memorizzata l’immagine. L’array mat deve essere dimensionato secondo:
1826 dimx x dimy x 2.
1827 6. camera, specifica quale camera dell’array viene utilizzata (se questa modalita’ e’ attiva).
1828 */
1829 
1830 
1831 
1832  if(!DAS.DOption.imgtype) //Mono image
1833  /******* MONO IMAGE ***************/
1834  {
1835  DIL.Img.Mat[Server].type = INTIMG;
1836  if(mod) // Light Measurement
1837  {
1838 /*
1839  sx = DAS.Paramini.ccdXoff;
1840  sy = DAS.Paramini.ccdYoff;
1841  dx = DAS.Paramini.ccdWx - DAS.Paramini.ccdXoff;
1842  dy = DAS.Paramini.ccdWy - DAS.Paramini.ccdYoff;
1843  DC_StopNow;
1844  DC_FGetWinCCD(1, TExp, sx, sy, dx, dy, Mat[Server].Imat, 0);
1845 */
1846  if(DAS.DOption.darkremove) // Remove Dark from Measurements
1847  {
1848  if(DAS.DOption.darksource) // Dark Source = Full Frame
1849  {
1850  /*******************************/
1851  //Dark Measurement
1852  DC_GetCCD(0, TExp, DIL.Img.Mat[Server].Imat, 0);
1853  if (DAS.DOption.mirrorimg[XAX] == 1)
1854  DB_MirrorX(Server); //Mirror X axis
1855  if (DAS.DOption.pixcorr)
1856  DB_DefectPixCorr();
1857  for(t = 0; t < IDY; t++)
1858  {
1859  for(j = 0; j < IDX ; j++)
1860  {
1861  ImatDark[t * IDX + j] = (unsigned short) DIL.Img.Mat[Server].Imat[t * IDX + j];
1862  }
1863  }
1864  /********************************/
1865  // Light Measurement
1866  // count opening shutter
1867  countsopsh++;
1868  delay(ShuttDelay);
1869  DC_GetCCD(1, TExp, DIL.Img.Mat[Server].Imat, 0);
1870  //Image Rotation
1871  /* if (DAS.Paramini.EqType == SPATRAM2)
1872  {
1873  U16 sav[3];
1874  U32 len = (unsigned long) IDX * (unsigned long) IDY; //DC_CCD.dx * DC_CCD.dy; // dimX e dimY = dimensione immagine
1875 
1876  sav[0] = DIL.Img.Mat[Server].Imat[0];
1877  sav[1] = DIL.Img.Mat[Server].Imat[1];
1878  memcpy(&DIL.Img.Mat[Server].Imat[0], &DIL.Img.Mat[Server].Imat[2], (IDX << 1) );
1879 
1880  DIL.Img.Mat[Server].Imat[len - 1] = sav[1];
1881  DIL.Img.Mat[Server].Imat[len ] = sav[0];
1882 
1883  sav[0] = DIL.Img.Mat[Server].Imat[IDX-3];
1884  sav[1] = DIL.Img.Mat[Server].Imat[IDX-2];
1885 
1886  DIL.Img.Mat[Server].Imat[IDX-1] = sav[0];
1887  DIL.Img.Mat[Server].Imat[IDX-3] = sav[1];
1888  }
1889 
1890  */
1891 
1892  delay(ShuttDelay);
1893  if (DAS.DOption.mirrorimg[XAX] == 1)
1894  DB_MirrorX(Server); //Mirror X axis
1895  if (DAS.DOption.pixcorr)
1896  DB_DefectPixCorr();
1897  //sottrae la dark alla misura
1898  //ATTENZIONE quando la dark e' maggiore della misura
1899  //la sottrazione non funziona RIVEDERE
1900  //RIVISTO - si aggiunge 100 alla misura!!!! (Manuele Dixit)
1901  for(t = 0; t < IDY; t++)
1902  {
1903  for(j = 0; j < IDX ; j++)
1904  {
1905  DIL.Img.Mat[Server].Imat[t * IDX + j] = (unsigned short) (DIL.Img.Mat[Server].Imat[t * IDX + j] + 100) - ImatDark[t * IDX + j];
1906  }
1907  }
1908  }
1909  else // Dark Source = Blind Pixels
1910  {
1911  // Light Measurement
1912  // count opening shutter
1913  countsopsh++;
1914  delay(ShuttDelay);
1915  DC_GetCCD(1, TExp, DIL.Img.Mat[Server].Imat, 0);
1916  //Image Rotation
1917  /* if (DAS.Paramini.EqType == SPATRAM2)
1918  {
1919  U16 sav[3];
1920  U32 len = (unsigned long) IDX * (unsigned long) IDY; //DC_CCD.dx * DC_CCD.dy; // dimX e dimY = dimensione immagine
1921 
1922  sav[0] = DIL.Img.Mat[Server].Imat[0];
1923  sav[1] = DIL.Img.Mat[Server].Imat[1];
1924  memcpy(&DIL.Img.Mat[Server].Imat[0], &DIL.Img.Mat[Server].Imat[2], (IDX << 1) );
1925 
1926  DIL.Img.Mat[Server].Imat[len - 1] = sav[1];
1927  DIL.Img.Mat[Server].Imat[len ] = sav[0];
1928 
1929  sav[0] = DIL.Img.Mat[Server].Imat[IDX-3];
1930  sav[1] = DIL.Img.Mat[Server].Imat[IDX-2];
1931 
1932  DIL.Img.Mat[Server].Imat[IDX-1] = sav[0];
1933  DIL.Img.Mat[Server].Imat[IDX-3] = sav[1];
1934  }
1935 
1936  */
1937  delay(ShuttDelay);
1938  if (DAS.DOption.mirrorimg[XAX] == 1)
1939  DB_MirrorX(Server); //Mirror X axis
1940  if (DAS.DOption.pixcorr)
1941  DB_DefectPixCorr();
1943  }
1944  }
1945  else // NOT Remove Dark from Measurements
1946  {
1947  // count opening shutter
1948  countsopsh++;
1949  delay(ShuttDelay);
1950  DC_GetCCD(1, TExp, DIL.Img.Mat[Server].Imat, 0);
1951  //Image Rotation
1952  /*if (DAS.Paramini.EqType == SPATRAM2)
1953  {
1954  U16 sav[3];
1955  U32 len = (unsigned long) IDX * (unsigned long) IDY; //DC_CCD.dx * DC_CCD.dy; // dimX e dimY = dimensione immagine
1956 
1957  sav[0] = DIL.Img.Mat[Server].Imat[0];
1958  sav[1] = DIL.Img.Mat[Server].Imat[1];
1959  memcpy(&DIL.Img.Mat[Server].Imat[0], &DIL.Img.Mat[Server].Imat[2], (IDX << 1) );
1960 
1961  DIL.Img.Mat[Server].Imat[len - 1] = sav[1];
1962  DIL.Img.Mat[Server].Imat[len ] = sav[0];
1963 
1964  sav[0] = DIL.Img.Mat[Server].Imat[IDX-3];
1965  sav[1] = DIL.Img.Mat[Server].Imat[IDX-2];
1966 
1967  DIL.Img.Mat[Server].Imat[IDX-1] = sav[0];
1968  DIL.Img.Mat[Server].Imat[IDX-3] = sav[1];
1969  }
1970 
1971  */
1972  delay(ShuttDelay);
1973  if (DAS.DOption.mirrorimg[XAX] == 1)
1974  DB_MirrorX(Server); //Mirror X axis
1975  if (DAS.DOption.pixcorr)
1976  DB_DefectPixCorr();
1977  }
1978 
1979  }
1980  else // Make Dark Measurement
1981  {
1982 /*
1983  sx = DAS.Paramini.ccdXoff;
1984  sy = DAS.Paramini.ccdYoff;
1985  dx = DAS.Paramini.ccdWx - DAS.Paramini.ccdXoff;
1986  dy = DAS.Paramini.ccdWy - DAS.Paramini.ccdYoff;
1987  DC_StopNow;
1988  DC_FGetWinCCD(0, TExp, sx, sy, dx, dy, Mat[Server].Imat, 0);
1989 */
1990  DC_GetCCD(0, TExp, DIL.Img.Mat[Server].Imat, 0);
1991  //Image Rotation
1992  /*if (DAS.Paramini.EqType == SPATRAM2)
1993  {
1994  U16 sav[3];
1995  U32 len = (unsigned long) IDX * (unsigned long) IDY; //DC_CCD.dx * DC_CCD.dy; // dimX e dimY = dimensione immagine
1996 
1997  sav[0] = DIL.Img.Mat[Server].Imat[0];
1998  sav[1] = DIL.Img.Mat[Server].Imat[1];
1999  memcpy(&DIL.Img.Mat[Server].Imat[0], &DIL.Img.Mat[Server].Imat[2], (IDX << 1) );
2000  DIL.Img.Mat[Server].Imat[len - 1] = sav[1];
2001  DIL.Img.Mat[Server].Imat[len ] = sav[0];
2002 
2003  sav[0] = DIL.Img.Mat[Server].Imat[IDX-3];
2004  sav[1] = DIL.Img.Mat[Server].Imat[IDX-2];
2005 
2006  DIL.Img.Mat[Server].Imat[IDX-1] = sav[0];
2007  DIL.Img.Mat[Server].Imat[IDX-3] = sav[1];
2008  }
2009  */
2010  if (DAS.DOption.mirrorimg[XAX] == 1)
2011  DB_MirrorX(Server); //Mirror X axis
2012  if (DAS.DOption.pixcorr)
2013  DB_DefectPixCorr();
2014 
2015  }
2016 
2020  DI_Regen(Server);
2021 
2022  GetDateTime(&DIL.Img.Mat[Server].DATE, &DIL.Img.Mat[Server].TIME, &today);
2023  // gettime( &DIL.Img.Mat[Server].TIME );
2024  // getdate( &DIL.Img.Mat[Server].DATE );
2025 
2026  if(DAS.DOption.savingmode)
2027  D_BuildH(1, Server);
2028  else
2029  D_BuildH(0, Server);
2030  /******* END MONO IMAGE ***************/
2031  }
2032  if(DAS.DOption.imgtype) //RGB image
2033  {
2034  DIL.Img.Mat[Server].type = RGBIMG;
2035  if(mod)
2036  {
2037  if(DAS.DOption.darkremove)
2038  {
2039  /*******************************/
2040  //Dark Measurement
2041  DC_GetCCD(0, TExp, DIL.Img.Mat[Server].Imat, 0);
2042  if (DAS.DOption.mirrorimg[XAX] == 1)
2043  DB_MirrorX(Server); //Mirror X axis
2044  if (DAS.DOption.pixcorr)
2045  DB_DefectPixCorr();
2046 
2047  for(t = 0; t < IDY; t++)
2048  {
2049  for(j = 0; j < IDX ; j++)
2050  {
2051  ImatDark[t * IDX + j] = (unsigned short) DIL.Img.Mat[Server].Imat[t * IDX + j];
2052  }
2053  }
2054  /********************************/
2055  // Light Measurement
2056  // count opening shutter
2057  countsopsh++;
2058  delay(ShuttDelay);
2059  DC_GetCCD(1, TExp, DIL.Img.Mat[Server].Imat, 0);
2060  delay(ShuttDelay);
2061  if (DAS.DOption.mirrorimg[XAX] == 1)
2062  DB_MirrorX(Server); //Mirror X axis
2063  if (DAS.DOption.pixcorr)
2064  DB_DefectPixCorr();
2065  //sottrae la dark alla misura
2066  //ATTENZIONE quando la dark e' maggiore della misura
2067  //la sottrazione non funziona RIVEDERE
2068  //RIVISTO - si aggiunge 100 alla misura!!!! (Manuele Dixit)
2069  for(t = 0; t < IDY; t++)
2070  {
2071  for(j = 0; j < IDX ; j++)
2072  {
2073  DIL.Img.Mat[Server].Imat[t * IDX + j] = (unsigned short) (DIL.Img.Mat[Server].Imat[t * IDX + j] + 50) - ImatDark[t * IDX + j];
2074  }
2075  }
2076  }
2077  else
2078  {
2079  // count opening shutter
2080  countsopsh++;
2081  delay(ShuttDelay);
2082  DC_GetCCD(1, TExp, DIL.Img.Mat[Server].Imat, 0);
2083  delay(ShuttDelay);
2084  if (DAS.DOption.mirrorimg[XAX] == 1)
2085  DB_MirrorX(Server); //Mirror X axis
2086  if (DAS.DOption.pixcorr)
2087  DB_DefectPixCorr();
2088  }
2089  }
2090  else
2091  {
2092  DC_GetCCD(0, TExp,DIL.Img.Mat[Server].Imat, 0);
2093  if (DAS.DOption.mirrorimg[XAX] == 1)
2094  DB_MirrorX(Server); //Mirror X axis
2095  if (DAS.DOption.pixcorr)
2096  DB_DefectPixCorr();
2097  }
2098 
2099 
2100 
2104 
2105 
2106 
2107 // Mat[Server].ILow = Mat[Server].Low ;
2108 // Mat[Server].IHigh = Mat[Server].High ;
2109 
2110  DI_Regen(Server);
2111 
2112  }
2113 
2114 if(DAS.DOption.measmode == 0) //Single
2115 {
2116  if(TExp > .5)
2117  {
2118  MShellUnrealize(sh);
2119  MShellDestroy(sh);
2120  }
2121 }
2122  return 0;
2123 
2124 
2125 
2126 }
2127 
2128 //mod = 0 Save in xxxYYMMDD.bhg
2129 //mod = 1 Save in xxxYYMMDD.bqj
2130 void SaveCalib(int mod)
2131 {
2132 
2133 
2134  char tmpbuf[128] ;
2135  static char tit[_MAX_PATH];
2136  char tit1[80];
2137  time_t ltime;
2138  struct tm *today;
2139  int er = 0;
2140 
2141 
2142 
2143 
2144  if(fftsavecal) //flag first time program run here
2145  {
2146  /* Get UNIX-style time */
2147  time( &ltime );
2148  /* Use time structure to build a customized time string. */
2149  today = localtime( &ltime );
2150  /* Use strftime to build a customized time string. */
2151  strftime( tmpbuf, 7,"%y%m%d", today );
2152 
2153  //Build name of Daily data directory
2154  sprintf(BIL.DFILEM.DAILYDATADIRECTORY,"%s\\%s",BIL.DFILEM.DATADIRECTORY,tmpbuf);
2155  //Create Daily data dir
2156  er = _mkdir( BIL.DFILEM.DAILYDATADIRECTORY );
2157 
2158  fftsavecal=0;
2159  if(DAS.DOption.savingmode) // Binary saving
2160  {
2161 
2162  if (mod)
2163  sprintf(tit, "%s\\%s%s.bqj",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2164  else
2165  sprintf(tit, "%s\\%s%s.bhg",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2166  DB_Save(tit, 1);
2167  }
2168  else //ASCII saving
2169  {
2170  if (mod)
2171  sprintf(tit, "%s\\%s%s.aqj",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2172  else
2173  {
2174  sprintf(tit, "%s\\%s%s.ahg",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2175  sprintf(tit1, "%s", tit+5);
2176  }
2177  DB_Save(tit, 0);
2178  }
2179  sprintf(BIL.SPH.olddate, "%s",tmpbuf);
2180  }
2181  else
2182  {
2183 
2184  /* Get UNIX-style time */
2185  time( &ltime );
2186  /* Use time structure to build a customized time string. */
2187  today = localtime( &ltime );
2188  /* Use strftime to build a customized time string. */
2189  strftime( BIL.SPH.newdate, 7,"%y%m%d", today );
2190  /* Use strftime to build a customized time string. */
2191  strftime( tmpbuf, 7,"%y%m%d", today );
2192 
2193 
2194  // Se e' cambiata la data rimette a 1 il contatore del numero dei files
2195  if(strcmp(BIL.SPH.newdate, BIL.SPH.olddate) != 0)
2196  {
2197  sprintf(BIL.SPH.olddate, "%s",BIL.SPH.newdate);
2198  Total = 0;
2199 
2200  sprintf(BIL.DFILEM.DAILYDATADIRECTORY,"%s\\%s",BIL.DFILEM.DATADIRECTORY,tmpbuf);
2201  er = _mkdir( BIL.DFILEM.DAILYDATADIRECTORY );
2202 
2203  /*****************************/
2204  if(DAS.DOption.savingmode) // Binary saving
2205  {
2206  if (mod)
2207  sprintf(tit, "%s\\%s%s.bqj",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2208  else
2209  sprintf(tit, "%s\\%s%s.bhg",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2210  DB_Save(tit, 1);
2211  }
2212  else //ASCII saving
2213  {
2214  if (mod)
2215  sprintf(tit, "%s\\%s%s.aqj",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2216  else
2217  {
2218  sprintf(tit, "%s\\%s%s.ahg",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2219  sprintf(tit1, "%s", tit+5);
2220  }
2221 
2222  DB_Save(tit, 0);
2223  }
2224  }
2225 /*****************************/
2226 
2227 
2228  if(DAS.DOption.savingmode)
2229  {
2230  if (mod)
2231  sprintf(tit, "%s\\%s%s.bqj",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2232  else
2233  sprintf(tit, "%s\\%s%s.bhg",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2234 
2235  DB_Save(tit, 1);
2236  }
2237  else
2238  {
2239  if (mod)
2240  sprintf(tit, "%s\\%s%s.aqj",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2241  else
2242  {
2243  sprintf(tit, "%s\\%s%s.ahg",BIL.DFILEM.DAILYDATADIRECTORY, DAS.DGeo.s_name, tmpbuf);
2244  sprintf(tit1, "%s", tit+5);
2245  }
2246  DB_Save(tit, 0);
2247  }
2248 
2249  }
2250 
2251 
2252  //sprintf(tit1, "%s", tit+5);
2253  //MObjectSetText(DAS.Gui.ContrPanel.Albl_CS[3], tit1);
2254  //sprintf(tit1, "%05d", SaveCnt);
2255  //MObjectSetText(DAS.Gui.ContrPanel.Albl_CS[2], tit1);
2256 
2257  //SaveCnt++;
2258 
2259 
2260 }
2261 
2262 
2263 /* int Lookfor4358(void)
2264  function to scan the wl() array in order to identify
2265  the position of the 4358 reference wavelength
2266  return value:
2267  er = -1 --> value 4358 not encountered
2268  er >= 0 --> value 4358 found at er position
2269 */
2270 int Lookfor4358(void)
2271 {
2272 
2273  int i, er = 0;
2274 
2275  for (i=0; i<17; i++)
2276  {
2277  if (wl[i] == 4358)
2278  {
2279  er = i;
2280  return er;
2281  }
2282  }
2283 
2284  return -1;
2285 }
2286 
2287 
2288 /* int LookforWl_X( int x)
2289  function to scan the wl() array in order to identify
2290  the position of the 'x' wavelength
2291  return value:
2292  er = -1 --> value 'x' not encountered
2293  er >= 0 --> value 'x' found at er position
2294 */
2295 int LookforWl_X(int x)
2296 {
2297 
2298  int i, er = 0;
2299 
2300  for (i=0; i<18; i++)
2301  {
2302  if (wl[i] == x)
2303  {
2304  er = i;
2305  return er;
2306  }
2307  }
2308 
2309  return -10;
2310 }
2311 
2312 
2313 int D_Calibra(void)
2314 {
2315 
2316 
2317  long n;
2318 // unsigned v;
2319  int x, y, px, DirCal, er = 0, c;
2320  int k;
2321  int PxHg;
2322  int exm;
2323 // int count, i;
2324  int PosRefWl;
2325 
2326  char buf[256];
2327  unsigned int mx;
2328 
2329  Status("Hg Calibration");
2330 
2331  if(DAS.Paramini.EqType == SPATRAMPLUS || DAS.Paramini.EqType == SPATRAM2)
2332  SD_FilterW(0); //Hole
2333  if(DAS.Paramini.EqType == GASCODNG2 || DAS.Paramini.EqType == GASCODNG3)
2334  SD_FilterW(1); //BG12
2335  if(DAS.Paramini.EqType == GASCODNG1)
2336  SD_FilterW(0); //Hole
2337  if(DAS.Paramini.EqType == GASCODNG4)
2338  SD_FilterW(2); //BG12F
2339 
2340 
2341 
2342  BlindPix = 0;
2343 // SaveCalib();
2344  if (IDX>=1024)
2345  BlindPix = IDX - 1024;
2346 
2347  PxHg = (IDX - BlindPix) / 2;
2348 
2349  exm = DAS.Flag.exemode;
2350 
2351 
2352  //PosRefWl = Lookfor4358();
2353  PosRefWl = LookforWl_X(4358);
2354  /************************************************************/
2355  if ((exm == DBDEMO) ||
2356  (exm == MANUAL) ||
2357  (FLAG.exec == 0 && exm == PRGFILE))
2358  {
2359 
2360  if (GRS.Wl != PosRefWl)
2361  {
2362  //If is not the 4358 spectral window
2363  sprintf(buf, "You are performing HG calibration in the %.1lf nm spectral window!!!\nUsually this operation is performed in the 435.8 nm window \nDo you want to continue ?", (double)wl[GRS.Wl]/10);
2364  er = MMessageDialog("DAS information", buf, "Yes, in this window", "Yes, but go to 435.8!", "Cancel Calibration", NULL);
2365  //if it was chosen to calibrate in the actual spectral window the filter is removed
2366  if (er==0)
2367  {
2368  SD_FilterW(0); //Hole
2369  }
2370 
2371  if (er == 1)
2372  {
2373 
2374  SD_Grating(PosRefWl); //Positioning at 4358 A
2375 
2376  }
2377  else if (er == 2)
2378  return 1;
2379  }
2380  }
2381  else if ((exm == DBDEMO) ||
2382  (exm == MANUAL) ||
2383  (FLAG.exec == 1 && exm == PRGFILE) ||
2384  (FLAG.exec == 1 && exm == TESTSZAMODE) ||
2385  (FLAG.exec == 1 && exm == SZAMODE))
2386 
2387  {
2388  ExeREM = 0;
2389  SD_Grating(PosRefWl); //Positioning at 4358 A
2390  ExeREM = 1;
2391 
2392 
2393  }
2394  /************************************************************/
2395 
2396  HgLamp(1);
2397  if (DAS.Paramini.EqType == TROPOGAS || DAS.Paramini.EqType == SPATRAM3 || DAS.Paramini.EqType == GASCODNG4)
2398  SD_Mirror(0);
2399  else
2400  SD_Mirror(2);
2401 
2402  sprintf(buf, "Wait 5 sec. for HG lamp Heating");
2403  Status(buf);
2404  delay(5000);
2405 // FlagCalibra = 1;
2406 
2407 
2408 
2409 
2410  for(c = 0; c < 10; c++)
2411  {
2412  sprintf(buf, "Guess %d", c+1);
2413  Status(buf);
2414 
2415 
2416  AutoRange(0);
2417 
2418  DB_GetCCD(1);
2419 
2420  //Save Calibration
2421  if (c==0)
2422  SaveCalib(0);
2423 
2424  y = IDY / 2 ;
2425 
2426 
2427  px = (IDX - BlindPix) / 2;
2428  mx = 5000;
2429  for(x = PxHg - 200; x < PxHg + 200; x++)
2430  {
2431  if(DIL.Img.Mat[Server].Imat[x + y * IDX] > mx)
2432  {
2433  px = x;
2434  mx = DIL.Img.Mat[Server].Imat[x + y * IDX];
2435  }
2436  }
2437  sprintf(buf, "Hg: %u, found at: %d", mx, px);
2438  Status(buf);
2439  delay(1000);
2440  if((px > (PxHg + 1)) || (px < (PxHg - 1))) //grosso
2441  {
2442 
2443  k=SM_stepr(DAS.StepM.step[AMS1][DAS.StepM.SM_B]);
2444  n = (PxHg - px) * (k/200-5) * 4; //4 e' il rapporto di riduzione
2445  // il 60
2446 
2447  if(n < 0)
2448  DirCal = 1;
2449  else
2450  DirCal = 0;
2451 
2452  n = labs(n);
2453  if(n & DirCal == 0) //si va verso l'UV
2454  n += GRS.dp; // add fixed step number for backlash recovery
2455  // Eseguo NStep
2456  if(n)
2457  {
2458  er = DB_StepMotor(AMS1, 1, n, DirCal);
2459  if (er)
2460  {
2461  MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[AMS1][1], "SYS\\ledGreen.bmp");
2462  }
2463  }
2464 
2465  //Se ci si e' mossi verso l'UV si fa il recupero del gioco
2466 
2467  if(n & DirCal == 0) // faccio recupero gioco
2468  {
2469  er=DB_StepMotor(AMS1, 1, GRS.dp, 1);
2470  if (er)
2471  {
2472  MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[AMS1][1], "SYS\\ledGreen.bmp");
2473  }
2474  }
2475 
2476  }
2477 
2478  else break;
2479  }
2480 
2481 
2482  HgLamp(0);
2483  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[1], "4358");
2484 
2485  SaveCalib(0);
2486 
2487 // er = AMS_AzzeraCoord(AMS1, 1);
2488 // if(er) { Message("No Zero Coord Grating", er); }
2489 
2490 
2491  if(DAS.Flag.plotW)
2492  D_Plot();
2493 
2494 
2495 
2496  sprintf(buf, "Hg: %u, found at: %d", mx, px);
2497  Status(buf);
2498  delay(500);
2499 
2500  return 0;
2501 
2502 }
2503 
2504 void D_Measure(void)
2505 {
2506  int er=0;
2507  int l, f, m, a, d, s, g, ss;
2508  double t;
2509  extern struct date dok;
2510 
2511 // char buf[80];
2512 // PrgCnt += 7;
2513 
2514 
2515  sscanf(&Program[PrgCnt], "%d%d%d%d%lf%d%d%d", &m, &l, &f, &a, &t, &d, &s, &g);
2516 
2517 
2518 
2519  //Posiziona specchio
2520  SD_Mirror(m);
2521  //Posiziona reticolo
2522  SD_Grating(l);
2523  //Posiziona Ruota dei filtri
2524  SD_FilterW(f);
2525 
2526 
2527 // ExeREM = 0;
2528  if(a)
2529  {
2530 
2531  AutoRange(0);
2532 
2533  // Ciclo ottimale per Autoranging;
2534  //Controlla che il valore ottenuto sia compreso tra 1000 e 50000,
2535  //inoltre esegue l'autoranging fino a che il min-max e' maggiore
2536  //di 1000
2537  //oppure il contatore ss sia maggiore di 8
2538  ss=0;
2539 
2540 /* while ((Low < 1000) || (High > 50000) || (((High-Low) < 400) && (ss < 8) ))
2541  {
2542  E_AutoRange();
2543  if(((TExp && 0.01) == 1) && (High > 50000) && (Low > 1000)) goto Salta;
2544  while ((High-Low) < 400 && ss < 8)
2545  {
2546  FlagTExp = FlagTExp * 3;
2547  E_AutoRange();
2548  if((Low < 1000) || (High > 50000) && ss > 0)
2549  {
2550  ss--;
2551  }
2552  else
2553  {
2554  ss++;
2555  }
2556  }
2557  FlagTExp = 1.0;
2558  }
2559 
2560  FlagTExp = 1.0;
2561 */
2562 
2563 
2564  }
2565  else
2566  {
2567  TExp = t;
2568  }
2569 
2570  //Measure (d=1) or dark (d=0)
2571  if(d)
2572  {
2573  DB_GetCCD(1);
2574  }
2575  else
2576  {
2577  DB_GetCCD(0);
2578  }
2579 
2580 
2581  if(s)
2582  {
2583  D_Saving();
2584  }
2585  if(g)
2586  {
2587  D_Plot();
2588  }
2589 // ExeREM = 1;
2590  E_REM();
2591 
2592 
2593 }
2594 
2595 
2596 
2597 void D_AverageTFix(void)
2598 {
2599 
2600  char buf[64];
2601  int c;
2602 // int fd;
2603  long n, np;
2604  double TT;
2605  int NM;
2606 
2607  NM = 0;
2608 
2609  if(QJFlg)
2610  TT = 1;
2611  if(HGFlg)
2612  TT = 2;
2613  if(!HGFlg & !QJFlg)
2614  TT = 15;
2615 
2616 
2617  AutoRange(0);
2618 
2619 
2620  NM = (int)(TT / TExp);
2621 
2622  np = (long) IDX * IDY * 2;
2623 
2624  DAS.Marconi.AVGmat = malloc(np * 4);
2625  memset(DAS.Marconi.AVGmat, 0, np * 4);
2626 
2627 
2628  if(NM > 0)
2629  {
2630  for(c = 0; c < NM; c++)
2631  {
2632  DB_GetCCD(1);
2633  sprintf(buf,"Averaging #: %d on %d", c, NM);
2634  Status(buf);
2635  for(n = 0; n < np; n++)
2636  DAS.Marconi.AVGmat[n] += (long) DIL.Img.Mat[Server].Imat[n];
2637  }
2638 
2639  for(n = 0; n < np; n++)
2640  DIL.Img.Mat[Server].Imat[n] = (unsigned short) DAS.Marconi.AVGmat[n] / NM;
2641 
2642  }
2643  if(NM == 0)
2644  {
2645  DB_GetCCD(1);
2646  }
2647 
2648  D_Plot();
2649 
2650  free(DAS.Marconi.AVGmat);
2651 
2652 }
2653 // mod = 1 --> perform autoranging
2654 // mod = 0 --> DO NOT PERFORM autoranging
2655 void D_Average(int mod)
2656 
2657 {
2658  char buf[64];
2659  int c;
2660 // int fd;
2661  long n, np;
2662  double TT, DeadTime,availabletime;
2663  int NM; //Number of calculated/setted measurements
2664  //int NRM; // Number of measurements effectively obtained (without the saturated measurements that can occur
2665  int tmpnavg = 0;
2666 
2667  MOBJECT sh;
2668  int t, j;
2669  unsigned long st, et, x;
2670  struct tm today;
2671 
2672 
2673 
2674  if(mod) // perform autoranging
2675  {
2676  if(DAS.DOption.avgautor)
2677  AutoRange(0);
2678  else
2679  {
2680  if (TExp==0)
2681  MMessageDialog("DAS information", "TExp = 0\nThe Average procedure can NOT be executed\nPlease provide a valid TExp\n(Executing a GetCCD or Autoranging)!!", " Ok ", NULL);
2682  return ;
2683  }
2684  }
2685 
2686  //DAS.Marconi.AVGmat = AllocFloatMat(IDX, IDY);
2687  np = IDX * IDY;// * 2;
2688  //memset(DAS.Marconi.AVGmat, 0, np);
2689  for(n = 0; n < np; n++)
2690  DAS.Marconi.AVGmat[n] = 0;
2691 
2692  // Fix Number of Measurements
2693  if(DAS.DOption.avgmode == 0)
2694  {
2695 
2696  tmpnavg = N_AVG;
2697  if(TExp > 0.5 && TExp <= 1.0) tmpnavg = 10;
2698  else
2699  if(TExp > 1.0) tmpnavg = 2;
2700 
2701  for(c = 0; c < tmpnavg; c++)
2702  {
2703  DB_GetCCD(1);
2704  MLoopWhileEvents(0);
2705  sprintf(buf,"Averaging #: %d", c+1);
2706  Status(buf);
2707 
2708 
2709  for(n = 0; n < np; n++)
2710  DAS.Marconi.AVGmat[n] += DIL.Img.Mat[Server].Imat[n];
2711  //AVGmat[n] += (long) DIL.Img.Mat[Server].Imat[n];
2712  delay(50);
2713  }
2714 
2715  for(n = 0; n < np; n++)
2716  DIL.Img.Mat[Server].Imat[n] = (DAS.Marconi.AVGmat[n] / tmpnavg);
2717  //DIL.Img.Mat[Server].Imat[n] = AVGmat[n] / tmpnavg;
2718 
2719 
2720 
2721 // if(DAS.DOption.measmode == 0) //Single
2722 // {
2726  DI_Regen(Server);
2727 // }
2728  Status("Ready");
2729 
2730  }
2731  //Fix TOT Exposure Time
2732  else if(DAS.DOption.avgmode == 1)
2733  {
2734  NM = 0;
2735  if(QJFlg)
2736  TT = 1;
2737  if(HGFlg)
2738  TT = 2;
2739  if(!HGFlg & !QJFlg)
2740  TT = TOTTEXP;
2741 
2742  //If there is a delay in the shutter opening, this is accounted for
2743  //in the computation of the number of measurements
2744  if(ShuttDelay > 0)
2745  DeadTime = (TT * (double) ShuttDelay/1000 );
2746  else
2747  DeadTime = 0;
2748 
2749 
2750  availabletime = TT - DeadTime;
2751 
2752  if (availabletime > 0)
2753  TT = availabletime;
2754  else
2755  TT = TT;
2756 
2757  //First computation of number of measurement
2758  NM = (int)(TT / (TExp + 0.5)); //0.5 is the average download time for each measurement
2759  // This is tested for SPATRAM2 instrument
2760 
2761 
2762 /*
2763  Computation of CCD reading time
2764  DTA Formula: RT (in ms)= (vp/vb)*Kv + (vp/vb)*(hp/hb)*Kh
2765  where:
2766  vp = vertical pixels number
2767  vb = vertical binning
2768  Kv = 0.024 for hires
2769  hp = horizontal pixels number
2770  hb = horizontal binning
2771  Kh = 0.0125
2772 
2773 */
2774 /* RT = (((float)DC_CCD.dy / (float)DAS.Paramini.VBin) * 0.024 + ((float)DC_CCD.dy / (float)DAS.Paramini.VBin) * DC_CCD.dx * 0.0125)/1000;
2775 
2776 
2777  if(TExp > RT)
2778  {
2779  RTot = NM * RT;
2780  TT = TT - RTot;
2781  //Second computation of number of measurement (after the correction due to the reading time
2782  NM = (int)(TT / TExp);
2783  }
2784 
2785 
2786 /*
2787  Computation of CCD downloading time ( in ms)
2788  DT = ((vp/vb)*(hp/hb))/KCCD
2789  where:
2790  vp = vertical pixels number
2791  vb = vertical binning
2792  hp = horizontal pixels number
2793  hb = horizontal binning
2794  KCCD = frequency of the CCD (Front = 40Khz; Back = 60Khz)
2795 */
2796 /* DT = (((float)DC_CCD.dy / (float)DAS.Paramini.VBin) * DC_CCD.dx) / 60000;
2797 
2798 // if(TExp > DT)
2799 // {
2800  DTot = NM * DT;
2801  TT = TT - DTot;
2802 // }
2803 
2804  //Third computation of number of measurement (after the correction due to the Downloading time
2805  NM = (int)(TT / TExp);
2806 
2807 */
2808  N_RM = NM;
2809  if(NM > 1)
2810  {
2811  for(c = 0; c < NM; c++)
2812  {
2813 
2814  //st = GetTickCount();
2815 
2816  DB_GetCCD(1);
2817  MLoopWhileEvents(0);
2818 
2819  //et = GetTickCount();
2820 
2821  //x = labs(et - st);
2822 
2823  //sprintf(buf, "%6.3lf", x - (TExp * 1000.0) );
2824  sprintf(buf,"Avg #: %d on %d. TotTExp: %d s", c+1, NM, TOTTEXP);
2825 
2826  Status(buf);
2827  // If the spectrum is saturated
2828  if ((DIL.Img.Mat[Server].IHigh > 65000) & (c != 0))
2829  {
2830  N_RM = N_RM - 1;
2831  }
2832  else
2833  {
2834 
2835  for(n = 0; n < np; n++)
2836  DAS.Marconi.AVGmat[n] += DIL.Img.Mat[Server].Imat[n];
2837  }
2838  }
2839 
2840  for(n = 0; n < np; n++)
2841  DIL.Img.Mat[Server].Imat[n] = ( DAS.Marconi.AVGmat[n] / N_RM);
2842 
2843 
2844 
2845 // if(DAS.DOption.measmode == 0) //Single
2846 // {
2850  DI_Regen(Server);
2851 // }
2852 
2853  }
2854  else //if(NM == 0 or NM ==1)
2855  {
2856 /* if(TExp > .5)
2857  {
2858  sh = MDCreateExpo("EXPOSURE");
2859  sprintf(buf, "%6.1lf", TExp);
2860  DI_WriteDigit(DAS.Marconi.LblExpP, buf);
2861  DI_WriteDigit(DAS.Marconi.LblExpD, buf);
2862  }
2863 */
2864  sprintf(buf,"Measuring with TExp = %3.1lf" , TExp);
2865  Status(buf);
2866 
2867  DB_GetCCD(1);
2868 
2869 /* GetDateTime(&DIL.Img.Mat[Server].DATE, &DIL.Img.Mat[Server].TIME, &today);
2870 
2871  st = GetTickCount();
2872  do
2873  {
2874  et = GetTickCount();
2875  x = labs(et - st);
2876  MLoopWhileEvents(1);
2877  //if((x % 10) == 0 && x > 0)
2878  if(x > 0)
2879  {
2880  sprintf(buf, "%6.1lf", TExp - x / 1000.0 );
2881  //sprintf(TTot, "%6.1lf", TExp );
2882  //ShowDown(buf, TTot);
2883 
2884  ShowDown(buf, 0);
2885 
2886 
2887  }
2888  }while(labs(et - st) < (TExp * 1000));
2889 */
2890 /* if(TExp > .5)
2891  {
2892  MShellUnrealize(sh);
2893  MShellDestroy(sh);
2894  }
2895 */
2896  N_RM = 1;
2897  }
2898  }
2899 
2900  Status("Ready");
2901 // D_Plot();
2902  //free(DAS.Marconi.AVGmat);
2903 
2904 }
bil BIL
Definition: Procedures.c:42
stepm StepM
Stepper motors parameters structure.
Status of the Zenith .
gui Gui
Graphic User Interface Structure.
MOBJECT LedSM[AMSMAX][2]
Leds for stepper motors status.
int SD_Grating(int ang)
Positioning Grating. The Grating move to the position specified by ang (Wavelength) ...
Definition: Spat_Device.c:2108
int Pos
Filter Wheel current Position.
int SD_Mirror(int where)
Positioning Input Mirror. The Input Mirror reaches the position specified by where.
Definition: Spat_Device.c:1898
int IDX
Number of sensible horizontal pixels.
Definition: DAS_Spat.c:118
MOBJECT Albl_LS[8]
Active Control Panel Labels for LIS status.
double S_zenetr
Definition: SOLPOS.C:92
void HgLamp(int mod)
HG Lamp ON/OFF .
Definition: Spat_Device.c:2673
U16 Idx[MAXIMG]
Definition: Dildef.h:570
int Server
Definition: DAS_Spat.c:253
dil DIL
Definition: 2DPlot.c:26
void MLoopWhileEvents(int discard)
optionini DOption
Options for DAS execution.
int LookforWl_X(int x)
Definition: Procedures.c:2295
Status of the ND Filter wheel .
int savingmode
Flag to set saving mode: 0=ASCII, 1=Binary.
double alititude
Station altitude.
const char * Mir_Code[]
const char array for the realisation of the Mirrors buttons in the Mirror shell
Definition: DAS_Spat.c:231
hdrsp HDRSP
Definition: Procedures.c:32
float GHigh
Definition: Dildef.h:299
void SD_FNTorqueM(int mod)
Positioning NDFilter. .
Definition: Spat_Device.c:3066
void D_VRange(char *str, int val, int, int g, int b)
Create and display the status bar.
Definition: Utils.c:206
ptf DC_HP
Definition: Procedures.c:30
unsigned int imgmin
Image minimum value.
Definition: Dildef.h:710
double realazpos
float MaxNumbers
Definition: Dildef.h:309
#define TORQUEM
Torque Motor.
int ExeREM
Definition: DAS_Spat.c:143
C_DEF void DI_Regen(int s)
ccd DC_CCD
Definition: Procedures.c:29
int ccdwy
Total Number of Y Pixels.
Definition: Dildef.h:693
C_DEF double DI_ScanMaxMin(int w, float *mx, float *mi)
int Total
Progressive acquisition number for file (MAX defined = 50)
Definition: Exec.c:34
int plotW
Flag for the plot display.
void MPixmapSetImageFile(MOBJECT pixmap, const char *fname)
das structure (contain all the previous structures) .
void D_Average(int mod)
Performs averaged measurements .
Definition: Procedures.c:2655
#define TESTSZAMODE
execution driven by SZAPRG file
int ccdWx
CCD X End.
double latitude
Station latitude.
int ccdwx
Total Number of X Pixels.
Definition: Dildef.h:692
int dummy4
Definition: Dildef.h:716
char DAILYDATADIRECTORY[_MAX_PATH]
Definition: bildef.h:203
int dummy3
Definition: Dildef.h:715
int Pos
Mirror Current Position.
int QJFlg
Definition: DAS_Spatram.h:2969
flag Flag
Structure for different flags.
#define FLTIMG
Definition: Dildef.h:12
void MaskPix(int a, int b, int c, int d, int e, int f)
Definition: Procedures.c:1538
int darkremove
Flag to set Dark Removal from measurements; motors0 = No Dark Removal, 1 = Dark Removal.
void MaskRow(int a, int b, int c, int d, int e)
Definition: Procedures.c:1557
struct date DATE
Registration date.
Definition: Dildef.h:701
int filtertype
type of installed filter module: 0=Simple-SM3, 1=Double-SM3+SM4, 2=simpleplus-SM3+TM) ...
U16 * Imat
Definition: Dildef.h:286
int ccdys
Y Start.
Definition: Dildef.h:695
#define DBDEMO
execution in demo mode
#define XAX
X Axis.
S16 type
Definition: Dildef.h:280
int SD_FilterW(int filter)
Positioning Filter wheel. The filter wheel move on the filter number specified by filter...
Definition: Spat_Device.c:2244
int ccdXoff
CCD X Start.
char DATADIRECTORY[_MAX_PATH]
Definition: bildef.h:201
double AutoR_No_Shutt(void)
Definition: Procedures.c:468
float ccdtemp
CCD Temperature.
Definition: Dildef.h:707
float minar
Definition: Procedures.c:252
NDFilterWheelSt NDFWS
ND Filter Wheel.
Definition: Procedures.c:35
void D_Plot(void)
ViewPlot callback .
Definition: Procedures.c:192
#define ROTSHUT
Rotating Shutter.
geographical coordinate structure .
spectrumheader SPH
Spectrum Header Structure.
Definition: bildef.h:256
int avgautor
autoranging for averaged measurement, 0 = NO AutoR before average; 1 = AutoR before average; ...
S16 dx
Num. of X pixels.
Definition: dcldef.h:1022
Definition: Dildef.h:687
img Mat[MAXIMG]
Definition: Dildef.h:552
char * Fil_Code8H[]
const char array for the realisation of the Filters buttons in the Filter shell
Definition: DAS_Spat.c:204
MOBJECT Albl_CS[4]
Active Control Panel Labels for Camera Status 4.
void MShellUnrealize(MOBJECT obj)
void DB_Save(char *tit, int mod)
Save the acquired spectrum. Procedure that Save the acquired spectrum.
Definition: Save.c:552
struct date DATE
Definition: Dildef.h:348
int ccdxe
X End.
Definition: Dildef.h:696
char olddate[8]
Definition: bildef.h:158
void MObjectSetText(MOBJECT obj, const char *text)
void D_BuildH(int mod, int s)
Build Header of the measurements . .
Definition: Procedures.c:63
int TMFlg
Definition: DAS_Spatram.h:2972
config Cfg
Definition: Procedures.c:31
int MaskedPix[100][6]
Masked pixel&#39;s array.
Definition: Load.c:37
image Img
Definition: Dildef.h:675
int Pos
Filter Wheel current Position.
float maxar
Definition: Procedures.c:252
das DAS
DAS structure.
Definition: Procedures.c:47
d_geo DGeo
struct for Station location
void MShellDestroy(MOBJECT obj)
#define LIS_
Lampedusa Island Spectrometer - ENEA.
short unsigned U16
2 byte without sign, range 0-65535
Definition: dcldef.h:375
float BHigh
Definition: Dildef.h:301
paramini PARAM
Param.ini.
Definition: Procedures.c:39
#define INTIMG
Definition: Dildef.h:10
double DB_ScanMaxMin(int w, float *mx, float *mi, int bp)
Find Max and Min values for the buffer of order w.
Definition: Procedures.c:1632
Control Flags.
int ccdWy
CCD Y End.
U16 Idy[MAXIMG]
Definition: Dildef.h:571
int exec
= 0 -> PRG Execution Paused; = 1 -> PRG Execution Resumed
int Wl
Grating Wavelength.
#define TROPOGAS
TROPOspheric Gas Analyzer Spectrometer.
MOBJECT LblExpP[7]
Label for presetted time exposure.
unsigned int s_alt
station altitude
Definition: Dildef.h:722
float s_longitude
station longitude
Definition: Dildef.h:720
char * Fil_CodeNG[]
const char array for the realisation of the Filters buttons in the Filter shell for GASCODNG2 and NG3...
Definition: DAS_Spat.c:216
char * Program
buffer for the PRG file
Definition: DAS_Spat.c:90
char s_name[4]
Station name.
long dp
Grating backslash recovery steps.
unsigned long countsopsh
counting the number of opening shutter
Definition: DAS_Spat.c:133
float RHigh
Definition: Dildef.h:297
float CCDTemp
Actual CCD Temperature.
struct time TIME
Registration time.
Definition: Dildef.h:700
int D_Calibra(void)
Spectral Calibration Procedure for the spectral calibration with HG lamp. Shift of HG spectral line a...
Definition: Procedures.c:2313
#define FDOUBLE
double filter wheel
#define FPLUS
Filter Wheel Torque Motor.
void DB_DefectPixCorr()
Apply thew pixel correction. .
Definition: Procedures.c:1568
char binmirpos[8]
Mirror position.
Definition: Dildef.h:703
d_fileman DFILEM
Definition: bildef.h:257
int avgmode
Flag to set Averaging mode: 0 = Repeat measurements for fixed number, 1 = Repeat measurements for fix...
int Lookfor4358(void)
function to scan the wl() array in order to identify the position of the 4358 reference wavelength re...
Definition: Procedures.c:2270
unsigned short * ImatDark
buffer for the dark image
Definition: DAS_Spatram.h:3045
#define RGBIMG
Definition: Dildef.h:11
#define GASCODNG2
GASCODNG 2 (with ATX power supply & HiresIV)
void __stdcall ShowDown(const char *str, int port)
Shows the countdown. Procedure for the visualisation of the countdown.
Definition: Init.c:98
#define SAVECNT
maximum number of spectra in one file
int ShuttDelay
Definition: Procedures.c:255
char * comment
comment
Definition: Dildef.h:717
long PrgCnt
Counter for the position in the PRG file.
Definition: DAS_Spat.c:93
MOBJECT MDCreateExpo(char *tit)
Create Exposure/countdown shell. Procedure that build the Exposure/countdown shell during the calcula...
Definition: Init.c:55
int VBin
CCD Vertical Binning.
paramini structure . It contains some of the parameters defined in the file PARAM.INI .
#define SPATRAM2
SPATRAM2 (with ATX power supply & HiresV)
int imgfmt
Image format: 0 = ASCII, 1 = BIN.
Definition: Dildef.h:688
int ViewPlot(int mod)
Create the plot shell. Procedure that build the plot shell for the data visualization.
Definition: Spat_Plot.c:3775
float maxtt
Definition: Procedures.c:252
#define SPATRAMPLUS
As SPATRAM plus AltAzimuth platform.
float RLow
Definition: Dildef.h:296
int RItem
number of Pixel, Column, Row Item
Definition: Load.c:40
char CONFIGDIREQUIPMENT[_MAX_PATH]
Definition: bildef.h:208
void Status(char *tit)
Writes information&#39;s on the Status label .
Definition: Load.c:1556
double FlagTExp
Definition: Exec.c:38
void SaveCalib(int mod)
Definition: Procedures.c:2130
int dummy2
Definition: Dildef.h:714
int emshutter
Presence of the CCD Shutter, emshutter = 1 –> Shutter IS Installed, = 0 –> Shutter NOT Installed...
int ccdnr
Row number (ccdyoff-ccdwx)/ccdvbin.
Definition: Dildef.h:699
void delay(unsigned long d)
Pauses for a specified number of milliseconds. .
Definition: Dxl.c:1113
Definition: Dildef.h:240
#define GASCODNG3
GASCODNG 3 (with ATX power supply & HiresIV)
float IHigh
Definition: Dildef.h:303
float s_sza
sun solar zenith angles
Definition: Dildef.h:724
#define PRGFILE
execution driven by *.prg file
char bincomment[255]
comment
Definition: Dildef.h:718
#define VS25
Uniblitz Shutter VS25.
flag FLAG
flags structure
Definition: Procedures.c:37
void D_AverageTFix(void)
NOT Used .
Definition: Procedures.c:2597
float mintt
Definition: Procedures.c:252
int IDY
Number of sensible vertical pixels.
Definition: DAS_Spat.c:119
#define GASCODNG1
GASCODNG 1 (with ATX power supply & HiresIV)
double AutoRange(int mod)
Calculate the exposure time The optimum exposure time (TExp) is calculated with an initial TExpAR=0...
Definition: Procedures.c:899
FilterWheelSt FWS
Filter Wheel.
Definition: Procedures.c:34
Function prototypes.
#define GASCODNG4
GASCODNG 4.
char * Fil_CodeNG1[]
const char array for the realisation of the Filters buttons in the Filter shell for GASCODNG1 instrum...
Definition: DAS_Spat.c:213
void D_Saving(void)
Saving image. Procedure for saving the CCD Image Used since version 2.1.0. The coiche between binary ...
Definition: Procedures.c:1369
int TOTTEXP
Total Exposure time in Averaging mode.
Definition: DAS_Spat.c:115
float s_latitude
station latitude
Definition: Dildef.h:719
#define _MAX_PATH
Definition: Dildef.h:202
#define FSIMPLE
simple filter wheel
int MMessageDialog(const char *t, const char *msg, const char *btn1, const char *btn2,...)
S16 dy
Num. of Y pixels.
Definition: dcldef.h:1023
#define SPATRAM3
SPATRAM3.
double TExp
CCD Exposure time.
Definition: DAS_Spat.c:130
void D_Measure(void)
Definition: Procedures.c:2504
int CItem
Definition: Load.c:40
CTYPE void DLL DC_GetCCD(int Mode, double Exposure, U16 *mat, int Camera)
Get an image at full resolution. Procedure that allows you to read an image, given an exposure time e...
Function prototypes.
float ILow
Definition: Dildef.h:302
char binfilpos[5]
Filter position.
Definition: Dildef.h:706
const char * filpos
Filter position.
Definition: Dildef.h:705
int PItem
Definition: Load.c:40
#define SZAMODE
execution driven by SZAPRG file
int SM_B
Motor B (su Y12) respectively: ID=0->Grating,ID=1->Not Connected,ID=2->Azimuth.
ZenithSt ZST
Definition: Procedures.c:44
char s_name[4]
station localization
Definition: Dildef.h:721
int mirrorimg[2]
Flag to set the mirroring of the CCD image: 0 = NO Mirroring, 1= YES Mirroring (element 0 for X axis;...
char * Fil_Code[]
const char array for the realisation of the Filters buttons in the Filter shell
Definition: DAS_Spat.c:207
void E_REM(void)
Flush string since the Carriege Return. Procedure for flushing the readed string since the Carriege R...
Definition: Exec.c:47
int DB_GetCCD(int mod)
Get CCD Image. .
Definition: Procedures.c:1749
float * Fmat
Definition: Dildef.h:287
void GetDateTime(struct date *dp, struct time *tp, struct tm *when)
Definition: Save.c:43
int wl[]
WaveLength definition - for SD_Grating.
Definition: DAS_Spat.c:182
int pixcorr
Flag to set defective pixel correction: 0=NOT Corrected, 1=Corrected.
controlpanel ContrPanel
Control Panel Structure.
AzimuthSt AST
Definition: Procedures.c:45
char mirpos[8]
Mirror position.
Definition: Dildef.h:702
d_geo DGEO
geographical coordinates structure
Definition: Procedures.c:41
Bortul Image Library Structure .
Definition: bildef.h:250
int imgtype
Flag to set Image type: 0=MONOINT, 1=RGB.
long * AVGmat
buffer for averaging measurements
void MaskCol(int a, int b, int c, int d, int e)
Definition: Procedures.c:1548
double longitude
Station longitude.
#define AMS1
AMS1 address.
int ccdye
Y End.
Definition: Dildef.h:697
int measmode
measuring mode: 0 = Single; 1 = Averaged
void RemoveBlindPixels(void)
Definition: Procedures.c:1600
int dummy1
Definition: Dildef.h:713
int ccdxs
X Start.
Definition: Dildef.h:694
static double st
Definition: SOLPOS.C:142
void ShutterOpening(int mod)
Definition: Procedures.c:1685
MOBJECT Albl_IE[1]
Active Control Panel Labels for In Execution status.
char newdate[8]
Definition: bildef.h:159
int step[AMSMAX][2]
Stepper Motors Step.
void * MOBJECT
Definition: Mguidefs.h:192
C_DEF void DI_WriteDigit(MOBJECT *pix, char *fmt)
Definition: Dildef.h:234
Status of the Mirror .
double maxtexp
Maximum exposure time (used in Autorange())
Definition: dcldef.h:1020
void AutoR_SetND_Filter(void)
Definition: Procedures.c:261
Definition: dcldef.h:1043
int MaskedRow[100][6]
Masked rows array.
Definition: Load.c:39
char * Fil_CodeTropo[]
const char array for the realisation of the Filters buttons in the Filter shell for TROPOGAS instrume...
Definition: DAS_Spat.c:210
struct time TIME
Definition: Dildef.h:347
double realzenpos
int fftprh
Definition: Procedures.c:50
int EqType
Spectrometer Type.
int N_RM
Number of measurements for Averaging mode in fixed exposure time mode.
Definition: DAS_Spat.c:114
int HGFlg
Definition: DAS_Spatram.h:2968
int fftsavecal
Definition: Procedures.c:51
int lambda
wavelength value
Definition: Dildef.h:704
float GLow
Definition: Dildef.h:298
int BlindPix
Number of blind pixels (usefull for Dark Current)
Definition: DAS_Spat.c:120
int N_AVG
Number of measurements for Averaging mode in fixed number of meas. mode.
Definition: DAS_Spat.c:113
int darksource
Flag to set Dark source: 0 = Blind Pixels, 1 = Full Frame.
float Low
Definition: Dildef.h:294
float ccdtexp
ccd Exposure time
Definition: Dildef.h:708
Status of the Azimuth .
int SD_NDFilterW(int filter)
Positioning Filter wheel. The filter wheel move on the filter number specified by filter...
Definition: Spat_Device.c:2354
Status of the Filter wheel .
int ccdYoff
CCD Y Start.
GratingSt GRS
Grating.
Definition: Procedures.c:36
int shuttertype
type of installed shutter: 0 = VS25, 1 = Rotating Shutter, 2 = TorqueMotor
unsigned int maxlvlar
maximum level of autoranging (max 65000)
unsigned int imgmax
Image maximum value.
Definition: Dildef.h:709
float High
Definition: Dildef.h:295
MirrorSt MIS
Mirror.
Definition: Procedures.c:33
MOBJECT LblExpD[7]
Label for time exposure countdown.
Status of the Grating .
int SM_stepr(int t)
AMS &#39;steps/round&#39; value return the AMS &#39;steps/round&#39; value (microstepping) depending on the setted st...
Definition: Spat_Device.c:209
int exemode
Set the Execution MODE (Execution modes)
void DB_MirrorX(int w)
Definition: Procedures.c:201
double AutoR_TestMeas(int mod, double texpmin, double texpar, float level, float levelinf)
Definition: Procedures.c:334
int ccdvbin
Vertical binning value.
Definition: Dildef.h:698
float BLow
Definition: Dildef.h:300
int DB_StepMotor(int id, int motor, long step, int dir)
Send steps to the selected stepper motor. .
Definition: Dxl.c:816
#define MANUAL
execution in manual mode (for lab use)
double AutoR_With_Shutt()
Definition: Procedures.c:627
marconi Marconi
CCD Sensor struct.
int MaskedCol[100][6]
Masked column array.
Definition: Load.c:38
const char * Mir_CodeTropo[]
const char array for the realisation of the Mirrors buttons in the Mirror shell for TROPOGAS ...
Definition: DAS_Spat.c:234
paramini Paramini
structure for the parameters defined in param.ini configuration file
int SaveCnt
Definition: Procedures.c:49
______________________________________________________________________________________
Generated on Mon Sep 18 2017 11:44:09 for DAS - Rel. 3.1.6 - 18/09/2017.