1  """Time Series Functions 
 2   
 3   
 4  """ 
 5   
 6  from GP_Util import * 
 7   
 8 -def Import(study,varname,infile="",lag="",invalues=""): 
  9          """ Import a time series variable 
10      A time series variable is one that has only a temporal component and no spatial component. All points in the study will get the same value associated with the corresponding date. The input csv file must contain three unlabeled (no header) fields in the order startdate, enddate, value where startdate and enddate describe the period over which the value applies. No interpolation occurs so a sample interval must contain the entire time series interval to be included in it. Start and end dates need to match the study sample dates if they are of similar duration (ie annual precipitation) 
11   
12      Inputs 
13          varname     name of output variable 
14          infile      csv input file (fields=[start,end,value] no header)  
15          lag         lag time (years) to sum the input time series over """ 
16   
17          vardict = { 
18                  'type'          : 'time', 
19                  'infile'        : infile, 
20                  'lag'           : lag,  
21                  'invalues'      : invalues,              
22                  } 
23                   
24          study.AddSource(varname,vardict)         
 25   
27          """ 
28          Extract a Time Series  
29                   
30                  MORE  
31                   
32          """ 
33          log.info('Extract Time Series : '+varname.ljust(30)) 
34          vardict = study.infodict[varname] 
35          infile = vardict['infile'] 
36          lag = vardict['lag'] 
37           
38          os.chdir(study.workingdir) 
39          MakeFolder(os.path.abspath(varname)) 
40          npyfile = os.path.abspath(varname+'.npy') 
41   
42          if infile != "":  
43                  pre = num.zeros(study.enddates.shape) 
44                  rawtable = num.loadtxt(infile,delimiter=',') 
45                  startdates = rawtable[:,0] 
46                  enddates = rawtable[:,1] 
47                  vals = rawtable[:,2]     
48                  for (i,(sd,ed)) in enumerate(zip(study.enddates - lag, study.enddates)): 
49                          dix = num.where(num.logical_and(startdates >= sd,enddates <= ed))[0] 
50                          pre[i] = num.sum( num.take(vals,dix) ) / float(lag) 
51   
52          else: 
53                  pre = vardict['invalues'] 
54   
55           
56          rowvect = pre.reshape(( 1, pre.shape[0] )) 
57           
58           
59          dtable = rowvect * num.ones((study.rawpointcount,1)) 
60           
61           
62          num.save(npyfile,dtable) 
63          vardict = {'name':varname,'file':npyfile,'list':[] }     
64          study.AddSeries(vardict) 
 65