Ejemplo n.º 1
0
def getStartMonth(timeSet):
    """ Get the starting month number (1-12) from a timeset.
  """
    from visad.util import DataUtility as du
    from visad import DateTime
    r = du.getSample(timeSet, 0).getComponent(0)
    dt = DateTime(r)
    month = dt.formattedString("MM", DateTime.getFormatTimeZone())
    return int(month)
Ejemplo n.º 2
0
def getStartMonth(timeSet):
  """ Get the starting month number (1-12) from a timeset.
  """
  from visad.util import DataUtility as du
  from visad import DateTime
  r = du.getSample(timeSet, 0).getComponent(0)
  dt = DateTime(r)
  month = dt.formattedString("MM",DateTime.getFormatTimeZone())
  return int(month)
Ejemplo n.º 3
0
def makeFlatFieldSequence(sequence):
    """Turn list of _MappedGeoGridFlatField's into a FieldImpl with time domain that is suitable for displaying.

    This will work if the flatfield's have a time associated with them via
    getMetadataMap, but if that doesn't work we're out of luck because a
    plain old FlatField doesn't have any timestamp.  How do handle we that case?  
    Do we put in fake timestamps so the data can at least get displayed still?
    """
    from ucar.unidata.data import DataUtil
    from ucar.visad import Util
    from visad import FunctionType
    from visad import RealType
    from visad import DateTime
    dateTimes = []
    try:
        for ff in sequence:
            if ff.geogrid.getCoordinateSystem().hasTimeAxis1D():
                timeAxis = ff.geogrid.getCoordinateSystem().getTimeAxis1D()
                dateTimes.append(DataUtil.makeDateTimes(timeAxis)[0])
            else:
                # fix for ABI data / data with no time coord: just return plain FF
                # this will allow data to get displayed, but w/o time info
                return ff
    except AttributeError:
        # no geogrid ... try to read from getMetadataMap
        if sequence[0].getMetadataMap().get('times'):
            # this was a _MappedGeoGridFlatField
            for ff in sequence:
                # should be a visad.DateTime:
                timeStr = ff.getMetadataMap().get('times')[0].toString() 
                dateTimes.append(DateTime.createDateTime(timeStr))
        elif sequence[0].getMetadataMap().get('nominal-time'):
            # this was a _MappedAreaImageFlatField
            for ff in sequence:
                time = ff.getMetadataMap().get('nominal-time')
                dateTimes.append(time)
    timeSet = Util.makeTimeSet(dateTimes)
    ftype = FunctionType(RealType.Time, ff.getType())
    fi = FieldImpl(ftype, timeSet)
    for i, ff in enumerate(sequence):
        fi.setSample(i, ff)
    return fi
Ejemplo n.º 4
0
def makeFlatFieldSequence(sequence):
    """Turn list of _MappedGeoGridFlatField's into a FieldImpl with time domain that is suitable for displaying.

    This will work if the flatfield's have a time associated with them via
    getMetadataMap, but if that doesn't work we're out of luck because a
    plain old FlatField doesn't have any timestamp.  How do handle we that case?  
    Do we put in fake timestamps so the data can at least get displayed still?
    """
    from ucar.unidata.data import DataUtil
    from ucar.visad import Util
    from visad import FunctionType
    from visad import RealType
    from visad import DateTime
    dateTimes = []
    try:
        for ff in sequence:
            if ff.geogrid.getCoordinateSystem().hasTimeAxis1D():
                timeAxis = ff.geogrid.getCoordinateSystem().getTimeAxis1D()
                dateTimes.append(DataUtil.makeDateTimes(timeAxis)[0])
            else:
                # fix for ABI data / data with no time coord: just return plain FF
                # this will allow data to get displayed, but w/o time info
                return ff
    except AttributeError:
        # no geogrid ... try to read from getMetadataMap
        if sequence[0].getMetadataMap().get('times'):
            # this was a _MappedGeoGridFlatField
            for ff in sequence:
                # should be a visad.DateTime:
                timeStr = ff.getMetadataMap().get('times')[0].toString()
                dateTimes.append(DateTime.createDateTime(timeStr))
        elif sequence[0].getMetadataMap().get('nominal-time'):
            # this was a _MappedAreaImageFlatField
            for ff in sequence:
                time = ff.getMetadataMap().get('nominal-time')
                dateTimes.append(time)
    timeSet = Util.makeTimeSet(dateTimes)
    ftype = FunctionType(RealType.Time, ff.getType())
    fi = FieldImpl(ftype, timeSet)
    for i, ff in enumerate(sequence):
        fi.setSample(i, ff)
    return fi
