time driver

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

time driver

Post by joleenf »

Hi,

I will be writing a script which is plotting the following

1.) a text file
2.) a radar file
3.) a vis image

I need to match the closest times from these files. What would be the best method to perform time matching in a script?

Joleen
User avatar
jayh
Posts: 424
Joined: Thu Jan 15, 2009 10:34 pm

Re: time driver

Post by jayh »

Hi Joleen-

Sorry for the delay in responding. Currently there is no way with scripting to directly use the time driver functionality, you would need to write code or python to choose files and such. I've written inq. 2051 for evaluation to add the time driver features in our scripting methods/functions (http://mcidas.ssec.wisc.edu/inquiry-v/?inquiry=2051).

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

Re: time driver

Post by joleenf »

Hi,

Working with SRSO data, I am trying to match the closest AMV time to a satellite image time. The AMV files are text files, the sattelite image times are being derived from an ADDE connection. I think I have found a way to match the times of the AMV to the satellite as best as possible. However, when I move forward to appropriate index in the animation set, the animation jumps forward to the correct time, but reverses back to an earlier time before the image is saved. I think this is similar to the nextStep() problem, but looking at that post, I did not see how to apply a large skip, I only saw how to move forward a single frame at a time.

Thanks,
Joleen

Code: Select all

 #jython import
import os, math, warnings
from java.util import SimpleTimeZone
from visad import DateTime
tz=SimpleTimeZone(0,"UTC")

#variables

timeSets={'19b':['19:30','19:59'],'20a':['20:01','20:30'],\
          '20b':['20:31','20:59'],'21a':['21:00','21:30'],\
          '21b':['21:31','21:59'],\
          '22a':['22:00','22:30'],'22b':['22:31','22:59'],\
          '23a':['23:00','23:30'],'23b':['23:31','23:59'],\
          'test': ['19:33', '19:33']}
useKey='20a'
thisSet=timeSets[useKey]

homeDir = expandpath('$HOME')   # or expandpath('~') if that does not work

aDay='2014-05-21'
splitDate=aDay.split('-')
compressDate=''.join(splitDate)
serverName='localhost'
dataSet='CO_SRSO'
times=(thisSet[0],thisSet[1])

centerLocation = {'lat':39.8, 'lon':-103.8}
projection="US>States>A-M>Colorado"

baseDir=os.path.join(homeDir,'data','goesr','20140520Storms')
amvDir=os.path.join(baseDir,'amv','5min')
imageDir=os.path.join(baseDir,'images')
directory=os.path.join(imageDir,'topView')

fontName='ArialRoundedMTBold'
colorName='Yellow'

panel = buildWindow(height=360,width=640)
#hack to force full screen size to size I want...
panel[0].setFullScreenSize(640,360)
panel[0].setFullScreen()
panel[0].setProjection(projection)
panel[0].setCenter(40,-103.9)
panel[0].setScaleFactor(5.0)
panel[0].setWireframe(False)
panel[0].setLogoVisibility(0)

mapLayer=panel[0].getMapLayer()
mapLayer.setLayerVisible(False)

### For G-14 SRSOR
visDataSet = getLocalADDEEntry(dataset=dataSet, imageType='Colorado VIS May21')

parmsVIS = dict(
        localEntry=visDataSet,
        unit='BRIT',
        band=1,
        mag=(1,1),
        size=(720,1280),
        place=CENTER,
        coordinateSystem=LATLON,
        location=(centerLocation['lat'],centerLocation['lon'])
)

#Point Data File Load and Display
runHour=thisSet[0][0:2]
amvFile='_'.join(['amv',compressDate,runHour])
amvFile=amvFile+'.txt'
fileName=os.path.join(amvDir,amvFile)
amvDataSource = makeDataSource(fileName, 'FILE.POINTTRACKTEXT')
amvDataSet = getData(fileName)

pointChoice = amvDataSource.findDataChoice("Text Point Data")
SPEED = amvDataSource.getData(pointChoice,None,None,None)

