Ejemplo n.º 1
0
 def date_conv(date_str): 
   # return a float, rather than an integer, to be consistent with 
   # the other data types
   return float(mjd(date_str,fmt))
Ejemplo n.º 2
0
  tg,yg = np.meshgrid(times,y,indexing='ij')
  u = 0.0*ms*tg*xg + 0.0*ms*tg*yg
  v = 0.0*ms*tg*xg + 3.0*ms*tg*yg
  z = 0.0*tg
  u = u - u[0,:]
  v = v - v[0,:]
  z = z - z[0,:]
  return u,v,z

lon = np.array([-83.3,-82.75,-85.26,-83.36])
lat = np.array([42.31,42.91,45.20,42.92])
id = np.array(['STA1','STA2','STA3','STA4'])
bm = make_basemap(lon,lat)
x,y = bm(lon,lat)
xy = np.array([x,y]).T
start_date = mjd('2000-01-01','%Y-%m-%d')
stop_date = mjd('2000-02-01','%Y-%m-%d')
times = np.arange(start_date,stop_date+1)
u,v,z = make_data(xy,times)
su = 0.001*np.ones_like(u)
sv = 0.001*np.ones_like(v)
sz = 0.001*np.ones_like(z)
u += np.random.normal(0.0,su)
v += np.random.normal(0.0,sv)
z += np.random.normal(0.0,sz)

data = {}
data['id'] = id
data['longitude'] = lon
data['latitude'] = lat
data['time'] = times
Ejemplo n.º 3
0
def pygeons_clean(input_file,
                  resolution='i',
                  input_edits_file=None,
                  break_lons=None,
                  break_lats=None,
                  break_conn=None,
                  no_display=False,
                  output_stem=None,
                  **kwargs):
    ''' 
  runs the PyGeoNS Interactive Cleaner
  
  Parameters
  ----------
    data : dict
      data dictionary

    resolution : str
      basemap resolution    
    
    input_edits_file : str
      Name of the file containing edits which will automatically be 
      applied before opening up the interactive viewer.
    
    output_edits_file : str
      Name of the file where all edits will be recorded.   
      
    **kwargs : 
      gets passed to pygeons.clean.clean
         
  Returns
  -------
    out : dict
      output data dictionary 
    
  '''
    logger.info('Running pygeons clean ...')
    data = dict_from_hdf5(input_file)
    out = dict((k, np.copy(v)) for k, v in data.iteritems())

    ts_fig, ts_ax = plt.subplots(3,
                                 1,
                                 sharex=True,
                                 num='Time Series View',
                                 facecolor='white')
    _setup_ts_ax(ts_ax)
    map_fig, map_ax = plt.subplots(num='Map View', facecolor='white')
    bm = make_basemap(data['longitude'],
                      data['latitude'],
                      resolution=resolution)
    _setup_map_ax(bm, map_ax)
    x, y = bm(data['longitude'], data['latitude'])
    pos = np.array([x, y]).T
    t = data['time']
    dates = [mjd_inv(ti, '%Y-%m-%d') for ti in t]
    units = _unit_string(data['space_exponent'], data['time_exponent'])
    conv = 1.0 / unit_conversion(units, time='day', space='m')
    u = conv * data['east']
    v = conv * data['north']
    z = conv * data['vertical']
    su = conv * data['east_std_dev']
    sv = conv * data['north_std_dev']
    sz = conv * data['vertical_std_dev']
    ic = InteractiveCleaner(t,
                            pos,
                            u=u,
                            v=v,
                            z=z,
                            su=su,
                            sv=sv,
                            sz=sz,
                            map_ax=map_ax,
                            ts_ax=ts_ax,
                            time_labels=dates,
                            units=units,
                            station_labels=data['id'],
                            **kwargs)

    # make edits to the data set prior to displaying it
    if input_edits_file is not None:
        with open(input_edits_file, 'r') as fin:
            for line in fin:
                # ignore blank lines
                if line.isspace():
                    continue

                type, sta, a, b = line.strip().split()
                # set the current station in *ic* to the station for this edit
                xidx, = (data['id'] == sta).nonzero()
                if len(xidx) == 0:
                    # continue because the station does not exist in this
                    # dataset
                    continue

                ic.xidx = xidx[0]
                if type == 'outliers':
                    start_time = mjd(a, '%Y-%m-%d')
                    stop_time = mjd(b, '%Y-%m-%d')
                    ic.remove_outliers(start_time, stop_time)
                elif type == 'jump':
                    jump_time = mjd(a, '%Y-%m-%d')
                    delta = int(b)
                    ic.remove_jump(jump_time, delta)
                else:
                    raise ValueError(
                        'edit type must be either "outliers" or "jump"')

    if not no_display:
        ic.update()
        ic.connect()

    # set output file name
    if output_stem is None:
        output_stem = _remove_extension(input_file) + '.clean'

    output_file = output_stem + '.h5'
    output_edits_file = output_stem + '.txt'

    with open(output_edits_file, 'w') as fout:
        for i in ic.log:
            type, xidx, a, b = i
            if type == 'outliers':
                station = data['id'][xidx]
                start_date = mjd_inv(a, '%Y-%m-%d')
                stop_date = mjd_inv(b, '%Y-%m-%d')
                fout.write('outliers %s %s %s\n' %
                           (station, start_date, stop_date))
            elif type == 'jump':
                station = data['id'][xidx]
                jump_date = mjd_inv(a, '%Y-%m-%d')
                fout.write('jump     %s %s %s\n' % (station, jump_date, b))
            else:
                raise ValueError(
                    'edit type must be either "outliers" or "jump"')

    logger.info('Edits saved to %s' % output_edits_file)
    clean_data = ic.get_data()
    out['east'] = clean_data[0] / conv
    out['north'] = clean_data[1] / conv
    out['vertical'] = clean_data[2] / conv
    out['east_std_dev'] = clean_data[3] / conv
    out['north_std_dev'] = clean_data[4] / conv
    out['vertical_std_dev'] = clean_data[5] / conv

    hdf5_from_dict(output_file, out)
    logger.info('Cleaned data written to %s' % output_file)
    logger.info('Edits written to %s' % output_edits_file)
    return
