Coverting move time to clock time in pgn

Discussion of anything and everything relating to chess playing software and machines.

Moderator: Ras

Odd Gunnar Malin
Posts: 310
Joined: Wed Mar 08, 2006 9:59 pm
Location: Norway, Vads?
Full name: Odd Gunnar Malin

Coverting move time to clock time in pgn

Post by Odd Gunnar Malin »

Is there a program to convert [%emt to [%clk in a pgn file?

I want to export game played in Fritz to Lichess study. Fritz use move time but Lichess can only import clock time.

What I try to do is to play "serious" game against an engine, If there is other GUI that support what I need, let me know. I have Hiarcs, Shredder, Aquarium and Fritz.

My requirements are:
* Playing with Fide timecontrol. e.g. 90 min. in 40 moves the 30 min. to finish the game with 30 se. inc. from move one.
* Using the DGT board
* Save time usage in the game, but not the pve or eval.
* Can use Dragon and Hiarcs as an engine.

Fritz are coming close with onlty using wrong time format.

I will play an "important" tournament in a month and need to play some training games that I share with my coach as Lichess study. One of my problem is time management so it is important to have the time usage in the score.
Ferdy
Posts: 4851
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Coverting move time to clock time in pgn

Post by Ferdy »

Odd Gunnar Malin wrote: Thu Jun 09, 2022 8:34 am Is there a program to convert [%emt to [%clk in a pgn file?

I want to export game played in Fritz to Lichess study. Fritz use move time but Lichess can only import clock time.

What I try to do is to play "serious" game against an engine, If there is other GUI that support what I need, let me know. I have Hiarcs, Shredder, Aquarium and Fritz.

My requirements are:
* Playing with Fide timecontrol. e.g. 90 min. in 40 moves the 30 min. to finish the game with 30 se. inc. from move one.
* Using the DGT board
* Save time usage in the game, but not the pve or eval.
* Can use Dragon and Hiarcs as an engine.

Fritz are coming close with onlty using wrong time format.

I will play an "important" tournament in a month and need to play some training games that I share with my coach as Lichess study. One of my problem is time management so it is important to have the time usage in the score.
Here is a python script to convert emt to clk.

emt2clk.py

Code: Select all

"""
requirements:
    pip install chess
"""


import chess.pgn


def convert(fn):
    with open(fn, 'r') as f:
        while True:
            game = chess.pgn.read_game(f)
            if game is None:
                break

            # TC 90m+30s in 40moves + 30m+30s for the rest of the game.
            base_time = 90  # min
            base_time *= 60  # to sec
            inc_time = 30 #sec
            time_added_after_forty = 30  # min
            time_added_after_forty *= 60  # to sec
            movenum_addtime = 41

            start_time = base_time + inc_time
            clk = [start_time, start_time]

            mygame = chess.pgn.Game()
            mygame.headers = game.headers.copy()
            mynode = mygame

            for node in game.mainline():
                board = node.parent.board()
                stm = board.turn
                fmvn = board.fullmove_number

                mynode = mynode.add_main_variation(node.move)

                # 1. Subtract emt from start time.
                clk[not stm] -= node.emt()

                # 2. Add 30m after 40 moves are completed, add once only.
                if fmvn == movenum_addtime:
                    clk[not stm] += time_added_after_forty

                # 3. Add 30s increment.
                clk[not stm] += inc_time

                # 4. Set clk as move comment.
                mynode.set_clock(clk[not stm])

            print(f'{mygame}\n\n')


if __name__ == '__main__':
    fn = 's1.pgn'
    convert(fn)
command line:

Code: Select all

python emt2clk.py
Rename your input pgn to s1.pgn or revise the code. You may revise the time control see the comment in the code "# TC 90m+30s in 40moves + 30m+30s for the rest of the game." Experiment if adding 30min is at move 40 or 41 by modifying the value in "movenum_addtime = 41".

I tested it in a 3+0 game in chessbase site then convert the game.

Code: Select all

[Event "3 min, unrated"]
[Site "Café"]
[Date "2022.06.09"]
[Round ""]
[White "Guest24951933"]
[Black "Guest24951905"]
[Result "1-0"]
[Annotator ""]
[PlyCount "105"]

1. d4 { [%clk 0:02:58] } 1... d5 { [%clk 0:02:58] } 2. c4 { [%clk 0:02:56] } 2... e5 { [%clk 0:02:57] } 3. cxd5 { [%clk 0:02:54] } 3... Qxd5 { [%clk 0:02:55] } 4. Nc3 { [%clk 0:02:51] } 4... Qxd4 { [%clk 
0:02:55] } 5. Qxd4 { [%clk 0:02:49] } 5... exd4 { [%clk 0:02:54] } 6. Nb5 { [%clk 0:02:48] } 6... Bb4+ { [%clk 0:02:45] } 7. Bd2 { [%clk 0:02:46] } 7... Bd6 { [%clk 0:02:43] } 8. Nxd4 { [%clk 0:02:44] } 8... Nf6 { [%clk 0:02:42] } 9. f3 { [%clk 0:02:42] } 9... Nc6 { [%clk 0:02:41] } 10. Nxc6 { [%clk 0:02:40] } 10... bxc6 { [%clk 0:02:40] } 11. e4 { [%clk 0:02:39] } 11... O-O { [%clk 0:02:37] } 12. Rc1 { [%clk 0:02:37] } 12... c5 { [%clk 0:02:35] } 13. Ne2 { [%clk 0:02:31] } 13... Nd7 { [%clk 0:02:34] } 14. Nc3 { [%clk 0:02:29] } 14... Bb7 { [%clk 0:02:26] } 15. Be2 { [%clk 0:02:24] } 15... Rad8 { [%clk 0:02:23] } 16. Be3 { [%clk 0:02:21] } 16... Rfe8 { [%clk 0:02:19] } 17. Kf2 { [%clk 0:02:19] } 17... Be5 { [%clk 0:02:07] } 18. g3 { [%clk 0:02:16] } 18... Bd4 { [%clk 0:02:06] } 19. Rhd1 { [%clk 0:02:15] } 19... Ne5 { [%clk 0:01:56] } 20. Na4 { [%clk 0:02:05] } 20... Bxe3+ { [%clk 0:01:23] } 21. Kxe3 { [%clk 0:02:03] } 21... Rxd1 { [%clk 0:01:22] } 22. Rxd1 { [%clk 0:02:00] } 22... c4 { [%clk 0:01:20] } 23. Nc5 { [%clk 0:01:50] } 23... Bc6 { [%clk 0:01:15] } 24. Rc1 { [%clk 0:01:44] } 24... f5 { [%clk 0:01:04] } 25. Bxc4+ { [%clk 0:01:42] } 25... Nxc4+ { [%clk 0:01:02] } 26. Rxc4 { [%clk 0:01:41] } 26... fxe4 { [%clk 0:01:01] } 27. fxe4 { [%clk 0:01:40] } 27... Bb5 { [%clk 0:00:57] } 28. Rd4 { [%clk 0:01:38] } 28... a6 { [%clk 0:00:56] } 29. b3 { [%clk 0:01:37] } 29... Kf7 { [%clk 0:00:45] } 30. a4 { [%clk 0:01:36] } 30... Bc6 { [%clk 0:00:44] } 31. Nxa6 { [%clk 0:01:35] } 31... Re5 { [%clk 0:00:41] } 32. Nxc7 { [%clk 0:01:33] } 32... Rc5 { [%clk 0:00:39] } 33. Rc4 { [%clk 0:01:31] } 33... Rxc4 { [%clk 0:00:36] } 34. bxc4 { [%clk 0:01:30] } 34... Bxa4 { [%clk 0:00:36] } 35. Kd4 { [%clk 0:01:29] } 35... Ke7 { [%clk 0:00:32] } 36. Nd5+ { [%clk 0:01:27] } 36... Kd6 { [%clk 0:00:31] } 37. e5+ { [%clk 0:01:26] 
} 37... Ke6 { [%clk 0:00:28] } 38. Ne3 { [%clk 0:01:24] } 38... g6 { [%clk 0:00:23] } 39. h4 { [%clk 0:01:23] } 39... Bc6 { [%clk 0:00:19] } 40. Kc5 { [%clk 0:01:22] } 40... Bf3 { [%clk 0:00:17] } 41. Kd4 { [%clk 0:01:21] } 41... Bc6 { [%clk 0:00:15] } 42. Nd5 { [%clk 0:01:19] } 42... Ba4 { [%clk 0:00:12] } 43. Nb6 { [%clk 0:01:15] } 43... Bc6 { [%clk 0:00:10] } 44. Nd5 { [%clk 0:01:13] } 44... Ba4 { [%clk 0:00:08] } 45. Nf4+ { [%clk 0:01:12] } 45... Kf5 { [%clk 0:00:07] } 46. e6 { [%clk 0:01:11] } 46... Kg4 { [%clk 0:00:04] } 47. e7 { [%clk 0:01:10] } 47... Kxg3 { [%clk 0:00:04] } 48. Ne6 { [%clk 0:01:07] } 48... Kxh4 { [%clk 0:00:03] } 49. c5 { [%clk 0:01:05] } 49... g5 { [%clk 0:00:02] } 50. Kd5 { [%clk 0:01:04] } 50... g4 { [%clk 0:00:01] } 51. Kd6 { [%clk 0:01:03] } 51... g3 { [%clk 0:00:00] } 52. c6 { [%clk 0:01:02] } 52... g2 { [%clk 0:00:00] } 53. e8=Q { [%clk 0:01:01] } 1-0
My opponent with color black loses on time in this game and it shows - 52... g2 { [%clk 0:00:00] }
Odd Gunnar Malin
Posts: 310
Joined: Wed Mar 08, 2006 9:59 pm
Location: Norway, Vads?
Full name: Odd Gunnar Malin

Re: Coverting move time to clock time in pgn

Post by Odd Gunnar Malin »

Ferdy wrote: Thu Jun 09, 2022 2:05 pm
Here is a python script to convert emt to clk.
Thanks, I will test it. I'm not too familiar with python but should manage this. By the way Chessbase set the timecontrol as a tag (following the standard with an extra 0/0 in the middle). So it should be doable to get this from the pgn.
But for my use this script is enough. I don't play too many games with 90+30 time control.
Odd Gunnar Malin
Posts: 310
Joined: Wed Mar 08, 2006 9:59 pm
Location: Norway, Vads?
Full name: Odd Gunnar Malin

Re: Coverting move time to clock time in pgn

Post by Odd Gunnar Malin »

Odd Gunnar Malin wrote: Thu Jun 09, 2022 2:50 pm
Ferdy wrote: Thu Jun 09, 2022 2:05 pm
Here is a python script to convert emt to clk.
Thanks, I will test it. I'm not too familiar with python but should manage this. By the way Chessbase set the timecontrol as a tag (following the standard with an extra 0/0 in the middle). So it should be doable to get this from the pgn.
But for my use this script is enough. I don't play too many games with 90+30 time control.
Tested it and it worked great with importing Fritz games into Lichess and have the clock showing time used.
One warning, Fritz break lines in the middle of a emt command so this must be fixed manually. There could be added up some small (seconds) difference but this doesn't matter. The important is to see where you are in timetrouble and where you used too much or too little time.

Thanks again, you saved me for some hours with c++ programming.
JohnS
Posts: 215
Joined: Sun Feb 24, 2008 2:08 am

Re: Coverting move time to clock time in pgn

Post by JohnS »

Odd Gunnar Malin wrote: Fri Jun 10, 2022 1:14 pm
Tested it and it worked great with importing Fritz games into Lichess and have the clock showing time used.
One warning, Fritz break lines in the middle of a emt command so this must be fixed manually. There could be added up some small (seconds) difference but this doesn't matter. The important is to see where you are in timetrouble and where you used too much or too little time.

Thanks again, you saved me for some hours with c++ programming.
You can use sed to fix the line breaks. Tested on win10.

1. Create a sed text input file say emt.dat with just the following line:
s/\(\[%emt\)\s*\(.*\]\)/\1 \2/g

2. Run the command:
sed -e "1h;2,$H;$!d;g" -f emt.dat infile.pgn > outfile.pgn

This will put all the emt codes on one line and will work on inputs like

[%emt
xxxx]

with spaces after emt and before xxxx.
Odd Gunnar Malin
Posts: 310
Joined: Wed Mar 08, 2006 9:59 pm
Location: Norway, Vads?
Full name: Odd Gunnar Malin

Re: Coverting move time to clock time in pgn

Post by Odd Gunnar Malin »

JohnS wrote: Sat Jun 11, 2022 7:13 am
Odd Gunnar Malin wrote: Fri Jun 10, 2022 1:14 pm
Tested it and it worked great with importing Fritz games into Lichess and have the clock showing time used.
One warning, Fritz break lines in the middle of a emt command so this must be fixed manually. There could be added up some small (seconds) difference but this doesn't matter. The important is to see where you are in timetrouble and where you used too much or too little time.

Thanks again, you saved me for some hours with c++ programming.
You can use sed to fix the line breaks. Tested on win10.

1. Create a sed text input file say emt.dat with just the following line:
s/\(\[%emt\)\s*\(.*\]\)/\1 \2/g

2. Run the command:
sed -e "1h;2,$H;$!d;g" -f emt.dat infile.pgn > outfile.pgn

This will put all the emt codes on one line and will work on inputs like

[%emt
xxxx]

with spaces after emt and before xxxx.
Thanks, but for my use it is faster to do it manually, it's only one game at the time. I see the error when I do the copy/paste from Fritz. Could though be usefull if someone who want to do it on all their games in one go. You could maybe make a script that run on right click, but then it would be better to put this regex line into the python script. For me I maybe manage ten games in this training month so there is no need for it.

I did only mention it if other will try the script and get an error code when running it.