| 1. |
Solve : Command Prompt - add date? |
|
Answer» Hello all, Hello all,dude, you can do the date calculation in your Perl script. Code: [Select]$nowdate= strftime "%Y%m%d", localtime; add that into your perl script, and you don't need to use DOS to get the date.Quote from: ghostdog74 on June 23, 2007, 12:53:55 AM Quote from: gucci_406 on June 22, 2007, 02:01:55 PMHello all,dude, you can do the date calculation in your Perl script. Hi Mate, I am not really able to write in perl, the script was writtern by some one else, so i dont really know where to place it. I have tied a couple times, but get errors. Would it be possible for you to have a look. I have inculded the script below. #!/usr/bin/perl -w # w. ebisuzaki CPC/NCEP/NWS/NOAA 10/2006 # # simple script to download gfs files # inspired by Dan Swank's get-narr.pl script # this script uses the tecnique described in # http://www.cpc.ncep.noaa.gov/products/wesley/fast_downloading_grib.html # # ARGUMENTS: action YYYYMMDDHH HR0 HR1 DHR VAR_LIST LEV_LIST DIRECTORY # # action = inv (display inventory of 1st file) # data (get data) # # HR0, HR1, DHR: forecast hour parameters # do f_hour = HR0, HR1, DHR (fortran) # for (f_hour = HR1; f_hour <= HR1; f_hour = f_hour + DHR) (C) # # VAR_LIST: list of variable separated by colons, blanks replaced by underscore # ex. HGT:TMP:OZONE # LEV_LIST: list of levesl separated by colons, blanks replaced by underscore # ex. 500_mb:sfc # # DIRECTORY: name of the directory in which to place the files # # v1.0 10/2006 who is going to find the first bug? # v1.0a 10/2006 better help page # v2.0beta 10/2006 no need for egrep, get_inv.pl and get_grib.pl # v2.0beta2 10/2006 no need for egrep, get_inv.pl and get_grib.pl # v2.0beta3 10/2006 update messages, ignore case on matches #------------ user customization section ----------------------------------------- # location of curl $curl="curl"; # the URLs of the inventory and grib must be defined by $URL$inv and $URL$grb # symbolic variables supported YYYY MM DD HH FHR (forecast hour) # # grib2 files from www.ftp.ncep.noaa.gov $URL='http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.$nowdate= strftime "%Y%m%d", $HH/gfs.t${HH}z.pgrb2f${FHR}'; # # grib1 files from www.ftp.ncep.noaa.gov $URL='http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.$YYYY$MM$DD$HH/gfs.t${HH}z.pgrbf${FHR}'; # $inv='.idx'; $grb=''; # $windows='no'; $windows='yes'; #------------- guts of script --------------------------------------------------- $version="2.0beta2"; if ($#ARGV != 7) { print "get_gfs.pl $version\n"; print "\nget_gfs.pl action YYYYMMDDHH HR0 HR1 DHR VAR_LIST LEV_LIST DIRECTORY\n\n"; print " action = inv (display inventory of first file)\n"; print " data (get data)\n"; print " HR0, HR1, DHR: forecast hour parameters\n"; print " do f_hour = HR0, HR1, DHR\n"; print " VAR_LIST: list of variable separated by colons, blanks replaced by underscore or all\n"; print " ex. HGT:TMP:OZONE, all\n"; print " LEV_LIST: list of levels separated by colons, blanks replaced by underscore or all\n"; print " ex. 500_mb:sfc, all\n"; print " DIRECTORY: name of the directory in which to place the files\n"; print "\n EXAMPLES\n\n Reading an inventory\n\n"; print " get_gfs.pl inv 2006101800 0 0 0 all all .\n\n"; print " Downloading 500 hPa Height and Temp 0/3/6 hours forecasts to current directory\n\n"; print " get_gfs.pl data 2006101800 0 6 3 HGT:TMP 500_mb .\n\n"; print " Downloading PRATE 12/18 hours forecasts to current directory\n\n"; print " get_gfs.pl data 2006101800 12 18 6 PRATE all .\n\n"; print " Of course the date code will have to be current.\n"; exit(; } $action = $ARGV[0]; $time = $ARGV[1]; $hr0=$ARGV[2]; $hr1=$ARGV[3]; $dhr=$ARGV[4]; $VARS=$ARGV[5]; $LEVS=$ARGV[6]; $OUTDIR = $ARGV[7]; $YYYY = substr($time,0,4); $MM = substr($time,4,2); $DD = substr($time,6,2); $HH = substr($time,8,2); # check values if ($action ne 'data' && $action ne 'inv') { print "action must be inv or data, not $action\n"; exit(; } if ($YYYY < 2006 || $YYYY > 2012) { print "bad date (year) code $time\n"; exit(; } if ($MM < 1 || $MM > 12) { print "bad date (month) code $time\n"; exit(; } if ($DD < 1 || $DD > 31) { print "bad date (day) code $time\n"; exit(; } if ($HH < 0 || $HH > 23) { print "bad date (hour) code $time\n"; exit(; } if ($hr0 == $hr1) { $dhr = 3; } if ($dhr != 3 && $dhr != 6 && $dhr != 12 && $dhr != 24) { print "dhr must be 3, 6, 12 or 24, not $dhr\n"; exit(; } if ($hr0 > $hr1) { print "hr0 needs to be <= hr1\n"; exit(; } if ($dhr <= 0) { print "dhr needs to be > 0\n"; exit(; } if (! -d $OUTDIR) { print "Directory $OUTDIR does not exist\n"; exit(; } $VARS =~ tr/:_/| /; if( $VARS =~ m/ALL/ig ) { $VARS = "."; } else { $VARS = "$VARS):"; } $LEVS =~ tr/:_/| /; if( $LEVS =~ m/ALL/ig ) { $LEVS = "."; } else { $LEVS = "$LEVS)" ; } $URL =~ s/\$YYYY/$YYYY/g; $URL =~ s/\${YYYY}/$YYYY/g; $URL =~ s/\$MM/$MM/g; $URL =~ s/\${MM}/$MM/g; $URL =~ s/\$DD/$DD/g; $URL =~ s/\${DD}/$DD/g; $URL =~ s/\$HH/$HH/g; $URL =~ s/\${HH}/$HH/g; $output = ''; $fhr=$hr0; while ($fhr <= $hr1) { if ($fhr <= 9) { $fhr="0$fhr"; } $url = $URL; $url =~ s/\$FHR/$fhr/g; $url =~ s/\${FHR}/$fhr/g; $file = $url; $file =~ s/^.*\///; # # read the inventory # $line[] = wgrib inventory, $start[] = start of record (column two of $line[]) # if ($windows eq 'yes') { $err = system("$curl -f -s $url$inv -o $OUTDIR/$file.tmp"); $err = $err >> 8; if ($err) { print STDERR "error code=$err, problem reading $url$inv\n"; sleep(10); exit(; } open (In, "$OUTDIR/$file.tmp"); } else { open (In, "$curl -f -s $url$inv |"); } $n=0; while () { chomp; $line[$n] = $_; s/^[^:]*://; s/:.*//; $start[$n] = $_; $n++; } close(In); if ($n == 0) { print STDERR "Problem reading file $url$inv\n"; sleep(10); exit(; } # # find end of record: $last[] # $lastnum = $start[$n-1]; for ($i = 0; $i < $n; $i++) { $num = $start[$i]; if ($num < $lastnum) { $j = $i + 1; while ($start[$j] == $num) { $j++; } $last[$i] = $start[$j] - 1; } else { $last[$i] = ''; } } if ($action eq 'inv') { for ($i = 0; $i < $n; $i++) { print "$line[$i]:range=$start[$i]-$last[$i]\n"; } exit(0); } # # make the range field for Curl # $range = ''; $lastfrom = ''; $lastto = '-100'; for ($i = 0; $i < $n; $i++) { $_ = $line[$i]; if (/$LEVS/i && /$VARS/i) { $from=$start[$i]; $to=$last[$i]; if ($lastto + 1 == $from) { $lastto = $to; } elsif ($lastto ne $to) { if ($lastfrom ne '') { if ($range eq '') { $range = "$lastfrom-$lastto"; } else { $range = "$range,$lastfrom-$lastto"; } } $lastfrom = $from; $lastto = $to; } } } if ($lastfrom ne '') { if ($range eq '') { $range="$lastfrom-$lastto"; } else { $range="$range,$lastfrom-$lastto"; } } if ($range ne '') { $err = system("$curl -f -v -s -r \"$range\" $url$grb -o $OUTDIR/$file.tmp"); $err = $err >> 8; if ($err != 0) { print STDERR "error in getting file $err $url$grb\n"; sleep(20); exit $err; } rename "$OUTDIR/$file.tmp", "$OUTDIR/$file"; $output = "$output $OUTDIR/$file"; } else { print "no matches (no download) for $file\n"; } $fhr += $dhr; } print "\n\nfinished download\n\n$output\n"; exit(0); the perl script can take in variable date, not just the current system date. Therefore better not to change it. However, IF only you just need system date, insert before the $YYYY,$MM,$DD declaration. Code: [Select]... $time= strftime "%Y%m%d%H", localtime; $YYYY = substr($time,0,4); $MM = substr($time,4,2); $DD = substr($time,6,2); $HH = substr($time,8,2); .... the other way, is to somehow find a way to derefence the %fdate% batch variable from the DOS version. |
|