DTU10MW pile processing by perl

  1. 创建替换字符串pattern
  2. 解析pattern得到修改后的内容
  3. 写入文件
    #!/usr/bin/env perl
    #===============================================================================
    #
    #         FILE: a.pl
    #
    #        USAGE: ./a.pl
    #
    #  DESCRIPTION:
    #
    #      OPTIONS: ---
    # REQUIREMENTS: ---
    #         BUGS: ---
    #        NOTES: ---
    #       AUTHOR: Ye Zhaoliang (Ye Zhaoliang), yezhaoliang@ncepu.edu.cn
    # ORGANIZATION:
    #      VERSION: 1.0
    #      CREATED: 2019-8-17 18:19:59
    #     REVISION: ---
    #===============================================================================
    
    use strict;
    use warnings;
    use utf8;
    
    use File::Spec;    ## catfile
    use File::Copy;    ## move
    my $ControlData_file_name = './DTUPile/controlerPrebend.csv';  # input file name
    my @records;
    my @cols = ( 'velocity', 'pitch', 'rpm' );
    
    # my $winddirname='./10MWRWT/';
    # my $windfilename="DTU_10MW_InflowWind.dat";
    # my  $windcurrentfilename=File::Spec->catfile($winddirname,$windfilename);
    my $windcurrentfilename = './10MWRWT/DTU_10MW_InflowWind.dat';
    
    # my $elastodirname='./10MWRWT/';
    # my $elastofilename="DTU_10MW_RWT_ElastoDyn.dat";
    # my  $elastocurrentfilename=File::Spec->catfile($elastodirname,$elastofilename);
    my $elastocurrentfilename = './10MWRWT/DTU_10MW_RWT_ElastoDyn.dat';
    
    open my $ControlData, '<', $ControlData_file_name
        or die "$0 : failed to open  input file '$ControlData_file_name' : $!\n";
    
    while (<$ControlData>) {
        chomp;
    
        my %rec;
        @rec{@cols} = split /,/;
        push @records, \%rec;
    }
    
    foreach my $case (@records) {
        print "$.  $case->{velocity}   $case->{pitch}  $case->{rpm} \n";
        handle( $windcurrentfilename,   "$case->{velocity}", "HWindSpeed" );
        handle( $elastocurrentfilename, "$case->{pitch}",    "BlPitch" );
        handle( $elastocurrentfilename, "$case->{rpm}",      "RotSpeed" );
    
        # system 'pause';
        my $fast = "FAST_x64.exe";
    
        # my  $configueFile="Test01.fst";
        my $special = "DTU_10MW_RWT";
    
        my $configueFile = "./$special.fst";
    
        system("$fast $configueFile");
    
        my $foldOutput = "Output";
        if ( -e $foldOutput ) {
            print "Existed $foldOutput already\n";
        }
        else {
            mkdir("Output");
        }
    
        my $newPrefix    = "$case->{velocity}-$case->{pitch}-$case->{rpm}.sum";
        my $newoutPrefix = "$case->{velocity}-$case->{pitch}-$case->{rpm}.out";
    
        move( "./$special.AD.sum", "Output/$special.AD.sum$newPrefix" );
        move( "./$special.ED.sum", "Output/$special.ED.sum$newPrefix" );
        move( "./$special.sum",    "Output/$special.sum$newPrefix" );
        move( "./$special.out",    "Output/$special.out$newoutPrefix" );
    
    }
    
    close $ControlData
        or warn "$0 : failed to close input file '$ControlData_file_name' : $!\n";
    
    # Perl call system commands
    # my  $fast="FAST_x64.exe";
    # my  $configueFile="Test01.fst";
    # system("$fast $configueFile");
    #
    #
    #handle( "Test03.fst", "15", "CompAero", "CompServo" );
    
    #pause("")
    #system 'pause';
    
    ## 1. find the pattern 1.1 if not print, 1.2 if yes ,modify
    ## 2. rename
    ## 3. delete temp files
    sub handle {
        my ( $filename, $newData, @words ) = @_;
        my $INPUTSource_file_name = $filename;    # input file name
    
        #print "$filename\n";
        print "$newData\n";
        print "@words\n";
        open my $INPUTSource, '<', $INPUTSource_file_name
            or die "$0 : failed to open  input file '$INPUTSource_file_name' : $!\n";
    
        my $TEMP_file_name = 'temp.md';           # output file name
    
        open my $TEMP, '>', $TEMP_file_name
            or die "$0 : failed to open  output file '$TEMP_file_name' : $!\n";
    
        my $pattern;
    
        ###  Use macro thinking to create expression, then evaluate it
        # if ( @_ == 1 ) {
        #     $pattern = "/$words[0]/";
        # }
        # else {
        #$pattern = "/($words[0])/ || /($words[1])/";
        #$pattern = '$line=~';
        $pattern .= ' /[^"]\b'.$_.'\b[^"]/ ||'
            foreach @words;    ## concat every parameters!!
        #print "Before: $pattern\n";
        $pattern = substr( $pattern, 0, length($pattern) - 2 );
    
        #    }
        #print "Ye: $pattern\n";
    
        #system 'pause';
    
        while (<$INPUTSource>) {
    
            #@temp = split /\s+/;
    
            my $line = $_;
            ##  evaluate it , then get the result
            #if ( eval{$_~~$pattern} ) {
            if ( eval($pattern) ) {
    
                my $currentWord;
    
                #print "Ye: $line\n";
                # Get the current Matched word, for indexing, so you can cut the string behind.
                foreach my $tempWord (@words) {
    
                    $currentWord = $tempWord if $line =~ /$tempWord/;
    
                }
    
                # print "YE2:  $_ :: $currentWord\n";
                # print "\n"  split /\s+/;
                my $position = index( $_, $currentWord );
    
                #print "CurrentPosition is : $position\n";
    
                my $format = sprintf( "%ds", $position - 4 );
    
                #print "Ye3: $format\n";
                my $newContent;
                $newContent = sprintf( "%$format    ", $newData );
                print $TEMP $newContent
                    . substr( $_, $position, length($_) - $position );
            }
            else {
                print $TEMP $_;
            }
    
        }
        close $INPUTSource
            or warn "$0 : failed to close input file '$INPUTSource_file_name' : $!\n";
        close $TEMP
            or warn "$0 : failed to close output file '$TEMP_file_name' : $!\n";
    
        rename $TEMP_file_name, $INPUTSource_file_name;    # Rename it
        unlink $TEMP_file_name;                            ## delete tempFile
        return;
    }    ## --- end sub handle
Related
叶昭良
叶昭良
Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.