Ejemplo n.º 5
0
def listADDEImageTimes(localEntry=None,
    server=None, dataset=None, descriptor=None,
    accounting=DEFAULT_ACCOUNTING,
    location=None,
    coordinateSystem=CoordinateSystems.LATLON,
    place=None,
    mag=None,
    position=None,
    unit=None,
    day=None,
    time=None,
    debug=False,
    band=None,
    size=None,
    showUrls=True):
    
    if localEntry:
        server = localEntry.getAddress()
        dataset = localEntry.getGroup()
        descriptor = localEntry.getDescriptor().upper()
    elif (server is None) or (dataset is None) or (descriptor is None):
        raise TypeError("must provide localEntry or server, dataset, and descriptor values.")
        
    if server == "localhost" or server == "127.0.0.1":
        port = EntryStore.getLocalPort()
    else:
        port = "112"
        
    # server = '%s:%s' % (server, port)
    
    user = accounting[0]
    proj = accounting[1]
    debug = str(debug).lower()
    
    if mag:
        mag = '&MAG=%s %s' % (mag[0], mag[1])
    else:
        mag = ''
        
    if unit:
        origUnit = unit
        unit = '&UNIT=%s' % (unit)
    else:
        # origUnit = None
        unit = ''
        
    if place is Places.CENTER:
        place = '&PLACE=CENTER'
    elif place is Places.ULEFT:
        place = '&PLACE=ULEFT'
    else:
        # raise ValueError()
        place = ''
        
    if coordinateSystem is CoordinateSystems.LATLON:
        coordSys = 'LATLON'
    elif coordinateSystem is CoordinateSystems.AREA or coordinateSystem is CoordinateSystems.IMAGE:
        coordSys = 'LINELE'
    else:
        raise ValueError()
        
    if location:
        location = '&%s=%s %s' % (coordSys, location[0], location[1])
    else:
        location = ''
        
    if size:
        if size == 'ALL':
            size = '&SIZE=99999 99999'
        else:
            size = '&SIZE=%s %s' % (size[0], size[1])
    else:
        size = ''
        
    if time:
        time = '&TIME=%s %s I' % (time[0], time[1])
    else:
        time = ''
        
    if band:
        band = '&BAND=%s' % (str(band))
    else:
        band = '&BAND=ALL'
        
    if position is not None:
        if isinstance(position, int):
            position = '&POS=%s' % (position)
        elif isinstance(position, tuple):
            if len(position) != 2:
                raise ValueError('position range may only contain values for the beginning and end of a range.')
            position = '&POS=%s %s' % (str(position[0]), str(position[1]))
        else:
            position = '&POS=%s' % (str(position).upper())
    else:
        position = '&POS=0'
        
    tz = TimeZone.getTimeZone('Z')
    
    dateFormat = SimpleDateFormat()
    dateFormat.setTimeZone(tz)
    dateFormat.applyPattern('yyyyDDD')
    
    timeFormat = SimpleDateFormat();
    timeFormat.setTimeZone(tz)
    timeFormat.applyPattern('HH:mm:ss')
    
    addeUrlFormat = "adde://%(server)s/imagedirectory?&PORT=%(port)s&COMPRESS=gzip&USER=%(user)s&PROJ=%(proj)s&VERSION=1&DEBUG=%(debug)s&TRACE=0&GROUP=%(dataset)s&DESCRIPTOR=%(descriptor)s%(band)s%(location)s%(place)s%(size)s%(unit)s%(mag)s%(day)s%(time)s%(position)s"
    
    urls = []
    areaDirectories = []
    
    dates = _normalizeDates(day)
    for date in dates:
        formatValues = {
            'server': server,
            'port': port,
            'user': user,
            'proj': proj,
            'debug': debug,
            'dataset': dataset,
            'descriptor': descriptor,
            'band': band,
            'location': location,
            'place': place,
            'size': size,
            'unit': unit,
            'mag': mag,
            'day': date,
            'time': time,
            'position': position,
        }
        url = addeUrlFormat % formatValues
        if showUrls:
            print url
        adl = AreaDirectoryList(url)
        results = adl.getSortedDirs()
        for imageTimes in results:
            for areaDirectory in imageTimes:
                urls.append(url)
                areaDirectories.append(areaDirectory)
                
    uniques = set()
    times = []
    for d in areaDirectories:
        dt = DateTime(d.getNominalTime())
        if dt not in uniques:
            d = { 
                'day': str(dt.formattedString('yyyyDDD', tz)), 
                'time': str(dt.formattedString('HH:mm:ss', tz)),
            }
            times.append(d)
            uniques.add(dt)
    uniques = None
    return sorted(times)
