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
def _numMatches(parseTree, data, name='a'): _calls = [] def _cb(): _calls.append(True) i = Interp(parseTree, _cb) try: i.receive(data) except ParseError as e: return 0 return len(_calls)
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
class many(object): def __init__(self, interp, rule): rule.success = [self._store, setRule(node=rule)] self.interp = Interp(rule) self.gathered = [] @property def need(self): return self.interp.current.need def receive(self, data, previous): try: self.interp.receive(data) except ParseError: return self.interp._ix, self.gathered return None, None def _store(self, interp, rv): self.gathered.append(rv)
def remap_entry(src_file, dst_file, alg_name, real_data_file, offline_flag, pole_flag, rank, size, comm, neighbor_num = 4): name = str(alg_name) name = name[name.index('.') + 1 : name.index('>') - 1] if offline_flag: if name == 'Idw': test_obj = alg_name(src_file, dst_file, False, real_data_file, neighbor_num, pole_flag) else: test_obj = alg_name(src_file, dst_file, False, real_data_file, pole_flag) else: if name == 'Idw': test_obj = alg_name(src_file, dst_file, False, real_data_file, neighbor_num, pole_flag) else: test_obj = alg_name(src_file, dst_file, True, real_data_file, pole_flag) test_obj.dst_distribute(rank, size) test_obj.interp() test_obj.dst_merge(rank, comm) if rank == 0: Interp.compact_remap_matrix(test_obj) if offline_flag: test_obj.gen_remap_matrix_file() else: #test_obj.gen_remap_matrix_file() remap_result = test_obj.remap() print remap_result
def remap(self): remap_result = Interp.remap(self) return remap_result
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 __init__(self, src_grid_file_name, dst_grid_file_name, online_flag, realdata_file_name): Interp.__init__(self, src_grid_file_name, dst_grid_file_name, online_flag, realdata_file_name) self.bilinearbox_obj = Bilinearbox(self.stree_base_obj, self.stree)
#test_obj = Bilinear('../../../grid/T42_Gaussian_POP43/T42_Gaussian.nc', '../../../grid/More/ne30np4-t2.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0007-08.nc', False) #test_obj = Bilinear('../../../grid/T42_Gaussian_POP43/T42_Gaussian.nc', '../../../grid/More/Ocean_1v1_triplepole.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0007-08.nc', False) >>>>>>> independent #test_obj = Bilinear('../../../grid/More/Ocean_1v1_triplepole.nc', '../../../grid/T42_Gaussian_POP43/T42_Gaussian.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0007-08.nc', False) #test_obj = Bilinear('../../../grid/T42_Gaussian_POP43/T42_Gaussian.nc', '../../../grid/More/Ocean_1v1_triplepole.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0007-08.nc', False) #test_obj = Bilinear('../../../grid/More/atmos_fv_0.9v1.25_for_SCRIP.nc', '../../../grid/T42_Gaussian_POP43/POP43.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0007-08.nc', False) test_obj = Bilinear('../../../grid/realdataT42_masked.nc', '../../../grid/realdataPOP43.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0007-08.nc', False) #test_obj = Bilinear('../../../grid/T42_Gaussian.nc', '../../../grid/Gamil_128x60_Grid.nc', False, '../../../data/real/T42_Gaussian_Grid/T42_avXa2c_a_Faxa_lwdn-0006-12.nc', False) #test_obj = Bilinear('../../../grid/T42_Gaussian_POP43/POP43.nc', '../../../grid/T42_Gaussian_POP43/T42_Gaussian.nc') print test_obj.dst_grid_size print test_obj.dst_grid_dims print test_obj.dst_grid_corners print test_obj.dst_grid_rank print len(test_obj.dst_grid_center_lat) # for mpi use test_obj.dst_distribute(rank, size) test_obj.interp() # for mpi use test_obj.dst_merge(rank, comm) if rank == 0: # compact remap matrix, gen remap_src_indx and remap_dst_indx print 'Compacting remap matrix...' Interp.compact_remap_matrix(test_obj) print 'Compacting finished!' test_obj.gen_remap_matrix_file() start = time.time() remap_result = test_obj.remap() end = time.time() print end - start print remap_result
def create_interp(): return Interp()
def __init__(self, interp, rule): rule.success = [self._store, setRule(node=rule)] self.interp = Interp(rule) self.gathered = []
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)
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 create_interp(self): return Interp()