Exemplo n.º 1
0
def morlighem_grid(xmin=-np.inf,
                   xmax=np.inf,
                   ymin=-np.inf,
                   ymax=np.Inf,
                   verticaldatum='geoid',
                   version=2017):
    '''
  xb,yb,bed = morlighem_grid(xmin,xmax,ymin,ymax,verticaldatum='geoid',version=2017)
  
  Export morlighem gridded bed.
  
  Inputs:
  xmin,xmax,ymin,ymax: extent of desired grid
  verticaldatum: geoid or ellipsoid
  version: 2014 or 2017
  
  Outputs:
  xb,yb: coordinates for grid
  bed: gridded bed
  '''

    # Load Bed DEM
    if float(version) == 2014:
        file = os.path.join(os.getenv("DATA_HOME"),
                            "Bed/Morlighem_2014/MCdataset-2015-04-27.tif")
        geoid = os.path.join(os.getenv("DATA_HOME"),
                             "Bed/Morlighem_2014/geoid.tif")
    elif float(version) == 2017:
        file = os.path.join(
            os.getenv("DATA_HOME"),
            "Bed/Morlighem_2017/BedMachineGreenland-2017-09-20.tif")
        geoid = os.path.join(os.getenv("DATA_HOME"),
                             "Bed/Morlighem_2017/geoid.tif")
    else:
        os.system("Unknown Morlighem version " + str(version))

    [xb, yb, zb] = geotifflib.read(file, xmin, xmax, ymin, ymax)
    zb[zb == -9999] = float('NaN')

    # Morlighem bed DEM is given as elevation above mean sea level (at geoid). So we need
    # to correct only if we want the ellipsoid height.
    if verticaldatum == "ellipsoid":
        # Load Geoid
        [xg, yg, zg] = geotifflib.read(geoid, xmin, xmax, ymin, ymax)
        bed = zb + zg
    elif verticaldatum == "geoid":
        bed = zb
    else:
        sys.exit("Unknown vertical datum, defaulting to geoid height")

    return xb, yb, bed
Exemplo n.º 2
0
def readvelocity(DIR, track, file):
    '''
  x,y,v,vx,vy,ex,ey,time,interval = readvelocity(DIR,track,file)
  '''

    # Filename
    filename = DIR + track + "/" + file
    # Get time
    if os.path.exists(filename + ".meta"):
        time, interval = readtime(filename)
        year, month, day = datelib.fracyear_to_date(time)
        date = "%04d%02d%02d" % (year, month, day)
    else:
        time = float('NaN')
        interval = float('NaN')
        date = float('NaN')

    if os.path.isfile(DIR + "TIF/" + track + "_v.tif"):
        x, y, v = geotifflib.read(DIR + "TIF/" + track + "_v.tif")
        x, y, vx = geotifflib.read(DIR + "TIF/" + track + "_vx.tif")
        x, y, vy = geotifflib.read(DIR + "TIF/" + track + "_vy.tif")
        x, y, ex = geotifflib.read(DIR + "TIF/" + track + "_ex.tif")
        x, y, ey = geotifflib.read(DIR + "TIF/" + track + "_ey.tif")
        # Check to see if there are geotiff files in the subdirectory. If not, read the binary data.
    elif os.path.isfile(DIR + "TIF/" + track + "_" + date + "_v.tif"):
        x, y, v = geotifflib.read(DIR + "TIF/" + track + "_" + date + "_v.tif")
        x, y, vx = geotifflib.read(DIR + "TIF/" + track + "_" + date +
                                   "_vx.tif")
        x, y, vy = geotifflib.read(DIR + "TIF/" + track + "_" + date +
                                   "_vy.tif")
        x, y, ex = geotifflib.read(DIR + "TIF/" + track + "_" + date +
                                   "_ex.tif")
        x, y, ey = geotifflib.read(DIR + "TIF/" + track + "_" + date +
                                   "_ey.tif")
    else:
        print "Unpacking binary velocity file ", track
        x, y, v, vx, vy, ex, ey, time, interval = readbinary(filename)

    return (x, y, v, vx, vy, ex, ey, time, interval)
Exemplo n.º 3
0
def load_satimages(glacier,xmin,xmax,ymin,ymax,time1=-np.inf,time2=np.inf,data='all'):

  '''
  images,times,types = load_satimages(glacier,xmin,xmax,ymin,ymax,time1=-np.inf,time2=np.inf,data='all')

  Load satellite images for a particular glacier for the grid defined by xmin,xmax,ymin,
  ymax, over the time interval time1 to time2. If time1 == time2, the code will find the 
  sat image closest to the chosen time.
  
  '''
  
  DIRLANDSAT = os.path.join(os.getenv("DATA_HOME"),"Imagery/Landsat/"+glacier+"/TIF/")
  DIRWV = os.path.join(os.getenv("DATA_HOME"),"Imagery/Worldview/"+glacier+"/")
  DIRTSX = os.path.join(os.getenv("DATA_HOME"),"Mosaics/"+glacier+"/")
  
  # Find files to load
  dirs = []
  if time1 == time2:
    times = 0.0
  else:
    times = []
  types = []
  images = []
  
  # Check landsat files
  if ('LANDSAT' in data) or (data=='all'):
    files = os.listdir(DIRLANDSAT)
    for file in files:
      if file.endswith('.tif'):
        filetime = datelib.date_to_fracyear(float(file[0:4]),float(file[4:6]),float(file[6:8]))
        if (time1 == time2): 
          if (abs(filetime - times) < abs(time1 - times)):
            # Last constrain is to prevent unnecessary loading of files, 
            types = 'Landsat'
            dirs = DIRLANDSAT+file
            times = filetime
        elif (filetime >= time1) and (filetime <= time2):
          types.append('Landsat')
          dirs.append(DIRLANDSAT+file)
          times.append(filetime)
          images.append(geotifflib.read(DIRLANDSAT+file))
  
  # Check TSX files
  if ('TSX' in data) or (data == 'all'):  
    files = os.listdir(DIRTSX)
    for file in files:
      if file.endswith('.tif'):
        if glacier == 'Helheim':
          filetime = datelib.doy_to_fracyear(float(file[14:18]),float(file[19:22]))
        elif glacier == 'Kanger':
          filetime = datelib.doy_to_fracyear(float(file[11:15]),float(file[16:19]))        
        if (time1 == time2):
          if (abs(filetime - times) <= abs(time1 - times)) and file.endswith('_1-20mgeo.tif'):
            types = 'TSX'
            dirs = DIRTSX+file
            times = filetime
        elif (filetime >= time1) and (filetime <= time2):
          types.append('TSX')
          dirs.append(DIRLANDSAT+file)
          times.append(filetime)
          images.append(geotifflib.read(DIRTSX+file))
  
  if time1 != time2:
    sortind = np.argsort(times)
    images_sorted = []
    types_sorted = []
    times_sorted = []
    for ind in sortind:
      images_sorted.append(images[ind])
      types_sorted.append(types[ind])
      times_sorted.append(times[ind])
  else:
    images_sorted = geotifflib.read(dirs)
    times_sorted = times
    types_sorted = types
    
          
  return images_sorted,times_sorted,types_sorted        
       
    
