示例#1
0
 def __init__(self, src_grid_file_name, dst_grid_file_name, online_flag, realdata_file_name, k, pole_flag):
   Interp.__init__(self, src_grid_file_name, dst_grid_file_name, online_flag, realdata_file_name)
   self.power = 1
   self.eps = 1.0e-6
   self.nearest_k = k
   self.idw_obj = Search(self.stree_base_obj, self.stree)
   self.pole_flag = pole_flag
示例#2
0
 def find_nearest_box(self, query_point):
   # find nearest 24
   res_indx, res_lst = Search.find_nearest_k(self, query_point, 24)
   # filter the masked points
   res_indx, res_lst = self.filter_mask(res_indx, res_lst)
   if len(res_indx) < 4:
     self.full = False
   # find bilinear_box 
   flag, box_indx, box = select_containing_rect(query_point, res_indx, res_lst)
   self.outside = flag
   if flag:
     print 'Can not be contained.'
   else:
     box_indx, box = clockwise_sort_indx(box_indx, box)
     box = clockwise_sort(box)
   return self.outside, self.full, box_indx, box
示例#3
0
 def find_nearest_box(self, query_point):
     # find nearest 24
     res_indx, res_lst = Search.find_nearest_k(self, query_point, 24)
     # filter the masked points
     res_indx, res_lst = self.filter_mask(res_indx, res_lst)
     if len(res_indx) < 4:
         self.full = False
     # find bilinear_box
     flag, box_indx, box = select_containing_rect(query_point, res_indx,
                                                  res_lst)
     self.outside = flag
     if flag:
         print 'Can not be contained.'
     else:
         box_indx, box = clockwise_sort_indx(box_indx, box)
         box = clockwise_sort(box)
     return self.outside, self.full, box_indx, box
示例#4
0
 def __init__(self, stree_base, stree):
   Search.__init__(self, stree_base, stree)
   self.threshold = 5.0
   self.outside = True
   self.full = True
示例#5
0
class Idw(Interp):
  
  # init Idw object.
  # init self.eps to avoid deviding by zero, init the search object self.idw_obj
  def __init__(self, src_grid_file_name, dst_grid_file_name, online_flag, realdata_file_name, k, pole_flag):
    Interp.__init__(self, src_grid_file_name, dst_grid_file_name, online_flag, realdata_file_name)
    self.power = 1
    self.eps = 1.0e-6
    self.nearest_k = k
    self.idw_obj = Search(self.stree_base_obj, self.stree)
    self.pole_flag = pole_flag
  
  # local select in idw algorithm 
  # select nearest k no-mask-pnts in intermediate result
  def select_k(self, indx, lst, k):
    num = 0
    a = []
    b = []
    for i in xrange(len(indx)):
      if self.src_grid_imask[indx[i]] == 0:
        continue
      a.append(indx[i])
      b.append(lst[i])
      num += 1
      if num == k:
        break
    if num < k:
      print "Not full k"
      #for i in indx:
      #  print self.src_grid_imask[i]
      #sys.exit()
    return a, b
  
  # calc k idw neighbors, sorted, with no mask 
  def find_idw_neighbors(self, dst_point):
    indx, lst = self.idw_obj.find_nearest_k(dst_point, self.nearest_k * 4)
    # there may be bugs.
    if Interp.check_all_masks(self, indx[0:self.nearest_k], self.nearest_k):
      indx = []
      lst = []
    else:
      indx, lst = self.select_k(indx, lst, self.nearest_k)
    return indx, lst
    
  # interp process in Idw subclass.
  def interp(self):
    n = len(self.dst_grid_center_lon)
    # traverse dst pnt
    for i in xrange(n):
      # ignore masked pnt
      if self.dst_grid_imask[i] == 0:
        print 'My mask is zero!'
        self.remap_matrix.append([])
        self.remap_matrix_indx.append([])
        continue

      dst_point = (self.dst_grid_center_lon[i], self.dst_grid_center_lat[i])
      neighbor_indx, neighbor_lst = self.find_idw_neighbors(dst_point)
      
      # suppose atm grid has no mask 
      # case ocn2atm, a atm cell with a land cell below
      if not neighbor_indx:
        print 'It must be a land cell.'
        self.remap_matrix.append([])
        self.remap_matrix_indx.append([])
        continue
      
      # better wish, but disappointed
      # tackle pole region
      if self.pole_flag:
        if dst_point[1] > self.pole_north_bnd:
          print 'Tackling pole region'
          idw_solver = Pole_Solver(dst_point, self.pole_north, self.nearest_k)
          idw_solver.solve()
          idw_box_indx = self.pole_north_indx[0:self.nearest_k]
          idw_box_indx = Interp.indx_recovery(self, idw_box_indx)
          self.remap_matrix.append(idw_solver.wgt_lst)
          self.remap_matrix_indx.append(idw_box_indx)
          continue
        if dst_point[1] < self.pole_south_bnd:
          print 'Tackling pole region'
          idw_solver = Pole_Solver(dst_point, self.pole_south, self.nearest_k)
          idw_solver.solve()
          idw_box_indx = self.pole_south_indx[0:self.nearest_k]
          idw_box_indx = Interp.indx_recovery(self, idw_box_indx)
          self.remap_matrix.append(idw_solver.wgt_lst)
          self.remap_matrix_indx.append(idw_box_indx)
          continue
     
      # normal case, init idw_solver 
      idw_solver = Idw_Solver(dst_point, neighbor_lst, self.eps, self.power)
      
      # decide if dst pnt is coincide with a src pnt
      if dst_point in neighbor_lst:
        print 'coincide'
        for item in neighbor_lst:
          if item == dst_point:
            idw_solver.wgt_lst.append(1.0)
          else:
            idw_solver.wgt_lst.append(0.0)
      else: 
        # solve normal case
        idw_solver.solve()
      
      print neighbor_indx 
      # transform ghost indx to original
      neighbor_indx = Interp.indx_recovery(self, neighbor_indx)
      print neighbor_indx 
      
      print ''
      print dst_point
      print neighbor_lst
      print neighbor_indx
      print idw_solver.wgt_lst

      # store result into objs
      #self.interp_wgt = idw_solver.wgt_lst
      #self.interp_box_indx = neighbor_indx
      #self.interp_box = neighbor_lst
  
      # set remap_matrix and remap_matrix_indx objs
      self.remap_matrix.append(idw_solver.wgt_lst)
      self.remap_matrix_indx.append(neighbor_indx)
      if len(idw_solver.wgt_lst) != len(neighbor_indx):
        print idw_solver.wgt_lst
        print neighbor_indx
        print 'ERRORRRR'
        sys.exit()

    print 'remap_matrix size is:'
    print len(self.remap_matrix)
   
    # compact remap matrix, gen remap_src_indx and remap_dst_indx
    #print 'Compacting remap matrix...'
    #Interp.compact_remap_matrix(self)
    #print 'Compacting finished!'

  def gen_remap_matrix_file(self):
    filename = 'rmp_' + self.src_grid_name + '_' + self.dst_grid_name + '_idw.nc'
    write_handler = Writenc(filename, self, 'idw')
    write_handler.write()
  
  def remap(self):
    remap_result = Interp.remap(self)
    return remap_result
示例#6
0
 def __init__(self, stree_base, stree):
     Search.__init__(self, stree_base, stree)
     self.threshold = 5.0
     self.outside = True
     self.full = True