
"""makemap.py:
generates map file for ESGF publication.
Reads in json records from ceda-cc and checksums, verifies files in correctlocation and generates a map file with one record per input file.
"""


##
## uses checksums generated by md5s.py (note md5s.py is truncating the file name at the first "." rather than the last) and other info from Rec.json generated by ceda-cc.
##
import json, os, stat, string

##cmip5.output1.MOHC.HadGEM2-ES.historical.mon.seaIce.OImon.r3i1p1 | /badc/cmip5/data/cmip5/output1/MOHC/HadGEM2-ES/historical/mon/seaIce/OImon/r3i1p1/v20111218/prsn/prsn_OImon_HadGEM2-ES_historical_r3i1p1_195912-200511.nc | 171730144 | mod_time=1324141107.000000 | checksum=8dcacf62707894fe283b4a4c9d3c3548 | checksum_type=MD5

## "path": "/neodc/esa_globsnow/data/v2.0/L3A_daily//ESA-GlobSnow-L3A-SWE-daily-19791003-fv2.0.nc", "drs": {"version": "fv2.0", "algorithm": "Pulliainen2006", "level": "L3A", "convention_version": "na", "project": "ESA-CCI", "ecv": "Snow cover", "standard_name": "lwe_thickness_of_surface_snow_amount", "frequency": "day", "platform": "NIMBUS", "tracking_id": "ba738ecc-259f-4299-ac76-71178d9987e3", "activity": "CLIPC", "variable": "SWE", "sensor": "SMMR", "spatial_resolution": "25 km"

##{"path": "/group_workspaces/jasmin/clipc/esa_globsnow/fixed_files/L3B_monthly//ESA-GlobSnow-L3B-SWE-monthly-197910-fv2.0.nc", "drs": {"version": "fv2.0", "algorithm": "Pulliainen2006", "level": "L3B", "convention_version": "na", "project": "ESA-CCI", "ecv": "Snow cover", "standard_name": "lwe_thickness_of_surface_snow_amount", "frequency": "mon", "platform": "NIMBUS", "tracking_id": "47092dd1-7a86-47b1-952c-3ae3163d3828", "activity": "CLIPC", "variable": "SWE", "sensor": "SMMR", "spatial_resolution": "25 km"}}


tmpl = "%(dsid)s | %(archPath)s | %(fsize)s | mod_time=%(modTime)s | checksum=%(md5sum)s | checksum_type=MD5\n"

this = 'globsnow'
this = 'globsnowMon'

if this == 'globsnowMon':
  dsid = 'CLIPC.ESACCI.SnowCover.mon.L3B.SWE.SMMR.NIMBUS.Pulliainen2006.r1'
  esgfVersion = 'v20150612'
  title = '''# mapfile for ESA Glob Snow
#Generated 12th June by Martin Juckes
''' 
elif this == 'globsnow':
  dsid = 'CLIPC.ESACCI.SnowCover.day.L3A.SWE.SMMR.NIMBUS.Pulliainen2006.r1'
  esgfVersion = 'v20150612'
  title = '''# mapfile for ESA Glob Snow
#Generated 12th June by Martin Juckes
''' 
else:
  dsid = 'CLIPC.ESACCI.SST.day.L4.SSTdepth.multi-sensor.multi-platform.OSTIA2-0.r1.v01-0'
  esgfVersion = 'v20150601'
  title = '''# mapfile for ESA CCI SST data
#Generate 31st May by Martin Juckes
'''

ll = []
oo = open( '%s.%s' % (dsid,esgfVersion), 'w' )
oo.write( title )
for l in open( 'Rec.json' ).readlines():
  ee = json.loads(l)
  ll.append( json.loads(l) )
  archPath = ee['path']
  assert os.path.isfile( archPath )
  fn = string.split( archPath, '/' )[-1]
  md5fn = string.split(fn,'.')[0] + '.md5sum'
  assert os.path.isfile( 'md5s/%s' % md5fn ), 'md5 file %s not found' % md5fn
  ii1 = open( 'md5s/%s' % md5fn ).readlines()
  md5sum, fn2 = string.split( string.strip( ii1[0] ) )
  assert fn2 == fn, 'file name mismatch %s, %s' % (fn,fn2 )
  st = os.stat( archPath )
  fsize = st[stat.ST_SIZE]
  modTime = float(st[stat.ST_MTIME])
  rec = tmpl % locals()
  oo.write(rec)

print rec
oo.close()

print len(ll)
print ll[-1]
  
