Skip to content

Instantly share code, notes, and snippets.

@drnextgis
Created July 10, 2011 12:29
Show Gist options
  • Save drnextgis/1074503 to your computer and use it in GitHub Desktop.
Save drnextgis/1074503 to your computer and use it in GitHub Desktop.
mod14-process
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# mod14-process
# Äàìïû è îáðàáîòêà CSV ôàéëîâ ïîñëå ïðîãîíà àëãîðèòìà MOD14
# Author: Maxim Dubinin (sim@gis-lab.info)
# Date created: 22:56 22.05.2011
# Last updated: 13:15 04.06.2011
# ---------------------------------------------------------------------------
import sys
import os
import glob
import csv
if __name__ == '__main__':
for mod14 in glob.glob("MYD14*.hdf"):
root = mod14
of = root[:-4] + ".tmp"
if os.path.exists(of) == True:
print "removing ......" + of
os.remove(of)
fldlist = "FP_line,FP_sample,FP_latitude,FP_longitude,FP_R2,FP_T21,FP_T31,FP_MeanT21,FP_MeanT31,FP_MeanDT,FP_MAD_T21,FP_MAD_T31,FP_MAD_DT,FP_power,FP_AdjCloud,FP_AdjWater,FP_WinSize,FP_NumValid,FP_confidence"
fldlistout = "line,sample,latitude,longitude,R2,T21,T31,MeanT21,MeanT31,MeanDT,MAD_T21,MAD_T31,MAD_DT,power,AdjCloud,AdjWater,WinSize,NumValid,confidence"
cmd = "/hdp dumpsds -n " + fldlist + " -o " + of + " -d " + mod14
print "extracting from ......" + mod14
os.system(cmd)
for f in glob.glob("MYD14*.tmp"):
nf = f[:-4] + ".csv"
#convert horizontal to vertical
fo = open(f,'r')
newlist = list()
templist = list()
ll = fo.readlines()
if len(ll) > 0:
res = open(nf,'wb')
res.write(fldlistout + ",GrName" + "\n")
res.close()
res=csv.writer(open(nf,'ab'),delimiter=",")
for l in ll:
if l != '\n':
nl = l.split(" ")
nl.remove('\n')
for val in nl: templist.append(val)
else:
newlist.append(templist)
cnt = len(templist)
templist = list()
grnamelist = (f[:-4] + " ") * cnt
grnamelist = grnamelist.split(" ")
grnamelist.remove('')
newlist.append(grnamelist)
res.writerows(zip(*newlist))
fo.close()
os.remove(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment