Home > Blockchain >  Reading and plotting HDF5(.h5) file and showing map for a specific latitude and longitude
Reading and plotting HDF5(.h5) file and showing map for a specific latitude and longitude

Time:02-26

I got a HDF5 file from MOSDAC website named 3DIMG_30MAR2018_0000_L1B_STD.h5 and I'm tying to read the this file and visualize the TIR and VIS dataset in the file in map. Since I am new to programming I don't have much idea how to do this for a specific location on the global data using the Latitude and longitude values. My problem statement is to show data for Indian region.

The coding I have done till now:

#Importing libraries`

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import h5py
import os
import netCDF4 as nc
import seaborn as sns
import geopandas as gpd  

#Reading and listing keys present in HDF

fn = '3DIMG_30MAR2018_0000_L1B_STD.h5' #filename (the ".h5" file)
f = h5py.File(fn)
list(f.keys())

#extract data from the HDF5 file
mir = f['IMG_MIR'][:]  #middle infra-red
swir = f['IMG_SWIR'][:] #shortwave IR
tir1 = f['IMG_TIR1'][:] #Thermal IR1
tir2 = f['IMG_TIR2'][:] #Thermal IR2
vis = f['IMG_VIS'][:]   #Visible count
wv = f['IMG_WV'][:]     #Water vapor count
lat = f['Latitude'][:] 
lon = f['Longitude'][:]
lat_vis = f['Latitude_VIS'][:]
lon_vis = f['Longitude_VIS'][:]
lat_wv = f['Latitude_WV'][:]
lon_wv = f['Longitude_WV'][:]

vis = f['IMG_MIR'][0,:,:]
fig,ax = plt.subplots(figsize=(10,10))
im1 = plt.imshow(vis)
plt.colorbar(im1)

tir1d = f['/IMG_TIR1'];
lat = f['/Latitude'];
lon = f['/Longitude'];
   
Lat = np.linspace(81.041527, -81.041527, 2805)
Lon = np.linspace(0.84329641, 163.15671, 2816)

shp = gpd.read_file(r'C:\Users\offic\shape\IND_adm0.shp')

%lat=0:gs:60;%INDIA
%lon=60:gs:100;
 import numpy as np
 import matplotlib.pyplot as plt
 from mpl_toolkits.basemap import Basemap

data = Dataset(r'C:\Users\offic\3DIMG_30MAR2018_0000_L1B_STD.h5')
data

