<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def wr_header_file(self, write_fields, write_dir):
 """ Write a header file using the specified fields """

        # Create record specification line
        record_line = ''
        # Traverse the ordered dictionary
        for field in RECORD_SPECS.index:
            # If the field is being used, add it with its delimiter
            if field in write_fields:
                record_line += RECORD_SPECS.loc[field, 'delimiter'] + str(getattr(self, field))

        header_lines = [record_line]

        # Create segment specification lines
        segment_lines = self.n_seg * ['']
        # For both fields, add each of its elements with the delimiter
        # to the appropriate line
        for field in SEGMENT_SPECS.index:
            for seg_num in range(self.n_seg):
                segment_lines[seg_num] += SEGMENT_SPECS.loc[field, 'delimiter'] + str(getattr(self, field)[seg_num])

        header_lines = header_lines + segment_lines

        # Create comment lines (if any)
        if 'comments' in write_fields:
            comment_lines = ['# '+ comment for comment in self.comments]
            header_lines += comment_lines

        lines_to_file(self.record_name + '.hea', header_lines, write_dir)