Exemplo n.º 4
0
def load_extent_timeseries(glacier,time1,time2,dt,nofront_shapefile='glacier_extent_nofront',datatypes=['Landsat','TSX','WV']):

  '''
  time, xextents, yxextents, bounds = load_extent_timeseries(glacier,time1,
     time2,dt,nofront_shapefile='glacier_extent_nofront',
     datatypes=['Landsat','TSX','WV'])
     
  Interpolates ice-front positions from picked ice-front positions to create 
  a timeseries of meshes to be used in the terminus-driven model.
  
  Inputs:
  glacier           : glacier name (Helheim, Kanger)
  time1             : fractional start time for timeseries
  time2             : fractional end time for timeseries
  dt                : timestep
  nofront_shapefile : nofront shapefile name for mesh extent
  datatypes         : satellite image types for picked ice fronts
  
  Outputs:
  time     : interpolated time between time1,time2 with timestep dt
  xextents : 2-d array of x-coordinates of extents
  yextents : 2-d array of y-coordinates of extents
  bounds   : boundary numbers for extents
  '''

  # Glacier extent with no ice front
  extent = meshlib.shp_to_xy(os.path.join(os.getenv("DATA_HOME"),"ShapeFiles/Glaciers/3D/"+glacier+"/"+nofront_shapefile))
  if extent[1,1] > extent[0,1]:
    extent = np.flipud(extent)
  
  xextent = extent[:,0]
  yextent = extent[:,1]
  bound_extent = extent[:,2]

  # Interpolate glacier extent to a finer grid to make ice-front interpolation easier
  dextent = distlib.transect(xextent,yextent)
  #dextent = np.arange(0,dold[-1],20.0)
  #xextent = np.interp(dextent,dold,xextent)
  #yextent = np.interp(dextent,dold,yextent)
  #f = scipy.interpolate.interp1d(dold,bound,kind='nearest')
  #bound = f(dextent)
  extent = LineString(np.column_stack([extent[:,0:2]]))

  # Load all ice front positions for that time period
  termx,termy,termt = icefrontlib.load_all(time1-0.5,time2+0.5,glacier,type='icefront',datatypes=datatypes)

  # In case we have multiple ice front picks for the same day, we want to
  # use only one of those for continuity
  [junk,ind] = np.unique(termt,return_index=True)
  termt = np.array(termt)[ind]
  termx = termx[:,ind]
  termy = termy[:,ind]

  # Load a velocity profile to use as interpolation direction to figure out ice-front position
  # on timesteps that fall between picked ice fronts
  x,y,u = geotifflib.read(os.path.join(os.getenv("DATA_HOME"),"Velocity/TSX/"+glacier+"/TIF/all-2008-2016_vx.tif"))
  x,y,v = geotifflib.read(os.path.join(os.getenv("DATA_HOME"),"Velocity/TSX/"+glacier+"/TIF/all-2008-2016_vy.tif"))  

  fu = scipy.interpolate.RegularGridInterpolator((y,x),u)
  fv = scipy.interpolate.RegularGridInterpolator((y,x),v)

  # Get ice-front position for each timestep
  time = np.arange(time1,time2+dt,dt)
  timeseries_x = np.zeros([len(termx[:,0]),len(time)])
  timeseries_y = np.zeros([len(termx[:,0]),len(time)])
  timeseries_advance = np.zeros([len(termx[:,0]),len(time)])
  timeseries_dist = np.zeros([len(termx[:,0]),len(time)])  
  timeseries_x[:,:] = float('nan')
  timeseries_y[:,:] = float('nan')
  timeseries_advance[:,:] = float('nan')
  timeseries_dist[:,:] = float('nan')
  xextents = np.zeros([len(termx[:,0])+len(xextent),len(time)])
  yextents = np.zeros([len(termx[:,0])+len(xextent),len(time)])
  bounds = np.zeros([len(termx[:,0])+len(xextent),len(time)])
  for i in range(0,len(time)):
    # Find picked ice-front positions for before and after timestep for the interpolation
    ind = np.argmin(abs(time[i]-termt))
    if termt[ind] < time[i]:
      ind1 = ind
      ind2 = ind+1
    else:
      ind1 = ind-1
      ind2 = ind
    
    # Fractional time between ind1,ind2 to use for interpolation
    frac = (time[i]-termt[ind1])/(termt[ind2]-termt[ind1])
    
    # Get picked ice-front positions that we will use for the interpolation
    nonnan = np.where(~(np.isnan(termx[:,ind1])))[0]
    termx1 = termx[nonnan,ind1]
    termy1 = termy[nonnan,ind1]
    nonnan = np.where(~(np.isnan(termx[:,ind2])))[0]
    termx2 = termx[nonnan,ind2]
    termy2 = termy[nonnan,ind2]   
    
    if termy1[-1] > termy1[0]:
      termx1 = np.flipud(termx1)
      termy1 = np.flipud(termy1)
    if termy2[-1] > termy2[0]:
      termx2 = np.flipud(termx2)
      termy2 = np.flipud(termy2)
  
    # Get locations where interpolate ice front intersects the glacier extent
    # First, get intersection pts for two closest ice-front positions in time
    term1 = LineString(np.column_stack([termx1,termy1]))
    intersect = extent.intersection(term1)
    try:
      if len(intersect) == 2:
        if intersect[0].y > intersect[1].y:
          top1 = [intersect[0].x,intersect[0].y]
          bot1 = [intersect[1].x,intersect[1].y]
        else:
          top1 = [intersect[1].x,intersect[1].y]
          bot1 = [intersect[0].x,intersect[0].y]
      else:
        print "Need to look at date ", datelib.fracyear_to_date(termt[ind1])
    except:
      print "Need to look at date ", datelib.fracyear_to_date(termt[ind1])
    
    term2 = LineString(np.column_stack([termx2,termy2]))
    intersect = extent.intersection(term2)
    try:
      if len(intersect) == 2:
        if intersect[0].y > intersect[1].y:
          top2 = [intersect[0].x,intersect[0].y]
          bot2 = [intersect[1].x,intersect[1].y]
        else:
          top2 = [intersect[1].x,intersect[1].y]
          bot2 = [intersect[0].x,intersect[0].y]
      else:
        print "Need to look at date ", datelib.fracyear_to_date(termt[ind2])
    except:
      print "Need to look at date ", datelib.fracyear_to_date(termt[ind2])
    # Now find new intersection points
    if top1[0] < top2[0]: # advancing on this side
      ind_top = np.where((xextent > top1[0]) & (xextent < top2[0]) & (abs(top1[1]-yextent) < 500.))[0]
      sortind = np.argsort(xextent[ind_top])
      xtops = np.r_[top1[0],xextent[ind_top[sortind]],top2[0]]
      ytops = np.r_[top1[1],yextent[ind_top[sortind]],top2[1]]
      dtops = distlib.transect(xtops,ytops)
      dtop = dtops[-1]*frac
    elif top1[0] > top2[0]: # retreating on this side
      ind_top = np.where((xextent < top1[0]) & (xextent > top2[0]) & (abs(top1[1]-yextent) < 500.))[0]
      sortind = np.argsort(xextent[ind_top])
      xtops = np.r_[top2[0],xextent[ind_top[sortind]],top1[0]]
      ytops = np.r_[top2[1],yextent[ind_top[sortind]],top1[1]]
      dtops = distlib.transect(xtops,ytops)
      dtop = dtops[-1]*(1-frac)
    else:
      print "not advancing or retreating on top"
    xtop = np.interp(dtop,dtops,xtops)
    ytop = np.interp(dtop,dtops,ytops)
      
    if bot1[0] < bot2[0]: # advancing on this side
      ind_bot = np.where((xextent > bot1[0]) & (xextent < bot2[0]) & (abs(bot1[1]-yextent) < 500.))[0]
      sortind = np.argsort(xextent[ind_bot])
      xbots = np.r_[bot1[0],xextent[ind_bot[sortind]],bot2[0]]
      ybots = np.r_[bot1[1],yextent[ind_bot[sortind]],bot2[1]]
      dbots= distlib.transect(xbots,ybots)
      dbot = (dbots[-1])*frac
    elif bot1[0] > bot2[0]: # retreating on this side
      ind_bot = np.where((xextent < bot1[0]) & (xextent > bot2[0]) & (abs(bot1[1]-yextent) < 500.))[0]
      sortind = np.argsort(xextent[ind_bot])
      xbots = np.r_[bot2[0],xextent[ind_bot[sortind]],bot1[0]]
      ybots = np.r_[bot2[1],yextent[ind_bot[sortind]],bot1[1]]
      dbots= distlib.transect(xbots,ybots)
      dbot = (dbots[-1])*(1-frac)
    else:
      print "not advancing or retreating on bot"

    xbot = np.interp(dbot,dbots,xbots)
    ybot = np.interp(dbot,dbots,ybots)

    # Now that we know the bottom and top points (extent of the ice front), we can start
    # calculating the shape, again based on linear interpolation
    # May need to change next expression to find indices between the sidewalls for Kanger, 
    # but this should work for Helheim
    ind_term1 = np.where((termy1 > bot1[1]) & (termy1 < top1[1]))[0]
    icefront_x = []
    icefront_y = []
    advance = []
    icefront_x.append(xtop)
    icefront_y.append(ytop)
    if i > 0:
      nonnan = np.where(~(np.isnan(xextents[:,i-1])))[0]
      extentpath = Path(np.column_stack([xextents[nonnan,i-1],yextents[nonnan,i-1]]))
      if extentpath.contains_point([xtop,ytop]) or (xtop < xtop_old):
        sign = -1
      else:
        sign = 1
      advance.append(sign*distlib.between_pts(xtop,ytop,xtop_old,ytop_old))
    else:
      advance.append(0)
    for j in ind_term1:
      # Get velocities to create a line, to interpolate between ice fronts
      uj = fu((termy1[j],termx1[j]))
      vj = fv((termy1[j],termx1[j]))
    
      # Create flowline that intersects that point of the ice front
      xunit = uj/(np.sqrt(uj**2+vj**2))
      yunit = vj/(np.sqrt(uj**2+vj**2))
      b = termy1[j] - (yunit/xunit)*termx1[j]
      flowlinex = np.arange(-3000.,3005.,10) + termx1[j]
      flowliney = (yunit/xunit)*flowlinex + b
      flowline = LineString(np.column_stack([flowlinex,flowliney]))
    
      # Find where flowline intersects the next ice-front position
      intersect = flowline.intersection(term2)
      add = False
      try:   
        if len(intersect) > 0: 
          ind = np.argmin(abs([intersect[k].x for k in range(0,len(intersect))]-termx1[j]))
          term2_flowline = [intersect[ind].x,intersect[ind].y]
          add = True
      except:
        try:
          term2_flowline = [intersect.x,intersect.y]
          add = True
        except:
          pass
      dflow = distlib.between_pts(termx1[j],termy1[j],term2_flowline[0],term2_flowline[1])
      dmid = frac*dflow
      xmid = np.interp(dmid,[0,dflow],[termx1[j],term2_flowline[0]])
      ymid = np.interp(dmid,[0,dflow],[termy1[j],term2_flowline[1]])
      if (add == True) and (ymid > ybot) and (ymid < ytop):  
        icefront_x.append(xmid)
        icefront_y.append(ymid)
        if i > 0:
          nonnan = np.where(~(np.isnan(timeseries_x[:,i-1])))[0]
          front_lasttime = LineString(np.column_stack([timeseries_x[nonnan,i-1],timeseries_y[nonnan,i-1]]))
          intersect = flowline.intersection(front_lasttime)
          try:
            diff = distlib.between_pts(xmid,ymid,intersect.x,intersect.y)
          except:
            try:
              diff = 1000.0
              for pt in intersect:
                newdiff = distlib.between_pts(xmid,ymid,pt.x,pt.y)
                if newdiff < diff:
                  diff = newdiff
              if diff == 1000.0:
                diff = 0
            except:
              diff = 0
          if extentpath.contains_point([xmid,ymid]):
              sign = -1
          else:
            sign = 1
          advance.append(sign*diff)
        else:
          advance.append(0)
    
    icefront_x.append(xbot)
    icefront_y.append(ybot)
    if i > 0:
      if extentpath.contains_point([xbot,ybot]) or (xbot < xbot_old):
        sign = -1
      else:
        sign = 1
      advance.append(sign*distlib.between_pts(xbot,ybot,xbot_old,ybot_old))
    else:
      advance.append(0)
    
    # Try sorting icefront to get rid of potential tangles
    icefront_x_old = np.asarray(icefront_x)
    icefront_y_old = np.asarray(icefront_y)
    advance_old = np.asarray(advance)
    icefront_x = np.zeros_like(icefront_x_old)
    icefront_y = np.zeros_like(icefront_y_old)
    advance = np.zeros_like(advance_old)
    icefront_x[0] = icefront_x_old[0]
    icefront_y[0] = icefront_y_old[0]
    advance[0] = advance_old[0]
    ind = range(1,len(icefront_x_old))
    for k in range(1,len(icefront_x_old)):
      mindist = 10000.
      for j in range(0,len(ind)):
        dist = distlib.between_pts(icefront_x[k-1],icefront_y[k-1],icefront_x_old[ind[j]],icefront_y_old[ind[j]])
        if dist < mindist:
          mindist = dist
          minind = ind[j]
      icefront_x[k] = icefront_x_old[minind]
      icefront_y[k] = icefront_y_old[minind]
      advance[k] = advance_old[minind]
      ind.remove(minind)
    
    # Save icefront in timeseries 
    timeseries_x[0:len(icefront_x),i] = icefront_x
    timeseries_y[0:len(icefront_y),i] = icefront_y
    timeseries_dist[0:len(icefront_x),i] = distlib.transect(icefront_x,icefront_y)
    timeseries_advance[0:len(icefront_x),i] = advance
    
    # Now create mesh extent and BC numbers using the interpolated ice front
    boundterminus = np.ones(len(icefront_x))*2.0
    ind1 = np.where((xextent > xbot) & (abs(yextent - ybot) < 1.0e3))[0][0] 
    ind2 = np.where((xextent > xtop) & (abs(yextent - ytop) < 1.0e3))[0][-1]

    extent_x = np.r_[xextent[0:ind1],np.flipud(icefront_x),xextent[ind2+1:]]
    extent_y = np.r_[yextent[0:ind1],np.flipud(icefront_y),yextent[ind2+1:]]
    bound = np.r_[bound_extent[0:ind1],boundterminus,bound_extent[ind2+1:]]
    
    xextents[0:len(extent_x),i] = extent_x
    yextents[0:len(extent_x),i] = extent_y
    bounds[0:len(extent_x),i] = bound
    
    xtop_old = float(xtop)
    ytop_old = float(ytop)
    xbot_old = float(xbot)
    ybot_old = float(ybot)
      
    
  return  time, xextents, yextents, bounds, timeseries_x, timeseries_y, timeseries_advance