Out: <class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    conventions: CF-1.6
    title: 3DIMG_30MAR2018_0000_L1B
    institute: BES,SAC/ISRO,Ahmedabad,INDIA.
    source: BES,SAC/ISRO,Ahmedabad,INDIA.
    Unique_Id: 3DIMG_30MAR2018_0000
    Satellite_Name: INSAT-3D
    Sensor_Id: IMG
    Sensor_Name: IMAGER
    HDF_Product_File_Name: 3DIMG_30MAR2018_0000_L1B_STD.h5
    Output_Format: hdf5-1.8.8
    Station_Id: BES
    Ground_Station: BES,SAC/ISRO,Ahmedabad,INDIA.
    Product_Type: STANDARD(FULL DISK)
    Processing_Level: L1B
    Imaging_Mode: FULL FRAME
    Acquisition_Date: 30MAR2018
    Acquisition_Time_in_GMT: 0000
    Acquisition_Start_Time: 30-MAR-2018T00:00:08
    Acquisition_End_Time: 30-MAR-2018T00:26:58
    Product_Creation_Time: 2018-03-30T06:03:39
    Radiometric_Calibration_Type: LAB CALIBRATED
    Nominal_Altitude(km): 36000.0
    Observed_Altitude(km): 35787.8215
    Field_of_View(degrees): 17.973925
    Nominal_Central_Point_Coordinates(degrees)_Latitude_Longitude: [ 0. 82.]
    Attitude_Source: STAR
    Sun_Azimuth(Degrees): 86.314133
    Sat_Azimuth(Degrees): 286.793152
    Sat_Elevation(Degrees): 89.813721
    Sun_Elevation(Degrees): -6.010917
    FastScan_Linearity_Enabled: no
    SlowScan_Linearity_Enabled: no
    MCD_FS_Enabled: no
    MCD_SS_Enabled: no
    Yaw_Flip_Flag: Y
    Software_Version: 1.0
    TIR1_Gain_Mode: 2
    TIR2_Gain_Mode: 2
    MIR_Gain_Mode: 2
    WV_Gain_Mode: 3
    VIS_Gain_Mode: 2
    SWIR_Gain_Mode: 3
    TIR1_Acquisition_Mode: MAIN
    TIR2_Acquisition_Mode: MAIN
    MIR_Acquisition_Mode: MAIN
    WV_Acquisition_Mode: MAIN
    VIS_Acquisition_Mode: MAIN
    SWIR_Acquisition_Mode: MAIN
    left_longitude: 0.8432964
    right_longitude: 163.15671
    upper_latitude: 81.04153
    lower_latitude: -81.04153
    Datum: WGS84
    Ellipsoid: WGS84
    dimensions(sizes): GeoX(2805), GeoX1(1402), GeoX2(11220), GeoY(2816), GeoY1(1408), GeoY2(11264), GreyCount(1024), time(1)
    variables(dimensions): int32 GeoX(GeoX), int32 GeoX1(GeoX1), int32 GeoX2(GeoX2), int32 GeoY(GeoY), int32 GeoY1(GeoY1), int32 GeoY2(GeoY2), int32 GreyCount(GreyCount), uint16 IMG_MIR(time, GeoY, GeoX), float32 IMG_MIR_RADIANCE(GreyCount), float32 IMG_MIR_TEMP(GreyCount), uint16 IMG_SWIR(time, GeoY2, GeoX2), float32 IMG_SWIR_RADIANCE(GreyCount), uint16 IMG_TIR1(time, GeoY, GeoX), float32 IMG_TIR1_RADIANCE(GreyCount), float32 IMG_TIR1_TEMP(GreyCount), uint16 IMG_TIR2(time, GeoY, GeoX), float32 IMG_TIR2_RADIANCE(GreyCount), float32 IMG_TIR2_TEMP(GreyCount), uint16 IMG_VIS(time, GeoY2, GeoX2), float32 IMG_VIS_ALBEDO(GreyCount), float32 IMG_VIS_RADIANCE(GreyCount), uint16 IMG_WV(time, GeoY1, GeoX1), float32 IMG_WV_RADIANCE(GreyCount), float32 IMG_WV_TEMP(GreyCount), int16 Latitude(GeoY, GeoX), int32 Latitude_VIS(GeoY2, GeoX2), int16 Latitude_WV(GeoY1, GeoX1), int16 Longitude(GeoY, GeoX), int32 Longitude_VIS(GeoY2, GeoX2), int16 Longitude_WV(GeoY1, GeoX1), <class 'str'> SCAN_LINE_TIME(GeoY1), uint16 Sat_Azimuth(time, GeoY, GeoX), int16 Sat_Elevation(time, GeoY, GeoX), uint16 Sun_Azimuth(time, GeoY, GeoX), int16 Sun_Elevation(time, GeoY, GeoX), float64 time(time)
    groups: 