if SPEED is not None:
       amvLayer= panel[0].createLayer('Point Data Plot', SPEED)

       theStationModelManager=idv.getStationModelManager()         
       stationModelObject=theStationModelManager.getStationModel('AMV')  # Note:  OT is a user created model
       amvLayer.setLayoutModel(stationModelObject)

       amvLayer.setLineWidth(2)
       amvLayer.setDeclutter(0)
       amvLayer.setShouldUseAltitude(False)
       amvLayer.setShowInDisplayList(False)


dateTimeListSatellite=listADDEImageTimes(server=serverName, localEntry=visDataSet, day=(aDay), position='ALL', time=times, band=1)

animationControl=panel[0].getAnimation()
animationTimes=animationControl.getTimes()

for dt in dateTimeListSatellite:
   imgTime=dt['time']
   imgDay=dt['day']
   currentDateString=' '.join([imgDay,imgTime])
   fileTime=''.join(imgTime.split(':'))

   visDateTime=DateTime.createDateTime(currentDateString,'yyyyDDD HH:mm:ss', tz)
   
   timeMatches=[]
   for amvTime in animationTimes:
      amvCalendarTime=amvTime.getCalendar()
      amvJavaDate=amvCalendarTime.getTime()
      amvDateTime=convertJavaUtilDateToDateTime(amvJavaDate)
      timeMatch=(visDateTime-amvDateTime)/60
      timeMatches.append(timeMatch.getValue())
 
   dfield=field(timeMatches)
   
   indicesWhereObsIsBeforeImage=None
   indicesWhereObsIsAtImage=None

   #want closest amv match within a 2 minutes on either side of the satellite image. 
   #These are 5 minute amvs, which means the amvTime is the middle image time of
   #5 consecutive images.  The range endpoints for maskWithinRange is exclusive.
   #However, there are a few gaps in the data, so make the range generous and pick
   #the closest match (-10,10).
   whereObIsBeforeOrAtImage=(maskWithinRange(dfield,-10,10,1))*dfield

   #don't care if before or after image, just closets match, so take absolute value
   #of remaining field
   whereObIsBeforeOrAtImage=abs_data(whereObIsBeforeOrAtImage)
       
   min=getMinMax(whereObIsBeforeOrAtImage)[0]
   useIndex=find(whereObIsBeforeOrAtImage,'==',min)
   
   try:
      useIndex=useIndex[0]
   except:
      warningMessage=' '.join(['No time match within Animation for', visDateTime.toString()])
      warnings.warn(warningMessage)
   
   if not math.isnan(min):
       animationControl.setCurrent(useIndex)
       amvTimeCurrent=animationTimes[useIndex]
       outputTimeString=visDateTime.formattedString('yyyyDDD_HHmm',tz)
       outputAMVTimeString=amvTimeCurrent.formattedString('yyyyDDD_HHmm',tz)
   
       outImage='_'.join(['amv',outputTimeString,outputAMVTimeString])
       outImage='.'.join([outImage,'png'])
       outImageName=os.path.join(directory,outImage)
       panel[0].captureImage(outImageName, bgtransparent=True)
   else:
       warningMessage=' '.join(['No time match within Animation for', visDateTime.toString()])
       warnings.warn(warningMessage)
   
       

print 'Done'
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: time driver

Post by joleenf »

I think I get it. If I change to the correct index for each time step desired (using animationControl.setCurrent(index)), then
insert the code from Jon which gets the current index and uses writeImageAtIndex code from Jon, I should get the correct image frame.

Joleen

animationControl.setCurrent(useIndex)
amvTimeCurrent=animationTimes[useIndex]
outputTimeString=visDateTime.formattedString('yyyyDDD_HHmm',tz)
outputAMVTimeString=amvTimeCurrent.formattedString('yyyyDDD_HHmm',tz)

outImage='_'.join(['amv',outputTimeString,outputAMVTimeString])
outImage='.'.join([outImage,'png'])
outImageName=os.path.join(directory,outImage)
idx = animationControl.getCurrent()
print idx

writeImageAtIndex(outImageName, idx)
#or just use "useIndex" instead of getting idx from getCurrent.
#panel[0].captureImage(outImageName, bgtransparent=True)
User avatar
Rick
Posts: 404
Joined: Fri Jan 16, 2009 8:20 pm

Re: time driver

Post by Rick »

Hi Joleen,

I wrote up inquiry 2176 to address the problem.

Rick
Post Reply