Hf_med = np.zeros([
    len(dates),
])
times = np.zeros([
    len(dates),
])

for i in range(0, len(dates)):
    for j in range(0, len(DIRs)):
        times[i] = datelib.date_to_fracyear(int(dates[i][0:4]),
                                            int(dates[i][4:6]),
                                            int(dates[i][6:]))

        if 'FS' in DIRs[j] and 'ConstantT' in DIRs[j]:
            # Get heights
            x, y, zs_grid = geotifflib.read(DIRs[j] + 'DEM' + dates[i] +
                                            '_surf_mea_zs.tif')
            x, y, zb_grid = geotifflib.read(DIRs[j] + 'DEM' + dates[i] +
                                            '_bed_mod_zb.tif')
            grid = zs_grid - zb_grid
            grid[ind_cutoff_grid_fast] = np.float('nan')
            H_fast[i] = np.nanmean(grid)
            grid = zs_grid - zb_grid
            grid[ind_cutoff_grid] = np.float('nan')
            zb_grid[ind_cutoff_grid] = np.float('nan')
            H_med[i] = np.nanmean(grid[vsurfini_cutoff_SSA < 4000])
            H[i] = np.nanmean(grid)
            P_i[i] = np.nanmean(rho_i * g * grid)
            P_w[i] = np.nanmean(-rho_sw * g * zb_grid)

            #Get height above flotation
            grid = zs_grid - floatlib.height(zb_grid)
