File: animate.py

package info (click to toggle)
gyoto 2.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,420 kB
  • sloc: cpp: 42,317; sh: 4,512; python: 3,436; xml: 2,865; makefile: 685; ansic: 346
file content (827 lines) | stat: -rwxr-xr-x 29,949 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#!/usr/bin/env python3
'''Create movies using Gyoto

This module provides facilities to animate Gyoto Sceneries and render
them as movie files. The module can also be called as a script on the
command line.

rayTrace() takes as argument a user-defined callable func and a
sceneray (among other parameters). func is responsible for mutating
the scenery for each frame. raTrace then ray-traces the scenery. Two
callables suitable as the func argument are provided.

mk_movie() is the top-level function. It opens a video file, calls
rayTrace() in a loop and closes the video.

Classes:
VideoWriter -- abstract interface with video library
OpenCVVideoWriter -- implements VideoWriter using OpenCV-Python
PyAVVideoWriter -- implements VideoWriter using PyAV
NullVideoWriter -- a VideoWriter that does not write anything
orbiting_screen -- a callable that can be used by rayTrace
orbiting_screen_forward -- idem
accelerating_tangential_screen -- idem
growing_mass -- idem

Function:
rayTraceFrame -- mutates Gyoto scenery and raytrace it
defaultScenery -- returns a default Gyoto scenery
defaultTrajectory -- returns a default screen trajectory
mk_video -- make a video

'''
try:
    from . import core, std
except ImportError:
    from gyoto import core, std
import numpy
import os
import matplotlib.pyplot as plt
import matplotlib
import argparse
import warnings

### Helper functions

## An API for video-writing facilities

class VideoWriter:
    '''Generic interface to write videos
    '''

    norm=None
    vmin=0.
    vmax=0.
    cmap=plt.cm.get_cmap('hot')
    
    def __init__(self, filename, fps, width, height):
        '''Initialize video

        Derived classes should open the video in there initializer and
        call the base class __init__.

        '''
        self.fps=fps
        self.width=width
        self.height=height

    def colorize(self, im_float):
        '''Colorize image

        Given a Gyoto frame (numpy array of doubles), returns and RGB
        image (3 planes of uint8 type).

        '''
        if self.norm is None:
            if self.vmax==0:
                self.vmax = numpy.nanmax(im_float)
            if self.vmax != 0.:
                self.norm=matplotlib.colors.Normalize(vmin=0., vmax=self.vmax, clip=True)
                im_float = self.norm(im_float)
        else:
            im_float=self.norm(im_float)
        return numpy.uint8(self.cmap(im_float)*255.)[:, :, :3]

    def write(self, frame):
        '''Write frame to video

        Frame is a numpy RGB image.
        '''
        raise NotImplementedError

    def close(self):
        '''Close video
        '''
        raise NotImplementedError

    def __del__(self):
        self.close()

class NullVideoWriter(VideoWriter):
    '''A VideoWriter that does not write anything

    Mostly useful during preparatory work in cunjunction with
    plot=True.
    '''
    def write(self, frame):
        '''Write frame to video

        Frame is a numpy RGB image.
        '''
        pass

    def close(self):
        '''Close video
        '''
        pass

class OpenCVVideoWriter(VideoWriter):
    '''An implementation of VideoWriter that uses OpenCV-python
    '''
    video=None
    fourcc=0

    def __init__(self, filename, fps, width, height):
        import cv2
        VideoWriter.__init__(self, filename, fps, width, height)
        self.video=cv2.VideoWriter(filename, self.fourcc, fps, (width, height), True)

    def write(self, frame):
        self.video.write(frame[::-1, :, ::-1])

    def close(self):
        if self.video is not None:
            self.video.release()
            self.video=None

class PyAVVideoWriter(VideoWriter):
    '''An implementation of VideoWriter that uses PyAV
    '''
    container=None
    stream=None
    fourcc=0

    def __init__(self, filename, fps, width, height,
                 codec_name='mpeg4', pix_fmt='yuv420p'):
        import av
        self.av=av
        VideoWriter.__init__(self, filename, fps, width, height)
        self.container = av.open(filename, mode='w')
        self.stream = self.container.add_stream(codec_name, rate=fps)
        self.stream.width = width
        self.stream.height = height
        self.stream.pix_fmt = pix_fmt

    def write(self, frame):
        avframe=self.av.VideoFrame.from_ndarray(frame[::-1,:,:], format='rgb24')
        for packet in self.stream.encode(avframe):
            self.container.mux(packet)

    def close(self):
        if self.container is not None:
            for packet in self.stream.encode():
                self.container.mux(packet)
            self.container.close()
            self.stream=None
            self.container=None