Ejemplo n.º 6
0
def listADDEImages(localEntry=None,
                   server=None,
                   dataset=None,
                   descriptor=None,
                   accounting=DEFAULT_ACCOUNTING,
                   location=None,
                   coordinateSystem=CoordinateSystems.LATLON,
                   place=None,
                   mag=None,
                   position=None,
                   unit=None,
                   day=None,
                   time=None,
                   debug=False,
                   band=None,
                   size=None,
                   showUrls=True):
    """Creates a list of ADDE images.
    
    Args:
        localEntry: Local ADDE dataset.
        server: ADDE server.
        dataset: ADDE dataset group name.
        descriptor: ADDE dataset descriptor.
        day: Day range. ('begin date', 'end date')
        time: ('begin time', 'end time')
        position: Position number. Values may be integers or the string "ALL". (default=0)
        band: McIDAS band number; only images that have matching band number will be returned.
        accounting: ('user', 'project number') User and project number required by servers using McIDAS accounting. default = ('idv','0')
        
    Returns:
        ADDE image matching the given criteria, if any.
    """
    if localEntry:
        server = localEntry.getAddress()
        dataset = localEntry.getGroup()
        descriptor = localEntry.getDescriptor().upper()
    elif (server is None) or (dataset is None) or (descriptor is None):
        raise TypeError(
            "must provide localEntry or server, dataset, and descriptor values."
        )

    if server == "localhost" or server == "127.0.0.1":
        port = EntryStore.getLocalPort()
    else:
        port = "112"

    # server = '%s:%s' % (server, port)

    user = accounting[0]
    proj = accounting[1]
    debug = str(debug).lower()

    if mag:
        mag = '&MAG=%s %s' % (mag[0], mag[1])
    else:
        mag = ''

    if unit:
        origUnit = unit
        unit = '&UNIT=%s' % (unit)
    else:
        # origUnit = None
        unit = ''

    if place is Places.CENTER:
        place = '&PLACE=CENTER'
    elif place is Places.ULEFT:
        place = '&PLACE=ULEFT'
    else:
        # raise ValueError()
        place = ''

    if coordinateSystem is CoordinateSystems.LATLON:
        coordSys = 'LATLON'
    elif coordinateSystem is CoordinateSystems.AREA or coordinateSystem is CoordinateSystems.IMAGE:
        coordSys = 'LINELE'
    else:
        raise ValueError()

    if location:
        location = '&%s=%s %s' % (coordSys, location[0], location[1])
    else:
        location = ''

    if size:
        if size == 'ALL':
            size = '&SIZE=99999 99999'
        else:
            size = '&SIZE=%s %s' % (size[0], size[1])
    else:
        size = ''

    if time:
        time = '&TIME=%s %s I' % (time[0], time[1])
    else:
        time = ''

    if band:
        band = '&BAND=%s' % (str(band))
    else:
        band = '&BAND=ALL'

    if position is not None:
        if isinstance(position, int):
            position = '&POS=%s' % (position)
        elif isinstance(position, tuple):
            if len(position) != 2:
                raise ValueError(
                    'position range may only contain values for the beginning and end of a range.'
                )
            position = '&POS=%s %s' % (str(position[0]), str(position[1]))
        else:
            position = '&POS=%s' % (str(position).upper())
    else:
        position = '&POS=0'

    tz = TimeZone.getTimeZone('Z')

    dateFormat = SimpleDateFormat()
    dateFormat.setTimeZone(tz)
    dateFormat.applyPattern('yyyyDDD')

    timeFormat = SimpleDateFormat()
    timeFormat.setTimeZone(tz)
    timeFormat.applyPattern('HH:mm:ss')

    addeUrlFormat = "adde://%(server)s/imagedirectory?&PORT=%(port)s&COMPRESS=gzip&USER=%(user)s&PROJ=%(proj)s&VERSION=1&DEBUG=%(debug)s&TRACE=0&GROUP=%(dataset)s&DESCRIPTOR=%(descriptor)s%(band)s%(location)s%(place)s%(size)s%(unit)s%(mag)s%(day)s%(time)s%(position)s"

    urls = []
    areaDirectories = []

    dates = _normalizeDates(day)
    for date in dates:
        formatValues = {
            'server': server,
            'port': port,
            'user': user,
            'proj': proj,
            'debug': debug,
            'dataset': dataset,
            'descriptor': descriptor,
            'band': band,
            'location': location,
            'place': place,
            'size': size,
            'unit': unit,
            'mag': mag,
            'day': date,
            'time': time,
            'position': position,
        }
        url = addeUrlFormat % formatValues
        if showUrls:
            print url
        adl = AreaDirectoryList(url)
        results = adl.getSortedDirs()
        for imageTimes in results:
            for areaDirectory in imageTimes:
                urls.append(url)
                areaDirectories.append(areaDirectory)

    temp = _AreaDirectoryList()
    for i, d in enumerate(areaDirectories):
        nominalTime = d.getNominalTime()
        tempDay = str(
            dateFormat.format(nominalTime, StringBuffer(), FieldPosition(0)))
        tempTime = str(
            timeFormat.format(nominalTime, StringBuffer(), FieldPosition(0)))

        bandList = list(d.getBands())
        # tempUnitList = list(d.getCalInfo()[0])
        # unitList = tempUnitList[::2]
        # unitDescList = tempUnitList[1::2]
        # calInfo = dict(zip(unitList, unitDescList))
        if unit:
            unitList = [origUnit]
        else:
            unitList = map(str, list(d.getCalInfo()[0])[::2])

        for band in bandList:
            for calUnit in unitList:
                dt = {
                    'server':
                    server,
                    'dataset':
                    dataset,
                    'descriptor':
                    descriptor,
                    'bandNumber':
                    band,
                    'bandList':
                    bandList,
                    'debug':
                    debug,
                    'accounting':
                    accounting,
                    'day':
                    tempDay,
                    'time':
                    tempTime,
                    'imageSize': (d.getLines(), d.getElements()),
                    'centerLocation':
                    (d.getCenterLatitude(), d.getCenterLongitude()),
                    'resolution': (d.getCenterLatitudeResolution(),
                                   d.getCenterLongitudeResolution()),
                    'unitList':
                    unitList,
                    'unitType':
                    calUnit,
                    'bands':
                    bandList,
                    'band-count':
                    d.getNumberOfBands(),
                    'calinfo':
                    map(str, list(d.getCalInfo()[0])),
                    'calibration-scale-factor':
                    d.getCalibrationScaleFactor(),
                    'calibration-type':
                    str(d.getCalibrationType()),
                    'calibration-unit-name':
                    d.getCalibrationUnitName(),
                    'center-latitude':
                    d.getCenterLatitude(),
                    'center-latitude-resolution':
                    d.getCenterLatitudeResolution(),
                    'center-longitude':
                    d.getCenterLongitude(),
                    'center-longitude-resolution':
                    d.getCenterLongitudeResolution(),
                    'directory-block':
                    list(d.getDirectoryBlock()),
                    'elements':
                    d.getElements(),
                    'lines':
                    d.getLines(),
                    'memo-field':
                    str(d.getMemoField()),
                    'nominal-time':
                    DateTime(d.getNominalTime()),
                    'sensor-id':
                    d.getSensorID(),
                    'sensor-type':
                    str(d.getSensorType()),
                    'source-type':
                    str(d.getSourceType()),
                    'start-time':
                    DateTime(d.getStartTime()),
                    'url':
                    urls[i],
                }
            temp.append(dt)
    return temp