Exemplo n.º 6
0
glacier = 'Helheim'

if glacier == 'Kanger':
  xmin = 468000.
  xmax = 498000.
  ymin = -2299000.
  ymax = -2264000.
elif glacier == 'Helheim':
  xmin = 283000.
  xmax = 313000.
  ymin = -2587000.
  ymax = -2552000.

file = os.path.join(os.getenv("DATA_HOME"),"Velocity/TSX/"+glacier+"/TIF/all-2008-2016")
x,y,u = geotifflib.read(file+'_vx.tif')
x,y,v = geotifflib.read(file+'_vy.tif')  

nx = len(x)
ny = len(y)

dudx = np.zeros_like(v)
dudy = np.zeros_like(v)
dvdx = np.zeros_like(v)
dvdy = np.zeros_like(v)

dudx[:,:] = float('nan')
dudy[:,:]  = float('nan')
dvdx[:,:]  = float('nan')
dvdy[:,:]  = float('nan')
Exemplo n.º 7
0
def inversion_3D(glacier,
                 x,
                 y,
                 time,
                 dir_velocity_out='none',
                 blur=False,
                 dx='none'):
    '''
  Inputs:
  x : list of x coordinates for grid interpolation
  y : list of y coordinates for grid interpolation
  time : primary time for velocities, which will be filled in with other data
  file_velocity_in : velocity file for interpolation
  dir_velocity_out : directory for outputting the velocity
  
  Outputs:
  u : velocity in x-dir on grid defined by x,y
  y : velocity in y-dir on grid defined by x,y
  '''

    xmin = np.min(x) - 5.0e3
    xmax = np.max(x) + 5.0e3
    ymin = np.min(y) - 5.0e3
    ymax = np.max(y) + 5.0e3

    OUTDIR = os.path.join(os.getenv("DATA_HOME"),
                          "Velocity/MosaicVelocities/" + glacier)

    # Large velocity map to fill in gaps in smaller velocity map
    file_velocity_all = os.path.join(
        os.getenv("DATA_HOME"),
        "Velocity/TSX/" + glacier + "/TIF/all-2008-2016")

    # If the region is bigger than what is covered by the TSX stripmaps, then we use Ian's big
    # inSAR velocity map
    file_velocity_global = os.path.join(
        os.getenv("DATA_HOME"),
        "Velocity/Random/Greenland/AllGLVel/mosaicOffsets")

    year, month, day = datelib.fracyear_to_date(time)
    date = "%04d%02d%02d" % (year, month, day)

    # Individual velocity map for time step
    if time <= 2008:
        HOWATDIR = os.path.join(os.getenv("DATA_HOME"),
                                "Velocity/Howat/" + glacier + "/")
        # Use Howat velocity maps
        print date
        if glacier == 'Kanger':
            if '200707' in date:
                filename1 = HOWATDIR + "OPT_E68.80N_2007-08/OPT_E68.80N_2007-08"
                filename2 = HOWATDIR + "OPT_E68.80N_2007-07/OPT_E68.80N_2007-07"
            elif '200107' in date:
                filename1 = HOWATDIR + "OPT_E68.80N_2001-07/OPT_E68.80N_2001-07"
                filename2 = filename1
            elif ('200308' in date) or ('200307' in date):
                filename1 = HOWATDIR + "OPT_E68.80N_2003-07/OPT_E68.80N_2003-07"
                filename2 = HOWATDIR + "OPT_E68.80N_2001-07/OPT_E68.80N_2001-07"
            elif '200506' in date:
                filename1 = HOWATDIR + "OPT_E68.80N_2005-06/OPT_E68.80N_2005-06"
                filename2 = filename1
            elif '200508' in date:
                filename1 = HOWATDIR + "OPT_E68.80N_2005-08/OPT_E68.80N_2005-08"
                filename2 = filename1
            elif '200605' in date:
                filename2 = HOWATDIR + "OPT_E68.80N_2006-05/OPT_E68.80N_2006-05"
                filename1 = HOWATDIR + "OPT_E68.80N_2006-04/OPT_E68.80N_2006-04"
            elif '200607' in date:
                filename2 = HOWATDIR + "OPT_E68.80N_2006-07/OPT_E68.80N_2006-07"
                filename1 = HOWATDIR + "OPT_E68.80N_2006-06/OPT_E68.80N_2006-06"
            elif '200609' in date:
                filename1 = HOWATDIR + "OPT_E68.80N_2006-09/OPT_E68.80N_2006-09"
                filename2 = filename1
        elif glacier == 'Helheim':
            if '200709' in date:
                filename1 = HOWATDIR + "OPT_E66.50N_2007-08/OPT_E66.50N_2007-08"
                filename2 = HOWATDIR + "OPT_E66.50N_2007-09/OPT_E66.50N_2007-09"
            elif '200408' in date:
                filename1 = HOWATDIR + "OPT_E66.50N_2004-08/OPT_E66.50N_2004-08"
                filename2 = HOWATDIR + "OPT_E66.50N_2004-07/OPT_E66.50N_2004-07"
            elif '200508' in date:
                filename1 = HOWATDIR + "OPT_E66.50N_2005-09/OPT_E66.50N_2005-09"
                filename2 = HOWATDIR + "OPT_E66.50N_2005-07/OPT_E66.50N_2005-07"
            elif '200608' in date:
                filename1 = HOWATDIR + "OPT_E66.50N_2006-09/OPT_E66.50N_2006-09"
                filename2 = HOWATDIR + "OPT_E66.50N_2006-07/OPT_E66.50N_2006-07"

        files_vx = ' '+filename1+'.vx.tif '+filename2+'.vx.tif '+\
                file_velocity_all+'_vx.tif'+' '+file_velocity_global+'_vx.tif'
        files_vy = ' '+filename1+'.vy.tif '+filename2+'.vy.tif '+\
                file_velocity_all+'_vy.tif'+' '+file_velocity_global+'_vy.tif'
    else:
        # Use TSX velocity maps
        filename1, time1 = tsx_near_time(time, glacier, just_filename=True)
        filename2, time2 = tsx_near_time(time - 11 / 365.,
                                         glacier,
                                         just_filename=True)
        filename3, time3 = tsx_near_time(time + 11 / 365.,
                                         glacier,
                                         just_filename=True)

        if abs(time - time2) < abs(time - time3):
            files_vx = ' '+filename1+'_vx.tif'+' '+filename2+'_vx.tif'+\
         ' '+filename3+'_vx.tif'+' '+file_velocity_all+'_vx.tif'+' '+file_velocity_global+'_vx.tif'
            files_vy = ' '+filename1+'_vy.tif'+' '+filename2+'_vy.tif'+\
         ' '+filename3+'_vy.tif'+' '+file_velocity_all+'_vy.tif'+' '+file_velocity_global+'_vy.tif'
        else:
            files_vx = ' '+filename1+'_vx.tif'+' '+filename3+'_vx.tif'+\
              ' '+filename2+'_vx.tif'+' '+file_velocity_all+'_vx.tif'+' '+file_velocity_global+'_vx.tif'
            files_vy = ' '+filename1+'_vy.tif'+' '+filename3+'_vy.tif'+\
              ' '+filename2+'_vy.tif'+' '+file_velocity_all+'_vy.tif'+' '+file_velocity_global+'_vy.tif'

    CURRENTDIR = os.getcwd()
    os.chdir(OUTDIR)
    filename_vx = 'mosaic-' + date + '-vx'
    filename_vy = 'mosaic-' + date + '-vy'
    if dx == 'none':
        os.system('dem_mosaic --hole-fill-length 5 --t_projwin '+str(xmin)+' '+str(ymin)+' '+str(xmax)+\
        ' '+str(ymax)+' --priority-blending-length 10 -o'+filename_vx+files_vx)
        os.system('dem_mosaic --hole-fill-length 5 --t_projwin '+str(xmin)+' '+str(ymin)+' '+str(xmax)+\
        ' '+str(ymax)+' --priority-blending-length 10 -o'+filename_vy+files_vy)
    else:
        os.system('dem_mosaic --hole-fill-length 5 --t_projwin '+str(xmin)+' '+str(ymin)+' '+str(xmax)+\
        ' '+str(ymax)+' --tr '+str(dx)+' --priority-blending-length 10 -o'+filename_vx+files_vx)
        os.system('dem_mosaic --hole-fill-length 5 --t_projwin '+str(xmin)+' '+str(ymin)+' '+str(xmax)+\
        ' '+str(ymax)+' --tr '+str(dx)+' --priority-blending-length 10 -o'+filename_vy+files_vy)

    xu, yu, uu = geotifflib.read(filename_vx + "-tile-0.tif")
    xv, yv, vv = geotifflib.read(filename_vy + "-tile-0.tif")

    if (blur == True) and (dx == 'none'):
        print "Blurring DEM over 17 pixels (roughly 1.5km in each direction)..."
        # 17 pixel gaussian blur
        vx_blur = scipy.ndimage.filters.gaussian_filter(uu,
                                                        sigma=2,
                                                        truncate=4)
        vy_blur = scipy.ndimage.filters.gaussian_filter(vv,
                                                        sigma=2,
                                                        truncate=4)
    else:
        vx_blur = uu
        vy_blur = vv

    os.chdir(CURRENTDIR)

    # Calculate velocity magnitude
    vmag = np.sqrt(vx_blur**2 + vy_blur**2)

    ######################################################
    # Write out velocities to files for inversion solver #
    ######################################################

    # Interpolate to input grid
    xgrid, ygrid = np.meshgrid(x, y)
    fu = scipy.interpolate.RegularGridInterpolator((yu, xu),
                                                   vx_blur,
                                                   method='linear')
    vx = fu((ygrid, xgrid))
    fv = scipy.interpolate.RegularGridInterpolator((yv, xv),
                                                   vy_blur,
                                                   method='linear')
    vy = fv((ygrid, xgrid))

    if dir_velocity_out != 'none':
        #files = os.listdir(OUTDIR):
        #for file in files:
        #  if file.startswith('mosaic-'+date):
        #    shutil.copy(file,dir_velocity_out)

        # File for velocity in x-dir
        fidu = open(dir_velocity_out + "/udem.xy", "w")
        fidu.write('{}\n{}\n'.format(len(x), len(y)))

        # File for velocity in y-dir
        fidv = open(dir_velocity_out + "/vdem.xy", "w")
        fidv.write('{}\n{}\n'.format(len(x), len(y)))

        for i in range(0, len(x)):
            for j in range(0, len(y)):
                fidu.write('{} {} {}\n'.format(x[i], y[j], vx[j, i]))
                fidv.write('{} {} {}\n'.format(x[i], y[j], vy[j, i]))

        fidv.close()
        fidu.close()

    return vx, vy