Ejemplo n.º 4
0
 def date_conv(date_str): 
   return float(mjd(date_str,fmt))
Ejemplo n.º 5
0
 def date_conv(date_str):
     return float(mjd(date_str, fmt))
Ejemplo n.º 6
0
 def date_conv(date_str):
     # return a float, rather than an integer, to be consistent with
     # the other data types
     return float(mjd(date_str, fmt))
Ejemplo n.º 7
0
def pygeons_crop(input_file,
                 start_date=None,
                 stop_date=None,
                 min_lat=-np.inf,
                 max_lat=np.inf,
                 min_lon=-np.inf,
                 max_lon=np.inf,
                 stations=None,
                 output_stem=None):
    ''' 
  Sets the time span of the data set to be between *start_date* and
  *stop_date*. Sets the stations to be within the latitude and
  longitude bounds. 
  
  Parameters
  ----------
  data : dict
    data dictionary
      
  start_date : str, optional
    start date of output data set in YYYY-MM-DD. Uses the start date 
    of *data* if not provided. Defaults to the earliest date.

  stop_date : str, optional
    Stop date of output data set in YYYY-MM-DD. Uses the stop date 
    of *data* if not provided. Defaults to the latest date.
      
  min_lon, max_lon, min_lat, max_lat : float, optional
    Spatial bounds on the output data set
  
  stations : str list, optional
    List of stations to be removed from the dataset. This is in 
    addition to the station removed by the lon/lat bounds.
    
  Returns
  -------
  out_dict : dict
    output data dictionary

  '''
    logger.info('Running pygeons crop ...')
    data = dict_from_hdf5(input_file)
    out = dict((k, np.copy(v)) for k, v in data.iteritems())

    if start_date is None:
        start_date = mjd.mjd_inv(data['time'].min(), '%Y-%m-%d')

    if stop_date is None:
        stop_date = mjd.mjd_inv(data['time'].max(), '%Y-%m-%d')

    if stations is None:
        stations = []

    # remove times that are not within the bounds of *start_date* and
    # *stop_date*
    start_time = int(mjd.mjd(start_date, '%Y-%m-%d'))
    stop_time = int(mjd.mjd(stop_date, '%Y-%m-%d'))
    idx = ((data['time'] >= start_time) & (data['time'] <= stop_time))
    out['time'] = out['time'][idx]
    for dir in ['east', 'north', 'vertical']:
        out[dir] = out[dir][idx, :]
        out[dir + '_std_dev'] = out[dir + '_std_dev'][idx, :]

    # find stations that are within the bounds
    in_bounds = ((data['longitude'] > min_lon) & (data['longitude'] < max_lon)
                 & (data['latitude'] > min_lat) & (data['latitude'] < max_lat))
    # find stations that are in the list of stations to be removed
    in_list = np.array([i in stations for i in data['id']])
    # keep stations that are in bounds and not in the list
    idx, = (in_bounds & ~in_list).nonzero()

    out['id'] = out['id'][idx]
    out['longitude'] = out['longitude'][idx]
    out['latitude'] = out['latitude'][idx]
    for dir in ['east', 'north', 'vertical']:
        out[dir] = out[dir][:, idx]
        out[dir + '_std_dev'] = out[dir + '_std_dev'][:, idx]

    # set output file name
    if output_stem is None:
        output_stem = _remove_extension(input_file) + '.crop'

    output_file = output_stem + '.h5'
    hdf5_from_dict(output_file, out)
    logger.info('Cropped data written to %s' % output_file)
    return
Ejemplo n.º 8
0
                    [-83.33,41.94,0.0]])  
Nx = len(pos_geo)
bm = make_basemap(pos_geo[:,0],pos_geo[:,1])
pos_cart = np.array(bm(pos_geo[:,0],pos_geo[:,1])).T
dx = pos_cart[:,0] - pos_cart[0,0]
dy = pos_cart[:,1] - pos_cart[0,1]

dispdx = np.array([[0.0,1e-6,0.0]]).repeat(Nx,axis=0)
dispdy = np.array([[0.0,0.0,0.0]]).repeat(Nx,axis=0)
disp = dispdx*dx[:,None] + dispdy*dy[:,None]
u,v,z = disp.T
dudx,dvdx,dzdx = dispdx.T
dudy,dvdy,dzdy = dispdy.T

# make disp. time dependent
start_time = mjd('2015-07-01','%Y-%m-%d')
stop_time = mjd('2017-07-01','%Y-%m-%d')
peak_time = float(mjd('2016-07-01','%Y-%m-%d'))
times = np.arange(start_time,stop_time+1).astype(float)
Nt = len(times)
# slip rate (m/day) through time
b = 0.005/(((times-peak_time)/10.0)**2 + 1.0)  
# slip (m) through time
intb = np.cumsum(b)

