class CoordTransfer(Exception): def __init__(self, srcfile, dstfile, newfile): self.srcfile = srcfile self.dstfile = dstfile self.newfile = newfile def loadsrcoords(self): self.ncfile = Dataset(self.srcfile, 'r') variable_name = 'grid_center_lat' __grid_center_lat = self.ncfile.variables[variable_name][:] variable_name = 'grid_center_lon' __grid_center_lon = self.ncfile.variables[variable_name][:] self.__grid_center_lat = __grid_center_lat.tolist() self.__grid_center_lon = __grid_center_lon.tolist() def loadstinfo(self): self.nc_obj = Loadnc(self.dstfile) self.grid_size, self.grid_corners, self.grid_rank, self.grid_dims, ach1, ach2, self.grid_imask = self.nc_obj.load( ) def transfercoord(self): self.resncfile = Dataset(self.newfile, 'w') tm = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) # set dimension info self.resncfile.createDimension('grid_size', self.grid_size) self.resncfile.createDimension('grid_rank', self.grid_rank) self.resncfile.createDimension('grid_corners', self.grid_corners) # set variable info grid_dims_var = self.resncfile.createVariable('grid_dims', dtype('int32').char, ('grid_rank', )) grid_center_lat_var = self.resncfile.createVariable( 'grid_center_lat', dtype('d').char, ('grid_size', )) grid_center_lat_var.units = 'degrees' grid_center_lon_var = self.resncfile.createVariable( 'grid_center_lon', dtype('d').char, ('grid_size', )) grid_center_lon_var.units = 'degrees' grid_imask_var = self.resncfile.createVariable('grid_imask', dtype('i').char, ('grid_size', )) grid_imask_var.units = 'unitless' grid_dims_var[:] = self.grid_dims grid_center_lat_var[:] = np.array(self.__grid_center_lat) grid_center_lon_var[:] = np.array(self.__grid_center_lon) buffer1 = [np.int32(i) for i in self.grid_imask] grid_imask_var[:] = np.array(buffer1) setattr(self.resncfile, 'title', 'Threp ' + self.newfile) setattr(self.resncfile, 'createdate', tm) setattr(self.resncfile, 'conventions', 'Threp') setattr(self.resncfile, 'grid', self.newfile) def finish(self): self.resncfile.close() self.nc_obj.closenc()
class CoordTransfer(Exception): def __init__(self, srcfile, dstfile, newfile): self.srcfile = srcfile self.dstfile = dstfile self.newfile = newfile def loadsrcoords(self): self.ncfile = Dataset(self.srcfile, 'r') variable_name = 'grid_center_lat' __grid_center_lat = self.ncfile.variables[variable_name][:] variable_name = 'grid_center_lon' __grid_center_lon = self.ncfile.variables[variable_name][:] self.__grid_center_lat = __grid_center_lat.tolist() self.__grid_center_lon = __grid_center_lon.tolist() def loadstinfo(self): self.nc_obj = Loadnc(self.dstfile) self.grid_size, self.grid_corners, self.grid_rank, self.grid_dims, ach1, ach2, self.grid_imask = self.nc_obj.load() def transfercoord(self): self.resncfile = Dataset(self.newfile, 'w') tm = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # set dimension info self.resncfile.createDimension('grid_size', self.grid_size) self.resncfile.createDimension('grid_rank', self.grid_rank) self.resncfile.createDimension('grid_corners', self.grid_corners) # set variable info grid_dims_var = self.resncfile.createVariable('grid_dims', dtype('int32').char, ('grid_rank',)) grid_center_lat_var = self.resncfile.createVariable('grid_center_lat', dtype('d').char, ('grid_size',)) grid_center_lat_var.units = 'degrees' grid_center_lon_var = self.resncfile.createVariable('grid_center_lon', dtype('d').char, ('grid_size',)) grid_center_lon_var.units = 'degrees' grid_imask_var = self.resncfile.createVariable('grid_imask', dtype('i').char, ('grid_size',)) grid_imask_var.units = 'unitless' grid_dims_var[:] = self.grid_dims grid_center_lat_var[:] = np.array(self.__grid_center_lat) grid_center_lon_var[:] = np.array(self.__grid_center_lon) buffer1 = [np.int32(i) for i in self.grid_imask] grid_imask_var[:] = np.array(buffer1) setattr(self.resncfile, 'title', 'Threp ' + self.newfile) setattr(self.resncfile, 'createdate', tm) setattr(self.resncfile, 'conventions', 'Threp') setattr(self.resncfile, 'grid', self.newfile) def finish(self): self.resncfile.close() self.nc_obj.closenc()
def __init__(self, src_grid_file_name, dst_grid_file_name, online_flag, src_realdata_file_name): src_nc_obj = Loadnc(src_grid_file_name) self.src_grid_size, self.src_grid_corners, self.src_grid_rank, self.src_grid_dims, self.src_grid_center_lat, self.src_grid_center_lon, self.src_grid_imask = src_nc_obj.load() src_nc_obj.closenc() # set south pole pnts pole_num = 0 self.pole_south = [] self.pole_south_indx = [] for i in xrange(len(self.src_grid_center_lat)): if self.src_grid_imask[i] == 1: self.pole_south.append((self.src_grid_center_lon[i], self.src_grid_center_lat[i])) self.pole_south_indx.append(i) pole_num += 1 if pole_num == 10: break self.pole_south_bnd = min([item[1] for item in self.pole_south]) # set north pole pnts pole_num = 0 self.pole_north = [] self.pole_north_indx = [] j = len(self.src_grid_center_lat) #while True: while 1: j -= 1 if self.src_grid_imask[j] == 1: self.pole_north.append((self.src_grid_center_lon[j], self.src_grid_center_lat[j])) self.pole_north_indx.append(j) pole_num += 1 if pole_num == 10: break self.pole_north_bnd = max([item[1] for item in self.pole_north]) # original grid info # used for remap matrix file self.original_src_grid_center_lat = copy.deepcopy(self.src_grid_center_lat) self.original_src_grid_center_lon = copy.deepcopy(self.src_grid_center_lon) self.original_src_grid_imask = copy.deepcopy(self.src_grid_imask) dst_nc_obj = Loadnc(dst_grid_file_name) self.dst_grid_size, self.dst_grid_corners, self.dst_grid_rank, self.dst_grid_dims, self.dst_grid_center_lat, self.dst_grid_center_lon, self.dst_grid_imask = dst_nc_obj.load() dst_nc_obj.closenc() self.stree_base_obj = Build(self.src_grid_size, self.src_grid_corners, self.src_grid_rank, self.src_grid_dims, self.src_grid_center_lat, self.src_grid_center_lon, self.src_grid_imask) self.recovery_indx_table, self.stree = self.stree_base_obj.grow() self.src_grid_name = src_grid_file_name.split('/')[-1].split('.')[0] self.dst_grid_name = dst_grid_file_name.split('/')[-1].split('.')[0] #self.interp_wgt = [] #self.interp_box_indx = [] #self.interp_box = [] self.remap_matrix = [] self.remap_matrix_indx = [] self.remap_matrix_compact = [] self.remap_src_indx = [] self.remap_dst_indx = [] # load real data if online remapping # self.src_data = [] if online_flag: src_data_nc_obj = Loadreal(src_realdata_file_name) size, self.src_data = src_data_nc_obj.load() if size != self.src_grid_size: print 'Real data size does not match grid size.' sys.exit() src_data_nc_obj.closenc() self.dst_data = []