예제 #1
0
def two_times_from_one(t):
    """Input is a time representation, either as the long int used in the cdscan
    script, or a string in the format "2010-08-25 15:26:00", or as a cdtime comptime
    (component time) object.
    Output is the same time, both as a long _and_ as a comptime."""
    if t == 0:
        t = 0L
    if isinstance(t, str):
        t = cdtime.s2c(t)
    if (isinstance(t, long) or isinstance(t, int)) and t > 1000000000L:
        tl = t
        year = tl / 1000000000L
        rem = tl % 1000000000L
        month = rem / 10000000L
        rem = rem % 10000000L
        day = rem / 100000
        allsecs = rem % 100000
        sec = allsecs % 60
        allmins = allsecs / 60
        min = allmins % 60
        hour = allmins / 60
        tc = cdtime.comptime(year, month, day, hour, min, sec)
    else:
        # I'd like to check that t is type comptime, but although Python
        # prints the type as <type 'comptime'> it won't recognize as a type
        # comptime or anything similar.  Note that cdtime.comptime is a C
        # function available from Python.
        tc = t
        tl = tc.year * 1000000000L
        tl += tc.month * 10000000L
        tl += tc.day * 100000
        tl += tc.hour * 3600
        tl += tc.minute * 60
        tl += tc.second.__int__()
    return tl, tc
예제 #2
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
예제 #3
0
파일: forecast.py 프로젝트: AZed/uvcdat
def two_times_from_one( t ):
    """Input is a time representation, either as the long int used in the cdscan
    script, or a string in the format "2010-08-25 15:26:00", or as a cdtime comptime
    (component time) object.
    Output is the same time, both as a long _and_ as a comptime."""
    if t==0:
        t = 0L
    if isinstance(t,str):
        t = cdtime.s2c(t)
    if (isinstance(t,long) or isinstance(t,int)) and t>1000000000L :
        tl = t
        year = tl / 1000000000L
        rem =  tl % 1000000000L
        month = rem / 10000000L
        rem =   rem % 10000000L
        day =     rem / 100000
        allsecs = rem % 100000
        sec =     allsecs%60
        allmins = allsecs/60
        min =  allmins%60
        hour = allmins/60
        tc = cdtime.comptime(year,month,day,hour,min,sec)
    else:
        # I'd like to check that t is type comptime, but although Python
        # prints the type as <type 'comptime'> it won't recognize as a type
        # comptime or anything similar.  Note that cdtime.comptime is a C
        # function available from Python.
        tc = t
        tl = tc.year * 1000000000L
        tl += tc.month * 10000000L
        tl += tc.day   * 100000
        tl += tc.hour * 3600
        tl += tc.minute *60
        tl += tc.second.__int__()
    return tl,tc
예제 #4
0
    def timestr2comp(self, date):
        """
        :func:`timestr2comp`: To convert date from yyyymmdd[hh] formate into
                          cdtime.comptime formate
        Condition :
                passing date must be yyyymmdd formate in either int or str
        Inputs:
                date in yyyymmdd formate or yyyymmddhh formate.
                i.e. hour(hh) is optional.
        Outputs:
                It should return the date in cdtime.comptime object type
        Usage:
            example1:
                >>> timestr2comp(20110423)
                2011-4-23 0:0:0.0
                  .. note:: It should return as cdtime.comptype. Here we didnt
                            pass the hour. i.e only yyyymmdd formate
            example2:
                >>> timestr2comp(2011082010)
                2011-8-20 10:0:0.0
                  ..note:: Here it should return cdtime with hours also.
                           We passed yyyymmddhh formate. i.e include hh
            example3:
                >>> timestr2comp(2011082023)
                2011-8-20 23:0:0.0
                  ..note:: we cannot pass 24 as hour here. Max 23 hours only.

        Written by: Arulalan.T

        Date: 23.04.2011
        Updated : 21.08.2011

        """
        if str(type(date)) == "<type 'comptime'>":
            # passed date itself comptime object only
            return date
        if isinstance(date, int):
            date = str(date)
        # re match
        if self.comppattern.match(date):
            # i.e. date is comptime in string formate
            # so make it as comptime object
            return cdtime.s2c(date)

        if self.ymdpattern.match(date):
            # i.e date is yyyymmdd string formate
            year = int(date[0:4])
            month = int(date[4:6])
            day = int(date[6:8])
            if len(date) == 10:
                hour = int(date[-2:])
                return cdtime.comptime(year, month, day, hour)
            else:
                return cdtime.comptime(year, month, day)
        else:
            raise _TimeUtilityStringError('The given date either comptime \
                         object or comptime string or yyyymmdd formate only')