## Two types of changes for the screen
#
# The rayTrace function below takes a callable as argument to mutate
# the screen between exposures. We define two such callables (as
# classes) for two kinds of videos below.
#

class accelerating_tangential_screen:
    '''The screen does not move but has increasing velocity
    
    Members:
    maxvel -- norm of velocity for last frame. Should never reach 1!
    '''

    maxvel=0.99

    def __init__(self, **args):
        for key in args:
            setattr(self, key, args[key])

    def __call__(self, sc, k, nframes):
        scr=sc.screen()
        metric=sc.metric()
        pos=scr.getObserverPos()
        ucirc=metric.circularVelocity(pos)
        uzamo=metric.zamoVelocity(pos)
        Gamma=-metric.ScalarProd(pos, ucirc, uzamo)
        Vzamo=ucirc/Gamma-uzamo           # ucirc as seen by zamo
        norm_circ=metric.ScalarProd(pos, Vzamo, Vzamo)
        norm_wanted=self.maxvel*k/(nframes-1)
        Vzamo *= norm_wanted/numpy.sqrt(norm_circ)    # rescale velocity
        Gamma2 = 1./(1.-metric.ScalarProd(pos, Vzamo, Vzamo))
        assert Gamma2 >= 0, 'Gamma2 < 0! VzamoxVzamo='+str(metric.ScalarProd(pos, Vzamo, Vzamo))+', norm_wanted='+str(norm_wanted)+', norm_circ='+str(norm_circ)
        Gamma=numpy.sqrt(Gamma2)
        fourvel=Gamma*(uzamo+Vzamo)
        scr.fourVel(fourvel)

class static_screen:
    '''The screen does not move

    This is meaningful when the astrobj itself is changing. The camera
    will typically be far away, with ObserverKind set to
    ObserverAtInfinity.

    members:
    tref -- reference time, read from sc.screen() the first time
          __call__ is called
    t0 -- coordinate time at movie start
    t1 -- coordinate time at movie end
    unit -- unit in which t0 and t1 are expressed
    '''
    tref=None
    t0=0.
    t1=1000.
    unit='geometric'

    def __init__(self, **args):
        for key in args:
            setattr(self, key, args[key])

    def __call__(self, sc, k, nframes):
        '''update time in screen
        '''
        if self.tref is None:
            self.tref=sc.screen().time(self.unit)
        t = self.t0+k*(self.t1-self.t0)/(nframes-1)
        sc.screen().time(self.tref+t, self.unit)

class orbiting_screen:
    '''The screen follows an orbit, camera looks along -er

    members:
    t0 -- proper time at movie start
    t1 -- proper time at movie end
    trajectory -- a gyoto.std.Star (or anything else that provides
          getCoord(t, coord, proper).
    '''
    t0=0.
    t1=1000.
    trajectory=None

    def __init__(self, **args):
        for key in args:
            setattr(self, key, args[key])

    def __call__(self, sc, k, nframes):
        t = self.t0+k*(self.t1-self.t0)/(nframes-1)
        coord=core.vector_double(8)
        self.trajectory.getCoord(t, coord, True)
        pos=[coord[i] for i in range(4)]
        vel=[coord[i] for i in range(4, 8)]
        screen=sc.screen()
        screen.setObserverPos(pos)
        screen.fourVel(vel)

