def read_vdata(FILENAME): """Reads all vdata fields in an HDF-EOS file and places those fields in a dictionary. PARAMETERS: ----------- FILENAME: Name of HDF-EOS file. OUTPUTS: -------- vdata_dict: Dictionary of vdata fields. """ # Prepare to read the data. f = HDF(FILENAME, SDC.READ) # Open the file vs = f.vstart() # Start the vdata interface data_info_list = vs.vdatainfo() # List the vdata fields vdata_fieldnames = [a[0] for a in data_info_list] # Get the names # Load the data, place in dictionary vdata_dict = {} for field in vdata_fieldnames: vdata_dict[field] = np.squeeze(np.asarray(vs.attach(field)[:])) # terminate the vdata interface, close the file. vs.end() f.close() return vdata_dict
def get_solor_angle(myd1km_filename_str): try: # open hdf myd021km_data = SD(myd1km_filename_str) # 1.SDS Reading zenith_sds = myd021km_data.select('SensorZenith') # 经纬度 zenith = zenith_sds.get() # 2 azimuth_sds = myd021km_data.select('SensorAzimuth') # 经纬度 azimuth = azimuth_sds.get() return zenith * 0.01, azimuth * 0.01 f = HDF(myd1km_filename_str, SDC.READ) vs = f.vstart() data_info_list = vs.vdatainfo() # Vset table # L1B swam matedata L1B_Swath_Matedata_VD = vs.attach('Level 1B Swath Metadata') # Read [Swath/scan type] sd_info = L1B_Swath_Matedata_VD.inquire() all_metadata = L1B_Swath_Matedata_VD.read(sd_info[0]) L1B_Swath_Matedata_VD.detach() # __del__ with handle this. vs.end() # Use with, so __del__ can do this. f.close() return all_metadata except HDF4Error: print("Unexpected error:(get_solor_angle)", sys.exc_info()[0]) print('READ ERROR......:' + myd1km_filename_str) return None
def hdf4lookup(self, path, swath): hdf = HDF(path) sd = SD(path) vs = hdf.vstart() v = hdf.vgstart() vg = v.attach(swath) vg_members = vg.tagrefs() vg0_members = {} for tag, ref in vg_members: vg0 = v.attach(ref) if tag == HC.DFTAG_VG: vg0_members[vg0._name] = vg0.tagrefs() vg0.detach vg.detach lookup_dict = {} for key in vg0_members.keys(): for tag, ref in vg0_members[key]: if tag == HC.DFTAG_NDG: # f = open(swath + '.txt', 'a'); f.writelines('#' + key + '#' + '\n'); f.close() sds = sd.select(sd.reftoindex(ref)) name = sds.info()[0] lookup_dict[name] = [tag, ref] sds.endaccess() elif tag == HC.DFTAG_VH: vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() lookup_dict[name] = [tag, ref] v.end() vs.end() sd.end() return lookup_dict
def getStartEndTime(fileAbsPath): hdf = HDF(fileAbsPath) vs = hdf.vstart() ref = vs.find('start_time') vd = vs.attach(ref) startTime = vd.read(1)[0][0] startDateTime = datetime.datetime(int(startTime[0:4]), int(startTime[4:6]), int(startTime[6:8]), int(startTime[8:10]), int(startTime[10:12]), int(startTime[12:14])) # startDateTime = startTime[0:4] + '-' + startTime[4:6] + '-' + startTime[6:8] + 'T' + startTime[8:10] + ':' + startTime[10:12] + ':' + startTime[12:14] + 'Z' ref = vs.find('end_time') vd = vs.attach(ref) endTime = vd.read(1)[0][0] endDateTime = datetime.datetime(int(endTime[0:4]), int(endTime[4:6]), int(endTime[6:8]), int(endTime[8:10]), int(endTime[10:12]), int(endTime[12:14])) # endDateTime = endTime[0:4] + '-' + endTime[4:6] + '-' + endTime[6:8] + 'T' + endTime[8:10] + ':' + endTime[10:12] + ':' + endTime[12:14] + 'Z' vs.end() return startDateTime, endDateTime
def require_VD_info_hdf(file_in): '''Print and Return VD variable names and dimentions in a HDF-EOS file''' #--information from input file-- f=HDF(file_in) vs=f.vstart() var_info=vs.vdatainfo() print("--Variables in ",file_in,"-->") for item in var_info: print(item) return var_info
def proc(indir, outdir, inname, outname): path = indir + "/" + inname hdf = HDF(path) sd = SD(path) vs = hdf.vstart() v = hdf.vgstart() mod_vg = v.attach("MOD_Grid_monthly_CMG_VI") vg_members = mod_vg.tagrefs() # print vg_members mod_vg = v.attach("MOD_Grid_monthly_CMG_VI") tag, ref = mod_vg.tagrefs()[0] # print tag, ref vg0 = v.attach(ref) # print vg0._name tagrefs = vg0.tagrefs() # print tagrefs for tag, ref in tagrefs: if tag == HC.DFTAG_NDG: sds = sd.select(sd.reftoindex(ref)) name = sds.info()[0] # print name if name == "CMG 0.05 Deg Monthly NDVI": sd = SD(path) sds = sd.select(sd.reftoindex(ref)) ndvi = np.float64(sds.get()) sds.endaccess() elif name == "CMG 0.05 Deg Monthly EVI": sd = SD(path) sds = sd.select(sd.reftoindex(ref)) evi = np.float64(sds.get()) sds.endaccess() sd.end() v.end() data = ndvi name = outdir + "/" + outname + ".tif" cols = 7200 rows = 3600 originX = -180.0 originY = 90.0 pixelWidth = 0.05 pixelHeight = -0.05 driver = gdal.GetDriverByName('GTiff') newRasterfn = name outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) outRaster.SetGeoTransform( (originX, pixelWidth, 0, originY, 0, pixelHeight)) outband = outRaster.GetRasterBand(1) outband.WriteArray(data) outRasterSRS = osr.SpatialReference() outRasterSRS.ImportFromEPSG(4326) outRaster.SetProjection(outRasterSRS.ExportToWkt()) outband.FlushCache()
def getAdjustmentParams(fileAbsPath): hdf = HDF(fileAbsPath) vs = hdf.vstart() ref = vs.find('Radar_Reflectivity.valid_range') vd = vs.attach(ref) validRange = vd.read(1)[0][0] ref = vs.find('Radar_Reflectivity.factor') vd = vs.attach(ref) reflectivityFactor = vd.read(1)[0][0] vs.end() return validRange, reflectivityFactor
def read_vd_hdf(file_in,var_in,dimsz): '''Read data stored as a VD variable in input HDF-EOS file.''' '''Inputs are the file path and the required variable name.''' '''Outputs are the data (numpy array). ''' #--information from input file-- hdf = HDF(file_in) vs = hdf.vstart() vd = vs.attach(var_in) var_data = np.array(vd.read(int(dimsz))) vd.detach() return var_data #,var_dimn
def load_vd(fname, varnames): '''return list containing vdata structures specified by list varnames, contained in hdf4 file with filename fname''' f = HDF(fname) data_list = [] vs = f.vstart() for name in varnames: vd = vs.attach(name) data_list.append(vd[:]) vd.detach() vs.end() f.close() return data_list
def read_hdf_VD(file_in,var_in): '''Read Vdata sets (table, 1D) in the input HDF-EOS file. ''' '''Currently coded for surface variables of A-Train granule. ''' '''Inputs are the file path and the required variable name. ''' '''Outputs are the data (numpy array) and dimentions(numpy). ''' #--information from input file-- f=HDF(file_in) vs=f.vstart() vd=vs.attach(var_in) #return var data from vs group var_data=np.array(vd[:]).ravel() #convert data into flatted ndarray #print(var_data.dtype) var_dimn=np.array(var_data.shape) return var_data,var_dimn
def read_data_from_hdf(inputfile): wavelengths = [] depth = [] downwelling_downcast = [] downwelling_upcast = [] upwelling_downcast = [] upwelling_upcast = [] # open the hdf file read-only # Initialize the SD, V and VS interfaces on the file. hdf = HDF(inputfile) vs = hdf.vstart() v = hdf.vgstart() # Attach and read the contents of the Profiler vgroup vg = v.attach(v.find('Profiler')) for tag, ref in vg.tagrefs(): assert(tag == HC.DFTAG_VH) vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() if name == "ED_hyperspectral_downcast": x = vd.read(nrecs) downwelling_downcast = np.asarray([i[3:] for i in x]) wavelengths = np.asarray([float(x) for x in fields[3:]]) depth = np.asarray([i[2] for i in x]) elif name == "ED_hyperspectral_upcast": downwelling_upcast = np.asarray([i[3:] for i in vd.read(nrecs)]) elif name == "LU_hyperspectral_downcast": upwelling_downcast = np.asarray([i[3:] for i in vd.read(nrecs)]) elif name == "LU_hyperspectral_upcast": upwelling_upcast = np.asarray([i[3:] for i in vd.read(nrecs)]) vd.detach() # Close vgroup vg.detach() #clean up v.end() vs.end() hdf.close() return wavelengths, depth, downwelling_downcast, downwelling_upcast, \ upwelling_downcast, upwelling_upcast
def HDFread1D(filename, variable): """ Read HDF file in vs in simple mode """ # Read the file f = HDF(filename) # Initialize v mode vs = f.vstart() # extrect data var = vs.attach(variable) Var = np.array(var[:]).ravel() # Close the file var.detach() vs.end() f.close() return Var
def HDFread(filename, variable, Class=None): """ Extract the data for non scientific data in V mode of hdf file """ hdf = HDF(filename, HC.READ) # Initialize the SD, V and VS interfaces on the file. sd = SD(filename) vs = hdf.vstart() v = hdf.vgstart() # Encontrar el puto id de las Geolocation Fields if Class == None: ref = v.findclass('SWATH Vgroup') else: ref = v.findclass(Class) # Open all data of the class vg = v.attach(ref) # All fields in the class members = vg.tagrefs() nrecs = [] names = [] for tag, ref in members: # Vdata tag vd = vs.attach(ref) # nrecs, intmode, fields, size, name = vd.inquire() nrecs.append(vd.inquire()[0]) # number of records of the Vdata names.append(vd.inquire()[-1]) # name of the Vdata vd.detach() idx = names.index(variable) var = vs.attach(members[idx][1]) V = var.read(nrecs[idx]) var.detach() # Terminate V, VS and SD interfaces. v.end() vs.end() sd.end() # Close HDF file. hdf.close() return np.array(V).ravel()
def get_train_data(self, index_tuple, band_select=[ 0, 1, 2, 3, 4, 6, 7, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 ]): ''' [Varification & Night eliminite] :param index_tuple: :return: ''' # [band_num, rowsize] raw_data = self.get_by_index(index_tuple, verify=False) # Verify raw_data = raw_data[band_select, ...] valid_flag = np.ones([raw_data.shape[1]], dtype=bool) for idx in range(raw_data.shape[0]): valid_flag = valid_flag & (raw_data[idx, ...] > 32767) # Daytime mode try: # open hdf f = HDF(self.filename, SDC.READ) vs = f.vstart() data_info_list = vs.vdatainfo() # Vset table # L1B swam matedata L1B_Swath_Matedata_VD = vs.attach('Level 1B Swath Metadata') # Read [Swath/scan type] svath_matedata = L1B_Swath_Matedata_VD[:] for idx in range(valid_flag.shape[0]): if svath_matedata[int( (index_tuple[0][idx]) / 10)][2] == 'D ': valid_flag[idx] = True else: valid_flag[idx] = False L1B_Swath_Matedata_VD.detach() # __del__ with handle this. vs.end() # Use with, so __del__ can do this. f.close() except ValueError: print("Unexpected error:", sys.exc_info()[0]) print('READ ERROR......(%d):' % self.filename) return 0, 0, False raw_data = self.get_by_index(index_tuple, verify=False, scaled=True) raw_data = raw_data[band_select, ...] return raw_data, valid_flag
def get_swath_metadata(myd1km_filename_str): try: # open hdf f = HDF(myd1km_filename_str, SDC.READ) vs = f.vstart() data_info_list = vs.vdatainfo() # Vset table # L1B swam matedata L1B_Swath_Matedata_VD = vs.attach('Level 1B Swath Metadata') # Read [Swath/scan type] sd_info = L1B_Swath_Matedata_VD.inquire() all_metadata = L1B_Swath_Matedata_VD.read(sd_info[0]) L1B_Swath_Matedata_VD.detach() # __del__ with handle this. vs.end() # Use with, so __del__ can do this. f.close() return all_metadata except HDF4Error: print("Unexpected error:", sys.exc_info()[0]) print('READ ERROR......:' + myd1km_filename_str) return None
def showHDFinfo(self): if len(self.dirLst) > 1: drs = self.dirLst[0] else: drs = self.dirLst hdf = HDF(drs, HC.READ) sd = SD(drs) vs = hdf.vstart() v = hdf.vgstart() # Scan all vgroups in the file. ref = -1 while 1: try: ref = v.getid(ref) describevg(ref, v, vs) except HDF4Error, msg: # no more vgroup break
def look(self, path, mem_list, lp_list): data = {} for name in mem_list: # subdata sets type data tag = lp_list[name][0] ref = lp_list[name][1] if tag == HC.DFTAG_NDG: sd = SD(path) sds = sd.select(sd.reftoindex(ref)) data[name] = np.float64(sds.get()) sds.endaccess() sd.end() elif tag == HC.DFTAG_VH: #vd type data hdf = HDF(path) vs = hdf.vstart() vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() data[name] = np.full(nrecs, np.float64(vd.read()[0])) vs.end() hdf.close() return data
def read_vd_hdf2(file_in,var_in): '''Read data stored as a VD variable in input HDF-EOS file.''' '''Inputs are the file path and the required variable name.''' '''Outputs are the data (numpy array). ''' #--information from input file-- hdf = HDF(file_in) # the HDF file vs = hdf.vstart() # initialize VS interface on HDF file ### vdinfo = vs.vdatainfo() # return info about all vdatas vd = vs.attach(var_in) # open a vdata given its name dimsz = vd.inquire()[0] # return 5 elements: # 1. # of records # 2. interlace mode # 3. list of vdata field names # 4. size in bytes of the vdata record # 5. name of the vdata var_data = np.array(vd.read(dimsz)) #read a number of records vd.detach() # close the vdata vs.end() # terminate the vdata interface hdf.close() # close the HDF file return var_data,dimsz
def get_myd1km_sci_time(myd1km_filename_str): try: # open hdf f = HDF(myd1km_filename_str, SDC.READ) vs = f.vstart() data_info_list = vs.vdatainfo() # Vset table # L1B swam matedata L1B_Swath_Matedata_VD = vs.attach('Level 1B Swath Metadata') # Read [Swath/scan type] begin = L1B_Swath_Matedata_VD[0] begin = begin[4] end = L1B_Swath_Matedata_VD[-1] end = end[4] L1B_Swath_Matedata_VD.detach() # __del__ with handle this. vs.end() # Use with, so __del__ can do this. f.close() return begin, end, True except HDF4Error: print("Unexpected error:", sys.exc_info()[0]) print('READ ERROR......:' + myd1km_filename_str) return 0, 0, False
def getCoordsULLR(fileAbsPath, start, stop): # coordinates every 10 pixels nodes = range(start, stop, 10) hdf = HDF(fileAbsPath) vs = hdf.vstart() ref = vs.find('Latitude') vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() latitude = np.array(vd.read(nrecs)) ref = vs.find('Longitude') vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() longitude = np.array(vd.read(nrecs)) lat = [] lon = [] for n in nodes: lat.append(latitude[n][0]) lon.append(longitude[n][0]) # Append the last pixel coordinates: it is 9 pixel distant from the # previous one, and not 10 px like the others!!! lat.append(latitude[stop - 1][0]) lon.append(longitude[stop - 1][0]) vs.end() coords = [] for x, y in zip(lat, lon): coords.append(x) coords.append(y) # Nodes are 101: 100 points with distance 10 px # and the last one with distance 9 px from the 990th return coords, len(nodes) + 1
from pyhdf.HDF import * from pyhdf.VS import * f = HDF('inventory.hdf', # Open file 'inventory.hdf' in write mode HC.WRITE|HC.CREATE) # creating it if it does not exist vs = f.vstart() # init vdata interface vd = vs.attach('INVENTORY', 1) # attach vdata 'INVENTORY' in write mode # Update the `status' vdata attribute. The attribute length must not # change. We call the attribute info() method, which returns a list where # number of values (eg string length) is stored at index 2. # We then assign a left justified string of exactly that length. len = vd.attr('status').info()[2] vd.status = '%-*s' % (len, 'phase 3 done') # Update record at index 1 (second record) vd[1] = ('Z4367', 'surprise', 10, 3.1, 44.5) # Update record at index 4, and those after vd[4:] = ( ('QR231', 'toy', 12, 2.5, 45), ('R3389', 'robot', 3, 45, 2000), ('R3390', 'robot2', 8, 55, 2050) ) vd.detach() # "close" the vdata vs.end() # terminate the vdata interface f.close() # close the HDF file
# Create a simple 3x3 float array. sds = sd.create(name, SDC.FLOAT32, (3, 3)) # Initialize array sds[:] = ((0, 1, 2), (3, 4, 5), (6, 7, 8)) # "close" dataset. sds.endaccess() # Create HDF file filename = 'inventory.hdf' hdf = HDF(filename, HC.WRITE | HC.CREATE) # Initialize the SD, V and VS interfaces on the file. sd = SD(filename, SDC.WRITE) # SD interface vs = hdf.vstart() # vdata interface v = hdf.vgstart() # vgroup interface # Create vdata named 'INVENTORY'. vdatacreate(vs, 'INVENTORY') # Create dataset named "ARR_3x3" sdscreate(sd, 'ARR_3x3') # Attach the vdata and the dataset. vd = vs.attach('INVENTORY') sds = sd.select('ARR_3x3') # Create vgroup named 'TOTAL'. vg = v.create('TOTAL') # Add vdata to the vgroup
def get_train_data_map(self, index_tuple, kernel_radius=5, band_select=[ 0, 1, 2, 3, 4, 6, 7, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 ]): ''' [Varification & Night eliminite] :param index_tuple: :return: ''' # [band_num, rowsize] selected_band_num = len(band_select) kernel_size = 2 * kernel_radius + 1 # Verify valid_flag = np.ones([index_tuple[0].shape[0]], dtype=bool) # Daytime mode try: # open hdf f = HDF(self.filename, SDC.READ) vs = f.vstart() data_info_list = vs.vdatainfo() # Vset table # L1B swam matedata L1B_Swath_Matedata_VD = vs.attach('Level 1B Swath Metadata') # Read [Swath/scan type] svath_matedata = L1B_Swath_Matedata_VD[:] for idx in range(valid_flag.shape[0]): if svath_matedata[int( (index_tuple[0][idx]) / 10)][2] == 'D ': valid_flag[idx] = True else: valid_flag[idx] = False L1B_Swath_Matedata_VD.detach() # __del__ with handle this. vs.end() # Use with, so __del__ can do this. f.close() except ValueError: print("Unexpected error:", sys.exc_info()[0]) print('READ ERROR......(%d):' % self.filename) return 0, 0, False if np.sum(valid_flag) == 0: return None, valid_flag raw_data = np.empty([ index_tuple[0].shape[0], selected_band_num, kernel_size, kernel_size ]) # Create a Mat of kernel kernel_i = np.empty([kernel_size, kernel_size], dtype=np.int16) for i in range(kernel_size): for j in range(kernel_size): kernel_i[i, j] = i kernel_i -= kernel_radius kernel_i = np.reshape(kernel_i, kernel_i.size) kernel_j = np.transpose(kernel_i) kernel_j = np.reshape(kernel_j, kernel_j.size) for idx in range(index_tuple[0].shape[0]): if not valid_flag[idx]: continue temp_kernel_i = kernel_i + index_tuple[0][idx] if np.sum((temp_kernel_i < 0) | (temp_kernel_i >= self.width)) > 0: valid_flag[idx] = False continue temp_kernel_j = kernel_j + index_tuple[1][idx] if np.sum((temp_kernel_j < 0) | (temp_kernel_j >= self.height)) > 0: valid_flag[idx] = False continue temp = (self.all_band[..., temp_kernel_i, temp_kernel_j])[band_select, ...] if np.sum(temp[..., int(kernel_size * kernel_size * 0.5)] > 32767): valid_flag[idx] = False else: indeces = temp > 32767 temp[indeces] = 0 raw_data[idx, ...] = temp.reshape(selected_band_num, kernel_size, kernel_size) for idx in range(selected_band_num): raw_data[:, idx, :, :] = ( raw_data[:, idx, :, :] - self.offset[band_select[idx]]) * self.scale[band_select[idx]] return raw_data, valid_flag
def PFlux(year1, month1, day1): filename = external_dir + "swepam_data_1day.hdf" hdf = HDF(filename) #Initialize the V interface on the HDF file. v = hdf.vgstart() vs = hdf.vstart() #Scan all vgroups in the file ref = -1 refnum = v.getid(ref) vg = v.attach(refnum) #print "----------------" #print "name:", vg._name, "class:",vg._class, "tag,ref:", #print vg._tag, vg._refnum # Show the number of members of each main object type. #print "members: ", vg._nmembers, #print "datasets:", vg.nrefs(HC.DFTAG_NDG), #print "vdataset: ", vg.nrefs(HC.DFTAG_VH), #print "vgroups: ", vg.nrefs(HC.DFTAG_VG) # Read the contents of the vgroup. members = vg.tagrefs() # Display info about each member. index = -1 for tag, ref in members: index += 1 # print "member index", index # Vdata tag if tag == HC.DFTAG_VH: vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() # print " vdata:",name, "tag,ref:",tag, ref # print " fields:",fields # print " nrecs:",nrecs vd.detach() # SDS tag elif tag == HC.DFTAG_NDG: sds = sd.select(sd.reftoindex(ref)) name, rank, dims, type, nattrs = sds.info() # print " dataset:",name, "tag,ref:", tag, ref # print " dims:",dims # print " type:",type sds.endaccess() # VS tag elif tag == HC.DFTAG_VG: vg0 = v.attach(ref) # print " vgroup:", vg0._name, "tag,ref:", tag, ref vg0.detach() # Unhandled tag else: print("unhandled tag,ref", tag, ref) # Close vgroup members = vg.tagrefs() (tag, ref) in members vd = vs.attach(ref) nrecs, intmode, fields, size, name = vd.inquire() alldata = vd.read(nrecs) vd.detach() vg.detach() v.end() hdf.close() data = np.array(alldata) # input # (y,m,d) = (1998,3,1) year = data[:, 0] day = data[:, 1] hr = data[:, 2] min = data[:, 3] sec = data[:, 4] fp_year = data[:, 5] fp_doy = data[:, 6] pdensity = data[:, 8] speed = data[:, 11] start = datetime.date(int(year[0]), 1, 1) + datetime.timedelta(int(day[0]) - 1) end = datetime.date(int(year[-1]), 1, 1) + datetime.timedelta(int(day[-1]) - 1) # print "star Date and End Date:", start, end delta1 = datetime.date(year1, month1, day1) - start index = delta1.days # print "index ....", index if index >= (nrecs - 1): print("ERROR: the input time is too new") # break elif index < 0: print("ERROR: the input time is too old") # break else: avePF = 0 num = 0 for i in range(0, 90): j = index - i if j < 0: print("ERROR: the index is out of the array") break else: if pdensity[j] > 0 and speed[j] > 0: avePF = avePF + pdensity[j] * speed[j] num = num + 1 avePF = avePF / num # print "the 90 days average proton flux is:",avePF, num return avePF
def hdfget(self, id=False, section=False, group=False): #for i, drs in enumerate(self.dirLst[0:500]): for i, drs in enumerate(self.dirLst[:]): #print drs hdf = HDF(drs, HC.READ) sd = SD(drs) vs = hdf.vstart() v = hdf.vgstart() # Scan all vgroups in the file. ref = -1 while 1: try: ref = v.getid(ref) vg = v.attach(ref) if vg._name == section: # Read the contents of the vgroup. members = vg.tagrefs() # Display info about each member. index = -1 for tag, ref2 in members: index += 1 #print "member index", index # Vdata tag if tag == HC.DFTAG_VH: vd = vs.attach(ref2) nrecs, intmode, fields, size, name = vd.inquire( ) if vd._name == group: self.HDF.setdefault(id + '_fields', []).append(fields) self.HDF.setdefault(id + '_nrecs', []).append(nrecs) self.HDF.setdefault(id + '_data', []).append( np.asarray(vd[:])) vd.detach() # SDS tag elif tag == HC.DFTAG_NDG: sds = sd.select(sd.reftoindex(ref2)) name, rank, dims, type, nattrs = sds.info() sds.endaccess() elif tag == HC.DFTAG_VG: vg0 = v.attach(ref2) vg0.detach() else: print "unhandled tag,ref", tag, ref2 except HDF4Error, msg: # no more vgroup break
def genCloudSatFigure(filepath, data_field, title, label, plot_type, cmap): """Ingest CloudSat overpasses (HDF) and plot parameters of interest like snowfall rates as observed by the CPR. """ # Load and unpack HDF f = HDF(filepath) vs = f.vstart() Latitude = vs.attach('Latitude') Longitude = vs.attach('Longitude') Time = vs.attach('Profile_time') UTC = vs.attach('UTC_start') if plot_type == PlotType.SFCS: snowfall_rate_sfc = vs.attach('snowfall_rate_sfc') c = snowfall_rate_sfc[:] c = flatten(c) snowfall_rate_sfc.detach() if plot_type == PlotType.TEMP: EC_Height = vs.attach('EC_height') b = EC_Height[:] b = flatten(b) EC_Height.detach() a = Time[:] a = flatten(a) d = Longitude[:] d = flatten(d) utc_start = UTC[0][0] Latitude.detach() # "close" the vdata Longitude.detach() # "close" the vdata Time.detach() # "close" the vdata UTC.detach() # "close" the vdata vs.end() # terminate the vdata interface f.close() #---------- Read HDF Files ----------# cpr_2b_geoprof = SD(filepath, SDC.READ) offset = 28000 # Position along granule span = 1000 # Plot length if plot_type != PlotType.SFCS: if plot_type == PlotType.REFL or plot_type == PlotType.MASK or plot_type == PlotType.SNOW: cpr_2b_geoprof_height = cpr_2b_geoprof.select('Height') cpr_2b_geoprof_height_data = cpr_2b_geoprof_height.get() cpr_2b_geoprof_radar_reflectivity = cpr_2b_geoprof.select(data_field) cpr_2b_geoprof_radar_reflectivity_data = cpr_2b_geoprof_radar_reflectivity.get() if plot_type == PlotType.TEMP or plot_type == PlotType.SNOW: cpr_2b_geoprof_radar_reflectivity_data[cpr_2b_geoprof_radar_reflectivity_data < 0] = math.nan elif plot_type == PlotType.POWR: cpr_2b_geoprof_radar_reflectivity_data[cpr_2b_geoprof_radar_reflectivity_data < 0] = math.nan fillvalue = 15360 missing = -8888 img = np.zeros((span,125)) if plot_type == PlotType.REFL: img.fill(-30) factor = 1 if plot_type == PlotType.REFL: factor = 0.01 for i in np.arange(span): for j in np.arange(125): if plot_type == PlotType.TEMP: k = int( 125 * (b[j] + 5000) / 35000 ) else: k = int( 125 * (cpr_2b_geoprof_height_data[i+offset,j] + 5000) / 35000 ) if cpr_2b_geoprof_radar_reflectivity_data[i+offset,j] > -3000 and \ cpr_2b_geoprof_radar_reflectivity_data[i+offset,j] < 2100: img[i,k] = cpr_2b_geoprof_radar_reflectivity_data[i+offset,j] * factor # Begin plotting granule fig = plt.figure(figsize=(18, 6)) ax = plt.subplot(111) im = ax.imshow(img.T, interpolation='bilinear', cmap=cmap, origin='lower', extent=[0,200,-10,60]) plt.title(title) plt.ylabel('Height (km)') plt.xlabel('Time') plt.ylim(0,20) pylab.yticks([0,5,10,15,20],[0,5,10,15,20]) position_tick_labels = [str(round(a[offset]+utc_start, 3)), str(round(a[offset+200]+utc_start, 3)), str(round(a[offset+400]+utc_start, 3)), str(round(a[offset+600]+utc_start, 3)), str(round(a[offset+800]+utc_start, 3)), str(round(a[offset+1000]+utc_start, 3))] pylab.xticks([0,40,80,120,160, 200], position_tick_labels) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="3%", pad=0.10) if plot_type == PlotType.MASK: plt.colorbar(im, cax=cax, boundaries=[-10,0,10,20,30,40], ticks=[-10,0,10,20,30,40], label=label) elif plot_type == PlotType.TEMP: plt.colorbar(im, cax=cax, label=label, boundaries=[200, 220, 240, 260, 280], ticks=[200, 220, 240, 260, 280]) else: plt.colorbar(im, cax=cax, label=label) plt.savefig("cloudsat_radar_reflectivity.png") plt.show() else: fig = plt.figure(figsize=(15.5, 2.4)) c = c[offset:offset+span] index = np.arange(len(c)) plt.plot(index, c, color="#6699ff") plt.fill(index, c, color="#6699ff") plt.grid() plt.title("CloudSat Surface Snowfall") plt.xlabel("Time") plt.ylabel("Rate (mm / hr)") plt.ylim(0,3) plt.xlim(0,span) position_tick_labels = [str(round(a[offset]+utc_start, 3)), str(round(a[offset+200]+utc_start, 3)), str(round(a[offset+400]+utc_start, 3)), str(round(a[offset+600]+utc_start, 3)), str(round(a[offset+800]+utc_start, 3)), str(round(a[offset+1000]+utc_start, 3))] pylab.xticks([0,200,400,600,800,1000], position_tick_labels) plt.show()
vg0.detach() # Unhandled tag else: print "unhandled tag,ref",tag,ref # Close vgroup vg.detach() # Open HDF file in readonly mode. filename = sys.argv[1] hdf = HDF(filename) # Initialize the SD, V and VS interfaces on the file. sd = SD(filename) vs = hdf.vstart() v = hdf.vgstart() # Scan all vgroups in the file. ref = -1 while 1: try: ref = v.getid(ref) except HDF4Error,msg: # no more vgroup break describevg(ref) # Terminate V, VS and SD interfaces. v.end() vs.end() sd.end()
# Unhandled tag else: print "unhandled tag,ref", tag, ref # Close vgroup vg.detach() # Open HDF file in readonly mode. filename = sys.argv[1] hdf = HDF(filename) # Initialize the SD, V and VS interfaces on the file. sd = SD(filename) vs = hdf.vstart() v = hdf.vgstart() # Scan all vgroups in the file. ref = -1 while 1: try: ref = v.getid(ref) except HDF4Error, msg: # no more vgroup break describevg(ref) # Terminate V, VS and SD interfaces. v.end() vs.end() sd.end()
def sdscreate(sd, name): # Create a simple 3x3 float array. sds = sd.create(name, SDC.FLOAT32, (3,3)) # Initialize array sds[:] = ((0,1,2),(3,4,5),(6,7,8)) # "close" dataset. sds.endaccess() # Create HDF file filename = 'inventory.hdf' hdf = HDF(filename, HC.WRITE|HC.CREATE) # Initialize the SD, V and VS interfaces on the file. sd = SD(filename, SDC.WRITE) # SD interface vs = hdf.vstart() # vdata interface v = hdf.vgstart() # vgroup interface # Create vdata named 'INVENTORY'. vdatacreate(vs, 'INVENTORY') # Create dataset named "ARR_3x3" sdscreate(sd, 'ARR_3x3') # Attach the vdata and the dataset. vd = vs.attach('INVENTORY') sds = sd.select('ARR_3x3') # Create vgroup named 'TOTAL'. vg = v.create('TOTAL') # Add vdata to the vgroup
import pprint import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl import matplotlib.cm as cm #----------------------------------------------------------------------------------------# path_LIDR = '/mnt/Sanduchera/ftp1.cloudsat.cira.colostate.edu/2B-CLDCLASS-LIDAR.P_R04/' file_path = '' file_name = '2008183012329_11573_CS_2B-CLDCLASS-LIDAR_GRANULE_P_R04_E02.hdf' #----------------------------------------------------------------------------------------# # Read HDF Files (VD data) Latitude & Longitude f = HDF(file_path + file_name, SDC.READ) vs = f.vstart() Latitude = vs.attach('Latitude') Longitude = vs.attach('Longitude') a = Latitude[:] Latitude.detach() Longitude.detach() vs.end() f.close() #----------------------------------------------------------------------------------------# # SDS Data file = SD(file_path + file_name, SDC.READ)
from __future__ import print_function from pyhdf.HDF import * from pyhdf.VS import * f = HDF('inventory.hdf') # open 'inventory.hdf' in read mode vs = f.vstart() # init vdata interface vd = vs.attach('INVENTORY') # attach vdata 'INVENTORY' in read mode # Display some vdata attributes print("status:", vd.status) print("vdata: ", vd._name) # predefined attribute: vdata name print("nrecs: ", vd._nrecs) # predefined attribute: num records # Display value of attribute 'unit' for all fields on which # this attribute is set print("units: ", end=' ') for fieldName in vd._fields: # loop over all field names try: # instantiate field and obtain value of attribute 'unit' v = vd.field(fieldName).unit print("%s: %s" % (fieldName, v), end=' ') except: # no 'unit' attribute: ignore pass print("") print("") # Display table header. header = "%-7s %-12s %3s %4s %8s" % tuple(vd._fields) print("-" * len(header)) print(header)