<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write(self, outname, dtype='default', format='ENVI', nodata='default', compress_tif=False, overwrite=False):
 """ write the raster object to a file. Parameters outname: str the file to be written dtype: str the data type of the written file; data type notations of GDAL (e.g. `Float32`) and numpy (e.g. `int8`) are supported. format: str the file format; e.g. 'GTiff' nodata: int or float the nodata value to write to the file compress_tif: bool if the format is GeoTiff, compress the written file? overwrite: bool overwrite an already existing file? Returns ------- """

        
        if os.path.isfile(outname) and not overwrite:
            raise RuntimeError('target file already exists')
        
        if format == 'GTiff' and not re.search(r'\.tif[f]*$', outname):
            outname += '.tif'
        
        dtype = Dtype(self.dtype if dtype == 'default' else dtype).gdalint
        nodata = self.nodata if nodata == 'default' else nodata
        
        options = []
        if format == 'GTiff' and compress_tif:
            options += ['COMPRESS=DEFLATE', 'PREDICTOR=2']
        
        driver = gdal.GetDriverByName(format)
        outDataset = driver.Create(outname, self.cols, self.rows, self.bands, dtype, options)
        driver = None
        outDataset.SetMetadata(self.raster.GetMetadata())
        outDataset.SetGeoTransform([self.geo[x] for x in ['xmin', 'xres', 'rotation_x', 'ymax', 'rotation_y', 'yres']])
        if self.projection is not None:
            outDataset.SetProjection(self.projection)
        for i in range(1, self.bands + 1):
            outband = outDataset.GetRasterBand(i)
            if nodata is not None:
                outband.SetNoDataValue(nodata)
            mat = self.matrix(band=i)
            dtype_mat = str(mat.dtype)
            dtype_ras = Dtype(dtype).numpystr
            if not np.can_cast(dtype_mat, dtype_ras):
                warnings.warn("writing band {}: unsafe casting from type {} to {}".format(i, dtype_mat, dtype_ras))
            outband.WriteArray(mat)
            del mat
            outband.FlushCache()
            outband = None
        if format == 'GTiff':
            outDataset.SetMetadataItem('TIFFTAG_DATETIME', strftime('%Y:%m:%d %H:%M:%S', gmtime()))
        outDataset = None
        if format == 'ENVI':
            hdrfile = os.path.splitext(outname)[0] + '.hdr'
            with HDRobject(hdrfile) as hdr:
                hdr.band_names = self.bandnames
                hdr.write()