Example of python 3 code to save in a json file the names and attributes of a VIIRS L2 CLDPROP netcdf file.
Note: the json file created can be found here
Read the file
import netCDF4
import numpy as np
import json
import pprint
f = netCDF4.Dataset('CLDPROP_L2_VIIRS_SNPP.A2018046.1436.001.2019068074007.nc')
Get all group names
group_dic = f.groups.keys()
Get names and attributes in each group
data_json = {}
for group in group_dic:
selected_group = f.groups[group]
group_variables_dic = selected_group.variables.keys()
data_group_json = {}
for group_variables in group_variables_dic:
attributes = selected_group.variables[group_variables]
#----------#
# Get variables attributes
attributes_dic = {}
try:
if group == 'scan_line_attributes':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
if group == 'geolocation_data':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
attributes_dic['scale_factor'] = attributes.scale_factor
attributes_dic['add_offset'] = attributes.add_offset
if group == 'geophysical_data':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['scale_factor'] = attributes.scale_factor
attributes_dic['add_offset'] = attributes.add_offset
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
if group == 'cloud_model_data':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
attributes_dic['description01'] = attributes.description01
attributes_dic['description02'] = attributes.description02
attributes_dic['description03'] = attributes.description03
attributes_dic['description04'] = attributes.description04
attributes_dic['description05'] = attributes.description05
attributes_dic['description06'] = attributes.description06
except:
pass
data_group_json[group_variables] = attributes_dic
data_json[group] = data_group_json
Save in a json file
with open('viirs_cldprop_l2_names_attributes.json', 'w') as fp:
json.dump(data_json, fp, sort_keys=False, indent=4)
Python script
import netCDF4
import numpy as np
import json
import pprint
#----------------------------------------------------------------------------------------#
# Read file
f = netCDF4.Dataset('CLDPROP_L2_VIIRS_SNPP.A2018046.1436.001.2019068074007.nc')
#----------------------------------------------------------------------------------------#
# Read group names
group_dic = f.groups.keys()
#----------------------------------------------------------------------------------------#
# Get names and attributes in each group
data_json = {}
for group in group_dic:
selected_group = f.groups[group]
group_variables_dic = selected_group.variables.keys()
data_group_json = {}
for group_variables in group_variables_dic:
attributes = selected_group.variables[group_variables]
#----------#
# Get variables attributes
attributes_dic = {}
try:
if group == 'scan_line_attributes':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
if group == 'geolocation_data':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
attributes_dic['scale_factor'] = attributes.scale_factor
attributes_dic['add_offset'] = attributes.add_offset
if group == 'geophysical_data':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['scale_factor'] = attributes.scale_factor
attributes_dic['add_offset'] = attributes.add_offset
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
if group == 'cloud_model_data':
attributes_dic['long_name'] = attributes.long_name
attributes_dic['FillValue'] = int( attributes._FillValue )
attributes_dic['valid_min'] = int( attributes.valid_min )
attributes_dic['valid_max'] = int( attributes.valid_max )
attributes_dic['units'] = attributes.units
attributes_dic['shape'] = attributes.shape
attributes_dic['description01'] = attributes.description01
attributes_dic['description02'] = attributes.description02
attributes_dic['description03'] = attributes.description03
attributes_dic['description04'] = attributes.description04
attributes_dic['description05'] = attributes.description05
attributes_dic['description06'] = attributes.description06
except:
pass
data_group_json[group_variables] = attributes_dic
data_json[group] = data_group_json
#----------------------------------------------------------------------------------------#
# Save in a json file
with open('viirs_cldprop_l2_names_attributes.json', 'w') as fp:
json.dump(data_json, fp, sort_keys=False, indent=4)
References
Link | WebSite |
---|---|
netCDF4 module | unidata.github.io |
Continuity MODIS-VIIRS Cloud Properties products available | ladsweb.modaps.eosdis.nasa.gov |
Search for Product Files | ladsweb.modaps.eosdis.nasa.gov |
CLDPROP_L2_VIIRS_SNPP - VIIRS/Suomi-NPP Cloud Properties | ladsweb.modaps.eosdis.nasa.gov |
READING NETCDF4 DATA IN PYTHON | iescoders.com |
Reading NetCDF files with Python | ceda.ac.uk |
MODIS and VIIRS Cloud Properties: User Guide | modis-atmosphere.gsfc.nasa.gov |
Continuity User Guides | modis-atmosphere.gsfc.nasa.gov |
pprint | python doc |
How to Get a List of Class Attributes in Python | blog.pythonlibrary.org |