Effacer les filtres
Effacer les filtres

I have a txt file which has data in netcdf format. How to read it?

48 vues (au cours des 30 derniers jours)
Shagun
Shagun le 19 Juil 2024 à 7:22
Commenté : Umar le 21 Juil 2024 à 11:47
so i downloaded imerg data for 90 days from GES DISC. the whole file got downloaded in txt but the data in it looks like this
https://data.gesdisc.earthdata.nasa.gov/data/GPM_L3/GPM_3IMERGDF.07/2023/06/3B-DAY.MS.MRG.3IMERG.20230618-S000000-E235959.V07B.nc4. i have 90 of these. how can i open this in matalb. i want to plot a graph like this. how can i do it.
  7 commentaires
Walter Roberson
Walter Roberson le 20 Juil 2024 à 21:23
NETCDF files are not text files ??
Umar
Umar le 21 Juil 2024 à 11:47
Hi Shagun,
Cris has answered your question and showed the plot you wanted,please give him a vote to solve your problem for going above and beyond.
@Walter & @Star, thanks for jumping in and helping us to solve this problem as well.

Connectez-vous pour commenter.

Réponses (1)

Cris LaPierre
Cris LaPierre le 21 Juil 2024 à 3:50
Modifié(e) : Cris LaPierre le 21 Juil 2024 à 4:13
The link you shared gives an error, but using the information in the url, I was able to locate the file
Use ncdisp to inspect the file contents to obtain the variable names and related info.
ncfile = '3B-DAY.MS.MRG.3IMERG.20230618-S000000-E235959.V07B.nc4';
ncdisp(ncfile)
Source:
GPM_3IMERGDF_07-20240720_125735\3B-DAY.MS.MRG.3IMERG.20230618-S000000-E235959.V07B.nc4
Format:
netcdf4
Global Attributes:
BeginDate = '2023-06-18'
BeginTime = '00:00:00.000Z'
EndDate = '2023-06-18'
EndTime = '23:59:59.999Z'
FileHeader = 'StartGranuleDateTime=2023-06-18T00:00:00.000Z;
StopGranuleDateTime=2023-06-18T23:59:59.999Z'
InputPointer = '3B-HHR.MS.MRG.3IMERG.20230618-S000000-E002959.0000.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S003000-E005959.0030.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S010000-E012959.0060.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S013000-E015959.0090.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S020000-E022959.0120.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S023000-E025959.0150.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S030000-E032959.0180.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S033000-E035959.0210.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S040000-E042959.0240.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S043000-E045959.0270.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S050000-E052959.0300.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S053000-E055959.0330.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S060000-E062959.0360.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S063000-E065959.0390.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S070000-E072959.0420.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S073000-E075959.0450.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S080000-E082959.0480.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S083000-E085959.0510.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S090000-E092959.0540.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S093000-E095959.0570.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S100000-E102959.0600.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S103000-E105959.0630.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S110000-E112959.0660.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S113000-E115959.0690.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S120000-E122959.0720.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S123000-E125959.0750.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S130000-E132959.0780.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S133000-E135959.0810.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S140000-E142959.0840.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S143000-E145959.0870.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S150000-E152959.0900.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S153000-E155959.0930.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S160000-E162959.0960.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S163000-E165959.0990.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S170000-E172959.1020.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S173000-E175959.1050.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S180000-E182959.1080.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S183000-E185959.1110.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S190000-E192959.1140.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S193000-E195959.1170.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S200000-E202959.1200.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S203000-E205959.1230.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S210000-E212959.1260.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S213000-E215959.1290.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S220000-E222959.1320.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S223000-E225959.1350.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S230000-E232959.1380.V07B.HDF5;3B-HHR.MS.MRG.3IMERG.20230618-S233000-E235959.1410.V07B.HDF5'
title = 'GPM IMERG Final Precipitation L3 1 day 0.1 degree x 0.1 degree (GPM_3IMERGDF)'
DOI = '10.5067/GPM/IMERGDF/DAY/07'
ProductionTime = '2024-01-12T16:20:48.000Z'
Dimensions:
lon = 3600
lat = 1800
time = 1
nv = 2
Variables:
precipitation
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: single
Attributes:
units = "mm/day"
long_name = "Daily mean precipitation rate (combined microwave-IR) estimate. Formerly precipitationCal."
coordinates = "time lon lat"
_FillValue = -9999.9004
precipitation_cnt
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: int8
Attributes:
units = "count"
long_name = "Count of all valid half-hourly precipitation retrievals for the day"
coordinates = "time lon lat"
precipitation_cnt_cond
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: int8
Attributes:
units = "count"
long_name = "Count of half-hourly precipitation retrievals for the day where precipitation is at least 0.01 mm/hr"
coordinates = "time lon lat"
MWprecipitation
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: single
Attributes:
units = "mm/day"
long_name = "Daily mean High Quality precipitation rate from all available microwave sources. Formerly HQprecipitation."
coordinates = "time lon lat"
_FillValue = -9999.9004
MWprecipitation_cnt
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: int8
Attributes:
units = "count"
long_name = "Count of all valid half-hourly MWprecipitation retrievals for the day"
coordinates = "time lon lat"
MWprecipitation_cnt_cond
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: int8
Attributes:
units = "count"
long_name = "Count of half-hourly MWprecipitation retrievals for the day where precipitation is at least 0.01 mm/hr"
coordinates = "time lon lat"
randomError
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: single
Attributes:
units = "mm/day"
long_name = "Root-mean-square error estimate for combined microwave-IR daily precipitation rate"
coordinates = "time lon lat"
_FillValue = -9999.9004
randomError_cnt
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: int8
Attributes:
units = "count"
long_name = "Count of valid half-hourly randomError retrievals for the day"
coordinates = "time lon lat"
probabilityLiquidPrecipitation
Size: 1800x3600x1
Dimensions: lat,lon,time
Datatype: int8
Attributes:
units = "percent"
long_name = "Probability of liquid precipitation"
description = "Probability of liquid precipitation estimated with a diagnostic parameterization using ancillary data. 0=missing values; 1=likely solid; 100=likely liquid or no precipitation. Screen by positive precipitation or precipitation_cnt_cond to locate meaningful probabilities."
coordinates = "time lon lat"
lon
Size: 3600x1
Dimensions: lon
Datatype: single
Attributes:
units = "degrees_east"
long_name = "Longitude"
lat
Size: 1800x1
Dimensions: lat
Datatype: double
Attributes:
units = "degrees_north"
long_name = "Latitude"
time
Size: 1x1
Dimensions: time
Datatype: double
Attributes:
units = "days since 1980-01-06 00:00:00Z"
standard_name = "time"
long_name = "time"
bounds = "time_bnds"
time_bnds
Size: 2x1
Dimensions: nv,time
Datatype: double
Attributes:
units = "days since 1980-01-06 00:00:00Z"
coordinates = "time nv"
Loading the data from the file is straightforward enough once you know the variable names
lat = ncread(ncfile,'lat');
lon = ncread(ncfile,'lon');
precip = ncread(ncfile,'precipitation');
The real challenge is only plotting the data that overlaps the United Kingdom. There are many viable approaches. I'll share what worked for me. I used the following functions
  • inpolygon - determine which points fall inside the borders of the United Kingdom
  • axesm - create a map axes with the projection of the data (WGS84 or mercator)
  • pcolorm - create a geolocated grid of the precipitation data.
  • reducem - reduce density of border data (speeds up plotting)
You will also need to find boundaries for the United Kingdom. Based on your example, the coastlines data in MATLAB is too coarse. You might consider using either of these File Exchange submissions:
Otherwise, you can find the data yourself. For example, see this answer: How to plot boundaries region contours of countries in worldmap?
The shapefile I used was very detailed, so I used reducem to reduce the number of points.
gb = shaperead('UKshapefile.shp')
[X, Y] = reducem(gb.X',gb.Y',0.005);
The data files are too big to upload here, so I am loading a mat file of the results of the above code (variables lat, lon, precip, X, Y)
load ncData.mat
% create matrices of lat and lon data so they are the same size as precip
[LN,LT] = meshgrid(double(lon),double(lat));
% identify points that lie inside boundaries of UK
IN = inpolygon(LN(:),LT(:),X,Y);
% plot borders of UK
figure
axesm('mercator','Frame','off')
geoshow(Y,X)
% Set all points outside the UK to nan so they are not colored
LN(~IN) = nan;
LT(~IN) = nan;
precip(~IN) = nan;
% plot precipitation data
pcolorm(LT,LN,precip)
colormap('jet')
c = colorbar;
c.Label.String = "Daily mean precipitation rate (mm/day)";
title("Daily mean precipitation rate (mm/day)")

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by