SetDirectory["E:\\Presentations\\BAO\\Amas 21cm\\Abell2440"]

data =Import["sca150585.230.txt","Table"];

start = StringSplit[FindList["sca150585.230.txt","HDEP"]];

DateOfStart= start[[1,2]]

HeureOfStart = start[[1,3]]

acq= StringSplit@FindList["sca150585.230.txt","PERI"];

PermierCycle = acq[[1,6]]

DernierCycle = acq[[-1,6]]

acq[[-1]]

Cycle2= Cases[acq,x_/;x[[6]]==ToString[2]]

cycle2Calib1Start = Cycle2[[1,3]]

cycle2calibType1 = Cases[Cycle2,x_/;x[[7]]==ToString[-1]]

cycle2calibType1[[All,3]]

cycle2FirstCalibStart = cycle2calibType1[[1,3]]

cycle2FirstCalibEnd =  cycle2calibType1[[Length[cycle2calibType1]/2,3]]

cycle2SecondCalibStart = cycle2calibType1[[Length[cycle2calibType1]/2+1,3]]

cycle2SecondtCalibEnd =  cycle2calibType1[[-1,3]]

data1cycle2=Cases[Cycle2,x_/;x[[7]]==ToString[1]]

cycle2Data1Start=data1cycle2[[1,3]]

DateString[DatePlus[cycle2Data1Start,{ToExpression[data1cycle2[[1,8]]],"Second"}],"Time"]

data2cycle2=Cases[Cycle2,x_/;x[[7]]==ToString[2]]

cycle2Data2Start=data2cycle2[[1,3]]

DateString[DatePlus[cycle2Data2Start,{ToExpression[data2cycle2[[1,8]]],"Second"}],"Time"]

Clear[decodeCycle];
decodeCycle[c_List]:=Module[
{i,calibType1,firstCalibStart,firstCalibEnd,secondCalibStart,secondCalibEnd,data1,data1Start,data1End,data2,data2Start,data2End},
calibType1 = Cases[c,x_/;x[[7]]==ToString[-1]];
firstCalibStart = calibType1[[1,2]]<>" "<>calibType1[[1,3]];
i = Length[calibType1]/2;
firstCalibEnd =  calibType1[[i,2]]<>" " <>calibType1[[i,3]];
secondCalibStart = calibType1[[i+1,2]]<>" "<> calibType1[[i+1,3]];
secondCalibEnd =  calibType1[[-1,2]]<>" "<>calibType1[[-1,3]];
data1=Cases[c,x_/;x[[7]]==ToString[1]];
data1Start=data1[[1,2]]<>" "<>data1[[1,3]];
data1End =DateString[DatePlus[data1Start,{ToExpression[data1[[1,8]]],"Second"}],{"Day","/","Month","/","YearShort"," ","Time"}];
data2=Cases[c,x_/;x[[7]]==ToString[2]];
data2Start=data2[[1,2]]<>" "<>data2[[1,3]];
data2End =DateString[DatePlus[data2Start,{ToExpression[data2[[1,8]]],"Second"}],{"Day","/","Month","/","YearShort"," ","Time"}];
Return[firstCalibStart<>", "<>firstCalibEnd<>", "<>data1Start<>", "<>data1End<>", "<>secondCalibStart<>", "<>secondCalibEnd<>", "<>data2Start<>", "<>data2End]
];

Clear[dump];
dump[f_String]:=Module[
{fileOut,start,acq,premierCycle,dernierCycle,cycles},
fileOut = OpenWrite[FileBaseName[f]<>".out"];
SetOptions[fileOut,FormatType->OutputForm,PageWidth->255];
start =StringSplit[FindList[f,"HDEP"]];
(*Write[fileOut,start[[1,2]]<>" "<>start[[1,3]]];*)
acq= StringSplit@FindList[f,"PERI"];
premierCycle = ToExpression[acq[[1,6]]];
dernierCycle = ToExpression[acq[[-1,6]]];
(* Print["cycle 2",Cases[acq,x_/;x[[6]]==ToString[2]]]; *)
cycles=Table[Cases[acq,x_/;x[[6]]==ToString[n]],{n,premierCycle,dernierCycle}];
(* Print[cycles[[2]]];*)
(*Write[fileOut,decodeCycle[cycles[[2]]]];*)
Map[Write[fileOut,decodeCycle[#]]&,cycles];
Close[fileOut];
];

dump["sca150585.230.txt"]

Close["sca150585.230.out"]

str=OpenWrite["test.out"]

Write[str,Flatten[{-1,2}]]

Close[str]