예제 #5
0
    def timestr2comp(self, date):
        """
        :func:`timestr2comp`: To convert date from yyyymmdd[hh] formate into
                          cdtime.comptime formate
        Condition :
                passing date must be yyyymmdd formate in either int or str
        Inputs:
                date in yyyymmdd formate or yyyymmddhh formate.
                i.e. hour(hh) is optional.
        Outputs:
                It should return the date in cdtime.comptime object type
        Usage:
            example1:
                >>> timestr2comp(20110423)
                2011-4-23 0:0:0.0
                  .. note:: It should return as cdtime.comptype. Here we didnt
                            pass the hour. i.e only yyyymmdd formate
            example2:
                >>> timestr2comp(2011082010)
                2011-8-20 10:0:0.0
                  ..note:: Here it should return cdtime with hours also.
                           We passed yyyymmddhh formate. i.e include hh
            example3:
                >>> timestr2comp(2011082023)
                2011-8-20 23:0:0.0
                  ..note:: we cannot pass 24 as hour here. Max 23 hours only.

        Written by: Arulalan.T

        Date: 23.04.2011
        Updated : 21.08.2011

        """
        if str(type(date)) == "<type 'comptime'>":
            # passed date itself comptime object only
            return date
        if isinstance(date, int):
            date = str(date)
        # re match
        if self.comppattern.match(date):
            # i.e. date is comptime in string formate
            # so make it as comptime object
            return cdtime.s2c(date)

        if self.ymdpattern.match(date):
            # i.e date is yyyymmdd string formate
            year = int(date[0:4])
            month = int(date[4:6])
            day = int(date[6:8])
            if len(date) == 10:
                hour = int(date[-2:])
                return cdtime.comptime(year, month, day, hour)
            else:
                return cdtime.comptime(year, month, day)
        else:
            raise _TimeUtilityStringError('The given date either comptime \
                         object or comptime string or yyyymmdd formate only')
예제 #6
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
예제 #7
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
예제 #8
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
예제 #9
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
예제 #10
0
import cdtime

# Creer un objet 'comptime' : temps absolu
# - en specifiant tout (annee,mois,jour,heure,minute,seconde)
ctime = cdtime.comptime(2000,1,1,0,0,0)
# - ou seulement les premiers arguments
ctime = cdtime.comptime(2000)
# - a partir d'une chaines de caracteres
ctime = cdtime.s2c('2000-1-1 0')
# - on verifie
print ctime
#  -> 2000-1-1 0:0:0.0
# - on verifie des valeurs numeriques
print ctime.year,ctime.day
#  -> 2000 1

# Creer un objet 'reltime' : temps relatif
# - en specifiant explicitement (valeur, unites CF)
rtime = cdtime.reltime(50, 'years since 1950')
print '%s | %s | %s' %(rtime,rtime.value,rtime.units)
#  -> 50.000000 years since 1950 | 50.0 | years since 1950
# - a partir d'une chaines de caracteres et d'unites
rtime = cdtime.s2r('2000-1-1 0','years since 1950')
print rtime.value
#  -> 50.0

