How to create netCDF file using python ?

You need netCDF python library in order to read and write netCDF files. If your system does not have this library installed, you can download them from uniData at [url]https://github.com/Unidata/netcdf4-python[/url].
Here is the sample code for reading and writing netCDF file, and you can also download the script here at: [url]ftp://podaac.jpl.nasa.gov/allData/common/sw/nc_writers/netcdf_write.py[/url] and the sample data file at [url]ftp://podaac.jpl.nasa.gov/allData/common/sw/nc_writers/data/20160101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc[/url]
Feel free to modify the code to fit your need.
[code]#~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
from netCDF4 import Dataset
import numpy as np
import os
### Read in sample netCDF file ##################################################################
infilename='data/20160101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc'
ncin = Dataset(infilename, 'r')
lons = ncin.variables['lon'][:]
lats = ncin.variables['lat'][:]
sst = ncin.variables['analysed_sst'][:]
ncin.close()
nlat = lats.size
nlon = lons.size
### Write into netCDF file ######################################################################
# Create the new netCDF file
outfilename = 'test.nc'
fid = Dataset(outfilename,'w', format='NETCDF3_64BIT')
# Define the dimensions
lat = fid.createDimension('lat', nlat) # Unlimited
lon = fid.createDimension('lon', nlon) # Unlimited
# Create global attributes
fid.title = "This is a test file"
fid.institution = "PODAAC of JPL"
fid.summary = "One day of SST data"
fid.acknowledgment = "The data from PODAAC"
fid.date_created = "20151026"
# Create each data variable
nc_var = fid.createVariable('lats', 'f8',('lat'))
fid.variables['lats'][:] = lats
fid.variables['lats'].standard_name='latitude'
fid.variables['lats'].units='degrees_north'
fid.variables['lats'].comment='Latitude'
nc_var = fid.createVariable('lons', 'f8',('lon'))
fid.variables['lons'][:] = lons
fid.variables['lons'].standard_name='longitude'
fid.variables['lons'].units='degrees_east'
fid.variables['lats'].comment='Longitude'
nc_var = fid.createVariable('sst', 'f8',('lon', 'lat'))
fid.variables['sst'][:] = sst
fid.variables['sst'].units='Kelvin'
fid.variables['sst'].comment='analysed_sst'
fid.close()[/code]
Here is the sample code for reading and writing netCDF file, and you can also download the script here at: [url]ftp://podaac.jpl.nasa.gov/allData/common/sw/nc_writers/netcdf_write.py[/url] and the sample data file at [url]ftp://podaac.jpl.nasa.gov/allData/common/sw/nc_writers/data/20160101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc[/url]
Feel free to modify the code to fit your need.
[code]#~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
from netCDF4 import Dataset
import numpy as np
import os
### Read in sample netCDF file ##################################################################
infilename='data/20160101-NCDC-L4LRblend-GLOB-v01-fv02_0-AVHRR_OI.nc'
ncin = Dataset(infilename, 'r')
lons = ncin.variables['lon'][:]
lats = ncin.variables['lat'][:]
sst = ncin.variables['analysed_sst'][:]
ncin.close()
nlat = lats.size
nlon = lons.size
### Write into netCDF file ######################################################################
# Create the new netCDF file
outfilename = 'test.nc'
fid = Dataset(outfilename,'w', format='NETCDF3_64BIT')
# Define the dimensions
lat = fid.createDimension('lat', nlat) # Unlimited
lon = fid.createDimension('lon', nlon) # Unlimited
# Create global attributes
fid.title = "This is a test file"
fid.institution = "PODAAC of JPL"
fid.summary = "One day of SST data"
fid.acknowledgment = "The data from PODAAC"
fid.date_created = "20151026"
# Create each data variable
nc_var = fid.createVariable('lats', 'f8',('lat'))
fid.variables['lats'][:] = lats
fid.variables['lats'].standard_name='latitude'
fid.variables['lats'].units='degrees_north'
fid.variables['lats'].comment='Latitude'
nc_var = fid.createVariable('lons', 'f8',('lon'))
fid.variables['lons'][:] = lons
fid.variables['lons'].standard_name='longitude'
fid.variables['lons'].units='degrees_east'
fid.variables['lats'].comment='Longitude'
nc_var = fid.createVariable('sst', 'f8',('lon', 'lat'))
fid.variables['sst'][:] = sst
fid.variables['sst'].units='Kelvin'
fid.variables['sst'].comment='analysed_sst'
fid.close()[/code]