Exemplo n.º 1
0
def getRelativeTimeValues( dataset ):
    rv = []
    dt = 0.0
    time_units = None
    if dataset <> None:
        dims = dataset.axes.keys()
        for dim in dims:
            axis = dataset.getAxis( dim )
            if axis.isTime():
                time_units = axis.units
                try:
                    if axis.calendar.lower() == 'gregorian':
                        cdtime.DefaultCalendar = cdtime.GregorianCalendar
                except: pass
                if hasattr( axis, 'partition' ):
                    for part in axis.partition:
                        for iTime in range( part[0], part[1] ):
                            rval = cdtime.reltime( axis[iTime], time_units )
                            rv.append( rval.torel(time_units) )
                    break
                else:
                    for tval in axis:
                        rval = cdtime.reltime( tval, time_units )
                        rv.append( rval.torel(time_units) )
        if (len(rv) > 1):
            dt = rv[1].value - rv[0].value
    return rv, dt, time_units
Exemplo n.º 2
0
def testCal(a,b,c,d,e,f,g,cal=cdtime.MixedCalendar):
    x = cdtime.comptime(d,e,f)
    units = "days since %d-%d-%d"%(a,b,c)
    r = x.torel(units,cal)
    if not isEqual(r.value,g):
        markError('component => relative failed: %s %s'%(`x`,`r`))
    r2 = cdtime.reltime(g, units)
    x2 = r2.tocomp(cal)
    if not cmpYear(x,x2):
        markError('relative => component failed: %s %s'%(`r2`,`x2`))
    units2 = "days since %d-%d-%d"%(d,e,f)
    r3 = cdtime.reltime(10.0,units2)
    r4 = r3.torel(units)
    if not isEqual(r4.value,(10.0+g)):
        markError('relative => relative: %s %s'%(`r3`,`r4`))
    bb = cdtime.comptime(a,b,c)
    x2 = bb.add(g,cdtime.Day,cal)
    if not cmpYear(x,x2):
        markError('component add failed: %s %s'%(`bb`,`x2`))
    x2 = x.sub(g,cdtime.Day,cal)
    if not cmpYear(bb,x2):
        markError('component sub failed: %s %s'%(`x`,`x2`))
    r2 = cdtime.reltime(g, units)
    r3 = r2.add(1000.0,cdtime.Day,cal)
    if not isEqual(r3.value, g+1000.0):
        markError('relative add failed: %s %s'%(`r2`,`r3`))
    r3 = r2.sub(1000.0,cdtime.Day,cal)
    if not isEqual(r3.value, g-1000.0):
        markError('relative sub failed: %s %s'%(`r2`,`r3`))
 def plotSlice( self, plot_index, slice_data, coord_index, coord_value ):
     self.slice_loc[plot_index] = coord_value
     if plot_index == 0: 
         self.xcoord = self.var.getLatitude()
         self.ycoord = self.var.getLevel()
         self.grid_annotation = "Longitude = %.1f" % ( coord_value )
     if plot_index == 1: 
         self.xcoord = self.var.getLongitude()
         self.ycoord = self.var.getLevel()
         self.grid_annotation = "Latitude = %.1f" % ( coord_value )
     if plot_index == 2: 
         self.ycoord = self.var.getLatitude()
         self.xcoord = self.var.getLongitude()
         lev_axis = self.var.getLevel()
         self.grid_annotation = "Level = %.1f" % ( lev_axis[coord_index] )
     if plot_index == 3: 
         time_axis = self.var.getTime()
         r = cdtime.reltime( coord_value, time_axis.units )
         ts = str( r.tocomp() )
         self.time_annotation = "Time = %s" % ts
         self.slice_loc[plot_index] = ts
     if self.time_annotation == None:
         time_axis = self.var.getTime()
         r = cdtime.reltime( 0.0, time_axis.units )
         ts = str( r.tocomp() )
         self.time_annotation = "Time = %s" % ts 
         self.slice_loc[ plot_index ] = ts      
     self.data = slice_data
     refresh_axes = ( self.current_plot_index <> plot_index ) and ( plot_index <> 3 )
     self.current_plot_index = plot_index 
     if plot_index <> 3: self.current_grid_index = plot_index 
     self.update_figure( refresh_axes, '\n'.join([ self.dset_annotation, self.grid_annotation, self.time_annotation ]) )
Exemplo n.º 4
0
 def getTimeValues( self, asComp = True ):
     if self.timeRange == None: return None
     start_rel_time = cdtime.reltime( float( self.timeRange[2] ), self.referenceTimeUnits )
     time_values = []
     for iTime in range( self.timeRange[0], self.timeRange[1]+1 ):
         rval = start_rel_time.value + iTime * self.timeRange[3]
         tval = cdtime.reltime( float( rval ), self.referenceTimeUnits )
         if asComp:   time_values.append( tval.tocomp() )
         else:        time_values.append( tval )
     return time_values
Exemplo n.º 5
0
    def checkTimes(firstValue, lastValue, units, calendar, deltaValue, deltaUnits, npoints):
        """
        Check that a time range in the form (firstValue, lastValue) is equivalent to
        a time range of the form (firstValue, deltaValue, npoints). The representations
        are considered equivalent if:

          lastValue = firstValue + (npoints-1)*deltaValue

        Return (equivalent, firstTime, lastTime, lastEstimated) where:
          - equivalent is a Boolean, True iff the two representations are equivalent
          - firstTime is a :meth:`cdtime.comptime`value representing the first timepoint
          - lastTime is a :meth:`cdtime.comptime`value representing the last timepoint
          - lastEstimated is a :meth:`cdtime.comptime`value representing the last timepoint, based on
            the (firstValue, deltaValue, npoints) representation. If the representations
            are not equivalent, it will differ from lastValue.

        The first timepoint in the range is a relative time (firstValue, units, calendar);
        similarly the last timepoint is (lastValue, units, calendar):

        firstValue
          Float value of first timepoint.

        lastValue
          Float value last timepoint.

        units
          String time units.

        calendar
          cdtime calendar type

        deltaValue
          Float value of time delta representation.

        deltaUnits
          cdtime interval, for example, cdtime.Month

        npoints
          Integer number of points in time delta representation.

        """
        first = reltime(firstValue, units)
        last = reltime(lastValue, units)
        firstAdjusted = first.tocomp(calendar).add(0, deltaUnits)
        lastAdjusted = last.tocomp(calendar).add(0, deltaUnits)
        lastEstimated = firstAdjusted.add((npoints-1)*deltaValue, deltaUnits, calendar)
        result = lastEstimated.cmp(lastAdjusted)
        return (result==0), firstAdjusted, lastAdjusted, lastEstimated
Exemplo n.º 6
0
def cmor_define_and_write(values, axes):
    table = 'CMIP5_cfSites'
    cmor.load_table(table)

    axis_ids = list()
    for axis in axes:
        axis_id = cmor.axis(**axis)
        axis_ids.append(axis_id)

    igrid = cmor.grid([axis_ids[1]], [0.], [0.])
    cmor.zfactor(axis_ids[2], 'b', axis_ids=[axis_ids[2]],
                 zfactor_values=range(2),
                 zfactor_bounds=[[x - 0.5, x + 0.5] for x in range(2)])

    cmor.zfactor(axis_ids[2], 'orog', 'm', axis_ids=[igrid],
                 zfactor_values=[0])

    ids_for_var = [axis_ids[0], igrid, axis_ids[2]]
    varid = cmor.variable('tnhus',
                          's-1',
                          ids_for_var,
                          history='variable history',
                          missing_value=-99,
                          )

    for time in [x * 1800. / 86400 for x in range(48)]:
        time += 1. / 3600. / 24.
        tr = cdtime.reltime(time, axes[0]["units"])
        print("Writing: %.03f" % time, "|", tr.tocomp(cdtime.Calendar360), "|", tr.tocomp())
        cmor.write(varid, values, time_vals=[time])
    return varid
Exemplo n.º 7
0
def checkDatawc(self, name, value):
    checkName(self, name, value)
    if isNumber(value):
        value = float(value), 0
    elif isinstance(value, str):
        t = cdtime.s2c(value)
        if t != cdtime.comptime(0, 1):
            t = t.torel(self.datawc_timeunits, self.datawc_calendar)
            value = float(t.value), 1
        else:
            checkedRaise(
                self, value, ValueError, 'The ' + name +
                ' attribute must be either an integer or a float value or a date/time.'
            )
    elif type(value) in [
            type(cdtime.comptime(1900)),
            type(cdtime.reltime(0, 'days since 1900'))
    ]:
        value = value.torel(self.datawc_timeunits,
                            self.datawc_calendar).value, 1
    else:
        checkedRaise(
            self, value, ValueError, 'The ' + name +
            ' attribute must be either an integer or a float value or a date/time.'
        )
    return value
