Scripting: loop through bundle time sequence

Useful hints
Post Reply
User avatar
joleenf
Posts: 1120
Joined: Mon Jan 19, 2009 7:16 pm

Scripting: loop through bundle time sequence

Post by joleenf »

While it is possible to save a bundle animation in a script using writeMovie, it can be difficult to add appropriate annotation from a script. This is a script which gets the animation within a bundle, loops through the sequence of frames, adds text to the movie frames and save each frame as a jpg. It is important that this is run with only one tab and one window, otherwise, the incorrect window may used when applying a new projection, colorbar and saving the image.

This is the zipped bundle

2012_02_09.mcvz
GOES-15 images saved with mag=-4 -4
(980.96 KiB) Downloaded 612 times


The Script
ams2012_forum.py
(1.66 KiB) Downloaded 620 times


Code: Select all

# Set the home directory, this works on my mac, but I am not certain it works on every system
import sys
homeDir=os.getenv("HOME")
print homeDir

# Open the bundle and change the size of the display frame to 640 by 480
loadBundle("/Users/joleenf/mcidas/bundles/2012_02_09.mcvz",None,640,480)
pause()

#---Get the display and use a preset projection
myDisplay=firstDisplay()                                             # get the view managers for the display
myDisplay.setProjectionByName("US>States>West>Oklahoma")             # this sets the new Projection
#---end of commands which center the image to user chosen location---

#Save the images (Loop through the time sequences and save each image)
myAnim=myDisplay.getAnimation()                                      # this gets the animation sequence in the bundle
loopTime=myAnim.getTimes()                                           # this returns an array of times in that time sequence
steps=myAnim.getNumSteps()                                           # this is the number of frames in the sequence

#Loop through the frames, save each to a filename of the form afterScript<frameNumber>.jpg
#Use writeImage to add colorbar

for x in range(steps):
  fileName=("%s%s%s%s" % (homeDir,"/afterScript",x,".jpg"))
  print fileName
  strTime=str(loopTime[x])
  writeImage(fileName,"matte background=white top=80; colorbar width=200 height=20 anchor=UM place=UM,0,30 showlines=true tickmarks=4 showunit=true;\
             overlay text="+strTime+" anchor=UM place=UM,0,50 color=black fontsize=20 fontface=Arial")
  myAnim.takeStep()


After running this code, notice the default text for the layer labels is still visible in the frame. This can be removed: obtain control for a layer from a script by adding a unique id for the layer.

1. Under the Layer Controls for each layer, select Edit>Properties
2. Add an id for each layer (see image)
Screen Shot 2012-02-09 at 11.05.47 AM.png


The expanded script gets the layer control by using "findDisplayControl" and turns off the layer label with the method "setShowInDisplay"
ams2012_forum2.py
(1.73 KiB) Downloaded 637 times

Code: Select all

# Get the home directory
import sys
homeDir=os.getenv("HOME")
print homeDir

# Open the bundle and change the size of the display frame to 640 by 480
loadBundle("/Users/joleenf/mcidas/bundles/2012_02_09.mcvz",None,640,480)
pause()

##### New Section #####
# Find the control for the Image Display layer assigned id of G15Layer in the bundle
controlG15Layer=findDisplayControl("G15Layer")

# Remove the layer label
controlG15Layer.setShowInDisplayList(0)
##### End New Section #####

#---Get the display and use a preset projection
myDisplay=firstDisplay()                                             # get the view managers for the display
myDisplay.setProjectionByName("US>States>West>Oklahoma")             # this sets the new Projection
#---end of commands which center the image to user chosen location---

#Save the images (Loop through the time sequences and save each image)
myAnim=myDisplay.getAnimation()                                      # this gets the animation sequence in the bundle
loopTime=myAnim.getTimes()                                           # this returns an array of times in that time sequence
steps=myAnim.getNumSteps()                                           # this is the number of frames in the sequence