data.variables
Out: {'GeoX': <class 'netCDF4._netCDF4.Variable'>
 int32 GeoX(GeoX)
 unlimited dimensions: 
 current shape = (2805,)
 filling off,
 'GeoX1': <class 'netCDF4._netCDF4.Variable'>
 int32 GeoX1(GeoX1)
 unlimited dimensions: 
 current shape = (1402,)
 filling off,
 'GeoX2': <class 'netCDF4._netCDF4.Variable'>
 int32 GeoX2(GeoX2)
 unlimited dimensions: 
 current shape = (11220,)
 filling off,
 'GeoY': <class 'netCDF4._netCDF4.Variable'>
 int32 GeoY(GeoY)
 unlimited dimensions: 
 current shape = (2816,)
 filling off,
 'GeoY1': <class 'netCDF4._netCDF4.Variable'>
 int32 GeoY1(GeoY1)
 unlimited dimensions: 
 current shape = (1408,)
 filling off,
 'GeoY2': <class 'netCDF4._netCDF4.Variable'>
 int32 GeoY2(GeoY2)
 unlimited dimensions: 
 current shape = (11264,)
 filling off,
 'GreyCount': <class 'netCDF4._netCDF4.Variable'>
 int32 GreyCount(GreyCount)
 unlimited dimensions: 
 current shape = (1024,)
 filling off,
 'IMG_MIR': <class 'netCDF4._netCDF4.Variable'>
 uint16 IMG_MIR(time, GeoY, GeoX)
     long_name: Middle Infrared Count
     invert: true
     central_wavelength: 3.9313
     bandwidth: 0.2
     wavelength_unit: micron
     bits_per_pixel: 10
     resolution: 4.0
     resolution_unit: km
     _FillValue: 1023
     lab_radiance_scale_factor: 0.000283937
     lab_radiance_add_offset: -0.00401529
     lab_radiance_quad: 0.0
     lab_radiance_scale_factor_gsics: 0.000366208
     lab_radiance_add_offset_gsics: -0.00833466
     lab_radiance_quad_gsics: 0.0
     radiance_units: mW.cm-2.sr-1.micron-1
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'IMG_MIR_RADIANCE': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_MIR_RADIANCE(GreyCount)
     long_name: Middle Infrared Radiance
     invert: true
     units: mW.cm-2.sr-1.micron-1
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_MIR_TEMP': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_MIR_TEMP(GreyCount)
     long_name: Middle Infrared Brightness Temperature
     invert: true
     units: K
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_SWIR': <class 'netCDF4._netCDF4.Variable'>
 uint16 IMG_SWIR(time, GeoY2, GeoX2)
     long_name: Shortwave Infrared Count
     invert: false
     central_wavelength: 1.625
     bandwidth: 0.15
     wavelength_unit: micron
     bits_per_pixel: 10
     resolution: 1.0
     resolution_unit: km
     _FillValue: 0
     lab_radiance_scale_factor: 0.007835
     lab_radiance_add_offset: -0.172166
     lab_radiance_quad: 0.0
     lab_radiance_scale_factor_gsics: 0.007835
     lab_radiance_add_offset_gsics: -0.172166
     lab_radiance_quad_gsics: 0.0
     radiance_units: mW.cm-2.sr-1.micron-1
     coordinates: time Latitude_VIS Longitude_VIS
 unlimited dimensions: 
 current shape = (1, 11264, 11220)
 filling on,
 'IMG_SWIR_RADIANCE': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_SWIR_RADIANCE(GreyCount)
     long_name: Shortwave Infrared Radiance
     invert: false
     units: mW.cm-2.sr-1.micron-1
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_TIR1': <class 'netCDF4._netCDF4.Variable'>
 uint16 IMG_TIR1(time, GeoY, GeoX)
     long_name: Thermal Infrared1 Count
     invert: true
     central_wavelength: 10.8288
     bandwidth: 1.0
     wavelength_unit: micron
     bits_per_pixel: 10
     resolution: 4.0
     resolution_unit: km
     _FillValue: 1023
     lab_radiance_scale_factor: 0.00163731
     lab_radiance_add_offset: -0.0199179
     lab_radiance_quad: 0.0
     lab_radiance_scale_factor_gsics: 0.00203982
     lab_radiance_add_offset_gsics: -0.118067
     lab_radiance_quad_gsics: 0.0
     radiance_units: mW.cm-2.sr-1.micron-1
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'IMG_TIR1_RADIANCE': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_TIR1_RADIANCE(GreyCount)
     long_name: Thermal Infrared1 Radiance
     invert: true
     units: mW.cm-2.sr-1.micron-1
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_TIR1_TEMP': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_TIR1_TEMP(GreyCount)
     units: K
     _FillValue: 999.0
     long_name: Thermal Infrared1 Brightness Temperature
     invert: true
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_TIR2': <class 'netCDF4._netCDF4.Variable'>
 uint16 IMG_TIR2(time, GeoY, GeoX)
     long_name: Thermal Infrared2 Count
     invert: true
     central_wavelength: 11.9593
     bandwidth: 1.0
     wavelength_unit: micron
     bits_per_pixel: 10
     resolution: 4.0
     resolution_unit: km
     _FillValue: 1023
     lab_radiance_scale_factor: 0.00143966
     lab_radiance_add_offset: -0.017032
     lab_radiance_quad: 0.0
     lab_radiance_scale_factor_gsics: 0.00181987
     lab_radiance_add_offset_gsics: -0.090434
     lab_radiance_quad_gsics: 0.0
     radiance_units: mW.cm-2.sr-1.micron-1
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'IMG_TIR2_RADIANCE': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_TIR2_RADIANCE(GreyCount)
     _FillValue: 999.0
     long_name: Thermal Infrared2 Radiance
     invert: true
     units: mW.cm-2.sr-1.micron-1
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_TIR2_TEMP': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_TIR2_TEMP(GreyCount)
     long_name: Thermal Infrared2 Brightness Temperature
     invert: true
     units: K
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_VIS': <class 'netCDF4._netCDF4.Variable'>
 uint16 IMG_VIS(time, GeoY2, GeoX2)
     long_name: Visible Count
     invert: false
     central_wavelength: 0.65
     bandwidth: 0.25
     wavelength_unit: micron
     bits_per_pixel: 10
     resolution: 1.0
     resolution_unit: km
     _FillValue: 0
     lab_radiance_scale_factor: 0.0630857
     lab_radiance_add_offset: -1.85891
     lab_radiance_quad: 0.0
     lab_radiance_scale_factor_gsics: 0.0630857
     lab_radiance_add_offset_gsics: -1.85891
     lab_radiance_quad_gsics: 0.0
     radiance_units: mW.cm-2.sr-1.micron-1
     coordinates: time Latitude_VIS Longitude_VIS
 unlimited dimensions: 
 current shape = (1, 11264, 11220)
 filling on,
 'IMG_VIS_ALBEDO': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_VIS_ALBEDO(GreyCount)
     long_name: Visible Albedo
     invert: false
     units: %
 unlimited dimensions: 
 current shape = (1024,)
 filling on, default _FillValue of 9.969209968386869e 36 used,
 'IMG_VIS_RADIANCE': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_VIS_RADIANCE(GreyCount)
     long_name: Visible Radiance
     invert: false
     units: mW.cm-2.sr-1.micron-1
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_WV': <class 'netCDF4._netCDF4.Variable'>
 uint16 IMG_WV(time, GeoY1, GeoX1)
     long_name: Water Vapor Count
     invert: true
     central_wavelength: 6.8841
     bandwidth: 0.6
     wavelength_unit: micron
     bits_per_pixel: 10
     resolution: 8.0
     resolution_unit: km
     _FillValue: 1023
     lab_radiance_scale_factor: 0.000858417
     lab_radiance_add_offset: 0.000380381
     lab_radiance_quad: 0.0
     lab_radiance_scale_factor_gsics: 0.00125055
     lab_radiance_add_offset_gsics: -0.0299274
     lab_radiance_quad_gsics: 0.0
     radiance_units: mW.cm-2.sr-1.micron-1
     coordinates: time Latitude_WV Longitude_WV
 unlimited dimensions: 
 current shape = (1, 1408, 1402)
 filling on,
 'IMG_WV_RADIANCE': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_WV_RADIANCE(GreyCount)
     long_name: Water Vapor Radiance
     invert: true
     units: mW.cm-2.sr-1.micron-1
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'IMG_WV_TEMP': <class 'netCDF4._netCDF4.Variable'>
 float32 IMG_WV_TEMP(GreyCount)
     long_name: Water Vapor Brightness Temperature
     invert: true
     units: K
     _FillValue: 999.0
 unlimited dimensions: 
 current shape = (1024,)
 filling on,
 'Latitude': <class 'netCDF4._netCDF4.Variable'>
 int16 Latitude(GeoY, GeoX)
     long_name: latitude
     add_offset: 0.0
     scale_factor: 0.01
     units: degrees_north
     _FillValue: 32767
 unlimited dimensions: 
 current shape = (2816, 2805)
 filling on,
 'Latitude_VIS': <class 'netCDF4._netCDF4.Variable'>
 int32 Latitude_VIS(GeoY2, GeoX2)
     long_name: latitude
     add_offset: 0.0
     scale_factor: 0.001
     units: degrees_north
     _FillValue: 327670
 unlimited dimensions: 
 current shape = (11264, 11220)
 filling on,
 'Latitude_WV': <class 'netCDF4._netCDF4.Variable'>
 int16 Latitude_WV(GeoY1, GeoX1)
     long_name: latitude
     add_offset: 0.0
     scale_factor: 0.01
     units: degrees_north
     _FillValue: 32767
 unlimited dimensions: 
 current shape = (1408, 1402)
 filling on,
 'Longitude': <class 'netCDF4._netCDF4.Variable'>
 int16 Longitude(GeoY, GeoX)
     long_name: longitude
     units: degrees_east
     add_offset: 0.0
     scale_factor: 0.01
     _FillValue: 32767
 unlimited dimensions: 
 current shape = (2816, 2805)
 filling on,
 'Longitude_VIS': <class 'netCDF4._netCDF4.Variable'>
 int32 Longitude_VIS(GeoY2, GeoX2)
     add_offset: 0.0
     scale_factor: 0.001
     long_name: longitude
     units: degrees_east
     _FillValue: 327670
 unlimited dimensions: 
 current shape = (11264, 11220)
 filling on,
 'Longitude_WV': <class 'netCDF4._netCDF4.Variable'>
 int16 Longitude_WV(GeoY1, GeoX1)
     long_name: longitude
     add_offset: 0.0
     scale_factor: 0.01
     units: degrees_east
     _FillValue: 32767
 unlimited dimensions: 
 current shape = (1408, 1402)
 filling on,
 'SCAN_LINE_TIME': <class 'netCDF4._netCDF4.Variable'>
 vlen SCAN_LINE_TIME(GeoY1)
     long_name: Scan Time for Water Vapor Resolution
 vlen data type: <class 'str'>
 unlimited dimensions: 
 current shape = (1408,),
 'Sat_Azimuth': <class 'netCDF4._netCDF4.Variable'>
 uint16 Sat_Azimuth(time, GeoY, GeoX)
     long_name: Satellite Azimuth
     add_offset: 0.0
     scale_factor: 0.01
     units: degree
     _FillValue: 65535
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'Sat_Elevation': <class 'netCDF4._netCDF4.Variable'>
 int16 Sat_Elevation(time, GeoY, GeoX)
     long_name: Satellite Elevation
     add_offset: 0.0
     units: degree
     scale_factor: 0.01
     _FillValue: 32767
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'Sun_Azimuth': <class 'netCDF4._netCDF4.Variable'>
 uint16 Sun_Azimuth(time, GeoY, GeoX)
     long_name: Sun Azimuth
     add_offset: 0.0
     units: degree
     scale_factor: 0.01
     _FillValue: 65535
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'Sun_Elevation': <class 'netCDF4._netCDF4.Variable'>
 int16 Sun_Elevation(time, GeoY, GeoX)
     long_name: Sun Elevation
     add_offset: 0.0
     scale_factor: 0.01
     units: degree
     _FillValue: 32767
     coordinates: time Latitude Longitude
 unlimited dimensions: 
 current shape = (1, 2816, 2805)
 filling on,
 'time': <class 'netCDF4._netCDF4.Variable'>
 float64 time(time)
     units: minutes since 2000-01-01 00:00:00
 unlimited dimensions: 
 current shape = (1,)
 filling off}