Exemplo n.º 8
0
 def getWorldCoord(self, image_coord, iAxis, latLonGrid):
     plotType = self.metadata['plotType']
     if plotType == 'zyt':
         axisNames = ['Longitude', 'Latitude', 'Time'
                      ] if latLonGrid else ['X', 'Y', 'Time']
     else:
         axisNames = ['Longitude', 'Latitude', 'Level'
                      ] if latLonGrid else ['X', 'Y', 'Level']
     try:
         axes = ['lon', 'lat', 'time'
                 ] if plotType == 'zyt' else ['lon', 'lat', 'lev']
         world_coord = self.metadata[axes[iAxis]][image_coord]
         if (plotType == 'zyt') and (iAxis == 2):
             timeAxis = self.metadata['time']
             timeValue = cdtime.reltime(float(world_coord), timeAxis.units)
             world_coord = str(timeValue.tocomp())
         return axisNames[iAxis], getFloatStr(world_coord)
     except:
         if (plotType == 'zyx') or (iAxis < 2):
             gridSpacing = self.input().GetSpacing()
             gridOrigin = self.input().GetOrigin()
             return axes[iAxis], getFloatStr(gridOrigin[iAxis] +
                                             image_coord *
                                             gridSpacing[iAxis])
         return axes[iAxis], ""
Exemplo n.º 9
0
    def normalizeTime(fromDimensionValue,
                      fromUnits,
                      toUnits,
                      calendar=DefaultCalendar):
        """
        Normalize a relative time value (fromDimensionValue, fromUnits) to different units.

        Returns the float value of the normalized time. In other words, the normalized
        time is (return_value, toUnits).

        fromDimensionValue
          Float value of input relative time.

        fromUnits
          String units of input relative time.

        toUnits
          String time units of result time.

        calendar
          cdtime calendar type.
        """
        result = reltime(fromDimensionValue, fromUnits)
        relresult = result.torel(toUnits, calendar)
        relvalue = relresult.value

        return float(relvalue)
Exemplo n.º 10
0
 def _getdatawc_x2(self):
     if getmember(self, '_tdatawc_x2'):
         return cdtime.reltime(self._datawc_x2,
                               self.datawc_timeunits).tocomp(
                                   self.datawc_calendar)
     else:
         return self._datawc_x2
Exemplo n.º 11
0
 def getWorldCoordsAsFloat(self, image_coords):
     plotType = self.metadata['plotType']
     world_coords = None
     try:
         if plotType == 'zyt':
             lat = self.metadata['lat']
             lon = self.metadata['lon']
             timeAxis = self.metadata['time']
             tval = timeAxis[image_coords[2]]
             relTimeValue = cdtime.reltime(float(tval), timeAxis.units)
             timeValue = str(relTimeValue.tocomp())
             world_coords = [
                 lon[image_coords[0]], lat[image_coords[1]], timeValue
             ]
         else:
             lat = self.metadata['lat']
             lon = self.metadata['lon']
             lev = self.metadata['lev']
             world_coords = [
                 lon[image_coords[0]], lat[image_coords[1]],
                 lev[image_coords[2]]
             ]
     except:
         gridSpacing = self.input().GetSpacing()
         gridOrigin = self.input().GetOrigin()
         world_coords = [
             gridOrigin[i] + image_coords[i] * gridSpacing[i]
             for i in range(3)
         ]
     return world_coords
Exemplo n.º 12
0
def cmor_define_and_write(values, axes):
    table = 'CMIP5_cfSites'
    cmor.load_table(table)

    axis_ids = list()
    for axis in axes:
        axis_id = cmor.axis(**axis)
        axis_ids.append(axis_id)

    igrid = cmor.grid([axis_ids[1]], [0.], [0.])
    cmor.zfactor(axis_ids[2], 'b', axis_ids = [axis_ids[2]],
                 zfactor_values = range(2),
                 zfactor_bounds = [[x-0.5, x+0.5] for x in range(2)])

    cmor.zfactor(axis_ids[2], 'orog', 'm', axis_ids = [igrid],
                 zfactor_values = [0])

    ids_for_var = [axis_ids[0], igrid, axis_ids[2]]
    varid = cmor.variable('tnhus',
                          's-1',
                          ids_for_var,
                          history = 'variable history',
                          missing_value = -99,
                          )

    for time in [x * 1800./ 86400 for x in range(48)]:
        time += 1./3600./24.
        tr = cdtime.reltime(time,axes[0]["units"])
        print "Writing: %.03f" % time,"|",tr.tocomp(cdtime.Calendar360),"|",tr.tocomp()
        cmor.write(varid, values, time_vals = [time])
    return varid
Exemplo n.º 13
0
    def write(self, t, data, lev=None):

	# If the variable is multilevel, require the level value
	# Else if it is an explicit dimension, just use the single value
	# Else use level=0.0

	if self.vertdim and lev==None:
	    if len(self.vertdim.levs)>1:
		raise LatsLevelNeededError, self.name
	    else:
		lev = self.vertdim.levs[0]
	elif lev==None:
	    lev=0.0

	# Require floats to be Float32 or Float64, and integers to be Int or Int32
	if self.datatype==LatsFloat and data.typecode() not in Numeric.typecodes['Float']:
	    raise LatsWrongDatatype, "variable %s, at time %s, level %d"%(self.name,`t`,lev)
	
	if self.datatype==LatsInt and data.typecode() not in Numeric.typecodes['Integer']:
	    raise LatsWrongDatatype, "variable %s, at time %s, level %d"%(self.name,`t`,lev)

	if type(t)==type(cdtime.reltime(0,"years")):
	    c = t.tocomp()
	else:
	    c = t
	if data.shape!=self.grid.shape:
	    raise LatsDataShapeError, "%s, variable %s, at time %s, level %d\nInput field shape: %s"%(`self.grid.shape`,self.name,`t`,lev,`data.shape`)
	err = slats.write(self.file.id, self.id, lev, c.year, c.month, c.day, c.hour, data)
	if err==0:
	    raise LatsWriteError, "%s, at time %s, level %d"%(self.name,`t`,lev)
Exemplo n.º 14
0
def _extremum_(func,ctime,i0,i,var,spline):
    """Extremum possibly using splines"""
    nt = len(var)
    if spline and nt >= 4: # and i != 0 and i != (nt-1)
        if i == 0:
            ifirst, ilast = 0, 4
        elif i == nt-1:
            ifirst, ilast = nt-4, nt
        else:
            icenter = i - int(var[i-1] > var[i+1])
            ifirst = max(icenter-1, 0)
            ilast = ifirst + 4
            if ilast > nt:
                ilast -= 1
                ifirst -= 1
        mn_units = 'minutes since %s'%ctime[i0+ifirst]
        old_rts = cdms2.createAxis(N.asarray([ct.torel(mn_units).value for ct in ctime[i0+ifirst:i0+ilast]],dtype='d'))
        old_var = MV2.array(var[ifirst:ilast], axes=[old_rts], copyaxes=0)
        mn_rts =  cdms2.createAxis(N.arange(int(old_rts[-1]+1),dtype='d'))
        mn_var = interp1d(old_var, mn_rts, method='cubic')
        del old_var, old_rts
#       mn_var = spline_interpolate(old_rts,var[i-1:i+2],mn_rts)
#       mn_var = splev(mn_rts, splrep(old_rts,var[ifirst:ilast]))
        mn_i = func(mn_var)
        val = mn_var[mn_i]
        del mn_var
        this_ctime = cdtime.reltime(mn_i,mn_units).tocomp()
    else:
        this_ctime = ctime[i0+i]
        val = var[i]
    return val,this_ctime
Exemplo n.º 15
0
    def post(self,fetched,slab,axes,specifications,confined_by,aux,axismap):
        ''' Post processing retouches the bounds and later will deal with the mask'''
        import cdms2 as cdms
        fetched=cdms.createVariable(fetched,copy=1)
        faxes=fetched.getAxisList()
        a=None
        for i in range(len(faxes)):
            if confined_by[i] is self:
                newaxvals=[]
                bounds=[]
                a=None
                sh=list(fetched.shape)
                sh[i]=1
                for l in self.aux[i]:
                    try:
                        tmp=fetched(**{faxes[i].id:(l,l)})
                        ax=tmp.getAxis(i)
                        #print ax
                        newaxvals.append(ax[0])
			if ax.getBounds()!=None:
                   	     bounds.append(ax.getBounds()[0])
			else:
			     bounds=None
                    except Exception,err:
                        #print 'err:',err,'match:',self.match
                        if self.match==1:
                            raise Exception,'Error axis value :'+str(l)+' was requested but is not present in slab\n(more missing might exists)'
                        elif self.match==0:
                            tmp=MV2.ones(sh,typecode=MV2.float)
                            tmp=MV2.masked_equal(tmp,1)
                            if type(l)==type(cdtime.comptime(1999)) or type(l)==type(cdtime.reltime(0,'days since 1999')) or type(l)==type(''):
                                if type(l)!=type(''):
                                    newaxvals.append(l.torel(faxes[i].units).value)
                                else:
                                    newaxvals.append(cdtime.s2r(l,faxes[i].units).value)
                            else:
                                newaxvals.append(l)
                            if bounds is not None:
                                bounds.append([ax[-1]-1.,ax[-1]+1])
                        else:
                            tmp=None
                    if not tmp is None:
                        if a is None:
                            a=tmp
                        elif not tmp is None:
                            a=MV2.concatenate((a,tmp),i)
                if bounds is not None:
			newax=cdms.createAxis(numpy.array(newaxvals),bounds=numpy.array(bounds),id=ax.id)
		else:
			newax=cdms.createAxis(numpy.array(newaxvals),id=ax.id)
                for att in faxes[i].attributes.keys():
                    setattr(newax,att,faxes[i].attributes.get(att))
                for j in range(len(fetched.shape)):
                    if j==i:
                        a.setAxis(i,newax)
                    else:
                        a.setAxis(j,faxes[j])
                fetched=a.astype(fetched.dtype.char)
                faxes=fetched.getAxisList()