#Loop through the frames, save each to a filename of the form afterScript<frameNumber>.jpg
#Use writeImage to add colorbar

for x in range(steps):
  fileName=("%s%s%s%s" % (homeDir,"/afterScript",x,".jpg"))
  print fileName
  strTime=str(loopTime[x])
  writeImage(fileName,"matte background=white top=80; colorbar width=200 height=20 anchor=UM place=UM,0,15 showlines=true tickmarks=4 showunit=true;\
             overlay text="+strTime+" anchor=UM place=UM,0,50 color=black fontsize=20 fontface=Arial")
  myAnim.takeStep()


Image after running the first script.
Image after running the first script.
Image after second script has run
Image after second script has run
Last edited by joleenf on Wed Mar 07, 2012 2:37 pm, edited 1 time in total.
User avatar
hproe
Posts: 503
Joined: Sat Nov 27, 2010 3:46 pm

Re: Scripting: loop through bundle time sequence

Post by hproe »

Hi -

Joleen's script is an elegant way to circumvent the shortcomings of the writeMovie command. However, when running the script from command line using the -pyfile option (not documented in the Guide!) execution stops already a the loadBundle(...) command. No splash screen appears and no message whatsoever is found in the log. Issuing loadBundle from the Jython shell works fine. I am running Windows 7 64-bit.

HP
User avatar
jayh
Posts: 414
Joined: Thu Jan 15, 2009 10:34 pm

Re: Scripting: loop through bundle time sequence

Post by jayh »

Hi HP-

We are noticing similar issues with our Windows machines where loadBundle works in the foreground, but not in the background. I have added this forum information to an inquiry we just wrote up for this problem.

http://mcidas.ssec.wisc.edu/inquiry-v/?inquiry=1225

Let us know if you have any more comments or questions.

Thanks, Jay
User avatar
joleenf
Posts: 1120
Joined: Mon Jan 19, 2009 7:16 pm

Re: Scripting: loop through bundle time sequence

Post by joleenf »

Hi Jay,

The loadBundle issue that was cited by HP has been corrected in the most recent nightly:

For reference to this specific loadBundle issue, please view this post:
viewtopic.php?f=24&t=1037&hilit=Confirmation+from+old+europe

Joleen
User avatar
joleenf
Posts: 1120
Joined: Mon Jan 19, 2009 7:16 pm

Re: Scripting: loop through time sequence

Post by joleenf »

This can also help with save a sequence of images from an animation in the main display window. This could be handy if the user wishes to save png files with transparent background. In the case where an animation is created in a single panel main display, open the jython shell from the main toolbar using Tools>Formulas>Jython Shell. Expand the shell using the double drop down arrows (see image)
Screen Shot 2012-07-19 at 7.45.18 PM.png


Enter the following

Code: Select all

#---Get the display and use a preset projection
myDisplay=firstDisplay()                                             # get the view managers for the display

#Save the images (Loop through the animation and save each image)
myAnim=myDisplay.getAnimation()                                      # this gets the animation sequence in the display
loopTime=myAnim.getTimes()                                           # this returns an array of times in that time sequence
steps=myAnim.getNumSteps()                                           # this is the number of frames in the sequence

#Loop through the frames, save each to a filename of the form image<frameNumber>.png
for x in range(steps):
     zeroPadX=str(x).zfill(3)
     fileName=("%s%s%s" % ("image",zeroPadX,".png"))
     print fileName
     strTime=str(loopTime[x])

     #Use writeImage to save as png with a transparent background.
     writeImage(fileName,"backgroundtransparent; overlay text="+strTime+" anchor=UM place=UM,0,50 color=black fontsize=20 fontface=Arial")
     myAnim.takeStep()


As written, it is best to use just one panel, one tab to display the animation. IDs can be added when multiple panels have been created.
viewtopic.php?f=26&t=1048&hilit=panels

Joleen
Post Reply