data.variables.keys()
Out: dict_keys(['GeoX', 'GeoX1', 'GeoX2', 'GeoY', 'GeoY1', 'GeoY2', 'GreyCount', 'IMG_MIR', 'IMG_MIR_RADIANCE', 'IMG_MIR_TEMP', 'IMG_SWIR', 'IMG_SWIR_RADIANCE', 'IMG_TIR1', 'IMG_TIR1_RADIANCE', 'IMG_TIR1_TEMP', 'IMG_TIR2', 'IMG_TIR2_RADIANCE', 'IMG_TIR2_TEMP', 'IMG_VIS', 'IMG_VIS_ALBEDO', 'IMG_VIS_RADIANCE', 'IMG_WV', 'IMG_WV_RADIANCE', 'IMG_WV_TEMP', 'Latitude', 'Latitude_VIS', 'Latitude_WV', 'Longitude', 'Longitude_VIS', 'Longitude_WV', 'SCAN_LINE_TIME', 'Sat_Azimuth', 'Sat_Elevation', 'Sun_Azimuth', 'Sun_Elevation', 'time'])

lats = data.variables['Latitude'][:]
lons = data.variables['Longitude'][:]
tir = data.variables['IMG_TIR1'][:]

lats.shape
Out:(2816, 2805)
lons.shape
Out:(2816, 2805)
tir.shape
Out:(1, 2816, 2805)

