def read_mpef(file_name): s = Segment(file_name) if s.file_type == 144: return s else: raise mipp.DecodeError("this is no 'MPEF (type=144)' file: '%s'" % file_name)
def read_gts_message(file_name): s = Segment(file_name) if s.file_type == 1: return s else: raise mipp.DecodeError("this is no 'GTS Message' file: '%s'" % file_name)
def read_imagedata(file_name): s = Segment(file_name) if s.file_type == 0: return ImageSegment(file_name) else: raise mipp.DecodeError("this is no 'image data' file: '%s'" % file_name)
def readline(self, nlines=1): if not self.fp: self.fp = open(self.file_name) read_headers(self.fp) data = self.fp.read(self.bytes_per_line*nlines) if not data: raise mipp.DecodeError("could not read", self.bytes_per_line*nlines, "bytes") return data
def decompress(infile, outdir='.'): """Will decompress an XRIT data file and return the path to the decompressed file. It expect to find Eumetsat's xRITDecompress through the environment variable XRIT_DECOMPRESS_PATH """ from subprocess import Popen, PIPE cmd = os.environ.get('XRIT_DECOMPRESS_PATH', None) if not cmd: raise IOError("XRIT_DECOMPRESS_PATH is not defined" + " (complete path to xRITDecompress)") infile = os.path.abspath(infile) cwd = os.getcwd() os.chdir(outdir) question = ("Did you set the environment variable " + "XRIT_DECOMPRESS_PATH correctly?") if not os.path.exists(cmd): raise IOError(str(cmd) + " does not exist!\n" + question) elif os.path.isdir(cmd): raise IOError(str(cmd) + " is a directory!\n" + question) p = Popen([cmd, infile], stdout=PIPE) stdout = StringIO(p.communicate()[0]) status = p.returncode os.chdir(cwd) outfile = '' for line in stdout: try: k, v = [x.strip() for x in line.split(':', 1)] except ValueError: break if k == 'Decompressed file': outfile = v break if status != 0: raise mipp.DecodeError("xrit_decompress '%s', failed, status=%d" % (infile, status)) if not outfile: raise mipp.DecodeError( "xrit_decompress '%s', failed, no output file is generated" % infile) return outdir + '/' + outfile
def _decode_data_definition(buf): dd = dict() lines = [x.strip() for x in buf.strip().split('\r')] for a in lines: k, v = [x.strip() for x in a.split(':=')] if k[0] == '$': dd[k] = int(v) elif k[0] == '_': dd[k] = v elif k.isdigit(): dd[int(k)] = float(v) else: raise mipp.DecodeError("could not decode data definition: '%s'"%a) return dd
def read_header(fp): hdr_type = rbin.read_uint1(fp.read(1)) if hdr_type != 0: raise mipp.DecodeError("first header has to be a Primary Header, this one is of type %d"%hdr_type) phdr = PrimaryHeader(fp) yield phdr current_size = phdr.rec_len while current_size < phdr.total_hdr_len: hdr_type = rbin.read_uint1(fp.read(1)) cls = header_map.get(hdr_type, None) if cls: hdr = cls(fp) else: hdr = UnknownHeader(hdr_type, fp) yield hdr current_size += hdr.rec_len
def read_epilogue(file_name): s = Segment(file_name) if s.file_type == 129: return s else: raise mipp.DecodeError("this is no 'epilogue' file: '%s'"%file_name)