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
def interp(self): #print self.dst_grid_center_lon[0] #print self.dst_grid_center_lat[0] #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 # 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() bilinear_solver = Idw_Solver(dst_point, bilinear_box, 1.0e-6, 1) bilinear_solver.solve() # 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) # compact remap matrix, gen remap_src_indx and remap_dst_indx print 'Compacting remap matrix...' Interp.compact_remap_matrix(self) print 'Compacting finished!'
def interp(self): #print self.dst_grid_center_lon[0] #print self.dst_grid_center_lat[0] #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 # 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() bilinear_solver = Idw_Solver(dst_point, bilinear_box, 1.0e-6, 1) bilinear_solver.solve() # 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) # compact remap matrix, gen remap_src_indx and remap_dst_indx print 'Compacting remap matrix...' Interp.compact_remap_matrix(self) print 'Compacting finished!'