<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def generate_headerlet(outwcs,template,wcsname,outname=None):
 """ Create a headerlet based on the updated HSTWCS object This function uses 'template' as the basis for the headerlet. This file can either be the original wcspars['refimage'] or wcspars['coeffsfile'], in this order of preference. If 'template' is None, then a simple Headerlet will be generated with a single SIPWCS extension and no distortion """

    # Create header object from HSTWCS object
    siphdr = True
    if outwcs.sip is None:
        siphdr = False
    outwcs_hdr = outwcs.wcs2header(sip2hdr=siphdr)
    outwcs_hdr['NPIX1'] = outwcs.pixel_shape[0]
    outwcs_hdr['NPIX2'] = outwcs.pixel_shape[1]

    # create headerlet object in memory; either from a file or from scratch
    if template is not None and siphdr:
        print('Creating headerlet from template...')
        fname,extn = fileutil.parseFilename(template)
        extnum = fileutil.parseExtn(extn)
        extname = ('sipwcs',extnum[1])
        hdrlet = headerlet.createHeaderlet(fname,wcsname)
        # update hdrlet with header values from outwcs
        for kw in outwcs_hdr.items():
            hdrlet[extname].header[kw[0]] = kw[1]
        hdrlet[extname].header['WCSNAME'] = wcsname
    else:
        print('Creating headerlet from scratch...')
        hdrlet = fits.HDUList()
        hdrlet.append(fits.PrimaryHDU())
        siphdr = fits.ImageHDU(header=outwcs_hdr)
        siphdr.header['EXTNAME'] = 'SIPWCS'
        siphdr.header['WCSNAME'] = wcsname
        hdrlet.append(siphdr)

    # Write out header to a file as the final product
    if outname is not None:
        if outname.find('_hdr.fits') < 0:
            outname += '_hdr.fits'
        if os.path.exists(outname):
            print('Overwrite existing file "%s"'%outname)
            os.remove(outname)
        hdrlet.writeto(outname)
        print('Wrote out headerlet :',outname)