Ejemplo n.º 7
0
def listADDEImageTimes(localEntry=None,
                       server=None,
                       dataset=None,
                       descriptor=None,
                       accounting=DEFAULT_ACCOUNTING,
                       location=None,
                       coordinateSystem=CoordinateSystems.LATLON,
                       place=None,
                       mag=None,
                       position=None,
                       unit=None,
                       day=None,
                       time=None,
                       debug=False,
                       band=None,
                       size=None,
                       showUrls=True):

    if localEntry:
        server = localEntry.getAddress()
        dataset = localEntry.getGroup()
        descriptor = localEntry.getDescriptor().upper()
    elif (server is None) or (dataset is None) or (descriptor is None):
        raise TypeError(
            "must provide localEntry or server, dataset, and descriptor values."
        )

    if server == "localhost" or server == "127.0.0.1":
        port = EntryStore.getLocalPort()
    else:
        port = "112"

    # server = '%s:%s' % (server, port)

    user = accounting[0]
    proj = accounting[1]
    debug = str(debug).lower()

    if mag:
        mag = '&MAG=%s %s' % (mag[0], mag[1])
    else:
        mag = ''

    if unit:
        origUnit = unit
        unit = '&UNIT=%s' % (unit)
    else:
        # origUnit = None
        unit = ''

    if place is Places.CENTER:
        place = '&PLACE=CENTER'
    elif place is Places.ULEFT:
        place = '&PLACE=ULEFT'
    else:
        # raise ValueError()
        place = ''

    if coordinateSystem is CoordinateSystems.LATLON:
        coordSys = 'LATLON'
    elif coordinateSystem is CoordinateSystems.AREA or coordinateSystem is CoordinateSystems.IMAGE:
        coordSys = 'LINELE'
    else:
        raise ValueError()

    if location:
        location = '&%s=%s %s' % (coordSys, location[0], location[1])
    else:
        location = ''

    if size:
        if size == 'ALL':
            size = '&SIZE=99999 99999'
        else:
            size = '&SIZE=%s %s' % (size[0], size[1])
    else:
        size = ''

    if time:
        time = '&TIME=%s %s I' % (time[0], time[1])
    else:
        time = ''

    if band:
        band = '&BAND=%s' % (str(band))
    else:
        band = '&BAND=ALL'

    if position is not None:
        if isinstance(position, int):
            position = '&POS=%s' % (position)
        elif isinstance(position, tuple):
            if len(position) != 2:
                raise ValueError(
                    'position range may only contain values for the beginning and end of a range.'
                )
            position = '&POS=%s %s' % (str(position[0]), str(position[1]))
        else:
            position = '&POS=%s' % (str(position).upper())
    else:
        position = '&POS=0'

    tz = TimeZone.getTimeZone('Z')

    dateFormat = SimpleDateFormat()
    dateFormat.setTimeZone(tz)
    dateFormat.applyPattern('yyyyDDD')

    timeFormat = SimpleDateFormat()
    timeFormat.setTimeZone(tz)
    timeFormat.applyPattern('HH:mm:ss')

    addeUrlFormat = "adde://%(server)s/imagedirectory?&PORT=%(port)s&COMPRESS=gzip&USER=%(user)s&PROJ=%(proj)s&VERSION=1&DEBUG=%(debug)s&TRACE=0&GROUP=%(dataset)s&DESCRIPTOR=%(descriptor)s%(band)s%(location)s%(place)s%(size)s%(unit)s%(mag)s%(day)s%(time)s%(position)s"

    urls = []
    areaDirectories = []

    dates = _normalizeDates(day)
    for date in dates:
        formatValues = {
            'server': server,
            'port': port,
            'user': user,
            'proj': proj,
            'debug': debug,
            'dataset': dataset,
            'descriptor': descriptor,
            'band': band,
            'location': location,
            'place': place,
            'size': size,
            'unit': unit,
            'mag': mag,
            'day': date,
            'time': time,
            'position': position,
        }
        url = addeUrlFormat % formatValues
        if showUrls:
            print url
        adl = AreaDirectoryList(url)
        results = adl.getSortedDirs()
        for imageTimes in results:
            for areaDirectory in imageTimes:
                urls.append(url)
                areaDirectories.append(areaDirectory)

    uniques = set()
    times = []
    for d in areaDirectories:
        dt = DateTime(d.getNominalTime())
        if dt not in uniques:
            d = {
                'day': str(dt.formattedString('yyyyDDD', tz)),
                'time': str(dt.formattedString('HH:mm:ss', tz)),
            }
            times.append(d)
            uniques.add(dt)
    uniques = None
    return sorted(times)