Skip to content

Commit 261c93b

Browse files
committed
convenience scripts
1 parent b468be4 commit 261c93b

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed

date2time

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/perl
2+
3+
## This script converts a date string to the corresponding time for a
4+
## given netcdf file by using the NCL function ut_inv_calendar.
5+
6+
$usage = "Usage: date2time netcdf-file yyyy-mm-dd [hh:mm]\n";
7+
8+
9+
if (@ARGV < 2 or @ARGV > 3) {
10+
die $usage;
11+
}
12+
13+
$file = $ARGV[0];
14+
15+
($year, $month, $day) = split /[-\/]/, $ARGV[1];
16+
17+
($hour, $minute, $second) = split /\:/, $ARGV[2];
18+
19+
$hour += 0;
20+
$minute += 0;
21+
$second += 0;
22+
23+
# Check for numeric validity
24+
25+
if ($year !~ /\d{4}/ or
26+
$month < 0 or $month > 12 or
27+
$day < 0 or $day > 31 or
28+
$hour < 0 or $hour > 24 or
29+
$minute < 0 or $minute > 60 or
30+
$second < 0 or $second > 60){
31+
die "Invalid date specification: $year/$month/$day $hour:$minute:$second (should be YYYY/MM/DD [hh:mm:ss])\n";
32+
}
33+
34+
35+
$units = `ncdump -h $file | grep time:units | cut -f 2 -d \\\"`;
36+
unless ($units) {die "No units found in netcdf file $file.\n";}
37+
38+
$calendar = `ncdump -h $file | grep time:calendar | cut -f 2 -d \\\"`;
39+
unless ($calendar) {die "No calendar found in netcdf file $file.\n";}
40+
41+
chomp $units;
42+
chomp $calendar;
43+
44+
$tempfile = ".date2time.$$";
45+
46+
open(NCL, ">$tempfile");
47+
48+
49+
$nclscript = <<END;
50+
c = 0
51+
c\@calendar = \"$calendar\"
52+
date = cd_inv_calendar($year,$month,$day,$hour,$minute,$second,\"$units\",c)
53+
print(sprintf("%.4f",date))
54+
END
55+
56+
print NCL "$nclscript";
57+
58+
close(NCL);
59+
60+
$result = `ncl -n -Q $tempfile`;
61+
print "$result";
62+
63+
`rm $tempfile`;
64+
65+
# Copyright 2010-2012 Univ. Corp. for Atmos. Research
66+
# Author: Seth McGinnis, [email protected]

extract.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
args<-commandArgs(TRUE)
2+
3+
library(ncdf4)
4+
5+
6+
tlat = as.numeric(args[1])
7+
tlon = as.numeric(args[2])
8+
infile = args[3]
9+
outfile = args[4]
10+
11+
fin<-nc_open(infile)
12+
13+
lat<-ncvar_get(fin,"lat")
14+
lon<-ncvar_get(fin,"lon")
15+
16+
dy = abs(lat - tlat)
17+
dx = abs(lon%%360 - tlon%%360)
18+
19+
#image(dx+dy)
20+
21+
ind = arrayInd(which.min(dx+dy),dim(lat))-1
22+
23+
xind = paste0("-d xc,", ind[1], ",", ind[1])
24+
yind = paste0("-d yc,", ind[2], ",", ind[2])
25+
26+
command = paste("ncks", xind, yind, infile, outfile)
27+
28+
#print(command)
29+
30+
system(command)
31+
32+
33+

nclwrap

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/tcsh -f
2+
3+
# wrapper script for making passing of arguments to NCL scripts easier.
4+
5+
# invocation: nclwrap nclscript infile [ [otherfile] outfile]
6+
7+
8+
if ($#argv < 2) then
9+
echo Usage: nclwrap nclscript infile \[ \[otherfile\] outfile\]
10+
exit
11+
12+
endif
13+
14+
set ncl = `basename $1 .ncl`
15+
set var = `basename $2 | cut -f 1 -d _ | cut -f 1 -d .`
16+
17+
if ($ncl == regrid-rcm2rcm || $ncl == coarsen) then
18+
set echo
19+
ncl -Q -n $1 datafile=\"$2\" gridfile=\"$3\" outfile=\"$4\" varname=\"$var\"
20+
unset echo
21+
exit
22+
endif
23+
24+
if ($ncl == patch-regrid) then
25+
set echo
26+
ncl -Q -n $1 infile=\"$2\" wgtfile=\"$3\" outfile=\"$4\" varname=\"$var\"
27+
unset echo
28+
exit
29+
endif
30+
31+
32+
if ($ncl == aggregate) then
33+
set echo
34+
ncl -Q -n $1 infile=\"$2\" outfile=\"$3\" interval=\"$4\" varname=\"$var\" $argv[5-]
35+
unset echo
36+
exit
37+
endif
38+
39+
40+
if ($ncl == wind-vector-plot) then
41+
set echo
42+
ncl -Q -n $1 ufile=\"$2\" vfile=\"$3\" outfile=\"$4\" $argv[5-]
43+
unset echo
44+
exit
45+
endif
46+
47+
48+
if ($ncl == gap-find ) then
49+
ncl -Q -n $1 infile=\"$2\" outfile=\"$3\" varname=\"$var\" $argv[4-]
50+
exit
51+
endif
52+
53+
54+
if ($ncl == text2mask ) then
55+
set echo
56+
ncl -Q -n $1 infile=\"$2\" $argv[3-]
57+
unset echo
58+
exit
59+
endif
60+
61+
62+
if ($ncl == datetime ) then
63+
ncl -Q -n $1 infile=\"$2\" $argv[3-]
64+
exit
65+
endif
66+
67+
68+
if ($#argv == 2) then
69+
ncl -Q -n $1 varname=\"$var\" infile=\"$2\"
70+
exit
71+
endif
72+
73+
74+
75+
# [should] work for: time-section-plot, plot-timestep, 3d-plots
76+
77+
set echo
78+
ncl -Q -n $1 varname=\"$var\" infile=\"$2\" outfile=\"$3\" $argv[4-]
79+
unset echo
80+
exit
81+
82+
endif
83+
84+
85+
# Copyright 2009-2012 Univ. Corp. for Atmos. Research
86+
# Author: Seth McGinnis, [email protected]

template.pbs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/tcsh
2+
3+
##### parameters for PBS scheduling via qsub
4+
##### submit jobs as "qsub scriptname"
5+
6+
# account code to charge to
7+
#PBS -A P86850057
8+
9+
# which queue to use
10+
#PBS -q premium
11+
12+
# job name
13+
#PBS -N JOBNAME
14+
15+
# stdout file
16+
#PBS -o OUTFILE
17+
18+
# stderr file
19+
#PBS -e ERRFILE
20+
21+
# runtime limit
22+
#PBS -l walltime=00:10:00
23+
24+
# CPU resource request
25+
# ntask = number of lines in file
26+
# nnode = ceiling(NTASK / 36)
27+
# ncpus is always set to 36 (use all CPUs in node)
28+
# mpiprocs is always set to 36 also (1 process per CPU)
29+
# ompthreads is always set to 1 (1 thread per process)
30+
#PBS -l select=NNODE:ncpus=36:mpiprocs=36:ompthreads=1
31+
32+
#source /glade/u/apps/opt/lmod/4.2.1/init/tcsh
33+
34+
module load mpt
35+
module load netcdf
36+
module load R
37+
module load nco
38+
module load cdo
39+
module load ncl
40+
41+
setenv MPI_SHEPHERD true
42+
43+
mpiexec_mpt -n NTASK launch_cf.sh CMDFILE

vectorize

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/tcsh -f
2+
3+
## given a file of commands (one command per line), runs them in
4+
## parallel on cheyenne using commandfile MPMD approach
5+
6+
set tmp = `mktemp -d`
7+
set cmd = $tmp/cmdfile
8+
set out = $tmp/out
9+
set err = $tmp/err
10+
set pbs = $tmp/pbs
11+
set job = `basename $1`
12+
13+
## commands need to be wrapped in tcsh shell invocation
14+
15+
sed "s|\(.*\)|tcsh -c '\1 >>\& $tmp/log'|g" $1 > $cmd
16+
17+
set nt = `cat $cmd | wc -l`
18+
set nn = `perl -w -e "use POSIX; print ceil($nt/36)"`
19+
20+
sed -e "s|NTASK|$nt|; s|NNODE|$nn|; s|JOBNAME|$job|; s|OUTFILE|$out|; s|ERRFILE|$err|; s|CMDFILE|$cmd|" ~/template.pbs > $pbs
21+
22+
qsub < $pbs &
23+
24+
25+
26+
# Copyright 2017 Univ. Corp. for Atmos. Research
27+
# Author: Seth McGinnis, [email protected]

0 commit comments

Comments
 (0)