# create deformation rate gradients
#ddudx
# create displacements
u = u[None,:]*intb[:,None]
v = v[None,:]*intb[:,None]
z = z[None,:]*intb[:,None]
Ejemplo n.º 9
0
def pygeons_strain(input_file,
                   network_prior_model=('spwen12-se',),
                   network_prior_params=(1.0,0.1,100.0),
                   network_noise_model=(),
                   network_noise_params=(),
                   station_noise_model=('linear',),
                   station_noise_params=(),
                   start_date=None,stop_date=None,
                   positions=None,positions_file=None,
                   rate=True,vertical=True,covariance=False,
                   output_stem=None):
  ''' 
  calculates strain
  '''
  logger.info('Running pygeons strain ...')
  data = dict_from_hdf5(input_file)
  if data['time_exponent'] != 0:
    raise ValueError('input dataset must have units of displacement')

  if data['space_exponent'] != 1:
    raise ValueError('input dataset must have units of displacement')
    
  out_dx = dict((k,np.copy(v)) for k,v in data.iteritems())
  out_dy = dict((k,np.copy(v)) for k,v in data.iteritems())

  # convert params to a dictionary of hyperparameters for each direction
  network_prior_params = _params_dict(network_prior_params)
  network_noise_params = _params_dict(network_noise_params)
  station_noise_params = _params_dict(station_noise_params)
  
  # convert geodetic input positions to cartesian
  bm = make_basemap(data['longitude'],data['latitude'])
  x,y = bm(data['longitude'],data['latitude'])
  xy = np.array([x,y]).T

  # set output positions
  if (positions is None) & (positions_file is None):
    # no output positions were specified so return the solution at the
    # input data positions
    output_id = np.array(data['id'],copy=True)
    output_lon = np.array(data['longitude'],copy=True)
    output_lat = np.array(data['latitude'],copy=True)

  else:  
    output_id = np.zeros((0,),dtype=str)
    output_lon = np.zeros((0,),dtype=float)
    output_lat = np.zeros((0,),dtype=float)
    if positions_file is not None:
      # if positions file was specified
      pos = np.loadtxt(positions_file,dtype=str,ndmin=2)
      if pos.shape[1] != 3:
        raise ValueError(
          'positions file must contain a column for IDs, longitudes, '
          'and latitudes')
          
      output_id = np.hstack((output_id,pos[:,0]))
      output_lon = np.hstack((output_lon,pos[:,1].astype(float)))
      output_lat = np.hstack((output_lat,pos[:,2].astype(float)))
    
    if positions is not None:  
      # if positions were specified via the command line
      pos = np.array(positions,dtype=str).reshape((-1,3))
      output_id = np.hstack((output_id,pos[:,0]))
      output_lon = np.hstack((output_lon,pos[:,1].astype(float)))
      output_lat = np.hstack((output_lat,pos[:,2].astype(float)))

  # convert geodetic output positions to cartesian
  output_x,output_y = bm(output_lon,output_lat)
  output_xy = np.array([output_x,output_y]).T 
  
  # set output times
  if start_date is None:
    start_date = mjd_inv(np.min(data['time']),'%Y-%m-%d')

  if stop_date is None:
    stop_date = mjd_inv(np.max(data['time']),'%Y-%m-%d')
  
  start_time = mjd(start_date,'%Y-%m-%d')  
  stop_time = mjd(stop_date,'%Y-%m-%d')  
  output_time = np.arange(start_time,stop_time+1)
  
  # set output file names
  if output_stem is None:
    output_stem = _remove_extension(input_file) + '.strain'

  output_dx_file = output_stem + '.dudx.h5'
  output_dy_file = output_stem + '.dudy.h5'

  _log_strain(input_file,
              network_prior_model,network_prior_params, 
              network_noise_model,network_noise_params, 
              station_noise_model,station_noise_params, 
              start_date,stop_date,output_id,rate,vertical,
              covariance,output_dx_file,output_dy_file)

  for dir in ['east','north','vertical']:
    if (dir == 'vertical') & (not vertical):
      logger.debug('Not computing vertical deformation gradients')
      # do not compute the deformation gradients for vertical. Just
      # return zeros.
      dx = np.zeros((output_time.shape[0],output_xy.shape[0])) 
      sdx = np.zeros((output_time.shape[0],output_xy.shape[0])) 
      dy = np.zeros((output_time.shape[0],output_xy.shape[0])) 
      sdy = np.zeros((output_time.shape[0],output_xy.shape[0])) 
      if covariance:
        # if covariance is True then create an empty array of
        # covariances
        cdx = np.zeros((output_time.shape[0],output_xy.shape[0],
                        output_time.shape[0],output_xy.shape[0]))
        cdy = np.zeros((output_time.shape[0],output_xy.shape[0],
                        output_time.shape[0],output_xy.shape[0]))
        soln = (dx,sdx,cdx,dy,sdy,cdy)

      else:
        soln = (dx,sdx,dy,sdy)
              
    else:      
      soln = strain(t=data['time'][:,None],
                    x=xy,
                    d=data[dir],
                    sd=data[dir+'_std_dev'],
                    network_prior_model=network_prior_model,
                    network_prior_params=network_prior_params[dir],
                    network_noise_model=network_noise_model,
                    network_noise_params=network_noise_params[dir],
                    station_noise_model=station_noise_model,
                    station_noise_params=station_noise_params[dir],
                    out_t=output_time[:,None],
                    out_x=output_xy,
                    rate=rate,
                    covariance=covariance)

    if covariance:
      # soln contains six entries when covariance is True
      dx,sdx,cdx,dy,sdy,cdy = soln
      out_dx[dir] = dx
      out_dx[dir+'_std_dev'] = sdx
      out_dx[dir+'_covariance'] = cdx
      out_dy[dir] = dy
      out_dy[dir+'_std_dev'] = sdy
      out_dy[dir+'_covariance'] = cdy

    else:      
      # soln contains four entries when covariance is False
      dx,sdx,dy,sdy = soln
      out_dx[dir] = dx
      out_dx[dir+'_std_dev'] = sdx
      out_dy[dir] = dy
      out_dy[dir+'_std_dev'] = sdy

  out_dx['time'] = output_time
  out_dx['longitude'] = output_lon
  out_dx['latitude'] = output_lat
  out_dx['id'] = output_id
  out_dx['time_exponent'] = -int(rate)
  out_dx['space_exponent'] = 0
  
  out_dy['time'] = output_time
  out_dy['longitude'] = output_lon
  out_dy['latitude'] = output_lat
  out_dy['id'] = output_id
  out_dy['time_exponent'] = -int(rate)
  out_dy['space_exponent'] = 0

  hdf5_from_dict(output_dx_file,out_dx)
  hdf5_from_dict(output_dy_file,out_dy)
  if rate:
    logger.info('Posterior velocity gradients written to %s and %s' % (output_dx_file,output_dy_file))

  else:  
    logger.info('Posterior displacement gradients written to %s and %s' % (output_dx_file,output_dy_file))

  return
Ejemplo n.º 10
0
def pygeons_clean(input_file,resolution='i',
                  input_edits_file=None,
                  break_lons=None,break_lats=None,
                  break_conn=None,no_display=False,
                  output_stem=None,**kwargs):
  ''' 
  runs the PyGeoNS Interactive Cleaner
  
  Parameters
  ----------
    data : dict
      data dictionary

    resolution : str
      basemap resolution    
    
    input_edits_file : str
      Name of the file containing edits which will automatically be 
      applied before opening up the interactive viewer.
    
    output_edits_file : str
      Name of the file where all edits will be recorded.   
      
    **kwargs : 
      gets passed to pygeons.clean.clean
         
  Returns
  -------
    out : dict
      output data dictionary 
    
  '''
  logger.info('Running pygeons clean ...')
  data = dict_from_hdf5(input_file)
  out = dict((k,np.copy(v)) for k,v in data.iteritems())

  ts_fig,ts_ax = plt.subplots(3,1,sharex=True,num='Time Series View',facecolor='white')
  _setup_ts_ax(ts_ax)
  map_fig,map_ax = plt.subplots(num='Map View',facecolor='white')
  bm = make_basemap(data['longitude'],data['latitude'],resolution=resolution)
  _setup_map_ax(bm,map_ax)
  x,y = bm(data['longitude'],data['latitude'])
  pos = np.array([x,y]).T
  t = data['time']
  dates = [mjd_inv(ti,'%Y-%m-%d') for ti in t]
  units = _unit_string(data['space_exponent'],data['time_exponent'])
  conv = 1.0/unit_conversion(units,time='day',space='m')
  u = conv*data['east']
  v = conv*data['north']
  z = conv*data['vertical']
  su = conv*data['east_std_dev']
  sv = conv*data['north_std_dev']
  sz = conv*data['vertical_std_dev']
  ic = InteractiveCleaner(
         t,pos,u=u,v=v,z=z,su=su,sv=sv,sz=sz,
         map_ax=map_ax,ts_ax=ts_ax,
         time_labels=dates,
         units=units,
         station_labels=data['id'],
         **kwargs)

  # make edits to the data set prior to displaying it
  if input_edits_file is not None:
    with open(input_edits_file,'r') as fin:
      for line in fin: 
        # ignore blank lines
        if line.isspace():
          continue
          
        type,sta,a,b = line.strip().split()
        # set the current station in *ic* to the station for this edit
        xidx, = (data['id'] == sta).nonzero()
        if len(xidx) == 0:
          # continue because the station does not exist in this 
          # dataset
          continue
          
        ic.xidx = xidx[0]
        if type == 'outliers':
          start_time = mjd(a,'%Y-%m-%d')
          stop_time = mjd(b,'%Y-%m-%d')
          ic.remove_outliers(start_time,stop_time)
        elif type == 'jump':
          jump_time = mjd(a,'%Y-%m-%d')
          delta = int(b)
          ic.remove_jump(jump_time,delta)
        else:
          raise ValueError('edit type must be either "outliers" or "jump"')

  if not no_display:
    ic.update()
    ic.connect()
    
  # set output file name
  if output_stem is None:
    output_stem = _remove_extension(input_file) + '.clean'

  output_file = output_stem + '.h5'
  output_edits_file = output_stem + '.txt'
  
  with open(output_edits_file,'w') as fout:
    for i in ic.log:
      type,xidx,a,b = i
      if type == 'outliers':
        station = data['id'][xidx]
        start_date = mjd_inv(a,'%Y-%m-%d')
        stop_date = mjd_inv(b,'%Y-%m-%d')
        fout.write('outliers %s %s %s\n' % (station,start_date,stop_date))
      elif type == 'jump':
        station = data['id'][xidx]
        jump_date = mjd_inv(a,'%Y-%m-%d')
        fout.write('jump     %s %s %s\n' % (station,jump_date,b))
      else:
        raise ValueError('edit type must be either "outliers" or "jump"')
        
  logger.info('Edits saved to %s' % output_edits_file)
  clean_data  = ic.get_data()                 
  out['east'] = clean_data[0]/conv
  out['north'] = clean_data[1]/conv
  out['vertical'] = clean_data[2]/conv
  out['east_std_dev'] = clean_data[3]/conv
  out['north_std_dev'] = clean_data[4]/conv
  out['vertical_std_dev'] = clean_data[5]/conv

  hdf5_from_dict(output_file,out)  
  logger.info('Cleaned data written to %s' % output_file)
  logger.info('Edits written to %s' % output_edits_file)
  return 