class orbiting_screen_forward:
    '''The screen follows an orbit, camera looks forward

    members:
    t0 -- proper time at movie start
    t1 -- proper time at movie end
    roll -- roll angle: left is rotated roll degrees towards up
    trajectory -- a gyoto.std.Star (or anything else that provides
          getCoord(t, coord, proper).
    '''
    t0=0.
    t1=1000.
    roll=0.
    trajectory=None

    def __init__(self, **args):
        for key in args:
            setattr(self, key, args[key])
        self.metric=self.trajectory.metric()

    def __call__(self, sc, k, nframes):
        metric=sc.metric()
        tau = self.t0+k*(self.t1-self.t0)/(nframes-1)
        coord=core.vector_double(8)
        self.trajectory.getCoord(tau, coord, True)
        pos=numpy.asarray([coord[i] for i in range(4)])
        vel=numpy.asarray([coord[i] for i in range(4, 8)])
        screen=sc.screen()
        screen.setObserverPos(pos)
        screen.fourVel(vel)

        # The 3 other elements of the tetrad are initialized as
        # follows: front is along the 3-velocity; up is er projected
        # on the plane orthogonal to front; left is the external
        # product up x front, thus (left, up, front) is direct.  Then
        # we orthonormalise this tetrad. Obviously, this will fail if
        # vel3c is along er.
        #
        # We convert everything to cartesian to compute the external
        # product.
        front=numpy.zeros(4)
        metric.cartesianVelocity(coord, front[1:])
        front /= numpy.sqrt((front*front).sum())
        if metric.coordKind()==core.GYOTO_COORDKIND_SPHERICAL:
            t=pos[0]
            r=pos[1]
            theta=pos[2]
            phi=pos[3]
            st=numpy.sin(theta)
            ct=numpy.cos(theta)
            sp=numpy.sin(phi)
            cp=numpy.cos(phi)
            posr=[pos[0], r*st*cp, r*st*sp, r*ct]
        else:
            posr=pos
            r=numpy.sqrt(pos[1:]**2).sum()
        up0=numpy.concatenate(([0.], posr[1:]/r))
        up0 -= (front*up0).sum()*front
        up0 /= numpy.sqrt((up0*up0).sum())
        left0=numpy.zeros(4)
        left0[1]=up0[2]*front[3]-up0[3]*front[2]
        left0[2]=up0[3]*front[1]-up0[1]*front[3]
        left0[3]=up0[1]*front[2]-up0[2]*front[1]
        rollr=self.roll*numpy.pi/180
        cr=numpy.cos(rollr)
        sr=numpy.sin(rollr)
        up=cr*up0-sr*left0
        left=cr*left0+sr*up0

        if metric.coordKind()==core.GYOTO_COORDKIND_SPHERICAL:
            er=posr/r
            ephi=numpy.asarray([0., -sp, cp, 0.])
            etheta=numpy.asarray([0., ct*cp, ct*sp, -st])
            front=numpy.asarray([
                0.,
                (er*front).sum(),
                (etheta*front).sum(),
                (ephi*front).sum()
            ])
            up=numpy.asarray([
                0.,
                (er*up).sum(),
                (etheta*up).sum(),
                (ephi*up).sum()
            ])
            left=numpy.asarray([
                0.,
                (er*left).sum(),
                (etheta*left).sum(),
                (ephi*left).sum()
            ])
        metric.GramSchmidt(pos, vel, up, front, left);
        screen.screenVector1(left)
        screen.screenVector2(up)
        screen.screenVector3(front)

class growing_mass:
    '''The mass of the central object changes

    The Astrobj needs to be PatternDisk-like (it needs to have the
    properties InnerRadius and OuterRadius).

    '''
    delta0=0.01
    deltaMax0=1.
    rin0=0.
    rout0=28.
    rmax0=50.
    d0=28.
    factor_first=100.
    factor_last=2.01/28.

    def __init__(self, scenery=None, **args):
        if scenery is not None:
            self.delta0=scenery.delta()
            scr=scenery.screen()
            self.d0=scr.distance('geometrical')
            ao=scenery.astrobj()
            self.deltaMax0=ao.deltaMaxInsideRMax()
            self.rin0=ao.get('InnerRadius', 'geometrical')
            self.rout0=ao.get('OuterRadius', 'geometrical')
            self.rmax0=ao.rMax()
            self.factor_last=2.01/self.d0
            scenery.screen().observerKind('ZAMO')
        for key in args:
            setattr(self, key, args[key])

    def __call__(self, sc, k, nframes):
        # factor=(((self.factor_last*k)
        #          +(self.factor_first*(self.nframes-1-k)))
        #         /(self.nframes-1))
        # print(factor)
        log2_first=numpy.log2(self.factor_first)
        log2_last=numpy.log2(self.factor_last)
        log2_k=(((log2_last*k)
                 +(log2_first*(nframes-1-k)))
                /(nframes-1))
        factor=2.**log2_k
        sc.delta(factor*self.delta0)
        scr=sc.screen()
        scr.distance(factor*self.d0, 'geometrical')
        print(factor, scr.distance('geometrical'))
        ao=sc.astrobj()
        ao.deltaMaxInsideRMax(factor*self.deltaMax0)
        ao.set('InnerRadius', factor*self.rin0, 'geometrical')
        ao.set('OuterRadius', factor*self.rout0, 'geometrical')
        ao.rMax(factor*self.rmax0)

