I have checked the above script with TRAJ and MERGE command. This works fine for DCD from Charmm but it gives error with DCD coming from NAMD.

I have Also noticed that while reading Charmm gets some extra information like number of previous steps while reading charmm dcd but this is set to 0 if dcd is read from namd.

I will have to ask in NAMD forum about this.