Exemplo n.º 8
0
def variability(glacier, time1, time2):

    ''
    ''

    DIR_TSX = os.path.join(os.getenv("DATA_HOME"),
                           "Velocity/TSX/" + glacier + "/")

    if glacier == 'Helheim':
        xmin = 270000.0
        xmax = 354900.0
        ymin = -2601000.0
        ymax = -2541000.0
    elif glacier == 'Kanger':
        xmin = 457000.0
        xmax = 517000.0
        ymin = -2319100.0
        ymax = -2247100.0

    dx = dy = 100.
    nx = int(np.ceil((xmax - xmin) / dx) + 1)
    x = np.linspace(xmin, (nx - 1) * dx + xmin, nx)
    ny = int(np.ceil((ymax - ymin) / dx) + 1)
    y = np.linspace(ymin, (ny - 1) * dy + ymin, ny)
    xgrid, ygrid = np.meshgrid(x, y)
    coords = np.column_stack([ygrid.flatten(), xgrid.flatten()])

    #################
    # LOAD TSX Data #
    #################

    DIRs = os.listdir(DIR_TSX)

    # Get number of velocity files
    nt = 0
    for DIR in DIRs:
        if DIR.startswith('track'):
            nt = nt + 1

    # Set up variables
    velgrid = np.zeros([ny, nx, nt])
    mask = np.zeros([ny, nx, nt])
    velgrid_mask = np.zeros([ny, nx, nt])
    time = np.zeros(nt)
    ergrid = np.zeros([ny, nx, nt])

    # Load velocity and mask
    count = 0
    for j in range(0, len(DIRs)):
        DIR = DIRs[j]
        if DIR.startswith('track'):
            # Load velocity
            x1, y1, v1, vx1, vy1, ex1, ey1, time_file, interval1 = geodatlib.readvelocity(
                DIR_TSX, DIR, "mosaicOffsets")

            time[count] = time_file
            year, month, day = datelib.fracyear_to_date(time_file)

            xind1 = np.argmin(abs(x1 - xmin))
            xind2 = np.argmin(abs(x1 - xmax)) + 1
            yind1 = np.argmin(abs(y1 - ymin))
            yind2 = np.argmin(abs(y1 - ymax)) + 1

            # Load velocity
            try:
                # If the input and output grids have the same dimensions...
                velgrid[:, :, count] = v1[yind1:yind2, xind1:xind2]
            except:
                # Otherwise interpolate onto output grid
                f_dem = scipy.interpolate.RegularGridInterpolator(
                    [y1, x1],
                    v1,
                    bounds_error=False,
                    method='linear',
                    fill_value=float('nan'))
                v_flatten = f_dem(coords)

                # Reshape to grid
                velgrid[:, :, count] = np.reshape(v_flatten, (ny, nx))

            # Load mask
            date = "%04d%02d%02d" % (year, month, day)
            maskfile = DIR_TSX + 'TIF/' + DIR + '_' + date + '_' + 'mask.tif'
            if os.path.isfile(maskfile):
                xmask, ymask, mask[:, :, count] = geotifflib.read(maskfile)
            else:
                xmask, ymask, mask[:, :, count] = masklib.load_grid(
                    glacier, xmin, xmax, ymin, ymax, dx, icefront_time=time1)
                geotifflib.write_from_grid(xmask, ymask,
                                           np.flipud(mask[:, :, count]),
                                           float('nan'), maskfile)

            velgrid_mask[:, :, count] = np.array(velgrid[:, :, count])
            velgrid_mask[mask[:, :, count] == 1, count] = float('nan')

            count = count + 1

    del count, maskfile, date, xind1, yind1, xind2, yind2, year, month, x1, y1, vx1, vy1, ex1, ey1, time_file, interval1

    # Throw out obvious outliers
    ind = np.where(velgrid > 16.0e3)
    velgrid[ind[0], ind[1], ind[2]] = float('nan')
    velgrid_mask[ind[0], ind[1], ind[2]] = float('nan')
    print "Throwing out velocities above 16 km/yr to deal with outliers in Kanger record"

    # Only keep data that falls between time1 and time2, and sort that data by time
    sortind = np.argsort(time)
    time = time[sortind]
    velgrid_mask = velgrid_mask[:, :, sortind]
    velgrid = velgrid[:, :, sortind]

    ind = np.where((time > time1) & (time < time2))[0]
    velgrid_mask = velgrid_mask[:, :, ind]
    time = time[ind]
    velgrid = velgrid[:, :, ind]

    # Get average and std values
    velmean = np.nanmean(velgrid_mask, axis=2)

    # Get linear trends
    veltrend = np.zeros_like(velmean)
    veltrend_time1 = np.zeros_like(velmean)
    veltrend_time2 = np.zeros_like(velmean)
    veltrend_count = np.zeros_like(velmean)
    veltrend_p = np.zeros_like(velmean)
    veltrend_error = np.zeros_like(velmean)
    veltrend_r = np.zeros_like(velmean)
    veltrend_intercept = np.zeros_like(velmean)
    veltrend_p[:, :] = float('nan')
    veltrend[:, :] = float('nan')
    veltrend_error[:, :] = float('nan')
    veltrend_r[:, :] = float('nan')
    veltrend_intercept[:, :] = float('nan')
    for j in range(0, len(y)):
        for i in range(0, len(x)):
            nonnan = np.where((~(np.isnan(velgrid_mask[j, i, :]))))[0]
            if len(nonnan) > 0.75 * len(time):
                if (np.floor(np.min(time[nonnan])) == time1) and np.ceil(
                        np.max(time[nonnan])) == time2:
                    slope, intercept, r, p, std_err = stats.linregress(
                        time[nonnan], velgrid_mask[j, i, nonnan])
                    veltrend_count[j, i] = len(nonnan)
                    veltrend[j, i] = slope
                    veltrend_p[j, i] = p
                    veltrend_error[j, i] = std_err
                    veltrend_time1[j, i] = np.min(time[nonnan])
                    veltrend_time2[j, i] = np.max(time[nonnan])
                    veltrend_r[j, i] = r
                    veltrend_intercept[j, i] = intercept

    # Detrend velocity timeseries
    veldetrend = np.zeros_like(velgrid_mask)
    for i in range(0, len(time)):
        trend = veltrend_intercept + time[i] * veltrend
        veldetrend[:, :, i] = velgrid_mask[:, :, i] - trend

    # Calculate range of observed values
    velrange = np.zeros_like(velmean)
    velrange[:, :] = float('nan')
    for i in range(0, len(x)):
        for j in range(0, len(y)):
            nonnan = np.where(~(np.isnan(veldetrend[j, i, :])))[0]
            if len(nonnan) > 1:
                velrange[j, i] = np.max(veldetrend[j, i, nonnan]) - np.min(
                    veldetrend[j, i, nonnan])

    # Remove insignifcant trends
    ind = np.where(veltrend_p > 0.05)
    veltrend[ind] = float('nan')
    veltrend_error[ind] = float('nan')

    # Get number of nonnan velocities for each pixel
    velcount = np.zeros([ny, nx])
    for j in range(0, ny):
        for i in range(0, nx):
            nonnan = len(np.where(~(np.isnan(velgrid_mask[j, i, :])))[0])
            velcount[j, i] = nonnan

    sortind = np.argsort(time)
    velgrid_mask = velgrid_mask[:, :, sortind]
    time = time[sortind]

    return x, y, velgrid_mask, veltrend, veldetrend, velrange, velcount, veltrend_error, time