## Helper function for tracing one frame

def rayTraceFrame(sc, func, k, nframes, width, height):
    '''Ray-trace one frame of a Gyoto video

    Parameters:
      sc:     Scenery to ray-trace
      func:   callable to mutate the Screen
      k:      number of the frame
      width:  width of the video
      height: height of the video

    Returns:
      The raytraced intensity as a NumPy array
    '''
    intensity=numpy.zeros((height, width))
    pintensity=core.array_double.fromnumpy2(intensity)
    func(sc, k, nframes)
    res=max(width, height)
    sc.screen().resolution(res)
    ii=core.Range(res//2-width//2+1, res//2-width//2+width, 1)
    jj=core.Range(res//2-height//2+1, res//2-height//2+height, 1)
    grid=core.Grid(ii, jj)
    aop=core.AstrobjProperties()
    aop.intensity=pintensity
    sc.rayTrace(grid, aop)
    # print(newpos)
    # print(newvel)
    # plt.imshow(intensity)
    # plt.show()
    return intensity
    
## Build default scenery and trajectory

def defaultScenery():
    '''Create a default scenery
    The astrobj is a PatternDisk.
    '''
    metric = core.Metric("KerrBL")
    metric.mass(4e6, "sunmass");
    gridshape=numpy.asarray( (1, 3, 11) , numpy.uint64)
    pgridshape=core.array_size_t.fromnumpy1(gridshape)
    opacity=numpy.zeros(gridshape)
    popacity=core.array_double.fromnumpy3(opacity)
    opacity[:, 0::2, 0::2]=100.
    opacity[:, 1::2, 1::2]=100.
    intensity=opacity*0.+1.;
    pintensity=core.array_double.fromnumpy3(intensity)
    pd=std.PatternDisk()
    pd.velocityKind('ZAMO')
    pd.copyIntensity(pintensity, pgridshape)
    pd.copyOpacity  (popacity, pgridshape)
    pd.innerRadius(0)
    pd.outerRadius(28)
    pd.repeatPhi(8)
    pd.metric(metric)
    pd.rMax(50)
    screen=core.Screen()
    screen.metric(metric)
    screen.resolution(64)
    screen.time(1000., "geometrical_time")
    screen.distance(28., "geometrical")
    # Standard 24x36 field of view after a 55mm objective
    screen.fieldOfView(2.*numpy.arctan(18./55), 'radians')
    screen.anglekind('Rectilinear')
    screen.inclination(95., "degree")
    screen.PALN(180., "degree")
    sc=core.Scenery()
    sc.metric(metric)
    sc.screen(screen)
    sc.astrobj(pd)
    sc.nThreads(8)
    return sc

def defaultTrajectory(screen):
    '''Get default trajectory and adapt screen (field-of-view etc.)
    '''
    screen.observerKind('VelocitySpecified')
    screen.fieldOfView(90, 'degree')
    traj=std.Star()
    traj.metric(screen.metric())
    traj.setInitCoord((0., 28., 0.8, 0.), (0., 0., 0.007))
    return traj

### The main function

def mk_video(scenery=None,
             func="orbiting_screen",
             orbit_trajectory=None, orbit_t0=0., orbit_t1=1000.,
             static_t0=0., static_t1=1000., static_unit='geometrical',
             acceleration_maxvel=0.99,
             growth_factor_first=None,
             growth_factor_last=None,
             duration=10, fps=3, width=128, height=72,
             dangle1=None, dangle2=None, fov=None,
             verbose=0, debug=False, nthreads=8,
             
             output=None,
             backend=OpenCVVideoWriter,
             cmap=None,
             observerkind=None,
             plot=False,
             frame_first=0,
             frame_last=None
             ):
    '''Make a video from a Gyoto Scenery

    Keyword arguments:

    scenery  -- the Scenery to animate, a string (XML file name),
                Gyoto.core.Scenery instance or None in which case a
                default Scenery is used
    func     -- a callable which will be called as func(scenery, k)
                where k is the frame number. func() is responsible to
                mutate the Scenery for each frame, for instance by
                moving the camera, changing its field-of view etc..
                func may also be a string, the name of one of the
                built-in callables: orbiting_screen (default),
                orbiting_screen_forward, static_screen,
                accelerating_tangential_screen or growing_mass. Those
                options take parameters below
    orbit_trajectory -- 
                only if func above starts with 'orbiting_screen', a
                gyoto.std.Star instance or a string (XML file name) or
                None (in which case a default trajectory is used)
                specifying the trajectory of the Screen.
    orbit_t0 -- only if func above starts with 'orbiting_screen',
                proper time along the trajectory at the beginning of
                the movie
    orbit_t1 -- only if func above starts with 'orbiting_screen',
                proper time along the trajectory at the end of the
                movie
    static_t0-- only if func above is 'static_screen', coordinate
                observing time offset at first frame
    static_t0-- only if func above is 'static_screen', coordinate
                observing time offset at last frame
    static_unit --
                only if func above is 'static_screen', unit in which
                static_t0 and static_t1 are expressed ('geometrical').
    acceleration_maxvel --
                only if func is 'accelerating_tangential_screen',
                maximum velocity of the observer, in terms of c
                (default: 0.99)
    growth_factor_first --
                only if func is 'growing_mass', scale factor for first
                frame.
    growth_factor_last --
                only if func is 'growing_mass', scale factor for last
                frame.
    duration -- duration pf the movie in seconds (default: 10.)
    fps      -- frames per second (default: 3)
    width    -- width of the movie in pixels (default: 128)
    height   -- height of the movie in pixels (default: 72)
    dangle1  -- rotate the camera dangle1 degrees horizontally
    dangle2  -- rotate the camera dangle2 degrees vertically
    fov      -- screen field-of-view
    verbose  -- verbosity level (default: 0)
    debug    -- debug mode (default: False)
    nthreads -- number of parallel threads to use (default: 8)
    output   -- output file name
    backend  -- class implementing VideoWriter
    observerkind --
                change observer kind (default: VelocitySpecified)
    plot     -- show each frame (default: False)

    '''

    # Set a few variables
    nframes=int(duration*fps)

    # Read or create Scenery
    if scenery is None:
        sc=defaultScenery()
    elif type(scenery) is core.Scenery:
        sc=scenery
    else:
        sc=core.Factory(scenery).scenery()
    screen=sc.screen()
    metric=sc.metric()

    # Select video type, init func callable
    if func == 'orbiting_screen':
        # Read or create trajectory
        if orbit_trajectory is None:
            traj=defaultTrajectory(screen)
        elif type(orbit_trajectory) is std.Star:
            traj=orbit_trajectory
        else:
            traj=std.Star(core.Factory(orbit_trajectory).astrobj())
            traj.metric(metric)
        screen.observerKind('VelocitySpecified')
        func=orbiting_screen(trajectory=traj, t0=orbit_t0, t1=orbit_t1)
    elif func == 'static_screen':
        func=static_screen(unit=static_unit, t0=static_t0, t1=static_t1)
    elif func == 'orbiting_screen_forward':
        # Read or create trajectory
        if orbit_trajectory is None:
            traj=defaultTrajectory(screen)
        elif type(orbit_trajectory) is std.Star:
            traj=orbit_trajectory
        else:
            traj=std.Star(core.Factory(orbit_trajectory).astrobj())
            traj.metric(metric)
        screen.observerKind('FullySpecified')
        func=orbiting_screen_forward(trajectory=traj, t0=orbit_t0, t1=orbit_t1)
    elif func == 'accelerating_tangential_screen':
        screen.observerKind('VelocitySpecified')
        screen.dangle1(-45, 'degree')
        screen.fieldOfView(90, 'degree')
        func=accelerating_tangential_screen(maxvel=acceleration_maxvel)
    elif func == 'growing_mass':
        func=growing_mass(sc)
        if growth_factor_first is not None:
            func.factor_first=growth_factor_first
        if growth_factor_last is not None:
            func.factor_last=growth_factor_last
    # else assume func is a callable

    # Override some values set on command-line
    if (observerkind is not None):
        screen.observerKind(observerkind)
    if dangle1 is not None:
        screen.dangle1(dangle1, 'degree')
    if dangle2 is not None:
        screen.dangle2(dangle2, 'degree')
    if fov is not None:
        screen.fieldOfView(fov, 'degree')

    # Prepare for ray-tracing
    nframes=int(duration*fps)
    screen.resolution(max(height, width))
    core.verbose(verbose)
    core.debug(debug)
    sc.nThreads(nthreads)

    # Open video
    if backend == 'OpenCV':
        backend=OpenCVVideoWriter
    elif backend == 'PyAV':
        backend=PyAVVideoWriter
    elif backend is None or backend == 'Null':
        backend=NullVideoWriter

    if type(output) == str or backend is NullVideoWriter:
        video=backend(output, fps, width, height)
    elif isinstance(output, VideoWriter):
        video=output
    else:
        raise ValueError('output needs to be a string or VideoWriter')
    if type(cmap) == str:
        video.cmap=plt.cm.get_cmap(cmap)
    elif cmap is not None:
        video.cmap=cmap

    # Loop on frame number
    if frame_last is None:
        frame_last=nframes-1
    for k in range(frame_first, frame_last+1):
        print(k, "/", nframes)
        intensity=rayTraceFrame(sc, func, k, nframes, width, height)
        frame=video.colorize(intensity)
        if plot:
            plt.imshow(frame, origin='lower')
            plt.show()
        video.write(frame)

    # Close video
    del video

### Define arguments

parser = argparse.ArgumentParser(description=
'''Make a video from a Gyoto Scenery

Synopsys:
gyoto mk-video --output=<out.avi> [--options...]

Several types of videos are available and can be selected with
--func. Currently, only the KerrBL Metric implements the methods
needed for those various videos (zamoVelocity, circularVelocity and
observerTetrad are needed). All parameters except --output have
reasonable defaults. For even more advanced usage, see the module
'gyoto.animate' in Python. This script requires one of the modules
OpenCV-python or PyAV to be installed (see --backend option).

gyoto mk-video --output=<out.avi> \\
               --func=static_screen \\
              [--scenery=<scenery.xml>] \\
              [--static-unit=<time unit>] \\
              [--static-t0=<t0>] \\
              [--static-t1=<t1>]

will produce a video of the Scenery <scenery.xml> with the Screen
(a.k.a. camera) fixed, with observing (coordinate) time varying from
Time+<t0> to Time+<t1> in <time unit>, where Time is the observing
date specified in<scenery.xml>. This kind of video makes sense only if
the astrobj in <scenery.xml> is time-variable.

gyoto mk-video --output=<out.avi> \\
               --func=orbiting_screen \\
              [--scenery=<scenery.xml>] \\
              [--orbit-trajectory=<orbit.xml>] \\
              [--orbit-t0=<t0>] \\
              [--orbit-t1=<t1>]

will produce a video of the Scenery <scenery.xml> with the Screen
(a.k.a. camera) orbiting the central object along the trajectory
described in <trajectory.xml>. <trajectory.xml> may be a scenery file
or a file produced by gyotoy and must contain a Star astrobj. The
geodesic of this Star will be used as the camera trajectory. The
Screen is pointing along -er, i.e. in the direction of the central object.


gyoto mk-video --output=<out.avi> \\
               --func=orbiting_screen_forward \\
              [--scenery=<scenery.xml>] \\
              [--orbit-trajectory=<orbit.xml>] \\
              [--orbit-t0=<t0>] \\
              [--orbit-t1=<t1>]

will produce a video of the Scenery <scenery.xml> with the Screen
(a.k.a. camera) orbiting the central object along the trajectory
described in <trajectory.xml>. <trajectory.xml> may be a scenery file
or a file produced by gyotoy and must contain a Star astrobj. The
geodesic of this Star will be used as the camera trajectory. The
Screen is pointing in the direction of motion, i.e. looking forward.


gyoto mk-video --output=<out.avi> \\
               --func=accelerating_tangential_screen \\
              [--scenery=<scenery.xml>] \\
              [--acceleration-maxvel=<maxvel>]

will produce a video where the Screen is fixed but changes velocity
from 0 to maxvel*c. This is not an actual time sequence but helps
vizualizing effects such as light aberration.

gyoto mk-video --output=<out.avi> \\
               --func=growing_mass \\
              [--scenery=<scenery.xml>] \\
              [--growth_factor_first=<ff>] \\
              [--growth_factor_last=<fl>]

will produce a video where the scale of the Scenery changes for each
frame, from ff to fl. For this particular type of video, the Astrobj
in the Scenery must be some kind of PatternDisk.


''',
                                 formatter_class=argparse.RawDescriptionHelpFormatter,
                                 prefix_chars='-+')
parser.add_argument('-s', '--scenery', type=str, default=None,
                    help='name of a Gyoto Scenery XML file. Default: use built-in Scenery.')
parser.add_argument('-t', '--orbit-trajectory', type=str, default=None,
                    dest='orbit_trajectory',
                    help='name of a Gyoto Scenery or Astrobj XML file containing a star '
                    'describing the screen motion. Default: use built-in trajectory.')
parser.add_argument('-o', '--output', type=str, default=None,
                    help='name of video file to save the movie in')
parser.add_argument('-B', '--backend', type=str, default='OpenCV',
                    choices=['OpenCV', 'PyAV', 'Null'],
                    help='name of backend to create video')
parser.add_argument('-c', '--cmap', type=str, default='hot',
                    help='name of pyplot color map')
parser.add_argument("-V", "--func", help="type of video to produce.",
                    type=str, default='orbiting_screen',
                    choices=['orbiting_screen', 'orbiting_screen_forward',
                             'accelerating_tangential_screen',
                             'growing_mass', 'static_screen'])
parser.add_argument("-D", "--duration", help="movie duration in seconds",
                    type=float, default=10.)
parser.add_argument("-f", "--fps", help="number of frames per second",
                    type=int, default=3)
parser.add_argument("-W", "--width", help="image width in pixels",
                    type=int, default=128)
parser.add_argument("-H", "--height", help="image height in pixels",
                    type=int, default=72)
parser.add_argument("-a", "--dangle1", help="camera azimuth offset in degrees",
                    type=float, default=None)
parser.add_argument("-b", "--dangle2", help="camera elevation offset in degrees",
                    type=float, default=None)
parser.add_argument("-F", "--fov", help="camera field-of-view in degrees",
                    type=float, default=None)
parser.add_argument("-O", "--observerkind", help="observer kind",
                    type=str, default=None)
parser.add_argument("-v", "--verbose", help="verbosity level",
                    type=int, default=0)
parser.add_argument("-d", "--debug", help="debug mode",
                    dest='debug', action='store_true', default=False)
parser.add_argument("-p", "--plot", help="plot each frame",
                    dest='plot', action='store_true', default=False)
parser.add_argument("-T", "--nthreads", help="number of threads to use",
                    type=int, default=8)
parser.add_argument("--orbit-t0", help="for orbit video type, initial time in geometrical units",
                    dest='orbit_t0', type=float, default=0)
parser.add_argument("--orbit-t1", help="for orbit video type, final time in geometrical units",
                    dest='orbit_t1', type=float, default=1000)
parser.add_argument("--static-t0", help="for static video type, initial time offset",
                    dest='static_t0', type=float, default=0)
parser.add_argument("--static-t1", help="for static video type, final time offset",
                    dest='static_t1', type=float, default=1000)
parser.add_argument("--static-unit", dest="static_unit", help="unit for static-t0 and static-t1",
                    type=str, default="geometrical")
parser.add_argument("--acceleration-maxvel", help="for acceleration video type, max velocity in terms of light velocity",
                    dest='acceleration_maxvel', type=float, default=0.99)
parser.add_argument("--growth_factor_first", help="for growth video type, scale factor on first frame",
                    dest='growth_factor_first', type=float, default=None)
parser.add_argument("--growth-factor-last", help="for growth video type, scale factor on last frame",
                    dest='growth_factor_last', type=float, default=None)

def main():
    args = parser.parse_args()
    mk_video(**args.__dict__)

# If called as script, process command line and produce video
if (__name__ == "__main__"):
    main()