Ejemplo n.º 11
0
def pygeons_crop(input_file,start_date=None,stop_date=None,
                 min_lat=-np.inf,max_lat=np.inf,
                 min_lon=-np.inf,max_lon=np.inf,
                 stations=None,output_stem=None):
  ''' 
  Sets the time span of the data set to be between *start_date* and
  *stop_date*. Sets the stations to be within the latitude and
  longitude bounds. 
  
  Parameters
  ----------
  data : dict
    data dictionary
      
  start_date : str, optional
    start date of output data set in YYYY-MM-DD. Uses the start date 
    of *data* if not provided. Defaults to the earliest date.

  stop_date : str, optional
    Stop date of output data set in YYYY-MM-DD. Uses the stop date 
    of *data* if not provided. Defaults to the latest date.
      
  min_lon, max_lon, min_lat, max_lat : float, optional
    Spatial bounds on the output data set
  
  stations : str list, optional
    List of stations to be removed from the dataset. This is in 
    addition to the station removed by the lon/lat bounds.
    
  Returns
  -------
  out_dict : dict
    output data dictionary

  '''
  logger.info('Running pygeons crop ...')
  data = dict_from_hdf5(input_file)
  out = dict((k,np.copy(v)) for k,v in data.iteritems())

  if start_date is None:
    start_date = mjd.mjd_inv(data['time'].min(),'%Y-%m-%d')

  if stop_date is None:
    stop_date = mjd.mjd_inv(data['time'].max(),'%Y-%m-%d')

  if stations is None:
    stations = []

  # remove times that are not within the bounds of *start_date* and 
  # *stop_date*
  start_time = int(mjd.mjd(start_date,'%Y-%m-%d'))
  stop_time = int(mjd.mjd(stop_date,'%Y-%m-%d'))
  idx = ((data['time'] >= start_time) &
         (data['time'] <= stop_time))
  out['time'] = out['time'][idx]
  for dir in ['east','north','vertical']:
    out[dir] = out[dir][idx,:]
    out[dir + '_std_dev'] = out[dir + '_std_dev'][idx,:]

  # find stations that are within the bounds
  in_bounds = ((data['longitude'] > min_lon) &
               (data['longitude'] < max_lon) &
               (data['latitude'] > min_lat) &
               (data['latitude'] < max_lat))
  # find stations that are in the list of stations to be removed
  in_list = np.array([i in stations for i in data['id']])
  # keep stations that are in bounds and not in the list
  idx, = (in_bounds & ~in_list).nonzero()

  out['id'] = out['id'][idx]
  out['longitude'] = out['longitude'][idx]
  out['latitude'] = out['latitude'][idx]
  for dir in ['east','north','vertical']:
    out[dir] = out[dir][:,idx]
    out[dir + '_std_dev'] = out[dir + '_std_dev'][:,idx]

  # set output file name
  if output_stem is None:
    output_stem = _remove_extension(input_file) + '.crop'

  output_file = output_stem + '.h5'
  hdf5_from_dict(output_file,out)
  logger.info('Cropped data written to %s' % output_file)
  return