Extracting Data from NC Files.

I was asked about extracting columns or fields of data from the HadCm3 model output when there are lots of files to be dealt with and maybe you don’t want them all. This python script achieves that and places a file fieldName.txt in the OutputFolder folder.

The script requires the following standard imports and uses the os > system so we can run system commands from within Python.

The following function actually extracts the column from the ncfiles using ncks. Ncks is part of the NCO package.

In the main program we start by doing som housekeeping with the command line arguments so that we ensure the program runs correctly.

The following lines of code load all the files from the folder that we passed as inFolder on the command line. *.nc tells glob only to load files of the nc type into the array.

If you wanted to limit the files processed by Ncks then this following line will do that. On the basis of the order the files have been loaded into the array. (It ‘appears’ to be time-created) the following will trim the array to the last 5. You can read about Python splicing in the documentation.

Best now check we have some files left.

Because we are appending data to the outfile with the >> command (see the ncksExtraction() method) we must reset the file. We can do this by passing it /dev/null.

Now for all the files left in our list lets process them!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.