Exemplo n.º 16
0
    def post(self,fetched,slab,axes,specifications,confined_by,aux,axismap):
        ''' Post processing retouches the bounds and later will deal with the mask'''
        import cdms2 as cdms
        fetched=cdms.createVariable(fetched,copy=1)
        faxes=fetched.getAxisList()
        a=None
        for i in range(len(faxes)):
            if confined_by[i] is self:
                newaxvals=[]
                bounds=[]
                a=None
                sh=list(fetched.shape)
                sh[i]=1
                for l in self.aux[i]:
                    try:
                        tmp=fetched(**{faxes[i].id:(l,l)})
                        ax=tmp.getAxis(i)
                        #print ax
                        newaxvals.append(ax[0])
			if ax.getBounds()!=None:
                   	     bounds.append(ax.getBounds()[0])
			else:
			     bounds=None
                    except Exception,err:
                        #print 'err:',err,'match:',self.match
                        if self.match==1:
                            raise Exception,'Error axis value :'+str(l)+' was requested but is not present in slab\n(more missing might exists)'
                        elif self.match==0:
                            tmp=MV2.ones(sh,typecode=MV2.float)
                            tmp=MV2.masked_equal(tmp,1)
                            if type(l)==type(cdtime.comptime(1999)) or type(l)==type(cdtime.reltime(0,'days since 1999')) or type(l)==type(''):
                                if type(l)!=type(''):
                                    newaxvals.append(l.torel(faxes[i].units).value)
                                else:
                                    newaxvals.append(cdtime.s2r(l,faxes[i].units).value)
                            else:
                                newaxvals.append(l)
                            if bounds is not None:
                                bounds.append([ax[-1]-1.,ax[-1]+1])
                        else:
                            tmp=None
                    if not tmp is None:
                        if a is None:
                            a=tmp
                        elif not tmp is None:
                            a=MV2.concatenate((a,tmp),i)
                if bounds is not None:
			newax=cdms.createAxis(numpy.array(newaxvals),bounds=numpy.array(bounds),id=ax.id)
		else:
			newax=cdms.createAxis(numpy.array(newaxvals),id=ax.id)
                for att in faxes[i].attributes.keys():
                    setattr(newax,att,faxes[i].attributes.get(att))
                for j in range(len(fetched.shape)):
                    if j==i:
                        a.setAxis(i,newax)
                    else:
                        a.setAxis(j,faxes[j])
                fetched=a.astype(fetched.dtype.char)
                faxes=fetched.getAxisList()
Exemplo n.º 17
0
    def getTime(self, cur_timeunits):
        '''
        '''
        nTime = self.data.shape[0]
        time_values = numpy.arange(nTime)
        fileTime = [cdtime.reltime(i, cur_timeunits) for i in time_values]

        return fileTime
Exemplo n.º 18
0
    def getData(self, variable=None):
        '''
        Aggreate all data from the file list in the order given.
        '''
        #pdb.set_trace()
        rel_time=[]
        filename = self.flist[0].strip()
        f = cdms2.open( filename, 'r' )

        if( variable != None ):
            self.vartoread = variable
            self.variable = f(variable)
            self.timeunits = self.variable.getTime().units
        else:
            self.vartoread = self.variable.id
            
        timeunits =   f(self.vartoread).getTime().units
        TimeValues = f(self.vartoread).getTime()[:]
        cur_time_values = [ cdtime.reltime(TimeValues[i], timeunits ) for i in range(len(TimeValues)) ]
        rel_time_values = [ cur_time_values[i].torel("days since 1900-01-01").value for i in range(len(TimeValues)) ]
        rel_time = rel_time + rel_time_values
        axisList = f(self.vartoread).getAxisList()
        data = f(self.vartoread)[:]
        print "reading %s" % filename.strip()
        if( self.prefix != [] ):
            data.prefix = self.prefix[0]

        # ---------------------------
        # Concatenate following files
        # ---------------------------
        for filename in self.flist[ 1: ]:
            print "reading %s" % filename.strip()
            f = cdms2.open( filename.strip(), 'r' )
            timeunits =   f(self.vartoread).getTime().units
            cur_time_values = [ cdtime.reltime(TimeValues[i], timeunits ) for i in range(len(TimeValues)) ]
            rel_time_values = [ cur_time_values[i].torel("days since 1900-01-01").value for i in range(len(TimeValues)) ]
            rel_time = rel_time + rel_time_values
            data2 = f(self.vartoread)[:]
            data = numpy.ma.concatenate((data,data2), axis=0)
            f.close()
        tupletime = tuple([i for i in rel_time])
        axisList[0]._data_=tupletime
        axisList[0].units="days since 1900-01-01"
        data.setAxisList(axisList)
        return data
Exemplo n.º 19
0
def timeindex(value, units, basetime, delta, delunits, calendar):
    """ Calculate (t - basetime)/delu
    where t = reltime(value, units)
    and delu is the time interval (delta, delunits) (e.g., 1 month).
    """
    tval = cdtime.reltime(value, units)
    tounits = "%s since %s" % (delunits, basetime)
    newval = tval.torel(tounits, calendar)
    return int(newval.value / delta)
Exemplo n.º 20
0
 def getTime( self, cur_timeunits ):
     '''
     '''
     nTime = self.data.shape[0]
     time_values = numpy.arange(nTime)
     fileTime =[ cdtime.reltime(i, cur_timeunits)
                 for i in time_values ]
     
     return fileTime
Exemplo n.º 21
0
def timeindex(value, units, basetime, delta, delunits, calendar):
    """ Calculate (t - basetime)/delu
    where t = reltime(value, units)
    and delu is the time interval (delta, delunits) (e.g., 1 month).
    """
    tval = cdtime.reltime(value, units)
    tounits = "%s since %s"%(delunits, basetime)
    newval = tval.torel(tounits, calendar)
    return int(newval.value/delta)
Exemplo n.º 22
0
def setup_data():
    ntimes = 7200
    tvals = numpy.arange(ntimes) * 6.
    tbnds = list(tvals)
    tbnds.append(43200)
    tbnds = numpy.array(tbnds) - 3.
    print "tvals:", tvals
    print "tbnds:", tbnds
    import cdtime
    tunits = 'hours since 209-01-01 06:00:00'
    t1 = cdtime.reltime(tvals[0], tunits)
    t2 = cdtime.reltime(tvals[-1], tunits)
    t3 = cdtime.reltime(tbnds[-1], tunits)
    print t1.tocomp(), t1.tocomp(cdtime.Calendar360)
    print t2.tocomp(), t2.tocomp(cdtime.Calendar360)
    print t3.tocomp(), t3.tocomp(cdtime.Calendar360)

    axes = [
        {
            'table_entry': 'time1',
            'units': tunits,
            'coord_vals': tvals,
            'cell_bounds': tbnds,
        },
        {
            'table_entry': 'latitude',
            'units': 'degrees_north',
            'coord_vals': [0],
            'cell_bounds': [-5, 5]
        },
        {
            'table_entry': 'longitude',
            'units': 'degrees_east',
            'coord_vals': [0],
            'cell_bounds': [-10, 10]
        },
        # {'table_entry': 'depth',
        # 'units': 'm',
        # 'coord_vals': [10],
        # 'cell_bounds': [5,15]},
    ]

    values = numpy.ones(ntimes) * 1013.
    return values.astype("f"), axes
Exemplo n.º 23
0
    def genMatch(self, axis, interval, matchnames):
        """Helper function for expertPaths.
        axis is a partitioned axis, either time or vertical level or forecast.
        interval is an index interval (istart, iend).
        matchnames is a partially filled list [id, timestart, timeend, levstart, levend, fc]
          If a filemap is used, matchnames has indices, otherwise has coordinates.

        Function modifies matchnames based on axis and interval,
        returns the modified matchnames tuple.
        """
        if axis.isTime():
            if hasattr(self.parent, 'cdms_filemap'):
                start = interval[0]
                end = interval[1]
            else:  # Use template method
                time0 = axis[interval[0]]
                time1 = axis[interval[1] - 1]
                isabs = (string.find(axis.units, " as ") != -1)
                if isabs:
                    start = cdtime.abstime(time0, axis.units)
                    end = cdtime.abstime(time1, axis.units)
                else:
                    cal = axis.getCalendar()
                    start = cdtime.reltime(time0, axis.units).tocomp(cal)
                    end = cdtime.reltime(time1, axis.units).tocomp(cal)
            matchnames[1] = start
            matchnames[2] = end
        elif axis.isForecast():
            start = axis.getValue()[interval[0]]
            end = axis.getValue()[interval[1] - 1]
            matchnames[5] = start
            matchnames[6] = end
        else:
            if hasattr(self.parent, 'cdms_filemap'):
                start = interval[0]
                end = interval[1]
            else:
                start = int(axis[interval[0]])
                end = int(axis[interval[1] - 1])
            matchnames[3] = start
            matchnames[4] = end

        return matchnames
