Skip to main content

Overview

This workflow was tested using QGIS Version 3.22.5. See the "Additional Info" section for special guidance for Non-Gridded data.

Example

The example data used in this tutorial is an hourly, time-averaged 2-dimensional data collection in NetCDF for the Modern-Era Retrospective analysis for Research and Applications, version 2 (MERRA-2). More specifically, the data collection longname and shortname are defined as, respectively: "MERRA-2 tavg1_2d_aer_Nx: 2d,1-Hourly,Time-averaged,Single-Level,Assimilation,Aerosol Diagnostics V5.12.4 (M2T1NXAER)" and "M2T1NXAER_5.12.4". The time slice used in this workflow is September 1, 2021. The filename for this time slice is "MERRA2_401.tavg1_2d_aer_Nx.20210901.nc4."

The MERRA-2 "M2T1NXAER_5.12.4" data collection also contains several variables that you can choose to plot as a raster directly from the NetCDF4 (*.nc4) file that is subsetted and downloaded. For this example, the variable "BCCMASS" will be plotted for the example time slice (referenced above). "BCCMASS" is "Black Carbon Column Mass Density" in units of "kg m-2". 

More information on the variables associated with these data can be found in the MERRA-2: File Specification document. NetCDF files can also be expressed with the file extension of *.nc (the example data is .nc4, or NetCDF4). They can be plotted in the same fashion.

Prerequisites

Software Requirements: QGIS Version 3.44.9* or above. Python workflow tested in Jupyter Notebook.

      *QGIS is an open-source GIS software, and can be downloaded via the web.

Estimated Completion Time: 15 minutes

Procedure

