3

I have a NetCDF file with 3 variables:

  • a 2D lat grid (degrees)
  • a 2D lon grid (degrees)
  • a 2D data grid

I am having troubles opening this file in QGIS with its corresponding latitude and longitude coordinates (see pictures below).

Format:
           netcdf4_classic
Dimensions:
           X12_251 = 240
           Y20_465 = 446
Variables:
    LON
           Size:       240x446
           Dimensions: X12_251,Y20_465
           Datatype:   single
           Attributes:
                       _FillValue = NaN
    LAT
           Size:       240x446
           Dimensions: X12_251,Y20_465
           Datatype:   single
           Attributes:
                       _FillValue = NaN
    SMB
           Size:       240x446
           Dimensions: X12_251,Y20_465
           Datatype:   single
           Attributes:
                       _FillValue = NaN

I am however able to open it in Matlab with the following script:

clear all
close all

ncdisp('nc_example.nc');
Z = ncread('nc_example.nc','SMB');
lat = ncread('nc_example.nc','LAT');
lon = ncread('nc_example.nc','LON');

geoshow(lat, lon, Z, 'DisplayType', 'texturemap')

I created a Google Drive with the .nc file here: https://drive.google.com/file/d/1pbmmDjMaw0eWM1XIBk_px3piaAUtDi2f/view?usp=sharing

enter image description here

1

May not be the most elegant solution but the file can be converted to CSV with the following python code:

import numpy as np
import pandas as pd

from netCDF4 import Dataset

ds=Dataset('nc_example.nc')

arr_lat=ds['LAT'][:].flatten()
arr_lon=ds['LON'][:].flatten()
arr_data=ds['SMB'][:].flatten()

arr_all=np.c_[arr_lat,arr_lon,arr_data]

df= pd.DataFrame(data=arr_all,columns=['lat','lon','smb'])
df.to_csv('nc_example.csv',index=False)

and then loaded to QGIS as Delimited Text and styled

enter image description here

Here's the file converted to geopackage, just in case..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.