Exemplo n.º 24
0
    def genMatch(self, axis, interval, matchnames):
        """Helper function for expertPaths.
        axis is a partitioned axis, either time or vertical level or forecast.
        interval is an index interval (istart, iend).
        matchnames is a partially filled list [id, timestart, timeend, levstart, levend, fc]
          If a filemap is used, matchnames has indices, otherwise has coordinates.

        Function modifies matchnames based on axis and interval,
        returns the modified matchnames tuple.
        """
        if axis.isTime():
            if hasattr(self.parent,'cdms_filemap'):
                start = interval[0]
                end = interval[1]
            else:                       # Use template method
                time0 = axis[interval[0]]
                time1 = axis[interval[1]-1]
                isabs = (string.find(axis.units," as ")!=-1)
                if isabs:
                    start = cdtime.abstime(time0,axis.units)
                    end = cdtime.abstime(time1,axis.units)
                else:
                    cal = axis.getCalendar()
                    start = cdtime.reltime(time0,axis.units).tocomp(cal)
                    end = cdtime.reltime(time1,axis.units).tocomp(cal)
            matchnames[1] = start
            matchnames[2] = end
        elif axis.isForecast():
            start = axis.getValue()[interval[0]]
            end   = axis.getValue()[interval[1]-1]
            matchnames[5] = start
            matchnames[6] = end
        else:
            if hasattr(self.parent,'cdms_filemap'):
                start = interval[0]
                end = interval[1]
            else:
                start = int(axis[interval[0]])
                end = int(axis[interval[1]-1])
            matchnames[3] = start
            matchnames[4] = end

        return matchnames
Exemplo n.º 25
0
def timeindex(value, units, basetime, delta, calendar):
    """ Calculate (t - basetime)/delu
    where t = reltime(value, units)
    and delu is the time interval (delta, delunits) (e.g., 1 month).
    """
    tval = cdtime.reltime(value, units)
    newval = tval.torel(basetime, calendar)
    if delta is None:
        return newval.value
    else:
        return newval.value / delta
Exemplo n.º 26
0
def timeindex(value, units, basetime, delta, calendar):
    """ Calculate (t - basetime)/delu
    where t = reltime(value, units)
    and delu is the time interval (delta, delunits) (e.g., 1 month).
    """
    tval = cdtime.reltime(value, units)
    newval = tval.torel(basetime, calendar)
    if delta is None:
        return newval.value
    else:
        return newval.value/delta
Exemplo n.º 27
0
    def getTime(self, timeunits=None):
        '''
        Return time.
        Assume that file time variable name will be the same in all files.
        '''
        flist = self.flist
        nbFiles = len(self.flist)
        # ----------------------------
        # Read first file in the list
        # -----------------------------
        time = self.variable.getTime()[:]

        # ---------------------------
        # Concatenate following files
        # ---------------------------
        for filename in self.flist[1:]:
            f = cdms2.open(filename.strip(), 'r')
            time2 = f(self.variable.id).getTime()
            timeunits2 = f(self.variable.id).getTime().units
            match = re.match('.*since.*', timeunits2)
            # ---------------------------------------------------
            # if timeunits is not UDUNITS format, pass back value
            # and let InputTimeUnits attribute to take care of
            # the conversion.
            # ----------------------------------------------------
            if (not match): timeunits2 = self.timeunits
            # --------------------------------
            # Make sure we have the same units
            # ---------------------------------
            if (timeunits2 != self.timeunits):
                file_time = [ cdtime.reltime(time2[ i ], timeunits2 ) \
                             for i in range( len( time2 ) )]
                time2 = [ file_time[i].torel( timeunits ).value       \
                          for i in range( len( time2 ) ) ]

            time2 = time2[:]
            time = numpy.concatenate((time, time2), axis=0)
            f.close()

        self.time = [cdtime.reltime(i, self.timeunits) for i in time]
        return self.time
Exemplo n.º 28
0
    def getTime(self,timeunits=None):
        '''
        Return time.
        Assume that file time variable name will be the same in all files.
        '''
        flist=self.flist
        nbFiles = len(self.flist)
        # ----------------------------
        # Read first file in the list
        # -----------------------------
        time = self.variable.getTime()[:]

        # ---------------------------
        # Concatenate following files
        # ---------------------------
        for filename in self.flist[ 1: ]:
            f = cdms2.open( filename.strip(), 'r' )
            time2      =   f(self.variable.id).getTime()
            timeunits2 =   f(self.variable.id).getTime().units
            match=re.match('.*since.*', timeunits2)
            # ---------------------------------------------------
            # if timeunits is not UDUNITS format, pass back value 
            # and let InputTimeUnits attribute to take care of
            # the conversion.  
            # ----------------------------------------------------
            if( not match ):  timeunits2=self.timeunits
            # --------------------------------
            # Make sure we have the same units
            # ---------------------------------
            if( timeunits2 != self.timeunits ):
                file_time = [ cdtime.reltime(time2[ i ], timeunits2 ) \
                             for i in range( len( time2 ) )]
                time2 = [ file_time[i].torel( timeunits ).value       \
                          for i in range( len( time2 ) ) ]

            time2=time2[:]
            time = numpy.concatenate((time,time2), axis=0)
            f.close()

        self.time = [cdtime.reltime(i,self.timeunits) for i in time]
        return self.time
Exemplo n.º 29
0
    def getTime(self, timeunits=None):
        '''
        Return time in cdtime format.
        '''
        if (timeunits == 'internal'):
            timeunits = self.getTimeUnits()

        time_values = numpy.arange(len(self.data.getTime()))
        time_bounds = numpy.arange(len(self.data.getTime()) + 1)
        cur_time = [cdtime.reltime(i, timeunits) for i in time_values]

        return cur_time
Exemplo n.º 30
0
    def getTime(self,timeunits=None):
        '''
        Return time.
        '''
        timeunits=self.getTimeUnits( timeunits )

        time_values =   self.f.variables[self.TimeKey][:]
 
        cur_time=[ cdtime.reltime( i, timeunits ) 
                   for i in time_values ]
        
        return cur_time
Exemplo n.º 31
0
 def getTime(self,timeunits=None):
     '''
     Return time in cdtime format.
     '''
     if(timeunits == 'internal'):
         timeunits=self.getTimeUnits()
     
     time_values = numpy.arange(len(self.data.getTime()))
     time_bounds = numpy.arange(len(self.data.getTime())+1)
     cur_time=[cdtime.reltime(i,timeunits) 
               for i in time_values]
     
     return cur_time
Exemplo n.º 32
0
    def updateMetadata(self, plotIndex):
        if self.metadata == None:
            scalars = None

            #            arr_names = []
            #            na = self.fieldData.GetNumberOfArrays()
            #            for iF in range( na ):
            #                arr_names.append( self.fieldData.GetArrayName(iF) )
            #            print " updateMetadata: getFieldData, arrays = ", str( arr_names ) ; sys.stdout.flush()

            if self.fieldData == None:
                print >> sys.stderr, ' NULL field data in updateMetadata: ispec[%x]  ' % id(
                    self)
                self.initializeMetadata()

            self.metadata = self.computeMetadata(plotIndex)

            if self.metadata <> None:
                self.rangeBounds = None
                self.datasetId = self.metadata.get('datasetId', None)
                tval = self.metadata.get('timeValue', 0.0)
                self.referenceTimeUnits = self.metadata.get('timeUnits', None)
                self.timeValue = cdtime.reltime(
                    float(tval), self.referenceTimeUnits) if tval else None
                self.dtype = self.metadata.get('datatype', None)
                scalars = self.metadata.get('scalars', None)
                self.rangeBounds = getRangeBounds(self.dtype)
                title = self.metadata.get('title', None)
                if title:
                    targs = title.split(':')
                    if len(targs) == 1:
                        self.titleBuffer = "\n%s" % (title)
                    elif len(targs) > 1:
                        self.titleBuffer = "%s\n%s" % (targs[1], targs[0])
                else:
                    self.titleBuffer = ""
                attributes = self.metadata.get('attributes', None)
                if attributes:
                    self.units = attributes.get('units', '')
                    srange = attributes.get('range', None)
                    if srange:
                        #                print "\n ***************** ScalarRange = %s, md[%d], var_md[%d] *****************  \n" % ( str(range), id(metadata), id(var_md) )
                        self.scalarRange = list(srange)
                        self.scalarRange.append(1)
                        if not self.seriesScalarRange:
                            self.seriesScalarRange = list(srange)
                        else:
                            if self.seriesScalarRange[0] > srange[0]:
                                self.seriesScalarRange[0] = srange[0]
                            if self.seriesScalarRange[1] < srange[1]:
                                self.seriesScalarRange[1] = srange[1]