Exemplo n.º 9
0
def howat_optical_at_pts(xpt, ypt, glacier, xy_velocities='False'):

    DIR = os.path.join(os.getenv("DATA_HOME"),
                       "Velocity/Howat/" + glacier + "/")

    dirs = os.listdir(DIR)

    m = 0
    for dir in dirs:
        if dir.startswith('OPT'):
            m = m + 1
    try:
        n = len(xpt)
    except:
        n = 1

    # Set up variables
    velocities = np.zeros([m, n])
    velocities[:, :] = 'nan'
    velocities_x = np.zeros([m, n])
    velocities_x[:, :] = 'nan'
    velocities_y = np.zeros([m, n])
    velocities_y[:, :] = 'nan'
    error = np.zeros([m, n])
    error[:, :] = 'nan'
    times = np.zeros([m, 2])

    count = 0
    for dir in dirs:
        if dir.startswith('OPT'):
            metafile = open(DIR + dir + '/' + dir + '.meta', "r")
            lines = metafile.readlines()
            metafile.close()
            jdates = []
            jdates.append(float(lines[0][36:48]))
            if len(lines[0]) > 50:
                jdates.append(float(lines[0][49:61]))
                if len(lines[0]) > 63:
                    jdates.append(float(lines[0][62:74]))
                    if len(lines[0]) > 75:
                        jdates.append(float(lines[0][75:87]))
                        if len(lines[0]) > 88:
                            jdates.append(float(lines[0][88:100]))
                            if len(lines[0]) > 101:
                                jdates.append(float(lines[0][101:113]))

            # Get date
            times_all = []
            for jdate in jdates:
                year, month, day, fracday = jdcal.jd2gcal(jdate, 0)
                times_all.append(
                    datelib.date_to_fracyear(year, month, day + fracday))

            times[count, 0] = np.mean(times_all)
            times[count, 1] = np.max(times_all) - np.min(times_all)

            x, y, vx = geotifflib.read(DIR + dir + '/' + dir + '.vx.tif',
                                       no_data_value=-99999)
            x, y, vy = geotifflib.read(DIR + dir + '/' + dir + '.vy.tif',
                                       no_data_value=-99999)
            x, y, ex = geotifflib.read(DIR + dir + '/' + dir + '.ex.tif',
                                       no_data_value=-99999)
            x, y, ey = geotifflib.read(DIR + dir + '/' + dir + '.ey.tif',
                                       no_data_value=-99999)
            v = np.sqrt(vx**2 + vy**2)

            fv = scipy.interpolate.RegularGridInterpolator([y, x],
                                                           v,
                                                           method='linear',
                                                           bounds_error=False)
            fex = scipy.interpolate.RegularGridInterpolator([y, x],
                                                            ex,
                                                            method='linear',
                                                            bounds_error=False)
            fey = scipy.interpolate.RegularGridInterpolator([y, x],
                                                            ey,
                                                            method='linear',
                                                            bounds_error=False)
            fvx = scipy.interpolate.RegularGridInterpolator([y, x],
                                                            vx,
                                                            method='linear',
                                                            bounds_error=False)
            fvy = scipy.interpolate.RegularGridInterpolator([y, x],
                                                            vy,
                                                            method='linear',
                                                            bounds_error=False)

            # Find velocities
            velocities[count, :] = fv(np.array([ypt, xpt]).T)
            velocities_x[count, :] = fvx(np.array([ypt, xpt]).T)
            velocities_y[count, :] = fvy(np.array([ypt, xpt]).T)
            error[count, :] = velocities[count, :] * np.sqrt(
                (fex(np.array([ypt, xpt]).T) /
                 fvx(np.array([ypt, xpt]).T))**2 +
                (fey(np.array([ypt, xpt]).T) / fvy(np.array([ypt, xpt]).T))**2)

            count = count + 1

    # Sort arrays by time
    sortind = np.argsort(times[:, 0], 0)
    tpt_sort = times[sortind]
    vpt_sort = velocities[sortind, :]
    ept_sort = error[sortind, :]
    vxpt_sort = velocities_x[sortind, :]
    vypt_sort = velocities_y[sortind, :]

    if xy_velocities == 'True':
        return vpt_sort, tpt_sort, ept_sort, vxpt_sort, vypt_sort
    else:
        return vpt_sort, tpt_sort, ept_sort
