def unwrap_py(inph, in_p=(), uv=2 * pi): """Return the input matrix unwrapped the value given in uv The same as unwrapv, but using for-s, written in python """ if not is_masked(inph): fasei = MaskedArray(inph, isnan(inph)) else: fasei = inph nx, ny = (fasei.shape[0], fasei.shape[1]) # If the initial unwraping point is not given, take the center of the image # as initial coordinate if in_p == (): in_p = (int(nx / 2), int(ny / 2)) # Create a temporal space to mark if the points are already unwrapped # 0 the point has not been unwrapped # 1 the point has not been unwrapped, but it is in the unwrapping list # 2 the point was already unwrapped fl = zeros((nx, ny)) # List containing the points to unwrap l_un = [in_p] fl[in_p] = 1 # unwrapped values faseo = fasei.copy() while len(l_un) > 0: # remove the first value from the list cx, cy = l_un.pop(0) # Put the coordinates of unwrapped the neigbors in the list # And check for wrapping nv = 0 wv = 0 for i in range(cx - 1, cx + 2): for j in range(cy - 1, cy + 2): if (i > -1) and (i < nx) and (j > -1) and (j < ny): if (fl[i, j] == 0) & (faseo.mask[i, j] == False): fl[i, j] = 1 l_un.append((i, j)) elif fl[i, j] == 2: wv = wv + rint((faseo[i, j] - faseo[cx, cy]) / uv) nv = nv + 1 if nv != 0: wv = wv / nv fl[cx, cy] = 2 faseo[cx, cy] = faseo[cx, cy] + wv * uv return faseo
def unwrap_py(inph,in_p=(), uv=2*pi): """Return the input matrix unwraped the valu given in uv The same as unwrapv, but using for-s, written in python """ if not is_masked(inph): fasei=MaskedArray(inph, isnan(inph)) else: fasei=inph nx, ny=(fasei.shape[0],fasei.shape[1]) # If the initial unwraping point is not given, take the center of the image # as initial coordinate if in_p==(): in_p=(int(nx/2),int(ny/2)) # Create a temporal space to mark if the points are already unwrapped # 0 the point has not been unwrapped # 1 the point has not been unwrapped, but it is in the unwrapping list # 2 the point was already unwrapped fl=zeros((nx, ny)) # List containing the points to unwrap l_un=[in_p] fl[in_p]=1 # unwrapped values faseo=fasei.copy() while len(l_un)>0: # remove the first value from the list cx, cy=l_un.pop(0) # Put the coordinates of unwrapped the neigbors in the list # And check for wrapping nv=0 wv=0 for i in range(cx-1, cx+2): for j in range(cy-1, cy+2): if (i>-1) and (i<nx) and (j>-1) and (j<ny): if (fl[i, j]==0)&(faseo.mask[i, j]==False): fl[i, j]=1 l_un.append((i, j)) elif fl[i, j]==2: wv=wv+rint((faseo[i, j]-faseo[cx, cy])/uv) nv=nv+1 if nv!=0: wv=wv/nv fl[cx, cy]=2 faseo[cx, cy]=faseo[cx, cy]+wv*uv return faseo
def sanitize_masked_array(data: ma.MaskedArray) -> np.ndarray: """ Convert numpy MaskedArray to ensure mask is softened. """ mask = ma.getmaskarray(data) if mask.any(): data, fill_value = maybe_upcast(data, copy=True) data.soften_mask() # set hardmask False if it was True data[mask] = fill_value else: data = data.copy() return data
def createNetCDF4(self,fpath,matrix,dtype, fillvalue = None): if not os.path.exists(os.path.dirname(fpath)): try: os.makedirs(os.path.dirname(fpath)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise rootgrp = Dataset(fpath, "w", format="NETCDF4") # add dimensions lat = rootgrp.createDimension("lat", DileGeometry.YSIZE) lon = rootgrp.createDimension("lon", DileGeometry.XSIZE) # add variables latitudes = rootgrp.createVariable("lat","f4",("lat",)) longitudes = rootgrp.createVariable("lon","f4",("lon",)) latitudes.units = 'degrees_north' latitudes.axis = 'Y' longitudes.units = 'degrees_east' longitudes.axis = 'X' bb = self.asBoundingBox() lats = linspace(bb['lat_min'],bb['lat_max'],self.YSIZE, endpoint=True) lons = linspace(bb['lon_min'],bb['lon_max'],self.XSIZE, endpoint=True) latitudes[:] = lats longitudes[:] = lons if fillvalue: data = rootgrp.createVariable(self.variable,dtype,("lat","lon",),fill_value = fillvalue) else: data = rootgrp.createVariable("data",dtype,("lat","lon",)) if isinstance(matrix, MaskedArray): data[:] = MaskedArray.copy(matrix) else: data[:] = matrix for attr in self.attributes: if attr != '_FillValue': data.setncattr(attr,self.attributes[attr]) rootgrp.close() return fpath
def unwrapv(inph, in_p=(), uv=2 * pi): """Return the input matrix unwrapped the value given in uv This is a vectorized routine, but is not as fast as it should """ if not is_masked(inph): fasei = MaskedArray(inph, isnan(inph)) else: fasei = inph.copy() size = fasei.shape nx, ny = size # If the initial unwraping point is not given, take the center of the image # as initial coordinate if in_p == (): in_p = (int(size[0] / 2), int(size[1] / 2)) # Create a temporal space to mark if the points are already unwrapped # 0 the point has not been unwrapped # 1 the point has not been unwrapped, but it is in the unwrapping list # 2 the point was already unwrapped fl = N.zeros(size) # List containing the points to unwrap l_un = [in_p] fl[in_p] = 1 # unwrapped values faseo = fasei.copy() XI_, YI_ = meshgrid(range(-1, 2), range(-1, 2)) XI_ = XI_.flatten() YI_ = YI_.flatten() while len(l_un) > 0: # remove the first value from the list unp = l_un.pop(0) #l_un[0:1]=[] XI = XI_ + unp[0] YI = YI_ + unp[1] #Remove from the list the values where XI is negative nxi = XI > -1 nyi = YI > -1 nxf = XI < nx nyf = YI < ny n = nonzero(nxi & nyi & nxf & nyf) lco = zip(XI[n], YI[n]) # Put the coordinates of unwrapped the neigbors in the list # And check for wrapping nv = 0 wv = 0 for co in lco: if (fl[co] == 0) & (faseo.mask[co] == False): fl[co] = 1 l_un.append(co) elif fl[co] == 2: wv = wv + rint((faseo[co] - faseo[unp]) / uv) nv = nv + 1 if nv != 0: wv = wv / nv #if wv>=0: wv=int(wv+0.5) #else: wv=int(wv-0.5) fl[unp] = 2 faseo[unp] = faseo[unp] + wv * uv return faseo
def unwrapv(inph,in_p=(), uv=2*pi): """Return the input matrix unwraped the value given in uv This is a vectorized routine, but is not as fast as it should """ if not is_masked(inph): fasei=MaskedArray(inph, isnan(inph)) else: fasei=inph.copy() size=fasei.shape nx, ny=size # If the initial unwraping point is not given, take the center of the image # as initial coordinate if in_p==(): in_p=(int(size[0]/2),int(size[1]/2)) # Create a temporal space to mark if the points are already unwrapped # 0 the point has not been unwrapped # 1 the point has not been unwrapped, but it is in the unwrapping list # 2 the point was already unwrapped fl=N.zeros(size) # List containing the points to unwrap l_un=[in_p] fl[in_p]=1 # unwrapped values faseo=fasei.copy() XI_, YI_= meshgrid(range(-1, 2), range(-1, 2)) XI_=XI_.flatten() YI_=YI_.flatten() while len(l_un)>0: # remove the first value from the list unp=l_un.pop(0) #l_un[0:1]=[] XI=XI_+unp[0] YI=YI_+unp[1] #Remove from the list the values where XI is negative nxi=XI>-1 nyi=YI>-1 nxf=XI<nx nyf=YI<ny n=nonzero(nxi& nyi & nxf & nyf) lco=zip(XI[n], YI[n]) # Put the coordinates of unwrapped the neigbors in the list # And check for wrapping nv=0 wv=0 for co in lco: if (fl[co]==0) & (faseo.mask[co]==False): fl[co]=1 l_un.append(co) elif fl[co]==2: wv=wv+rint((faseo[co]-faseo[unp])/uv) nv=nv+1 if nv!=0: wv=wv/nv #if wv>=0: wv=int(wv+0.5) #else: wv=int(wv-0.5) fl[unp]=2 faseo[unp]=faseo[unp]+wv*uv return faseo