adamabernathy / write-netcdf-idl

Save adamabernathy/7b7b3842ebebabc0c6ab to your computer and use it in GitHub Desktop.

How to write NetCDF files in IDL.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

; Set up the file & handler
n_profiles = n_elements(lat) ; just pick any of your data arrays
fid=ncdf_create('FILENAME-GOES-HERE'+'.nc',/CLOBBER)
d=indgen(2,/LONG) ; number of dimensions that will be created
; Set Dimensions
d[0]=ncdf_dimdef(fid,'n_profiles',n_profiles) ; length 'j'
d[1]=ncdf_dimdef(fid,'n_bins',n_bins) ; length 'i'
; Define variables to be stored
var_id=ncdf_vardef(fid,'Longitude',d[0],/DOUBLE)
var_id=ncdf_vardef(fid,'Latitude',d[0],/DOUBLE)
var_id=ncdf_vardef(fid,'Temp',[d[1],d[0]],/double)
var_id=ncdf_vardef(fid,'Pressure',[d[1],d[0]],/double)
; Change modes
ncdf_control,fid,/ENDEF
; Write the data
ncdf_varput,fid,'Longitude',rg_lon
ncdf_varput,fid,'Latitude',rg_lat
ncdf_varput,fid,'Temp',temp
ncdf_varput,fid,'Pressure',pressure
ncdf_varput,fid,'SH',sh
; Close the file & release the handler
ncdf_close,fid
print,' > Writing complete. Closing. '