def readCullFile(self, cullFile): ''' Read a cull file. For an original cvFile, a culling process was applied and the results saved in the cullFile. Parameters ---------- cullFile : str File name for cull file.The file contains a Returns ------- cullPoints: nparray Indices of culled points. ''' with open(cullFile, 'r') as fp: myParams = eval(fp.readline()) print(myParams["cvFile"]) print(self.cvFile) cullPoints = [] for line in fp: cullPoints.append(int(line)) if len(cullPoints) != myParams["nBad"]: myError(f'reading culled points expected {myParams["nBad"]}' ' but only found {len(cullPoints) }') # return np.array(cullPoints)
def readGeodatFromTiff(self, tiffFile): ''' Read geo information (x0,y0,sx,sy,dx,dy) from a tiff file. Assumes PS coordinates. Parameters ---------- tiffFile : str Name of tiff file. Returns ------- None. ''' if not os.path.exists(tiffFile): myError(f"readGeodatFromTiff: {tiffFile} does not exist") try: gdal.AllRegister() ds = gdal.Open(tiffFile) proj = osr.SpatialReference(wkt=ds.GetProjection()) self.epsg = int(proj.GetAttrValue('AUTHORITY', 1)) self.sx, self.sy = ds.RasterXSize, ds.RasterYSize gt = ds.GetGeoTransform() self.dx, self.dy = abs(gt[1]), abs(gt[5]) # lower left corner corrected to pixel centered values self.x0 = (gt[0] + self.dx/2) # y-coord check direction if gt[5] < 0: self.y0 = (gt[3] - self.sy * self.dy + self.dy/2) else: self.y0 = (gt[3] + self.dy/2) self.xyCoordinates() except Exception: myError(f"readGeodatFromTiff: {tiffFile} exists but cannot parse")
def checkCVFile(self): ''' Check cvfile exists. Parameters ---------- cvFile : str Name of cv file with cal/val points. Returns ------- None. ''' if self.cvFile is None: myError("No cvfile specified") if not os.path.exists(self.cvFile): myError("cvFile: {0:s} does not exist".format(self.cvFile))
def xyCoordinates(self): ''' Compute xy coordinates for image(y,x). Save as 1-D arrays: self.xx, self.yy. Returns ------- None. ''' # check specs exist if None in (self.x0, self.y0, self.sx, self.sy, self.dx, self.dy): myError('nisarVel.xyCoordinates: x0,y0,sx,sy,dx,dy undefined ' f'{self.x0},{self.y0},{self.sx},{self.sy},' f'{self.dx},{self.dy}') # x0...x0+(sx-1)*dx, y0... self.xx = np.arange(self.x0, self.x0 + int(self.sx) * self.dx, self.dx) self.yy = np.arange(self.y0, self.y0 + int(self.sy) * self.dy, self.dy)
def setSRS(self): ''' Set xy SRS proj based on wktFile or epsg,if neither defaults to northern or southern lat of the cv points. Returns ------- None. ''' if len(self.lat) <= 0: myError("Cannot set epsg without valid latlon") if self.wktFile is not None: wkt = self.readWKT(self.wktFile) self.xyproj = wkt else: if self.epsg is None: self.epsg = [3031, 3413][self.lat[0] > 0] self.xyproj = f"EPSG:{self.epsg}" self.lltoxyXform = pyproj.Transformer.from_crs(self.llproj, self.xyproj)
def getDomain(self, epsg): ''' Return names of domain name based on epsg (antartica or greenland). Parameters ---------- epsg : int epsg code. Returns ------- domain : str For now either 'greenland' or 'antarctica'. ''' if epsg is None or epsg == 3413: domain = 'greenland' elif epsg == 3031: domain = 'antarctica' else: myError('Unexpected epsg code: '+str(epsg)) return domain
def lltoxym(self, lat, lon): ''' Convert lat lon (deg) to x y (m) Parameters ---------- lat : nparray Latitude nparray. lon : nparray Longitude nparray. Returns ------- x : nparray x coordinate in m. y : nparray y coordinate in m.. ''' if self.xyproj is not None: x, y = self.lltoxyXform.transform(lat, lon) return x, y else: myError("lltoxy: proj not defined")
def __init__(self, cvFile=None, epsg=None, wktFile=None): ''' Initialize an object to read a cvfile (cal/val points file) and manipulate cv points. Parameters ---------- cvFile : TYPE, optional DESCRIPTION. The default is None. epsg : TYPE, optional DESCRIPTION. The default is None. wktFile : TYPE, optional DESCRIPTION. Wktfile instead of epsg None. Returns ------- None. ''' # # set everything to zero as default self.cvFile = None # File with cal/val points self.lat, self.lon = np.array([]), np.array([]) # lat/lon of points self.x, self.y = np.array([]), np.array([]) # x/y PS of points in m self.z = np.array([]) # Elevatin of points self.nocull = None # Used to mark points to cull (by default all 1) self.pound2 = False # Flag to write pound 2 to output (historical) self.header = [] # In dictionary used as header - needs some work self.vx, self.vy, self.vz = np.array([]), np.array([]), np.array([]) self.weight = np.array([]) # self.setCVFile(cvFile) if wktFile is not None and epsg is not None: myError('Specify epsg or wktfile but not both') self.epsg = epsg # Epsg set 3413 or 3031 depending on if N or S self.wktFile = wktFile self.llproj = "EPSG:4326" # Used for conversions self.xyproj = None # Defined once epsg set. if cvFile is not None: self.readCVs(cvFile=cvFile)
def computePixEdgeCornersXYM(self): ''' Return dictionary with corner locations. Note unlike pixel centered xx, x0 etc values, these corners are defined as the outer edges as in a geotiff Returns ------- corners : dict corners in xy coordinates: {'ll': {'x': xll, 'y': yll}...}. ''' nx, ny = self.sizeInPixels() x0, y0 = self.originInM() dx, dy = self.pixSizeInM() # Make sure geometry defined if None in [nx, ny, x0, y0, dx, dy]: myError(f'Geometry param not defined size {nx,ny}' ', origin {x0,y0}), or pix size {dx,dy}') xll, yll = x0 - dx/2, y0 - dx/2 xur, yur = xll + nx * dx, yll + ny * dy xul, yul = xll, yur xlr, ylr = xur, yll corners = {'ll': {'x': xll, 'y': yll}, 'lr': {'x': xlr, 'y': ylr}, 'ur': {'x': xur, 'y': yur}, 'ul': {'x': xul, 'y': yul}} return corners