# Operations
# - soustraction/addition
print ctime.add(1,cdtime.Year),'|',rtime.add(-1,cdtime.Year)
#  -> 2001-1-1 0:0:0.0 | 49.00 years since 1950
# - conversions
                    "For variable %s you requested start time to be at index: %i but the file only has %i time steps" %
                    (v, int(
                        A.start), len(tim)))
            i0 = int(A.start)
        elif A.index == "value":  # actual value used for slicing
            v0 = float(A.start)
            try:
                i0, tmp = tim.mapInterval((v0, v0), 'cob')
            except:
                raise RuntimeError(
                    "Could not find value %s for start time for variable %s" %
                    (A.start, v))
        elif A.index == "date":
            v0 = A.start
            # When too close from bounds it messes it up, adding a minute seems to help
            v0 = cdtime.s2c(A.start)
            v0 = v0.add(1, cdtime.Minute)
            try:
                i0, tmp = tim.mapInterval((v0, v0), 'cob')
            except:
                raise RuntimeError(
                    "Could not find start time %s for variable: %s" %
                    (A.start, v))

    if A.end is None:
        i1 = None
    else:  # Ok user specified a end time
        if A.index == "index":  # index-based slicing
            if int(A.end) >= len(tim):
                raise RuntimeError(
                    "For variable %s you requested end time to be at index: %i but the file only has %i time steps" %
예제 #12
0
                raise RuntimeError(
                    "For variable %s you requested start time to be at index: %i but the file only has %i time steps"
                    % (v, int(A.start), len(tim)))
            i0 = int(A.start)
        elif A.index == "value":  # actual value used for slicing
            v0 = float(A.start)
            try:
                i0, tmp = tim.mapInterval((v0, v0), 'cob')
            except:
                raise RuntimeError(
                    "Could not find value %s for start time for variable %s" %
                    (A.start, v))
        elif A.index == "date":
            v0 = A.start
            # When too close from bounds it messes it up, adding a minute seems to help
            v0 = cdtime.s2c(A.start)
            v0 = v0.add(1, cdtime.Minute)
            try:
                i0, tmp = tim.mapInterval((v0, v0), 'cob')
            except:
                raise RuntimeError(
                    "Could not find start time %s for variable: %s" %
                    (A.start, v))

    if A.end is None:
        i1 = None
    else:  # Ok user specified a end time
        if A.index == "index":  # index-based slicing
            if int(A.end) >= len(tim):
                raise RuntimeError(
                    "For variable %s you requested end time to be at index: %i but the file only has %i time steps"
예제 #13
0
def runClim(A):

    print("OK SO START IS:", A.start)
    # season dictionary
    season_function = {
        "djf": cdutil.times.DJF,
        "mam": cdutil.times.MAM,
        "jja": cdutil.times.JJA,
        "son": cdutil.times.SON,
        "ann": cdutil.times.ANNUALCYCLE,
        "year": cdutil.times.YEAR,
    }

    print("BEFORE RPOCESEED:", A.results_dir)
    #   print("A VAR:", A.variable)
    #   print("A REF:", A.reference)
    results_dir = A.process_templated_argument("results_dir")
    print("RESDIR:", results_dir.template)
    A.results_dir = results_dir()

    print("HERE?", os.path.join(A.modpath, A.filename_template))
    print("A.variable", A.variable, A.model)
    filename_in = A.process_templated_argument(
        os.path.join(A.modpath, A.filename_template))

    if A.verbose:
        print("filename in after templating:", filename_in())
    filename = glob.glob(filename_in())[0]

    if not os.path.exists(filename):
        raise RuntimeError("file '{}' doe not exits".format(filename))

    filein = cdms2.open(filename)

    fvars = list(filein.variables.keys())
    v = A.variable
    if v not in fvars:
        raise RuntimeError("Variable '%s' is not contained in input file(s)" %
                           v)
    V = filein[v]
    tim = V.getTime().clone()
    # "monthly"
    if A.bounds:
        cdutil.times.setTimeBoundsMonthly(tim)
    # Now make sure we can get the requested period
    if A.start is None:
        i0 = 0
    else:  # Ok user specified a start time
        if A.index == "index":  # index-based slicing
            if int(A.start) >= len(tim):
                raise RuntimeError(
                    "For variable %s you requested start time to be at index: %i but the file only has %i time steps"
                    % (v, int(A.start), len(tim)))
            i0 = int(A.start)
        elif A.index == "value":  # actual value used for slicing
            v0 = float(A.start)
            try:
                i0, tmp = tim.mapInterval((v0, v0), "cob")
            except Exception:
                raise RuntimeError(
                    "Could not find value %s for start time for variable %s" %
                    (A.start, v))
        elif A.index == "date":
            v0 = A.start
            # When too close from bounds it messes it up, adding a minute seems to help
            v0 = cdtime.s2c(A.start)
            v0 = v0.add(1, cdtime.Minute)
            try:
                i0, tmp = tim.mapInterval((v0, v0), "cob")
            except Exception:
                raise RuntimeError(
                    "Could not find start time %s for variable: %s" %
                    (A.start, v))

    if A.end is None:
        i1 = None
    else:  # Ok user specified a end time
        if A.index == "index":  # index-based slicing
            if int(A.end) >= len(tim):
                raise RuntimeError(
                    "For variable %s you requested end time to be at index: %i but the file only has %i time steps"
                    % (v, int(A.end), len(tim)))
            i1 = int(A.end)
        elif A.index == "value":  # actual value used for slicing
            v0 = float(A.end)
            try:
                tmp, i1 = tim.mapInterval((v0, v0), "cob")
            except Exception:
                raise RuntimeError(
                    "Could not find value %s for end time for variable %s" %
                    (A.end, v))
        elif A.index == "date":
            v0 = A.end
            # When too close from bounds it messes it up, adding a minute seems to help
            v0 = cdtime.s2c(A.end)
            v0 = v0.add(1, cdtime.Minute)
            try:
                tmp, i1 = tim.mapInterval((v0, v0), "cob")
            except Exception:
                raise RuntimeError(
                    "Could not find end time %s for variable: %s" % (A.end, v))
    # Read in data
    data = V(time=slice(i0, i1))
    if A.verbose:
        print(
            "DATA:",
            data.shape,
            data.getTime().asComponentTime()[0],
            data.getTime().asComponentTime()[-1],
        )
    if A.bounds:
        cdutil.times.setTimeBoundsMonthly(data)
    # Now we can actually read and compute the climo
    seasons = [s.lower() for s in A.seasons]
    if "all" in seasons:
        seasons = ["djf", "mam", "jja", "son", "year", "ann"]

    for season in seasons:
        s = season_function[season].climatology(
            data, criteriaarg=[A.threshold, None])
        g = season_function[season].get(data, criteriaarg=[A.threshold, None])
        # Ok we know we have monthly data
        # We want to tweak bounds
        T = data.getTime()
        Tg = g.getTime()
        istart = 0
        while numpy.ma.allequal(g[istart].mask, True):
            istart += 1
        iend = -1
        while numpy.ma.allequal(g[iend].mask, True):
            iend -= 1
        if iend == -1:
            iend = None
        else:
            iend += 1
        if iend is None:
            iend = len(Tg)
        Tg = Tg.subAxis(istart, iend)

        cal = T.getCalendar()
        cal_name = getCalendarName(cal)
        Tunits = T.units
        bnds = T.getBounds()
        tc = T.asComponentTime()

        if A.verbose:
            print("TG:", Tg.asComponentTime()[0])
            print("START END THRESHOLD:", istart, iend, A.threshold, len(Tg))
            # print "SEASON:", season, "ORIGINAL:", T.asComponentTime()
        b1 = cdtime.reltime(Tg.getBounds()[0][0], Tg.units)
        b2 = cdtime.reltime(Tg.getBounds()[-1][1], Tg.units)

        # First and last time points
        y1 = cdtime.reltime(Tg[0], T.units)
        y2 = cdtime.reltime(Tg[-1], T.units)

        # Mid year is:
        yr = (y2.value + y1.value) / 2.0
        y = cdtime.reltime(yr, T.units).tocomp(cal).year

        if A.verbose:
            print(
                "We found data from ",
                y1.tocomp(cal),
                "to",
                y2.tocomp(cal),
                "MID YEAR:",
                y,
            )
            print("bounds:", b1.tocomp(cal), b2.tocomp(cal))

        values = []
        bounds = []

        # Loop thru clim month and set value and bounds appropriately
        ts = s.getTime().asComponentTime()
        for ii in range(s.shape[0]):
            t = ts[ii]
            t.year = y
            values.append(t.torel(Tunits, cal).value)
            if s.shape[0] > 1:
                B1 = b1.tocomp(cal).add(ii, cdtime.Month)
                B2 = b2.tocomp(cal).add(ii - s.shape[0] + 1, cdtime.Month)
            else:
                B1 = b1
                B2 = b2
            # b2.year = y
            # b1.year = y
            #  if b1.cmp(b2) > 0:  # ooops
            #    if b1.month>b2.month and b1.month-b2.month!=11:
            #        b1.year -= 1
            #    else:
            #        b2.year += 1
            #  if b1.month == b2.month:
            #    b2.year = b1.year+1
            if A.verbose:
                print(B1.tocomp(cal), "<", t, "<", B2.tocomp(cal))
            bounds.append(
                [B1.torel(Tunits, cal).value,
                 B2.torel(Tunits, cal).value])

    fnmout = genutil.StringConstructor(A.output_filename_template)

    if "model_id" in fnmout.keys():
        model_id = checkCMORAttribute("model_id")
    if "experiment_id" in fnmout.keys():
        experiment_id = checkCMORAttribute("experiment_id")
    if "realization" in fnmout.keys():
        realization = checkCMORAttribute("realization")
    if "initialization_method" in fnmout.keys():
        initialization = checkCMORAttribute("initialization_method")
    if "physics_version" in fnmout.keys():
        physics_version = checkCMORAttribute("physics_version")
    if A.cmor and hasCMOR:
        dump_cmor(A, s, values, bounds, season)
    else:
        if A.cmor and not hasCMOR:
            print(
                "Your Python does not have CMOR, using regular cdms to write out files"
            )
        if not os.path.exists(A.results_dir):
            os.makedirs(A.results_dir)
        end_tc = tc[-1].add(1, cdtime.Month)

        # Populate fout template with values
        start = "{}{:02d}".format(tc[0].year, tc[0].month)
        end = "{}{:02d}".format(end_tc.year, end_tc.month)
        for k in fnmout.keys():
            try:
                setattr(fnmout, k, getattr(A, k))
            except Exception:
                pass
            # overwrite with locals
            try:
                setattr(fnmout, k, locals()[k])
            except Exception:
                pass
        nm = os.path.join(A.results_dir, fnmout())
        f = cdms2.open(nm, "w")
        # Global attributes copied
        for att, value in store_globals(filein).items():
            setattr(f, att, value)
        t = cdms2.createAxis(values)
        t.setBounds(numpy.array(bounds))
        t.designateTime()
        t.id = "time"
        s.setAxis(0, t)
        # copy orignal attributes
        for att, value in store_attributes(V).items():
            try:
                setattr(s, att, value)
            except Exception:
                pass
        f.write(s, dtype=data.dtype)
        f.close()
        if A.verbose:
            print("Results out to:", nm)
예제 #14
0
             break
     except Exception,err:
         pass
 if t is None:
     raise Exception,"Could not find a time axis for any of the following variables: %s" % self.origin.variables
 if self.debug:
     print "V:",v
 bout = None
 forced=True
 if branch is None:
     forced=False
     b = float(self.spawn.branch_time)
     if self.debug:
         print b
 elif type == 'component':
     b = cdtime.s2c(branch)
 elif type=='index':
     bout = int(branch)
     if bout!=float(branch):
         raise RuntimeError,"Index must be int, you passed %s\nDid you mean to pass the type as 'value'" % branch
     if len(t)<=bout:
         raise RuntimeError,"Your start index (%i) is greater than the length of the origin (%i)" % (bout,len(t))
 else:
     b=float(branch)
 if self.debug:
     print "b is",b,self.spawn
 if bout is None: # need to convert value to index
     try:
         bout,e = t.mapInterval((b,b,'ccb'))
         if self.debug:
             print "bout,e:",bout,e
예제 #15
0
    def load(self, restart_file=None, iterindex=None, nowtime=None):
        """Load the current instance from a netcdf file

        :Params:

            - **restart_file**, optional: Netcdf restart file.
            - **iterindex**, optional: If given, the restart file is not loaded if
              ``iterindex`` is greater or equal to the file's ``iterindex`` attribute.
            - **nowtime**, optional: If given, the restart file is not loaded if
              ``nowtime`` is greater or equal to the file's ``lasttime`` attribute.
        """

        # File
        if restart_file is None:
            restart_file = self.restart_file
        if restart_file is None:
            restart_file = self.default_restart_file
        self.restart_file = restart_file
        f = cdms2.open(restart_file)

        # Config
        # - check status
        if iterindex is not None:
            self.iterindex = iterindex
        if hasattr(self, 'iterindex') and f.iterindex<=self.iterindex:
            return -1
        if nowtime is not None:
            self.lasttime = comptime(nowtime)
        if (hasattr(self, 'lasttime') and f.withtime>0 and self.lasttime
                and reltime(f.lasttime, 'hours since 2000').value <=
                reltime(self.lasttime, 'hours since 2000').value):
            return -1
        # - what was initially asked and some more
        for sname in self.all_stats + ('sum', 'sqr', 'prod', 'stats'):
            for st in 'st':
                if not hasattr(f, st+sname): continue
                value = getattr(f, st+sname)
                setattr(self, st+sname, bool(value))
        # - current status
        self.iterindex = int(f.iterindex)
        self.nitems = int(f.nitems)
        if f.withtime==-1:
           self.withtime  = None
        else:
            self.withtime  = bool(f.withtime)
        if f.withtime:
            self.lasttime = cdtime.s2c(f.lasttime)
        if N.isscalar(f.bin_edges):
            self.bins = None
        else:
            self.bins = N.asarray(f.bin_edges)
            self.nbins = self.bins.shape[0]-1
            self._baxis = f.getAxis('hbin').clone()
        if self.nitems==0: # Still no data
            f.close()
            return 0
        # - already had some data
        self.dual = bool(f.dual)
        self.ns = int(f.ns)
        self.nt = int(f.nt)
        self._nts = f.nts.tolist()
        self.tstats = bool(f.tstats)
        self.sstats = bool(f.sstats)
        if not self.withtime:
            self._stimes = None

        # Spatial statistics
        if self.sstats:

            # Time axes
            if self.withtime:
                self._stimes = tuple([[] for i in xrange(self.nitems)])
                for i, tt in enumerate(self._stimes):
                    taxis = f.getAxis('t'+str(i))
                    tvalues = self._aslist_(taxis[:])
                    oldid = taxis.stataccum_oldid
                    for tvals in tvalues:
                        tx = create_time(tvals, taxis.units, id=oldid)
                        cp_atts(taxis, tx, id=False, exclude=[oldid])
                        self._stimes[i].append(tx)

            # Count
            self._scount = self._load_array_(f, id='scount')

            # Other stats
            self._sstats = {}
            for key in self.single_stats:
                if not self.dual: # single var
                    vid = 's' + key
                    if vid not in f.variables:
                        continue
                    self._sstats[key] = self._load_array_(f, vid),
                else: # two vars
                    for i in xrange(self.nitems):
                        vid = 's%s%s'%(key, str(i))
                        if vid not in f.variables:
                            break
                        self._sstats.setdefault(key, ())
                        self._sstats[key] += self._load_array_(f, vid),
            for key in self.dual_stats:
                vid = 's%s'%key
                if vid in f.variables:
                    self._sstats[key] = self._load_array_(f, vid)

        # Temporal statistics
        if self.tstats:

            # Count
            self._tcount = self._load_array_(f, 'tcount')

            # Other stats
            for key in self._dual_accums+self._single_accums:
                tid = 't'+key
                if not getattr(self, tid): continue
                if key in self._dual_accums:
                    value = self._load_array_(f, tid)
                    setattr(self, '_'+tid, value)
                else:
                    value = ()
                    for i in xrange(self.nitems):
                        value += self._load_array_(f, tid+str(i)),
                    setattr(self, '_'+tid, value)

            # Templates
            # - base arrays
            self._tbase = N.zeros(self.ns)
            if self.thist:
                self._thbase = N.zeros((self.nbins, self.ns), 'l')
            # - cdat templates
            self._ttemplates = ()
            self._thtemplates = None
            if self.thist:
                self._thtemplates = ()
            for i in xrange(self.nitems):
                prefix = 'var%i_'%i
                for vname in f.variables:
                    if vname.startswith(prefix) and vname != prefix+'atts': break
                ttpl = f(vname)
                _rm_id_prefix_(ttpl, 'var%i_'%i, exc=self._baxis)
                self._ttemplates += ttpl,
                if self.thist:
                    self._thtemplates += self._template_t2ht_(ttpl),

        # Attributes
        self._atts = ()
        for ivar in xrange(self.nitems):
            attrs = f['var%i_atts'%ivar].attributes.copy()
            attrs['id'] = attrs['stataccum_id']
            del attrs['stataccum_id']
            self._atts += attrs,

        f.close()
        return self.iterindex
예제 #16
0
파일: stats.py 프로젝트: jfleroux/vacumm
    def load(self, restart_file=None, iterindex=None, nowtime=None):
        """Load the current instance from a netcdf file

        :Params:

            - **restart_file**, optional: Netcdf restart file.
            - **iterindex**, optional: If given, the restart file is not loaded if
              ``iterindex`` is greater or equal to the file's ``iterindex`` attribute.
            - **nowtime**, optional: If given, the restart file is not loaded if
              ``nowtime`` is greater or equal to the file's ``lasttime`` attribute.
        """

        # File
        if restart_file is None:
            restart_file = self.restart_file
        if restart_file is None:
            restart_file = self.default_restart_file
        self.restart_file = restart_file
        f = cdms2.open(restart_file)

        # Config
        # - check status
        if iterindex is not None:
            self.iterindex = iterindex
        if hasattr(self, 'iterindex') and f.iterindex<=self.iterindex:
            return -1
        if nowtime is not None:
            self.lasttime = comptime(nowtime)
        if (hasattr(self, 'lasttime') and f.withtime>0 and self.lasttime
                and comptime(f.lasttime)<=comptime(self.lasttime)):
            return -1
        # - what was initially asked and some more
        for sname in self.all_stats + ('sum', 'sqr', 'prod', 'stats'):
            for st in 'st':
                if not hasattr(f, st+sname): continue
                value = getattr(f, st+sname)
                setattr(self, st+sname, bool(value))
        # - current status
        self.iterindex = int(f.iterindex)
        self.nitems = int(f.nitems)
        if f.withtime==-1:
           self.withtime  = None
        else:
            self.withtime  = bool(f.withtime)
        if f.withtime:
            self.lasttime = cdtime.s2c(f.lasttime)
        if N.isscalar(f.bin_edges):
            self.bins = None
        else:
            self.bins = N.asarray(f.bin_edges)
            self.nbins = self.bins.shape[0]-1
            self._baxis = f.getAxis('hbin').clone()
        if self.nitems==0: # Still no data
            f.close()
            return 0
        # - already had some data
        self.dual = bool(f.dual)
        self.ns = int(f.ns)
        self.nt = int(f.nt)
        self._nts = f.nts.tolist()
        self.tstats = bool(f.tstats)
        self.sstats = bool(f.sstats)
        if not self.withtime:
            self._stimes = None

        # Spatial statistics
        if self.sstats:

            # Time axes
            if self.withtime:
                self._stimes = tuple([[] for i in xrange(self.nitems)])
                for i, tt in enumerate(self._stimes):
                    taxis = f.getAxis('t'+str(i))
                    tvalues = self._aslist_(taxis[:])
                    oldid = taxis.stataccum_oldid
                    for tvals in tvalues:
                        tx = create_time(tvals, taxis.units, id=oldid)
                        cp_atts(taxis, tx, id=False, exclude=[oldid])
                        self._stimes[i].append(tx)

            # Count
            self._scount = self._load_array_(f, id='scount')

            # Other stats
            self._sstats = {}
            for key in self.single_stats:
                self._sstats[key] = ()
                for i in xrange(self.nitems):
                    self._sstats[key] += self._load_array_(f, 's%s%s'%(key, str(i))),
            for key in self.dual_stats:
                 self._sstats[key] = self._load_array_(f, 's%s'%key)

        # Temporal statistics
        if self.tstats:

            # Count
            self._tcount = self._load_array_(f, 'tcount')

            # Other stats
            for key in self._dual_accums+self._single_accums:
                tid = 't'+key
                if not getattr(self, tid): continue
                if key in self._dual_accums:
                    value = self._load_array_(f, tid)
                    setattr(self, '_'+tid, value)
                else:
                    value = ()
                    for i in xrange(self.nitems):
                        value += self._load_array_(f, tid+str(i)),
                    setattr(self, '_'+tid, value)

            # Templates
            # - base arrays
            self._tbase = N.zeros(self.ns)
            if self.thist:
                self._thbase = N.zeros((self.nbins, self.ns), 'l')
            # - cdat templates
            self._ttemplates = ()
            if self.thist:
                self._thtemplates = ()
            for i in xrange(self.nitems):
                prefix = 'var%i_'%i
                for vname in f.variables:
                    if vname.startswith(prefix): break
                ttpl = f(vname)
                _rm_id_prefix_(ttpl, 'var%i_'%i, exc=self._baxis)
                self._ttemplates += ttpl,
                if self.thist:
                    self._thtemplates += self._template_t2ht_(ttpl),

        # Attributes
        self._atts = ()
        for ivar in xrange(self.nitems):
            attrs = f['var%i_atts'%ivar].attributes.copy()
            attrs['id'] = attrs['stataccum_id']
            del attrs['stataccum_id']
            self._atts += attrs,

        f.close()
        return self.iterindex
예제 #17
0
import cdtime
ctime = cdtime.comptime(2000, 1, 1, 6, 23)              # mettre moins d'arguments
print ctime.hour                                        # verifier les autres composantes
print cdtime.comptime(2000) > cdtime.comptime(1999)
print cdtime.comptime(2000).cmp(cdtime.comptime(1999))
print ctime.add(1, cdtime.Hour).hour                    # essayez 70s

# - temps relatif
rtime = cdtime.reltime(23, 'minutes since 2000-1-1 6')
print rtime.value, rtime.units
print rtime.torel('hours since 2000').value

# - conversions
print rtime.tocomp()
print ctime.torel('months since 2000')                  # essayez les "weeks'
print cdtime.s2c('2000-10').month                       # testez cdtime.s2r, cdtime.r2r...





# Le module cdutil : utilitaires orientes climat

# - contenu
import cdutil
print dir(cdutil)

# - chargement des données (vent Pacifique central sur plusieurs années)
from vcmq import *
f = cdms2.open(data_sample('uv_pacific.nc'))
u = f('uwnd')
예제 #18
0
    def comp2timestr(self, comptime, returnHour='y'):
        """
        :func:`comp2timestr`: To convert date from cdtime.comptime into
                                'yyyymmdd' formate or 'yyyymmddhh' formate
                                as string

        Condition :   passing date must be comptime formate

        Inputs :    date in comptime.
                    returnHour takes 'y' or 'yes' or 'n' or 'no'.
                    Default it takes 'y'.

        Outputs : It should return the date in 'yyyymmddhh' string formate, if
                  returnHour passed 'y' or 'yes'.
                  It should return the date in 'yyyymmdd' string formate, if
                  returnHour passed 'n' or 'no'.

        Usage :
          example1 :
             >>> compobj = cdtime.comptime(2010,4,29) -> 2010-4-29 0:0:0.0
             >>> comp2timestr(compobj)
             >>> '2010042900'
           .. note:: It should return in yyyymmddhh string formate by default.
                     Hour is 00.

          example2 :
             >>> compobj = cdtime.comptime(2010,4,29,10) -> 2010-4-29 10:0:0.0
             >>> comp2timestr(compobj)
             >>> '2010042910'
           .. note:: It should return in yyyymmddhh string formate by default.
                     Hour is 10.

          example2 :
             >>> compobj = cdtime.comptime(2010,4,29,10) -> 2010-4-29 10:0:0.0
             >>> comp2timestr(compobj, returnHour = 'n')
             >>> '20100429'
           .. note:: It should return in yyyymmdd string formate only even
                     though hour passed in the component object. Because we
                     passed returnHour as 'n'.

        Written by : Arulalan.T

        Date : 29.04.2011
        Updated : 21.08.2011

        """

        if str(type(comptime)) == "<type 'comptime'>":
            # convert comptime into yyyymmdd
            yyyymmdd = str(int(comptime.absvalue))
        if isinstance(comptime, str):
            # if comptime is string
            if self.comppattern.match(comptime):
                comptime = cdtime.s2c(comptime)
                yyyymmdd = str(int(comptime.absvalue))
            else:
                raise _TimeUtilityTypeError("passed date is not \
                              cdtime.comptime object or its string formate")
        if returnHour in ['y', 'yes']:
            hh = str(comptime.hour)
            if len(hh) == 1:
                # make h as hh formate
                hh = '0' + hh
            return yyyymmdd + hh
        elif returnHour in ['n', 'no']:
            return yyyymmdd
        else:
            raise _TimeUtilityTypeError("returnHour takes either 'y/yes' \
                                                    or 'n/no' option only")
예제 #19
0
파일: r_sst_nar.py 프로젝트: acoat/vacumm
def rappatrie(WORKDIR, DIR_SST_NAR, andeb, mdeb, jdeb, hdeb, anfin, mfin, jfin,
              hfin, ZONE_NAR):
    import os, subprocess
    from cdtime import s2c
    import cdtime
    #import cdtime
    os.chdir(WORKDIR)  # on se place dans le repertoire de travail
    #----------------------------------------------------
    print ''
    print '---------- RECUPERATION FICHIERS NAR ----------'
    print ''
    #----------------------------------------------------

    #-------------------------------------------------------------
    #------- recuperation des donnees : generalites (site ftp, etc)
    URL_CERSAT = 'ftp.ifremer.fr/pub/ifremer/cersat'
    URL_NAR_DATA = os.path.join(URL_CERSAT, 'SAFOSI/Products/NARSST/netcdf')
    URL_NAR_GRID = os.path.join(URL_CERSAT,
                                'SAFOSI/Products/NARSST/netcdf/grids')
    gridfile = "grid_%(ZONE_NAR)s.nc" % vars()
    COMPRESS_NAR = 'gz'
    EXT_NAR = "00.nc.%(COMPRESS_NAR)s" % vars()
    HEURE_NAR = ['02', '10', '12', '20']
    NAR_DAYNIGHT = 'NIGHT'  # NIGHT, DAY ou ALL

    os.chdir(DIR_SST_NAR)
    #-- recuperation de la grille NAR
    #----------------------------------------------------
    if os.path.isfile(gridfile) == False:
        url_dir = "ftp://%(URL_NAR_GRID)s" % vars()
        print "Downloading NAR %(ZONE_NAR)s grid FILE" % vars()
        file_in = "%(gridfile)s.gz" % vars()
        remote_file = "%(url_dir)s/%(gridfile)s.gz" % vars()
        subprocess.call(["wget", "-O", file_in, remote_file])
        subprocess.call(["gunzip", "-f", file_in])
    else:
        print "Grille NAR deja presente dans le repertoire %(DIR_SST_NAR)s." % vars(
        )

    #-- recuperation des donnees par FTP anonymous
    #----------------------------------------------------

    # Conversion en "Component Time"
    ctdeb = s2c("%(andeb)s-%(mdeb)s-%(jdeb)s %(hdeb)s" % vars())
    ctfin = s2c("%(anfin)s-%(mfin)s-%(jfin)s %(hfin)s" % vars())

    ctest = ctdeb

    # prevoir un test pour les cas ou le fichier de donnees n'existe pas !!!
    while ctest <= ctfin:
        print ctest
        if (NAR_DAYNIGHT == 'NIGHT') or (NAR_DAYNIGHT == 'ALL'):
            print '--> donnees de nuit'
            H1 = HEURE_NAR[0]
            H2 = HEURE_NAR[3]
            recup_ftp_sst_nar(URL_NAR_DATA, ZONE_NAR, ctest.year, ctest.month,
                              ctest.day, H1, EXT_NAR, DIR_SST_NAR, '9')
            recup_ftp_sst_nar(URL_NAR_DATA, ZONE_NAR, ctest.year, ctest.month,
                              ctest.day, H2, EXT_NAR, DIR_SST_NAR, '9')

        if (NAR_DAYNIGHT == 'DAY') or (NAR_DAYNIGHT == 'ALL'):
            print '--> donnees de jour'
            H1 = HEURE_NAR[1]
            H2 = HEURE_NAR[2]
            recup_ftp_sst_nar(URL_NAR_DATA, ZONE_NAR, ctest.year, ctest.month,
                              ctest.day, H1, EXT_NAR, DIR_SST_NAR, '9')
            recup_ftp_sst_nar(URL_NAR_DATA, ZONE_NAR, ctest.year, ctest.month,
                              ctest.day, H2, EXT_NAR, DIR_SST_NAR, '9')

        # On incremente le temps "test" de 1 jour
        ctest = ctest.add(1, cdtime.Days)

    os.chdir(WORKDIR)
예제 #20
0
    def comp2timestr(self, comptime, returnHour='y'):
        """
        :func:`comp2timestr`: To convert date from cdtime.comptime into
                                'yyyymmdd' formate or 'yyyymmddhh' formate
                                as string

        Condition :   passing date must be comptime formate

        Inputs :    date in comptime.
                    returnHour takes 'y' or 'yes' or 'n' or 'no'.
                    Default it takes 'y'.

        Outputs : It should return the date in 'yyyymmddhh' string formate, if
                  returnHour passed 'y' or 'yes'.
                  It should return the date in 'yyyymmdd' string formate, if
                  returnHour passed 'n' or 'no'.

        Usage :
          example1 :
             >>> compobj = cdtime.comptime(2010,4,29) -> 2010-4-29 0:0:0.0
             >>> comp2timestr(compobj)
             >>> '2010042900'
           .. note:: It should return in yyyymmddhh string formate by default.
                     Hour is 00.

          example2 :
             >>> compobj = cdtime.comptime(2010,4,29,10) -> 2010-4-29 10:0:0.0
             >>> comp2timestr(compobj)
             >>> '2010042910'
           .. note:: It should return in yyyymmddhh string formate by default.
                     Hour is 10.

          example2 :
             >>> compobj = cdtime.comptime(2010,4,29,10) -> 2010-4-29 10:0:0.0
             >>> comp2timestr(compobj, returnHour = 'n')
             >>> '20100429'
           .. note:: It should return in yyyymmdd string formate only even
                     though hour passed in the component object. Because we
                     passed returnHour as 'n'.

        Written by : Arulalan.T

        Date : 29.04.2011
        Updated : 21.08.2011

        """

        if str(type(comptime)) == "<type 'comptime'>":
            # convert comptime into yyyymmdd
            yyyymmdd = str(int(comptime.absvalue))
        if isinstance(comptime, str):
            # if comptime is string
            if self.comppattern.match(comptime):
                comptime = cdtime.s2c(comptime)
                yyyymmdd = str(int(comptime.absvalue))
            else:
                raise _TimeUtilityTypeError("passed date is not \
                              cdtime.comptime object or its string formate")
        if returnHour in ['y', 'yes']:
            hh = str(comptime.hour)
            if len(hh) == 1:
                # make h as hh formate
                hh = '0' + hh
            return yyyymmdd + hh
        elif returnHour in ['n', 'no']:
            return yyyymmdd
        else:
            raise _TimeUtilityTypeError("returnHour takes either 'y/yes' \
                                                    or 'n/no' option only")