示例#1
0
  def interp(self):
    #point = (self.dst_grid_center_lon[0], self.dst_grid_center_lat[0])
    #point = (0.0, 0.0)
    n = len(self.dst_grid_center_lon)
    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
      
      # debug recovery function
      #dst_point = (357.625, -82.375)
      dst_point = (self.dst_grid_center_lon[i], self.dst_grid_center_lat[i])
      indx, lst = self.bilinearbox_obj.find_nearest_k(dst_point, 4)
      # suppose atom grid has no mask
      # case ocn2atm, a atm cell with a land cell below
      if Interp.check_all_masks(self, indx, 4):
        print 'It must be a land cell.'
        self.remap_matrix.append([])
        self.remap_matrix_indx.append([])
        continue
      # decide if dst pnt is coincide with a src pnt
      local_wgt = []
      if dst_point in lst:
        print 'coincide'
        for item in lst:
          if item == dst_point:
            local_wgt.append(1.0)
          else:
            local_wgt.append(0.0)
        
        Interp.check_wgt(self, local_wgt)
        Interp.check_wgtsum(self, local_wgt)
        # set remap_matrix and remap_matrix_indx objs
        self.remap_matrix.append(local_wgt)
        indx = Interp.indx_recovery(self, indx)
        self.remap_matrix_indx.append(indx)
        continue
      
      # better wish, but disappointed 
      # tackle pole region
      if self.pole_flag:
        if dst_point[1] > self.pole_north_bnd:
          print 'Tackling pole region'
          bilinear_solver = Pole_Solver(dst_point, self.pole_north, 4)
          bilinear_solver.solve()
          bilinear_box_indx = self.pole_north_indx[0:4]
          bilinear_box_indx = Interp.indx_recovery(self, bilinear_box_indx)
          self.remap_matrix.append(bilinear_solver.wgt_lst)
          self.remap_matrix_indx.append(bilinear_box_indx)
          continue
        if dst_point[1] < self.pole_south_bnd:
          print 'Tackling pole region'
          bilinear_solver = Pole_Solver(dst_point, self.pole_south, 4)
          bilinear_solver.solve()
          bilinear_box_indx = self.pole_south_indx[0:4]
          bilinear_box_indx = Interp.indx_recovery(self, bilinear_box_indx)
          self.remap_matrix.append(bilinear_solver.wgt_lst)
          self.remap_matrix_indx.append(bilinear_box_indx)
          continue
        
      # find a bilinear box
      outside_flag, full_flag, bilinear_box_indx, bilinear_box = self.bilinearbox_obj.find_nearest_box(dst_point)
      
      # can not find 4 cells with no masks
      # use idw algorithm instead
      if not full_flag:
        bilinear_solver = Idw_Solver(dst_point, bilinear_box, 1.0e-6, 1)
        bilinear_solver.solve()
        bilinear_box_indx = Interp.indx_recovery(self, bilinear_box_indx)
        Interp.check_wgt(self, bilinear_solver.wgt_lst)
        Interp.check_wgtsum(self, bilinear_solver.wgt_lst)
        self.remap_matrix.append(bilinear_solver.wgt_lst)
        self.remap_matrix_indx.append(bilinear_box_indx)
        continue
        
      predict_flag = False
      # if can not be contained or bounding rect is a triangle
      # deciding a triangle by checking if three of them is collinearion
      if outside_flag or self.check_triangle(bilinear_box):
        predict_flag = True
        print 'predictor case.'
        bilinear_solver = Bilinear_Predictor(dst_point, bilinear_box)
        bilinear_solver.predict() 
        # print debug info in detail
        if outside_flag:
          print 'outside'
          if not is_convex_quadrangle(bilinear_box):
            print 'it is a non-convex quadrangle, so it must be outside.'
          else:
            print 'trully meaning extrapolation.' 
        else:
          print 'it is a bounding triangle box.'
      else:
        print dst_point  
        print bilinear_box
        print 'normal case'
        bilinear_solver = Bilinear_Solver(dst_point, bilinear_box) 
        branch = self.switch(bilinear_box)
        if branch == 1:
          bilinear_solver.solve_bilinear_case1()
        if branch == 2:
          bilinear_solver.solve_bilinear_case2()
        if branch == 3: 
          bilinear_solver.solve_bilinear_case3()

      # transferm ghost bilinear_box_indx to original
      bilinear_box_indx = Interp.indx_recovery(self, bilinear_box_indx)
      
      print dst_point
      print bilinear_box
      print bilinear_box_indx
      print bilinear_solver.wgt_lst
      print ''

      # store result into objs
      #self.interp_wgt = bilinear_solver.wgt_lst
      #self.interp_box_indx = bilinear_box_indx
      #self.interp_box = bilinear_box
      
      if not predict_flag:
        Interp.check_wgt(self, bilinear_solver.wgt_lst) 
        Interp.check_wgtsum(self, bilinear_solver.wgt_lst) 
      # set remap_matrix and remap_matrix_indx objs 
      self.remap_matrix.append(bilinear_solver.wgt_lst)
      self.remap_matrix_indx.append(bilinear_box_indx)
    
    print 'remap_matrix size is:'
    print len(self.remap_matrix)
示例#2
0
  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)