Exemplo n.º 33
0
def setup_data():
    ntimes = 7200
    tvals = numpy.arange(ntimes) * 6.
    tbnds = list(tvals)
    tbnds.append(43200)
    tbnds = numpy.array(tbnds) - 3.
    print("tvals:", tvals)
    print("tbnds:", tbnds)
    import cdtime
    tunits = 'hours since 209-01-01 06:00:00'
    t1 = cdtime.reltime(tvals[0], tunits)
    t2 = cdtime.reltime(tvals[-1], tunits)
    t3 = cdtime.reltime(tbnds[-1], tunits)
    print(t1.tocomp(), t1.tocomp(cdtime.Calendar360))
    print(t2.tocomp(), t2.tocomp(cdtime.Calendar360))
    print(t3.tocomp(), t3.tocomp(cdtime.Calendar360))

    axes = [{'table_entry': 'time1',
             'units': tunits,
             'coord_vals': tvals,
             'cell_bounds': tbnds,
             },
            {'table_entry': 'latitude',
             'units': 'degrees_north',
             'coord_vals': [0],
             'cell_bounds': [-5, 5]},
            {'table_entry': 'longitude',
             'units': 'degrees_east',
             'coord_vals': [0],
             'cell_bounds':[-10, 10]},
            # {'table_entry': 'depth',
            # 'units': 'm',
            # 'coord_vals': [10],
            # 'cell_bounds': [5,15]},
            ]

    values = numpy.ones(ntimes) * 1013.
    return values.astype("f"), axes
Exemplo n.º 34
0
def checkTimeUnits(self, name, value):
    checkName(self, name, value)
    if not isinstance(value, str):
        raise ValueError, "time units must be a string"
    a = cdtime.reltime(1, "days since 1900")
    try:
        a.torel(value)
    except:
        raise ValueError, value + " is invalid time units"
    sp = value.split("since")[1]
    b = cdtime.s2c(sp)
    if b == cdtime.comptime(0, 1):
        raise ValueError, sp + " is invalid date"
    return value
Exemplo n.º 35
0
def checkTimeUnits(self, name, value):
    checkName(self, name, value)
    if not isinstance(value, str):
        raise ValueError, 'time units must be a string'
    a = cdtime.reltime(1, 'days since 1900')
    try:
        a.torel(value)
    except:
        raise ValueError, value + ' is invalid time units'
    sp = value.split('since')[1]
    b = cdtime.s2c(sp)
    if b == cdtime.comptime(0, 1):
        raise ValueError, sp + ' is invalid date'
    return value
Exemplo n.º 36
0
def checkTimeUnits(self,name,value):
     checkName(self,name,value)
     if not isinstance(value,str):
          raise ValueError, 'time units must be a string'
     a=cdtime.reltime(1,'days since 1900')
     try:
          a.torel(value)
     except:
          raise ValueError, value+' is invalid time units'
     sp=value.split('since')[1]
     b=cdtime.s2c(sp)
     if b==cdtime.comptime(0,1):
          raise ValueError, sp+' is invalid date'
     return value
Exemplo n.º 37
0
 def format(value):
     reltime = cdtime.reltime(value, units)
     comptime = reltime.tocomp(calendar)
     if time_increment[0:6] == "second":
         return "%d-%02d-%02d %02d:%02d:%02d" % (comptime.year, comptime.month, comptime.day, comptime.hour, comptime.minute, comptime.second)
     elif time_increment[0:6] == "minute":
         return "%d-%02d-%02d %02d:%02d" % (comptime.year, comptime.month, comptime.day, comptime.hour, comptime.minute)
     elif time_increment[0:4] == "hour":
         return "%d-%02d-%02d %02d:00" % (comptime.year, comptime.month, comptime.day, comptime.hour)
     elif time_increment[0:3] == "day" or time_increment[0:4] == "week":
         return "%d-%02d-%02d" % (comptime.year, comptime.month, comptime.day)
     elif time_increment[0:5] == "month" or time_increment[0:6] == "season":
         return "%d-%02d" % (comptime.year, comptime.month)
     elif time_increment[0:4] == "year":
         return comptime.year
    def updateMetadata( self, plotIndex ):
        if self.metadata == None:
            scalars = None
             
#            arr_names = [] 
#            na = self.fieldData.GetNumberOfArrays()
#            for iF in range( na ):
#                arr_names.append( self.fieldData.GetArrayName(iF) )
#            print " updateMetadata: getFieldData, arrays = ", str( arr_names ) ; sys.stdout.flush()
            
            if self.fieldData == None:
                print>>sys.stderr,  ' NULL field data in updateMetadata: ispec[%x]  ' % id(self)  
                self.initializeMetadata() 
    
            self.metadata = self.computeMetadata( plotIndex )
            
            if self.metadata <> None:
                self.rangeBounds = None              
                self.datasetId = self.metadata.get( 'datasetId', None )                
                tval = self.metadata.get( 'timeValue', 0.0 )
                self.referenceTimeUnits = self.metadata.get( 'timeUnits', None )
                self.timeValue = cdtime.reltime( float( tval ), self.referenceTimeUnits ) if tval else None              
                self.dtype =  self.metadata.get( 'datatype', None )
                scalars =  self.metadata.get( 'scalars', None )
                self.rangeBounds = getRangeBounds( self.dtype )
                title = self.metadata.get( 'title', None )
                if title:
                    targs = title.split(':')
                    if len( targs ) == 1:
                        self.titleBuffer = "\n%s" % ( title )
                    elif len( targs ) > 1:
                        self.titleBuffer = "%s\n%s" % ( targs[1], targs[0] )
                else: self.titleBuffer = "" 
                attributes = self.metadata.get( 'attributes' , None )
                if attributes:
                    self.units = attributes.get( 'units' , '' )
                    srange = attributes.get( 'range', None )
                    if srange: 
        #                print "\n ***************** ScalarRange = %s, md[%d], var_md[%d] *****************  \n" % ( str(range), id(metadata), id(var_md) )
                        self.scalarRange = list( srange )
                        self.scalarRange.append( 1 )
                        if not self.seriesScalarRange:
                            self.seriesScalarRange = list(srange)
                        else:
                            if self.seriesScalarRange[0] > srange[0]:
                                self.seriesScalarRange[0] = srange[0] 
                            if self.seriesScalarRange[1] < srange[1]:
                                self.seriesScalarRange[1] = srange[1] 
Exemplo n.º 39
0
def santerTime(array,calendar=None):
        """
        Documentation for santerTime(array,calendar):
        -------
        The santerTime(array) function converts a known-time array to the
        standard time calendar - if non-gregorian the source calendar should
        be specified for accurate conversion
        
        Specified calendars can be one of the 5 calendars available within
        the cdtime module:
            GregorianCalendar
            MixedCalendar
            JulianCalendar
            NoLeapCalendar
            Calendar360
        For more information consult:
            http://uvcdat.llnl.gov/documentation/cdms/cdms_3.html#3.2
    
        Author: Paul J. Durack : [email protected]
    
        Usage:
        ------
            >>> from durolib import santerTime
            >>> import cdtime
            >>> newVar = santerTime(var,calendar=cdtime.NoLeapCalendar)
    
        Notes:
        -----
        """
        # Test calendar
        if calendar:
            cdtCalendar  = calendar
        else:
            cdtCalendar  = cdt.GregorianCalendar
        # Set time_since - months 1800-1-1
        time                = array.getTime()
        time_new            = []
        for tt in time:
            reltime = cdt.reltime(tt,time.units)
            time_new.append(reltime.torel('months since 1800-1-1',cdtCalendar).value)
        time_axis           = cdm.createAxis(time_new)
        time_axis.id        = 'time'
        time_axis.units     = 'months since 1800-1-1'
        time_axis.axis      = 'T'
        time_axis.calendar  = 'gregorian'
        array.setAxis(0,time_axis)
        cdu.setTimeBoundsMonthly(array)
        return array
Exemplo n.º 40
0
def checkDatawc(self,name,value):
     checkName(self,name,value)
     if isNumber(value):
          value = float(value), 0
     elif isinstance(value,str):
          t=cdtime.s2c(value)
          if t!=cdtime.comptime(0,1):
               t=t.torel(self.datawc_timeunits,self.datawc_calendar)
               value = float(t.value), 1
          else:
               raise ValueError, 'The '+name+' attribute must be either an integer or a float value or a date/time.' 
     elif type(value) in [type(cdtime.comptime(1900)),type(cdtime.reltime(0,'days since 1900'))]:
          value = value.torel(self.datawc_timeunits,self.datawc_calendar).value, 1
     else:
              raise ValueError, 'The '+name+' attribute must be either an integer or a float value or a date/time.'
     return value
Exemplo n.º 41
0
    def getTime(self, timeunits=None):
        '''
        Return time.
        '''
        if (timeunits == 'internal'):
            timeunits = self.getTimeUnits()

        time_values = self.f.getAxis("time")[:]

        deltime = time_values[1] - time_values[0]
        time_bounds = time_values
        numpy.append(time_bounds, time_values[-1] + deltime)

        cur_time = [cdtime.reltime(i, timeunits) for i in time_values]

        return cur_time
Exemplo n.º 42
0
def _check_endtime(filename):
#----------------------------
    """
    See file header.
    """

    fin = cdms2.openDataset(filename)

    sst = fin.variables['sst']

    t = sst.getTime()

    print ('Final time: ', cdtime.reltime(t[-1], t.units).tocomp())

    fin.close()

    return
Exemplo n.º 43
0
    def getTime(self,timeunits=None):
        '''
        Return time.
        '''
        if( timeunits == 'internal' ):
            timeunits=self.getTimeUnits()

        time_values =   self.f.getAxis("time")[:]
 
        deltime = time_values[ 1 ] - time_values[ 0 ]
        time_bounds = time_values
        numpy.append(time_bounds, time_values[-1] + deltime )

        cur_time=[ cdtime.reltime( i, timeunits ) 
                   for i in time_values ]
        
        return cur_time