` I was not so sure about exact lat and lon that I want to plot so here's two of them. Although I think the 1st one is the correct one`

#mp = Basemap(projection = 'merc', 
            # llcrnrlon  = 68.766521,
            # llcrnrlat  = 3.323179,
            # urcrnrlon  = 100.202163, 
            # urcrnrlat  = 37.315495,
            # resolution = 'i')

mp = Basemap(projection = 'merc', 
             llcrnrlon  = 0.8432964,
             llcrnrlat  = -81.04153,
             urcrnrlon  = 163.15671, 
             urcrnrlat  = 81.04153,
             resolution = 'i')

` I'm getting an error in this line dk what it is`

lon, lat = np.meshgrid(lons, lats)
Out: ---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12060/1726626783.py in <module>
----> 1 lon, lat = np.meshgrid(lons, lats)

~\anaconda3\lib\site-packages\numpy\core\overrides.py in meshgrid(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\lib\function_base.py in meshgrid(copy, sparse, indexing, *xi)
   4947 
   4948     if copy:
-> 4949         output = [x.copy() for x in output]
   4950 
   4951     return output

~\anaconda3\lib\site-packages\numpy\lib\function_base.py in <listcomp>(.0)
   4947 
   4948     if copy:
-> 4949         output = [x.copy() for x in output]
   4950 
   4951     return output

~\anaconda3\lib\site-packages\numpy\ma\core.py in wrapped_method(self, *args, **params)
   2576     """
   2577     def wrapped_method(self, *args, **params):
-> 2578         result = getattr(self._data, funcname)(*args, **params)
   2579         result = result.view(type(self))
   2580         result._update_from(self)

MemoryError: Unable to allocate 227. TiB for an array with shape (7898880, 7898880) and data type float32

x,y = mp(lon, lat)
Out:---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6908/3885003198.py in <module>
----> 1 x,y = mp(lon, lat)

NameError: name 'lon' is not defined

c_scheme = mp.pcolor(x, y, np.squeeze(tir1[0, :, :]), cmap = 'jet')

Out: ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6908/254152092.py in <module>
----> 1 c_scheme = mp.pcolor(x, y, np.squeeze(tir1[0, :, :]), cmap = 'jet')

NameError: name 'x' is not defined

CodePudding user response:

Although I can't find the data, I used HDFView from The HDF Group to generate an image from the data. This is for IMG_TIR1. At this point, I need more info about the file schema to "find" this data. Does the MOSDAC website have anything that describes the file schema, or maybe some code that shows how to read the data?

enter image description here

CodePudding user response:

I think you found the data you need to create the plot. (Without the file, it's hard to be 100% sure, or test my code.) I put together a prototype(pseudo-code) to read the HDF5 file with h5py, then plot the data with matplotlib and cartopy. I picked cartopy based on this comment on the Basemap site: "Deprecation Notice: Basemap is deprecated in favor of the Cartopy project." I'm sure it can be modified to use Basemap to plot and NetCDF to read the HDF5 file.

Pseudo-code below:

import h5py
import matplotlib.pyplot as plt

from cartopy import config
import cartopy.crs as ccrs
import cartopy.feature as ccrf

# First get data from HDF5 file with h5py:
fn = '3DIMG_30MAR2018_0000_L1B_STD.h5' #filename (the ".h5" file)
with h5py.File(fn) as data: 
    lats = data['Latitude'][:]
    lons = data['Longitude'][:]
    tir = data['IMG_TIR1'][:].reshape(lats.shape)
# retrieve extent of plot from file attributes:
    left_lon = data.attrs['left_longitude'][0]
    right_lon = data.attrs['right_longitude'][0]
    lower_lat = data.attrs['lower_latitude'][0]
    upper_lat = data.attrs['upper_latitude'][0]  

# Define extent of plot as:
# extent=[longitude_top_left,longitude_top_right,
#         latitude_bottom_left,latitude_top_left].
img_extent = (left_lon, right_lon, lower_lat, upper_lat)

# values from original post
# Lat = np.linspace(81.041527, -81.041527, 2805)
# Lon = np.linspace(0.84329641, 163.15671, 2816)

# Create plot with cartopy and matplotlib    
ax = plt.axes(projection=ccrs.PlateCarree(), extent=img_extent)

plt.contourf(lons, lats, tir, 60,
             transform=ccrs.PlateCarree())

ax.add_feature(ccrf.BORDERS)
ax.coastlines()

plt.show()

I developed the code above from an example on the cartopy website: example image from cartopy

import os
import matplotlib.pyplot as plt
from scipy.io import netcdf

from cartopy import config
import cartopy.crs as ccrs
import cartopy.feature as ccrf

# get the path of the file. It can be found in the repo data directory.
fname = os.path.join(config["repo_data_dir"],
                     'netcdf', 'HadISST1_SST_update.nc'
                     )

dataset = netcdf.netcdf_file(fname, maskandscale=True, mmap=False)
sst = dataset.variables['sst'][0, :, :]
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]

# extent=[longitude_top_left,longitude_top_right,
#         latitude_bottom_left,latitude_top_left].
# Focused on Indian subcontinent:
img_extent = (68.0, 97.0, 0.0, 37.5)
# From Stack Overflow example:
img_extent = (0.84329641, 163.15671, -81.041527, 81.041527)

ax = plt.axes(projection=ccrs.PlateCarree(), extent=img_extent)

plt.contourf(lons, lats, sst, 60,
             transform=ccrs.PlateCarree())

ax.add_feature(ccrf.BORDERS)
ax.coastlines()

plt.show()
  • Related