Skip to content

Commit

Permalink
Add scripts to restore the "lev" dimension
Browse files Browse the repository at this point in the history
fixlev/fixlev_gcap2_104.sh
fixlev/fixlev_gcap2_74.sh
fixlev/fixlev_gcap2_40.sh
- Script that uses the NCO "ncap2" function to restore the "lev"
  dimension to netCDF files created by NCL scripts.

fixlev/nc_chunk.pl
- Script for compressing and chunking

README.md
- Updated accordingly

Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
  • Loading branch information
yantosca committed Aug 27, 2024
1 parent f443f2d commit 94eed21
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 1,282 deletions.
1,279 changes: 0 additions & 1,279 deletions LICENSE.txt.~1~

This file was deleted.

17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@ $ conda deactivate # Deactivate the environment
$ ls
make_102L.ncl make_40L.ncl make_74L.ncl PS.2005-2014.nc4
```

3. Run the script corresponding to the desired output grid:

```console
$ ncl make_40L.ncl
$ ncl make_102L.ncl # Regrids to 102 vertical levels, or
$ ncl make_74L.ncl # Regrids to 74 vertical levels, or
$ ncl make_40L.ncl # Regrids to 40 vertical levels
```

4. Copy the regridded data files to the proper GCAP2 data folder in the `ExtData` directory structure.

5. Deactivate the Python environment (as described above).

6. In some cases, the scripts may not have added the regridded `lev` dimension. You can then use one of the scripts in the `fixlev` folder to update the file:

```console
$ cd fixlev
$ ./fixlev_gcap2_102.sh /path/to/102/level/file
$ ./fixlev_gcap2_74.sh /path/to/74/level/file
$ ./fixlev_gcap2_40.sh /path/to/40/level/file
```
10 changes: 10 additions & 0 deletions fixlev/fixlev_gcap2_104.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Add the lev dimension back into the file
ncap2 -O -h -s 'lev[lev]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104}; lev@long_name="GEOS-Chem level"; lev@units="level"; lev@positive="up";lev@axis="Z"' "${1}" "tmp.nc"
mv "tmp.nc" "${1}"

# Compress and chunk the file
./nc_chunk.pl "${1}" 1

exit 0
10 changes: 10 additions & 0 deletions fixlev/fixlev_gcap2_40.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Add the lev dimension back into the file
ncap2 -O -h -s 'lev[lev]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40}; lev@long_name="GEOS-Chem level"; lev@units="level"; lev@positive="up";lev@axis="Z"' "${1}" "tmp.nc"
mv "tmp.nc" "${1}"

# Compress and chunk the file
./nc_chunk.pl "${1}" 1

exit 0
10 changes: 10 additions & 0 deletions fixlev/fixlev_gcap2_74.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Add the lev dimension back into the file
ncap2 -O -h -s 'lev[lev]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74}; lev@long_name="GEOS-Chem level"; lev@units="level"; lev@positive="up";lev@axis="Z"' "${1}" "tmp.nc"
mv "tmp.nc" "${1}"

# Compress and chunk the file
./nc_chunk.pl "${1}" 1

exit 0
229 changes: 229 additions & 0 deletions fixlev/nc_chunk.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
#!/usr/bin/perl -w

#------------------------------------------------------------------------------
# GEOS-Chem Global Chemical Transport Model !
#------------------------------------------------------------------------------
#BOP
#
# !MODULE: nc_chunk.pl
#
# !DESCRIPTION: This perl script can be used to chunk and/or deflate a
# netCDF file. It will first do a ncdump command to find out the dimensions
# of the file, and then chunk the file along lon and lat. This is optimal
# for GEOS-Chem and GCHP.
#\\
#\\
# !USES:
#
require 5.003; # Need this version of Perl or newer
use English; # Use English language
use Carp; # Get detailed error messages
use strict;

# !CALLING SEQUENCE:
# nc_chunk.pl NETCDF-FILE-NAME [DEFLATION-LEVEL]
#
# !REMARKS:
# Must have the NetCDF Operators (NCO) installed.
#
# !REVISION HISTORY:
# 12 Apr 2018 - R. Yantosca - Initial version
#EOP
#------------------------------------------------------------------------------
# GEOS-Chem Global Chemical Transport Model !
#------------------------------------------------------------------------------
#BOP
#
# !IROUTINE: chunkTheFile
#
# !DESCRIPTION: Calls nccopy to chunk a netCDF file along lon and lat
# dimensions. If the deflation factor is greater than zero, it will also
# tell nccopy to compress the netCDF file with that deflation level.
#\\
#\\
# !INTERFACE:
#
sub chunkTheFile($$) {
#
# !INPUT PARAMETERS:
#
my ( $ncFile, $deflate ) = @_; # NetCDF file name to be chunked
# and the deflation factor
#
# !REVISION HISTORY:
# 12 Apr 2018 - R. Yantosca - Initial version
#EOP
#------------------------------------------------------------------------------
#BOC
#
# !LOCAL VARIABLES:
#
# Scalars
my $isDims = 0;

# Strings
my $cmd = "";
my $latCmd = "";
my $latSize = "";
my $lonCmd = "";
my $lonSize = "";
my $levCmd = "";
my $timeCmd = "";
my $nfCmd = "";
my $line = "";
my $result = "";

# Arrays
my @ncDumpTxt = ();
my @subStrs = ();

# Exit if the file does not exist
if ( !( -f $ncFile ) ) { print "Could not find $ncFile!\n"; exit(-1); }

# Save the contents of ncdump to an array
@ncDumpTxt = qx/ncdump -cts $ncFile/;

# Parse the ncdump line
foreach $line ( @ncDumpTxt ) {

# Remove newlines
chomp( $line );

# We have entered the dimensions section
if ( $line =~ m/dimensions:/ ) { $isDims = 1; }

# Look for the lon and lat dimensions
if ( $isDims > 0 ) {

# Create chunking command for lon dimension
if ( $line =~ m/[Ll][0o][Nn]/ || $line =~ m/[Xx][Dd][Ii][Mm]/ ) {
$lonCmd = $line;
$lonCmd =~ s/ = /\//g;
$lonCmd =~ s/ ;//g;
$lonCmd =~ s/^\s+|\s+$//g
}

# Create chunking command for lat dimension
if ( $line =~ m/[Ll][Aa][Tt]/ || $line =~ m/[Yy][Dd][Ii][Mm]/) {
$latCmd = $line;
$latCmd =~ s/ = /\//g;
$latCmd =~ s/ ;//g;
$latCmd =~ s/^\s+|\s+$//g
}

# Create chunking command for level midpoints dimension
if ( $line =~ m/[Ll][Ee][Vv]/ ) {
@subStrs = split( ' = ', $line );
$levCmd = "$subStrs[0]/1";
$levCmd =~ s/^\s+|\s+$//g
}

# Create chunking command for level interfaces dimension
if ( $line =~ m/[Ii][Ll][Ee][Vv]/ ) {
@subStrs = split( ' = ', $line );
$levCmd = "$subStrs[0]/1";
$levCmd =~ s/^\s+|\s+$//g
}

# Create chunking command for time dimension
if ( $line =~ m/[Tt][Ii][Mm][Ee]/ ) {
@subStrs = split( ' = ', $line );
$timeCmd = "$subStrs[0]/1";
$timeCmd =~ s/^\s+|\s+$//g
}

# Create chunking command for Xdim dimension
if ( $line =~ m/[Nn][Ff]/ ) {
$nfCmd = $line;
$nfCmd =~ s/ = /\//g;
$nfCmd =~ s/ ;//g;
$nfCmd =~ s/^\s+|\s+$//g
}

}

# We have exited the dimensions section
if ( $line =~ m/variables:/ ) { goto quit; }
}

quit:

# Chunking command
if ( $nfCmd ne "" ) {

# Cubed-sphere options
if ( $levCmd eq "" && $timeCmd eq "" )
{ $cmd = "nccopy -c $lonCmd,$latCmd,$nfCmd"; }
elsif ( $levCmd eq "" )
{ $cmd = "nccopy -c $lonCmd,$latCmd,$nfCmd,$timeCmd"; }
else
{ $cmd = "nccopy -c $lonCmd,$latCmd,$nfCmd,$levCmd,$timeCmd"; }

} else {

# Lat-lon options
if ( $levCmd eq "" && $timeCmd eq "" )
{ $cmd = "nccopy -c $lonCmd,$latCmd"; }
elsif ( $levCmd eq "" )
{ $cmd = "nccopy -c $lonCmd,$latCmd,$timeCmd"; }
else
{ $cmd = "nccopy -c $lonCmd,$latCmd,$levCmd,$timeCmd"; }
}

# Deflation command
if ( $deflate > 0 ) { $cmd .= " -d$deflate" }

# Add file name to the command
$cmd .= " $ncFile tmp.$ncFile; mv tmp.$ncFile $ncFile";
print "$cmd\n";

# Execute the command
$result = qx/$cmd/;
chomp( $result );
if ( $result ne "" ) { print "$result\n"; }

# Exit and pass status code back
return( $? );
}
#EOP
#------------------------------------------------------------------------------
# GEOS-Chem Global Chemical Transport Model !
#------------------------------------------------------------------------------
#BOP
#
# !IROUTINE: main
#
# !DESCRIPTION: Driver program for the nc_chunk.pl script.
#\\
#\\
# !INTERFACE:
#
sub main(@) {
#
# !REVISION HISTORY:
# 12 Apr 2018 - R. Yantosca - Initial version
#EOP
#------------------------------------------------------------------------------
#BOC

# Error message
my $errMsg = "Usage: nc_chunk.pl NETCDF-FILE-NAME";

# If the user passes a filename from the command line, use it
# Otherwise, default to "UnitTest.input"
if ( scalar( @ARGV ) == 2 ) { &chunkTheFile( @ARGV, ); }
elsif ( scalar( @ARGV ) == 1 ) { &chunkTheFile( @ARGV, 0 ); }
else { print "$errMsg\n"; exit( -1 ); }

# Exit and pass status code backq
return( $? );
}
#EOC

#------------------------------------------------------------------------------

# Call main program
main();

# Exit and pass status code back to Unix shell
exit( $? );

0 comments on commit 94eed21

Please sign in to comment.