def chomp(line): """ !!!NOTE!!! This function calls the chomp in strutil. Thus, if you want to use chomp, use the one in there. This remains for backwards compatibility. Removes newline(s) from end of line if present. @param line: A possible corrupted line of code @type line: str @return: Line without any '\n' at the end. @rtype: str """ return strutil.chomp(line)
def checkImageParam(image, logBadlist=False): """ Tries to accomplish the same thing as most IRAF tasks in regards to how they handle the input file parameter. @param image: What is supported: Strings: 1) If the string starts with '@' then it is expected that after the '@' is a filename with the input image filenames in it. 2) Input image filename. List: 1) Must be a list of strings. It is assumed the each string refers an input image filename. @type image: String or List of Strings @param logBadlist: Controls if lists of images that cannot be accessed are written to the log or not. @return: The list of filenames of images to be run. If an error occurs, None is returned. @rtype: list, None """ global log if log==None: log = gemLog.getGeminiLog() root = os.path.dirname(image) imageName = os.path.basename(image) inList = [] if type(imageName) == str and len(imageName) > 0: if imageName[0] == '@': imageName = imageName[1:] try: image = os.path.join( root, imageName ) imageFile = open(image, 'r') readList = imageFile.readlines() # Removes any newline with strutil method 'chomp' for i in range(len(readList)): readList[i] = strutil.chomp(readList[i]) if (readList[i] == '') or (readList[i] == '\n')\ or (readList[i][0] == '#'): continue if os.path.dirname(readList[i]) == '': readList[i] = os.path.join(root, readList[i]) nospace_str = readList[i].replace(' ','') # Adds .fits if there is none inList.append(strutil.appendFits(nospace_str)) imageFile.close() except: log.critical('An error occurred when opening and reading '+ 'from the image '+os.path.basename(image)) return None else: inList.append(image) # append fits? no @@GENERALIZE inList[0] = strutil.appendFits(inList[0]) # Exception for an image of type 'List' elif type(image) == list: for img in image: if type(img) == str: inList.append(img) else: log.warning('Type '+str(type(image))+ ' is not supported. The only supported types are String'+ ' and List of Strings.') return None else: log.warning('Type'+ str(type(image))+ 'is not supported. The only supported types are String '+ 'and List of Strings.') return None outList = [] badList = [] for img in inList: if not os.access(img,os.R_OK): log.error('Cannot read file: '+str(img)) badList.append(img) else: outList.append(img) if badList: if logBadlist: err = "\n\t".join(badList) log.warning("Some files not found or cannot be opened:\n\t"+err) return None return outList
def checkOutputParam(outfile, defaultValue='out.fits'): """ Tries to accomplish the same thing as most IRAF tasks in regards to how they handle the output file parameter. @param outfile: What is supported: Strings: 1) If the string starts with '@' then it is expected that after the '@' is a filename with the output filenames in it. 2) Output file name. List: 1) Must be a list of strings. It is assumed the each string refers to a desired output file name. @type outfile: String or List of Strings @param defaultValue: If the outfile is '', then the defaultValue will returned. @type defaultValue: str @return: A list with all the desired output names. If an error occurs, None is returned. @rtype: list, None """ global log if log==None: log = gemLog.getGeminiLog() root = os.path.dirname(outfile) outfileName = os.path.basename(outfile) outList = [] if type(outfileName) == str: outfile = checkParam(outfile, type(''), defaultValue) if outfileName[0] == '@': outfileName = outfileName[1:] try: outfile = os.path.join( root, outfileName ) outfileFile = open(outfile, 'r') readList = outfileFile.readlines() # Removes any newline with strutil method 'chomp' for i in range(len(readList)): readList[i] = strutil.chomp(readList[i]) if (readList[i] == '') or (readList[i] == '\n')\ or (readList[i][0] == '#'): continue if os.path.dirname(readList[i]) == '': readList[i] = os.path.join(root, readList[i]) # Adds .fits if there is none outList.append(strutil.appendFits(readList[i])) except: log.critical('An error occurred when opening and reading '+ 'from the outfile '+os.path.basename(outfile)) return None finally: outfileFile.close() else: outList.append(outfile) outList[0] = strutil.appendFits(outList[0]) # Exception for an image of type 'List' elif type(outfile) == list: for img in outfile: if type(out) == str: outList.append(out) else: log.warning('Type '+str(type(outfile))+ ' is not supported. The only supported types are String'+ ' and List of Strings.') return None else: log.warning('Type'+ str(type(outfile))+ 'is not supported. The only supported types are String '+ 'and List of Strings.') return None for out in outList: if not os.access(out,os.R_OK): log.error('Cannot read file: '+str(out)) return outList