Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/NetCDF4: How to duplicate a NetCDF file?

Tags:

python

netcdf

I would like to duplicate a NetCDF file before:

a) updating the values of a variable within the second file, and/or

b) adding a new variable to the second file which corresponds to the structure of a variable from the original.

I can do this sort of duplication already but my process is tedious and feels piecemeal, so I wonder if there's a more elegant, simpler solution. For example, I want the resulting NetCDF file to contain all the global attributes, coordinate dimensions, coordinate variables, and possibly data variables as the original NetCDF file.

I would then want to create another variable in the second NetCDF with the same dimensions as one of the variables of the original (this is already doable as well, but looking for a better way). Probably what I'm looking for is a way to perform the functionality of nccopy within a Python script. Such a function doesn't appear to be present in the netcdf4 module, but perhaps there's another trick I can use for this?

Maybe this is as simple as just copying the original file and modifying the copy where appropriate? Or open the original NetCDF and use the copy module to create the second NetCDF?

Thanks in advance for any suggestions or insight.

like image 908
James Adams Avatar asked Oct 15 '25 14:10

James Adams


1 Answers

With NCO this would be something like

ncap2 -s 'new_var=old_var+20' in.nc out.nc

and you would use -v to control whether you want just new_var and old_var, or all the contents of in.nc to be in out.nc.

like image 195
Charlie Zender Avatar answered Oct 18 '25 07:10

Charlie Zender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!