Exemplo n.º 44
0
def check_time_axis(ifile):
    # Check the output to ensure that the time-axis is complete.
    c = cdms2.open(ifile)
    if 'time' in c.axes:
        t = c.axes['time']
        if len(t) <= 1:
            c.close()
            return 0

        # Need to allow time-axis with units in "days since ..." to
        # vary a little.
        dt = t[1] - t[0]
        if t.units.startswith("days"):
            mindt = dt - 3
            maxdt = dt + 3
        else:
            mindt = dt
            maxdt = dt
        diff_ts = [elem[1] - elem[0] for elem in \
                   zip(t[:-1], t[1:])]
        #check = np.logical_and(mindt <= diff_ts,
        #                       maxdt >= diff_ts)
        #print mindt <= diff_ts
        check = t[1:][np.logical_or(diff_ts < mindt, diff_ts > maxdt)]

        if len(check) > 0:
            # Not constantly increasing time-axis.
            print(
                '\nERROR:\tTime axis not consistently monotonically increasing.'
            )
            print('\tCheck that input files represent entire time period')
            #Print Problem Files
            import cdtime
            print("\tBoundaries of problem time points")
            for tp in check:
                print('\t\t'),
                tindex = np.where(t == tp)[0][0]
                for invalid_t in t[tindex - 1:tindex + 1]:
                    print('%s,' % cdtime.reltime(invalid_t, t.units).tocomp()),
                print('\n')
            c.close()
            return 1

    c.close()
    return 0
Exemplo n.º 45
0
def toRelativeTime(time_bnds, units, oldunits=None):
        """Convert values of time_bnds to another unit possibly in another calendar.
        The time variable, time_bnds.getTime(), should have already been converted."""
        # based on AbstractAxis.toRelativeTime(self,units,calendar)
        if not hasattr(time_bnds, 'units'):
            if oldunits==None:
                raise CDMSError, "No time units defined"
            time_bnds.units = oldunits
        tb = time_bnds.getTime()
        n=len(tb[:])
        calendar = tb.getCalendar()
        for i in range(n):
            for j in range(2):
                tmp=cdtime.reltime(time_bnds[i,j], time_bnds.units).tocomp(calendar)
                tmp2 = numpy.array(float(tmp.torel(units, calendar).value)).astype(time_bnds.dtype.char)
                time_bnds[i,j]=tmp2
        time_bnds.units=units
        return
Exemplo n.º 46
0
 def executeTask( self, skipCompletedTasks, **args ):
     cdmsDataset = self.getInputValue( "dataset"  ) 
     tValue = args.get( 'timeValue', cdmsDataset.timeRange[2] )
     self.timeValue = cdtime.reltime( float( tValue ), cdmsDataset.referenceTimeUnits )
     taskInputData = self.getInputValue( "task"  ) 
     taskMap =  deserializeTaskData( getItem( taskInputData ) ) if taskInputData else None   
     taskData =  taskMap.get( cdmsDataset.getDsetId(), None ) if taskMap else None
     task = None
     if taskData:
         taskName = taskData[0]
         if taskName:
             taskClass = TaskManager.getTask( taskName )
             if taskClass:
                 task = taskClass( cdmsDataset )
                 task_key = task.getInputMap( self )
                 if ( skipCompletedTasks and self.getTaskCompleted( task_key ) ):   print " Skipping completed task: %s " % task_key
                 else:  task.compute( [ self.timeValue, ] )
                 self.setTaskCompleted( task_key )
Exemplo n.º 47
0
def check_time_axis(ifile):
    # Check the output to ensure that the time-axis is complete.
    c = cdms2.open(ifile)
    if 'time' in c.axes:
       t = c.axes['time']
       if len(t) <= 1:
           c.close()
           return 0


       # Need to allow time-axis with units in "days since ..." to
       # vary a little. 
       dt = t[1] - t[0]
       if t.units.startswith("days"):
           mindt = dt - 3
           maxdt = dt + 3
       else:
           mindt = dt
           maxdt = dt
       diff_ts = [elem[1] - elem[0] for elem in \
                  zip(t[:-1], t[1:])]
       #check = np.logical_and(mindt <= diff_ts,
       #                       maxdt >= diff_ts)
       #print mindt <= diff_ts
       check = t[1:][np.logical_or(diff_ts < mindt,diff_ts > maxdt)]

       if len(check) > 0:
           # Not constantly increasing time-axis.
           print('\nERROR:\tTime axis not consistently monotonically increasing.')
           print('\tCheck that input files represent entire time period')
           #Print Problem Files
           import cdtime
           print("\tBoundaries of problem time points")
           for tp in check:
               print('\t\t'),
               tindex = np.where(t==tp)[0][0]
               for invalid_t in t[tindex-1:tindex+1]:
                   print('%s,' % cdtime.reltime(invalid_t,t.units).tocomp()),
               print('\n')
           c.close()
           return 1

    c.close()
    return 0
Exemplo n.º 48
0
    def __init__(self, path, convention, calendar, frequency, delta, basetime, center=Pcmdi, model="unknown", comments=" "):
	self.id = slats.create(path, convention, calendar, frequency, delta, center, model, comments)
	if self.id==0:
	    raise LatsFileCreateError, path
	self.path = path
	if type(basetime)==type(cdtime.reltime(0,"years")):
	    c = basetime.tocomp()
	else:
	    c = basetime
	slats.basetime(self.id, c.year, c.month, c.day, c.hour)
	self.basetime = c
	self.convention = convention
	self.calendar = calendar
	self.frequency = frequency
	self.delta = delta
	self.center = center
	self.model = model
	self.comments = comments
        self.varlist = []               # Only used to remove grid, vertdim references on close
Exemplo n.º 49
0
    def genTime(value, units, calendarTag):
        """
        Generate a string representation of a relative time value.

        Returns a string representation of the relative time (value, units)

        value
          Float time value

        units
          String time units

        calendarTag
          String cdtime calendar tag.
        """
        t = reltime(value, units)
        c = t.tocomp(_tagToCalendar[calendarTag])
        result = "%04d-%02d-%02dT%02d:%02d:%02d"%(c.year, c.month, c.day, c.hour, c.minute, int(c.second))
        return result
Exemplo n.º 50
0
    def genTime(value, units, calendarTag):
        """
        Generate a string representation of a relative time value.

        Returns a string representation of the relative time (value, units)

        value
          Float time value

        units
          String time units

        calendarTag
          String cdtime calendar tag.
        """
        t = reltime(value, units)
        c = t.tocomp(_tagToCalendar[calendarTag])
        result = "%04d-%02d-%02dT%02d:%02d:%02d"%(c.year, c.month, c.day, c.hour, c.minute, int(c.second))
        return result
Exemplo n.º 51
0
def getRange(gm, xm, xM, ym, yM):
    # Also need to make sure it fills the whole space
    rtype = type(cdtime.reltime(0, "days since 2000"))
    X1, X2 = gm.datawc_x1, gm.datawc_x2
    if isinstance(X1, rtype) or isinstance(X2, rtype):
        X1 = X1.value
        X2 = X2.value
    if not numpy.allclose([X1, X2], 1.e20):
        x1, x2 = X1, X2
    else:
        x1, x2 = xm, xM
    Y1, Y2 = gm.datawc_y1, gm.datawc_y2
    if isinstance(Y1, rtype) or isinstance(Y2, rtype):
        Y1 = Y1.value
        Y2 = Y2.value
    if not numpy.allclose([Y1, Y2], 1.e20):
        y1, y2 = Y1, Y2
    else:
        y1, y2 = ym, yM
    return x1, x2, y1, y2
Exemplo n.º 52
0
 def format(value):
     reltime = cdtime.reltime(axis[value], units)
     comptime = reltime.tocomp(calendar)
     if time_increment[0:6] == "second":
         return "%d-%02d-%02d %02d:%02d:%02d" % (
             comptime.year, comptime.month, comptime.day, comptime.hour,
             comptime.minute, comptime.second)
     elif time_increment[0:6] == "minute":
         return "%d-%02d-%02d %02d:%02d" % (comptime.year, comptime.month,
                                            comptime.day, comptime.hour,
                                            comptime.minute)
     elif time_increment[0:4] == "hour":
         return "%d-%02d-%02d %02d:00" % (comptime.year, comptime.month,
                                          comptime.day, comptime.hour)
     elif time_increment[0:3] == "day" or time_increment[0:4] == "week":
         return "%d-%02d-%02d" % (comptime.year, comptime.month,
                                  comptime.day)
     elif time_increment[0:5] == "month" or time_increment[0:6] == "season":
         return "%d-%02d" % (comptime.year, comptime.month)
     elif time_increment[0:4] == "year":
         return comptime.year