Exemplo n.º 10
0
def inversion_2D(x, y, d, glacier, time, dir_velocity_out, filt_len='none'):

    xmin = np.min(x) - 2.0e3
    xmax = np.max(x) + 2.0e3
    ymin = np.min(y) - 2.0e3
    ymax = np.max(y) + 2.0e3

    OUTDIR = os.path.join(os.getenv("DATA_HOME"),
                          "Velocity/MosaicVelocities/" + glacier)

    # Large velocity map to fill in gaps in smaller velocity map
    file_velocity_global = os.path.join(
        os.getenv("DATA_HOME"),
        "Velocity/Random/Greenland/AllGLVel/mosaicOffsets")

    # Individual velocity map
    filename1, time1 = tsx_near_time(time, glacier, just_filename=True)
    filename2, time2 = tsx_near_time(time - 0.1, glacier, just_filename=True)
    filename3, time3 = tsx_near_time(time + 0.1, glacier, just_filename=True)
    year, month, day = datelib.fracyear_to_date(time1)
    date = "%04d%02d%02d" % (year, month, day)

    files_vx = ' '+filename1+'_vx.tif'+' '+filename2+'_vx.tif'+\
      ' '+filename3+'_vx.tif'+' '+file_velocity_global+'_vx.tif'
    files_vy = ' '+filename1+'_vy.tif'+' '+filename2+'_vy.tif'+\
      ' '+filename3+'_vy.tif'+' '+file_velocity_global+'_vy.tif'

    CURRENTDIR = os.getcwd()
    os.chdir(OUTDIR)
    filename_vx = 'mosaic-' + date + '-vx'
    if not (os.path.isfile(filename_vx + '-tile-0.tif')):
        os.system('dem_mosaic --t_projwin '+str(xmin)+' '+str(ymin)+' '+str(xmax)+\
        ' '+str(ymax)+' --priority-blending-length 10 -o'+filename_vx+files_vx)
    filename_vy = 'mosaic-' + date + '-vy'
    if not (os.path.isfile(filename_vy + '-tile-0.tif')):
        os.system('dem_mosaic --t_projwin '+str(xmin)+' '+str(ymin)+' '+str(xmax)+\
        ' '+str(ymax)+' --priority-blending-length 10 -o'+filename_vy+files_vy)

    xu, yu, uu = geotifflib.read(filename_vx + "-tile-0.tif")
    xv, yv, vv = geotifflib.read(filename_vy + "-tile-0.tif")

    fu = scipy.interpolate.RegularGridInterpolator((yu, xu), uu)
    vx = fu((y, x))
    fv = scipy.interpolate.RegularGridInterpolator((yv, xv), vv)
    vy = fv((y, x))

    vnonnan = np.sqrt(vx**2 + vy**2)

    # Filter velocities
    if filt_len != 'none':
        cutoff = (1 / filt_len) / (1 / (np.diff(d[1:3]) * 2))
        b, a = scipy.signal.butter(4, cutoff, btype='low')
        filtered = scipy.signal.filtfilt(b, a, vnonnan)
    else:
        filtered = np.array(vcomb)

    # Write out the velocity data
    fid = open(dir_velocity_out + "velocity.dat", 'w')
    R = len(filtered)
    fid.write('{0}\n'.format(R))
    for j in range(0, R):
        fid.write('{} {}\n'.format(d[j], filtered[j]))
    fid.close()

    return filtered