docs: def get_val(dataset, timestamp=None):
      """
          Turn the string value of a dataset into an appropriate
          value, be it a decimal value, array or time series.
  
          If a timestamp is passed to this function,
          return the values appropriate to the requested times.
  
          If the timestamp is *before* the start of the timeseries data, return None
          If the timestamp is *after* the end of the timeseries data, return the last
          value.
  
          The raw flag indicates whether timeseries should be returned raw -- exactly
          as they are in the DB (a timeseries being a list of timeseries data objects,
          for example) or as a single python dictionary
  
      """
      if dataset.type == 'array':
          #TODO: design a mechansim to retrieve this data if it's stored externally
          return json.loads(dataset.value)
  
      elif dataset.type == 'descriptor':
          return str(dataset.value)
      elif dataset.type == 'scalar':
          return Decimal(str(dataset.value))
      elif dataset.type == 'timeseries':
          #TODO: design a mechansim to retrieve this data if it's stored externally
          val = dataset.value
  
          seasonal_year = config.get('DEFAULT','seasonal_year', '1678')
          seasonal_key = config.get('DEFAULT', 'seasonal_key', '9999')
          val = dataset.value.replace(seasonal_key, seasonal_year)
  
          timeseries = pd.read_json(val, convert_axes=True)
  
          if timestamp is None:
              return timeseries
          else:
              try:
                  idx = timeseries.index
                  #Seasonal timeseries are stored in the year
                  #1678 (the lowest year pandas allows for valid times).
                  #Therefore if the timeseries is seasonal,
                  #the request must be a seasonal request, not a
                  #standard request
  
                  if type(idx) == pd.DatetimeIndex:
                      if set(idx.year) == set([int(seasonal_year)]):
                          if isinstance(timestamp,  list):
                              seasonal_timestamp = []
                              for t in timestamp:
                                  t_1900 = t.replace(year=int(seasonal_year))
                                  seasonal_timestamp.append(t_1900)
                              timestamp = seasonal_timestamp
                          else:
                              timestamp = [timestamp.replace(year=int(seasonal_year))]
  
                  pandas_ts = timeseries.reindex(timestamp, method='ffill')
  
                  #If there are no values at all, just return None
                  if len(pandas_ts.dropna()) == 0:
                      return None
  
                  #Replace all numpy NAN values with None
                  pandas_ts = pandas_ts.where(pandas_ts.notnull(), None)
  
                  val_is_array = False
                  if len(pandas_ts.columns) > 1:
                      val_is_array = True
  
                  if val_is_array:
                      if type(timestamp) is list and len(timestamp) == 1:
                          ret_val = pandas_ts.loc[timestamp[0]].values.tolist()
                      else:
                          ret_val = pandas_ts.loc[timestamp].values.tolist()
                  else:
                      col_name = pandas_ts.loc[timestamp].columns[0]
                      if type(timestamp) is list and len(timestamp) == 1:
                          ret_val = pandas_ts.loc[timestamp[0]].loc[col_name]
                      else:
                          ret_val = pandas_ts.loc[timestamp][col_name].values.tolist()
  
                  return ret_val
  
              except Exception as e:
                  log.critical("Unable to retrive data. Check timestamps.")
                  log.critical(e)