Exemplo n.º 53
0
 def __init__(self, domain, dimname, unit):
     self.units = unit
     self.extent = []
     #patch to handle current limitations of multiple time dimension scanning in csml.
     if string.lower(self.units)[:10] in [
             'days_since', 'seconds_si', 'minutes_si', 'hours_sinc',
             'months _sin', 'years_sinc'
     ]:
         if type(domain[dimname][0]) is not str:
             tunits = self.units.replace('_', ' ')
             for val in domain[dimname]:
                 csmltime = csml.csmllibs.csmltime.UDtimeToCSMLtime(
                     cdtime.reltime(float(val), tunits).tocomp())
                 self.extent.append(csmltime)
         else:
             for val in domain[dimname]:
                 self.extent.append(str(val))
     else:
         for val in domain[dimname]:
             self.extent.append(str(val))
     #for time axis replace units with iso string
     if dimname == 'time':
         self.units = 'ISO8601'
Exemplo n.º 54
0
    def write(self, t, data, lev=None):

        # If the variable is multilevel, require the level value
        # Else if it is an explicit dimension, just use the single value
        # Else use level=0.0

        if self.vertdim and lev == None:
            if len(self.vertdim.levs) > 1:
                raise LatsLevelNeededError, self.name
            else:
                lev = self.vertdim.levs[0]
        elif lev == None:
            lev = 0.0

        # Require floats to be Float32 or Float64, and integers to be Int or Int32
        if self.datatype == LatsFloat and data.typecode(
        ) not in Numeric.typecodes['Float']:
            raise LatsWrongDatatype, "variable %s, at time %s, level %d" % (
                self.name, ` t `, lev)

        if self.datatype == LatsInt and data.typecode(
        ) not in Numeric.typecodes['Integer']:
            raise LatsWrongDatatype, "variable %s, at time %s, level %d" % (
                self.name, ` t `, lev)

        if type(t) == type(cdtime.reltime(0, "years")):
            c = t.tocomp()
        else:
            c = t
        if data.shape != self.grid.shape:
            raise LatsDataShapeError, "%s, variable %s, at time %s, level %d\nInput field shape: %s" % (
                ` self.grid.shape `, self.name, ` t `, lev, ` data.shape `)
        err = slats.write(self.file.id, self.id, lev, c.year, c.month, c.day,
                          c.hour, data)
        if err == 0:
            raise LatsWriteError, "%s, at time %s, level %d" % (self.name, ` t
                                                                `, lev)
Exemplo n.º 55
0
def _extremum_(func, ctime, i0, i, var, spline):
    """Extremum possibly using splines"""
    nt = len(var)
    if spline and nt >= 4:  # and i != 0 and i != (nt-1)
        if i == 0:
            ifirst, ilast = 0, 4
        elif i == nt - 1:
            ifirst, ilast = nt - 4, nt
        else:
            icenter = i - int(var[i - 1] > var[i + 1])
            ifirst = max(icenter - 1, 0)
            ilast = ifirst + 4
            if ilast > nt:
                ilast -= 1
                ifirst -= 1
        mn_units = 'minutes since %s' % ctime[i0 + ifirst]
        old_rts = cdms2.createAxis(
            N.asarray([
                ct.torel(mn_units).value
                for ct in ctime[i0 + ifirst:i0 + ilast]
            ],
                      dtype='d'))
        old_var = MV2.array(var[ifirst:ilast], axes=[old_rts], copyaxes=0)
        mn_rts = cdms2.createAxis(N.arange(int(old_rts[-1] + 1), dtype='d'))
        mn_var = interp1d(old_var, mn_rts, method='cubic')
        del old_var, old_rts
        #       mn_var = spline_interpolate(old_rts,var[i-1:i+2],mn_rts)
        #       mn_var = splev(mn_rts, splrep(old_rts,var[ifirst:ilast]))
        mn_i = func(mn_var)
        val = mn_var[mn_i]
        del mn_var
        this_ctime = cdtime.reltime(mn_i, mn_units).tocomp()
    else:
        this_ctime = ctime[i0 + i]
        val = var[i]
    return val, this_ctime
Exemplo n.º 56
0
    Ps = fps.getslab(varps, tm, tm)
    Ps = MA.average(Ps, axis=0)
    # create the pressure field
    #   print 'Creating Pressure field'
    P = make_P(Ps, A, B, Po)
    #   print 'Shapes,T,Ps,P',T.shape,Ps.shape,P.shape

    # interpolate
    #   print 'Interpolating now !'
    out = log_linear_vinterp(T, P, levels)
    sh = list(out.shape)
    sh.insert(0, 1)
    out = MA.reshape(out, tuple(sh))
    #tmp=MA.asarray(tmp,Float16)
    t = tim.subAxis(itim, itim + 1)
    xx = reltime(tim[itim], tim.units)
    t_new = xx.torel('days since 1800').value
    t[0] = t_new
    t.units = 'days since 1800'
    meta[0][0] = t
    #   print meta[0][0][:]
    levelsax = cdms.createAxis(levels / 100.)
    levelsax.id = 'plev'
    levelsax.units = 'hPa'
    levelsax.designateLevel()
    meta[0][1] = levelsax
    out = putMetadata(meta, out)
    out.id = varout
    cdutil.times.setTimeBoundsDaily(out)
    if itim == 0:
        f = cdms.open(outfile, 'w')
Exemplo n.º 57
0
def process_src(nm, code, typ):
    """Takes VCS script code (string) as input and generates oneD gm from it"""
    onm = nm + ""
    if typ == "GYx":
        nm += "_yxvsx"
    elif typ == "GXY":
        nm += "_xvsy"
    elif typ == "GXy":
        nm += "_xyvsy"
    elif typ == "GSp":
        nm += "_scatter"
    try:
        gm = G1d(nm)
    except:
        gm = vcs.elements["1d"][nm]
    # process attributes with = as assignement
    for att in ["projection",
                "xticlabels#1", "xticlabels#2",
                "xmtics#1", "xmtics#2",
                "yticlabels#1", "yticlabels#2",
                "ymtics#1", "ymtics#2",
                "xaxisconvert", "yaxisconvert",
                "datawc_tunits",
                "Tl",
                "Tm",
                "datawc_tunits",
                "datawc_calendar"]:
        i = code.find(att)
        if i == -1:
            continue
        j = code[i:].find(",") + i
        if j - i == -1:  # last one no comma
            j = None
        scode = code[i:j]
        sp = scode.split("=")
        nm = sp[0].strip()
        nm = nm.replace("#", "")
        if nm == "Tl":
            nm = "line"
        elif nm == "Tm":
            nm = "marker"
        elif nm == "datawc_tunits":
            nm = "datawc_timeunits"
        try:
            # int will be converted
            setattr(gm, nm, int(sp[1]))
        except:
            try:
                # int and floats will be converted
                setattr(gm, nm, eval(sp[1]))
            except:
                # strings
                try:
                    setattr(gm, nm, sp[1])
                except:
                    pass  # oh well we stick to default value
    # Datawc
    idwc = code.find("datawc(")
    if idwc > -1:
        jdwc = code[idwc:].find(")") + idwc
        cd = code[idwc + 7:jdwc]
        vals = cd.split(",")
        gm.datawc_x1 = float(vals[0])
        gm.datawc_y1 = float(vals[1])
        gm.datawc_x2 = float(vals[2])
        gm.datawc_y2 = float(vals[3])
    # idatawc
    idwc = code.find("idatawc(")
    if idwc > -1:
        jdwc = code[idwc:].find(")") + idwc
        cd = code[idwc + 8:jdwc]
        vals = cd.split(",")
        if int(vals[0]) == 1:
            gm.datawc_x1 = cdtime.reltime(
                gm.datawc_x1,
                gm.datawc_timeunits).tocomp(
                gm.datawc_calendar)
        if int(vals[1]) == 1:
            gm.datawc_y1 = cdtime.reltime(
                gm.datawc_x2,
                gm.datawc_timeunits).tocomp(
                gm.datawc_calendar)
        if int(vals[2]) == 1:
            gm.datawc_x2 = cdtime.reltime(
                gm.datawc_y1,
                gm.datawc_timeunits).tocomp(
                gm.datawc_calendar)
        if int(vals[3]) == 1:
            gm.datawc_y2 = cdtime.reltime(
                gm.datawc_y2,
                gm.datawc_timeunits).tocomp(
                gm.datawc_calendar)
    if typ == "GYx":
        vcs.elements["yxvsx"][onm] = gm
    elif typ == "GXY":
        vcs.elements["xvsy"][onm] = gm
    elif typ == "GXy":
        gm.flip = True
        vcs.elements["xyvsy"][onm] = gm
    elif typ == "GSp":
        gm.linewidth = 0
        vcs.elements["scatter"][onm] = gm
Exemplo n.º 58
0
    mincmor=c-c0
c0=c
f.write(s,id=varout)
c=time.time()
print 'cdms time:',c-c0
totcdms+=c-c0
if maxcdms<c-c0:
    maxcdms=c-c0
if mincdms>c-c0:
    mincdms=c-c0
c0=c
f.close()
cmor.close()

import cdtime,os
ltime = cdtime.reltime(ntimes-1,'month since 1980').tocomp()
lcmor = os.stat("CMIP6/CMIP/CSIRO-BOM/NICAM/piControl/r1i1p1f1/Amon/tas/gn/v%s/tas_Amon_piControl_NICAM_r1i1p1f1_gn_197901-197912.nc"%(today))[6]
print 'level:',level,"shuffle:",shuffle
print 'total cmor:',totcmor,mincmor,totcmor/ntimes,maxcmor,lcmor
lcdms = os.stat("Test/crap.nc")[6]
print 'total cdms:',totcdms,mincdms,totcdms/ntimes,maxcdms,lcdms
print 'Size diff:',float(lcmor)/float(lcdms)
print 'speed diff:', totcmor/totcdms