1) Download the example data (or desired NetCDF or NetCDF4/*.nc or *.nc4 granule and data collection). The example data are available and are also referenced above: MERRA2_401.tavg1_2d_aer_Nx.20210901.nc4.

2) Open the QGIS application on your desktop, and create and save a new project to your desired directory. Your blank QGIS project should look similar to Figure 1, below.

Image
Image Caption

Figure 1. Visual representation of a blank QGIS project in QGIS Version 3.22.5.

3) At the top of the QGIS application, navigate to "Layer >> Add Layer >> Add Raster Layer". See Figures 2 and 3 below for the location of this tool, and the pop-up window.

Image
Image Caption

Figure 2. Location of the "Add Raster Layer" tool in the QGIS application.

Image
Image Caption

Figure 3. Dialogue window of the QGIS "Add Raster Layer" tool.

4) Next, click the "..." icon to the right of the "Raster dataset(s)" box, navigate to your file, and select it. Next, click on "Add" in the lower-right corner of the Data Source Manager window. A secondary window will then appear, showing a list of all variables contained in the input raster dataset available for GIS plotting in QGIS. 

For this example, first select the single variable, "BCCMASS" (as described previously in this tutorial), then click the "Add Layers" button. Your layers will be added to the QGIS map view. Please see Figures 4 through 6 for a visual representation of this.

Note: Multiple variables can be selected - they will be added as separate layers to the "Layers" pane.

Image
Image Caption

Figure 4. Highlighted steps to select and add your NetCDF (*.nc4) file to the map view, located in the Data Source Manager window in QGIS.

Image
Image Caption

Figure 5. Highlighted steps to add a specific variable(s) to the map view, located in the "Select Items to Add" window in QGIS.

Image
Image Caption

Figure 6. MERRA-2, M2T1NXAER data collection, "BCCMASS" variable added to the QGIS map view, using default symbology.

5) If you wish to adjust the symbology to filter NoData values, the stretch of the image, or the color ramp, you can right-click the layer in the "Layers" pane, and select "Properties", and adjust the "Symbology" tab on the left-hand side of the "Layer Properties" pop-up window. Here, there are multiple settings that you can adjust, such as the Render Type, Mode, Min/Max display values, Interpolation method, and your preferred Color Ramp.

6) Since MERRA-2 contains one full day of data in 1-hour intervals, they are rendered as a multi-band image, with each band indicating a single hour for each daily NetCDF4 file. For this particular data collection, it is suggested that you change the render type to "Singleband pseudocolor". This will allow you to select the hour of interest for the particular daily granule you are displaying in the "Band" selection dropdown. 

It is also suggested that you pick a cool-to-hot color ramp (low-to-high values, respectively) for the greatest contrast between the high and low values in the data. Please see Figures 7 and 8 for a visual representation of these settings, and the rendered layer in the QGIS map view.

Image
Image Caption

Figure 7. Highlighted steps to change the layer symbology, located in the "Layer Properties >> Symbology" window in QGIS.

Image
Image Caption

Figure 8. Resulting visualization of the sample NetCDF4 file using the symbology outlined in Steps 5 and 6 in the QGIS map view.

Additional Info

Note: When you import the MERRA-2 NetCDF4 (.nc4) example data in this tutorial (gridded data) into QGIS, you may receive an error in QGIS about the Coordinate Reference System (CRS) being undefined in the source data, and that the display has defaulted to "CRS EPSG: 4326 - WGS 84" (Geographic Coordinate System). See Figure 9 below for a visual of this error. 

Your data should still display in the map view correctly in this defaulted coordinated system. To workaround this error, additional processing will be required by exporting the data to a GeoTIFF in the correct coordinate system. This can be done by right-clicking the generated item in the Layers pane, and selecting "Export >> Save As", and selecting your desired (global) CRS to be written permanently to the output file. 

The format should be selected as "GeoTIFF", which will still be exported as a multi-band image. The resulting output GeoTIFF can be symbolized the same way as described in Steps 5 and 6. See Figure 10 below for a visual of these settings. 

Please note that non-gridded (typically Level 1 or 2) data cannot be exported directly to GeoTIFF format from a NetCDF format without additional processing, such as using interpolation methods to produce a gridded raster image.

Image
Image Caption

Figure 9. Resulting Coordinate Reference System (CRS) error when importing the raw NetCDF4 (*.nc4) file into QGIS.

Image
Image Caption

Figure 10. Highlighted steps to save the imported NetCDF4 layer as a multi-band GeoTIFF (*.tiff), located in the "Export >> Save As" selection in the "Layers" pane in QGIS.

Special guidance for non-gridded (Level 2) NetCDF data collections

NASA's publicly available NetCDF data can be gridded (typically as Level 3 or Level 4) and non-gridded (typically as Level 1 or 2). The main procedure in this tutorial is written for gridded data, which can usually be directly imported and visualized as a raster image in QGIS, as explained. 

To work with non-gridded Level 2 data in QGIS, some pre-processing is likely required to convert the data to a usable format. Non-gridded data is best visualized as vector point data representing each satellite observation. Whereas gridded data is best visualized as a raster image.

The following Python script converts the XCO2 science variable ("Bias-corrected XCO2 on X2007 scale") from the Level 2 OCO-2 Lite Version 11.2r data collection data into a GeoJSON (.geojson) using Python's geopandas and xarray packages. GeoJSON files are widely interoperable with many online and desktop GIS software and applications. 

The Geopandas "to_file" function will infer from the file extension specified in the code what type of output file is generated, such as a Geopackage (.gpkg), shapefile (.shp), or GeoJSON (.geojson). Documentation for the "to_file" geopandas function outlining this is available.

Note: This script outlines one of many ways to convert Level 2 data into point features, and is only representative of one of the many potential ways Level 2 NetCDF data can be structured. For example, the latitude and longitude information can be stored as coordinate variables or dimensions, they can be nested into variable groups or at the top level of the data, or they can be multidimensional with time or spatially. 

The variable naming in the script also needs to match the data exactly, and is case-sensitive. It is recommended to review the documentation for your desired Level 2 collection to confirm which fields contain your variables of interest, and to explore how the data is structured so that the script can be altered to fit your needs.

import xarray as xr
import geopandas as gpd
in_netcdf = r"C:\...file\directory\oco2_LtCO2_260101_B11211Ar_260313230415s.nc4"
data_var = "xco2"
time = "time"
lon = "longitude"
lat = "latitude"
file_extension = ".geojson"
data = xr.open_dataset(in_netcdf)
df = data[data_var].to_dataframe()
out_folder = r"C:\...file\directory"
out_file = out_folder + "\\output_" + data_var + file_extension
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(data[lon], data[lat]), crs="EPSG:4326")
gdf = gdf.assign(time=data[time])
gdf.to_file(out_file, layer=data_var)

Related Tutorials

Details

Last Updated

Dec. 1, 2026

Published

Dec. 1, 2026

Data Archive

Goddard Earth Sciences Data and Information Services Center (GES DISC)