if os.path.exists("summary.txt"):
    f = open("summary.txt")
    s=f.read()
    f.close()
    dic = eval(s)
else:
    dic = {}
Exemplo n.º 59
0
def process_src(nm, code):

    # Takes VCS script code (string) as input and generates isoline gm from it
    try:
        g = Gi(nm)
    except:
        g = vcs.elements["isoline"][nm]
    # process attributes with = as assignement
    for att in ["projection",
                "xticlabels#1", "xticlabels#2",
                "xmtics#1", "xmtics#2",
                "yticlabels#1", "yticlabels#2",
                "ymtics#1", "ymtics#2",
                "xaxisconvert", "yaxisconvert",
                "datawc_tunits",
                "boxfill_type",
                "legend",
                "ext_1", "ext_2",
                "missing",
                "datawc_calendar", "make_labels"]:
        i = code.find(att)
        if i == -1:
            continue
        j = code[i:].find(",") + i
        if j - i == -1:  # last one no comma
            j = None
        scode = code[i:j]
        sp = scode.split("=")
        nm = sp[0].strip()
        nm = nm.replace("#", "")
        if nm == "make_labels":
            nm = "label"
        elif nm == "datawc_tunits":
            nm = "datawc_timeunits"
        try:
            # int will be converted
            setattr(g, nm, int(sp[1]))
        except:
            try:
                # int and floats will be converted
                setattr(g, nm, eval(sp[1]))
            except:
                # strings
                try:
                    setattr(g, nm, sp[1])
                except:
                    pass  # oh well we stick to default value
    # Datawc
    idwc = code.find(" datawc(")
    if idwc > -1:
        jdwc = code[idwc:].find(")") + idwc
        cd = code[idwc + 8:jdwc]
        vals = cd.split(",")
        g.datawc_x1 = float(vals[0])
        g.datawc_y1 = float(vals[1])
        g.datawc_x2 = float(vals[2])
        g.datawc_y2 = float(vals[3])
    # idatawc
    idwc = code.find("idatawc(")
    if idwc > -1:
        jdwc = code[idwc:].find(")") + idwc
        cd = code[idwc + 8:jdwc]
        vals = cd.split(",")
        if int(vals[0]) == 1:
            g.datawc_x1 = cdtime.reltime(
                g.datawc_x1,
                g.datawc_timeunits).tocomp(
                g.datawc_calendar)
        if int(vals[1]) == 1:
            g.datawc_y1 = cdtime.reltime(
                g.datawc_x2,
                g.datawc_timeunits).tocomp(
                g.datawc_calendar)
        if int(vals[2]) == 1:
            g.datawc_x2 = cdtime.reltime(
                g.datawc_y1,
                g.datawc_timeunits).tocomp(
                g.datawc_calendar)
        if int(vals[3]) == 1:
            g.datawc_y2 = cdtime.reltime(
                g.datawc_y2,
                g.datawc_timeunits).tocomp(
                g.datawc_calendar)
    irg = code.find("lines")
    if irg > -1:
        scode = code[irg:]
        tl = []
        tt = []
        to = []
        clock = []
        scales = []
        angles = []
        spacing = []
        levs = []
        # print code[irg:]
        iid = scode.find("(id=")
        while iid > -1:
            sub = genutil.get_parenthesis_content(scode[iid:])
            if get_att_from_sub(sub, "label") not in ["*", None]:
                g.label = 'y'
            levs.append(get_att_from_sub(sub, "level"))
            tl.append(get_att_from_sub(sub, "Tl"))
            to.append(get_att_from_sub(sub, "To"))
            tt.append(get_att_from_sub(sub, "Tt"))
            clock.append(get_att_from_sub(sub, "clockwise"))
            scales.append(get_att_from_sub(sub, "length"))
            angles.append(get_att_from_sub(sub, "angle"))
            spacing.append(get_att_from_sub(sub, "spacing"))
            iend = scode[iid:].find(")") + 1
            scode = scode[iid + iend:]
            iid = scode.find("(id=")
    g.level = levs
    try:
        g.setLineAttributes(tl)
    except ValueError:
        pass
    try:
        g.text = to
    except:
        g._text = to
    try:
        g.textcolors = tt
    except:
        g._textcolors = tt

    gd = vcs.elements["isoline"]["default"]
    try:
        g.scale = scales
    except:
        g.scale = gd.scale
    try:
        g.angle = angles
    except:
        g.angle = gd.angle
    try:
        g.clockwise = clock
    except:
        g.clockwise = gd.clockwise
Exemplo n.º 60
0
def generate_time_labels(d1, d2, units, calendar=cdtime.DefaultCalendar):
    """ generate_time_labels(self,d1,d2,units,calendar=cdtime.DefaultCalendar)
    returns a dictionary of time labels for an interval of time, in a user defined units system
    d1 and d2 must be cdtime object, if not they will be assumed to be in "units"

    Example:
    lbls = generate_time_labels(cdtime.reltime(0,'months since 2000'),
                                cdtime.reltime(12,'months since 2000'),
                                'days since 1800',
                                )
    This generated a dictionary of nice time labels for the year 2000 in units of 'days since 1800'

    lbls = generate_time_labels(cdtime.reltime(0,'months since 2000'),
                                cdtime.comptime(2001),
                                'days since 1800',
                                )
    This generated a dictionary of nice time labels for the year 2000 in units of 'days since 1800'

    lbls = generate_time_labels(0,
                                12,
                                'months since 2000',
                                )
    This generated a dictionary of nice time labels for the year 2000 in units of 'months since 2000'

    """
    if isinstance(d1, (int, long, float)):
        d1 = cdtime.reltime(d1, units)
    if isinstance(d2, (int, long, float)):
        d2 = cdtime.reltime(d2, units)
    d1r = d1.torel(units, calendar)
    d2r = d2.torel(units, calendar)
    d1, d2 = minmax(d1r.value, d2r.value)
    u = units.split('since')[0].strip().lower()
    dic = {}
    if u in ['month', 'months']:
        delta = (d2 - d1) * 30
    elif u in ['year', 'years']:
        delta = (d2 - d1) * 365
    elif u in ['hours', 'hour']:
        delta = (d2 - d1) / 24.
    elif u in ['minute', 'minutes']:
        delta = (d2 - d1) / 24. / 60.
    elif u in ['second', 'seconds']:
        delta = (d2 - d1) / 24. / 60.
    else:
        delta = d2 - d1
    if delta < .042:  # less than 1 hour
        levs = mkscale(d1, d2)
        for l in levs:
            dic[l] = str(cdtime.reltime(l, units).tocomp(calendar))
    elif delta < 1:  # Less than a day put a label every hours
        d1 = d1r.torel('hours since 2000').value
        d2 = d2r.torel('hours since 2000').value
        d1, d2 = minmax(d1, d2)
        levs = mkscale(d1, d2)
        for l in levs:
            t = cdtime.reltime(l, 'hours since 2000').tocomp(calendar)
            if t.minute > 30:
                t = t.add(1, cdtime.Hour)
            t.minute = 0
            t.second = 0
            tr = t.torel(units, calendar)
            dic[tr.value] = str(t).split(':')[0]
    elif delta < 90:  # Less than 3 month put label every day
        d1 = d1r.torel('days since 2000').value
        d2 = d2r.torel('days since 2000').value
        d1, d2 = minmax(d1, d2)
        levs = mkscale(d1, d2)
        for l in levs:
            t = cdtime.reltime(l, 'days since 2000').tocomp(calendar)
            if t.hour > 12:
                t = t.add(1, cdtime.Day)
            t.hour = 0
            t.minute = 0
            t.second = 0
            tr = t.torel(units, calendar)
            dic[tr.value] = str(t).split(' ')[0]
    elif delta < 800:  # ~ Less than 24 month put label every month
        d1 = d1r.torel('months since 2000').value
        d2 = d2r.torel('months since 2000').value
        d1, d2 = minmax(d1, d2)
        levs = mkscale(d1, d2)
        for l in levs:
            t = cdtime.reltime(l, 'months since 2000').tocomp(calendar)
            if t.day > 15:
                t = t.add(1, cdtime.Month)
            t.day = 1
            t.hour = 0
            t.minute = 0
            t.second = 0
            tr = t.torel(units, calendar)
            dic[tr.value] = '-'.join(str(t).split('-')[:2])
    else:  # ok lots of years, let auto decide but always puts at Jan first
        d1 = d1r.torel('years since 2000').value
        d2 = d2r.torel('years since 2000').value
        d1, d2 = minmax(d1, d2)
        levs = mkscale(d1, d2)
        for l in levs:
            t = cdtime.reltime(l, 'years since 2000').tocomp(calendar)
            if t.month > 6:
                t = t.add(1, cdtime.Year)
            t.month = 1
            t.day = 1
            t.hour = 0
            t.minute = 0
            t.second = 0
            tr = t.torel(units, calendar)
            dic[tr.value] = str(t).split('-')[0]
    return dic