Пример #1
0
import PyRaster

gridNames=["D:/projekt/Kenneths_jobb/case1/odor-y00a01.asc",
           "D:/projekt/Kenneths_jobb/case1/odor-y00a02.asc",
           "D:/projekt/Kenneths_jobb/case1/odor-y00a03.asc",
           "D:/projekt/Kenneths_jobb/case1/odor-y00a04.asc",
           "D:/projekt/Kenneths_jobb/case1/odor-y00a05.asc",
           "D:/projekt/Kenneths_jobb/case1/odor-y00a06.asc",
           "D:/projekt/Kenneths_jobb/case1/odor-y00a07.asc"]

outfile="D:/projekt/Kenneths_jobb/case1/case1_points.txt"
of=open(outfile,'w')
of.write("X\tY\tZ\n")

points=[]
for gn in gridNames:
    print "reading: ",gn
    grid=PyRaster.raster()
    grid.readAscii(gn)
    points+=grid.points()

for p in points:
    if p[2]!=0:
        of.write(str(p[0])+"\t"+str(p[1])+"\t"+str(p[2])+"\n")

of.close()
Пример #2
0
reduce = False


#--------------------------------------------------
#Emep grid raster definition
emepXll=0.5
emepYll=0.5
emepCellsize=1.0
emepNcols=150 #official EMEP 132
emepNrows=150 #Official EMEP 111
emepNodata=-9999

for substance in substances:
    rasters={}
    for id in searches:
        rastFileName=path.join(shipairInputDir,id+"_"+substance+".asc")
        rast=PyRaster.raster()
        rast.readAscii(rastFileName)
        rasters[id]=rast
    
    domesticRasters=[rasters[id] for id in searches if "dom" in id]
        
    emepFileName=path.join(emepInputDir,substance+"_"+unit+".asc")
    emepRast=PyRaster.raster()
    emepRast.readAscii(emepFileName)
    
    intNatNav=rasters['tot']
    for id in domesticRasters:
        intNatNav = intNatNav - rasters[id] 

print "finished!"
#corrFactorYtor=3.13320858120972 #2020v3
#--------------------
summaryFilePath=os.path.join(utDir,summaryFileName)
resFid=open(summaryFilePath,'w')

resFid.write("Correction factor for SAMS-surface emissions is: "+str(corrFactorYtor)+"\n")
for subst in substances:
    print "substance: ", subst
    roadPathStr=roadDir+"/*"+subst+"*.asc"
    fileList=glob.glob(roadPathStr)
    roadFile=fileList[0]
    ytPathStr=ytDir+"/*"+subst+"*"
    fileList=glob.glob(ytPathStr)
    ytFile=fileList[0]
    #print "ytfil: ", ytFile
    ytRast=PyRaster.raster()
    ytRast.readAscii(ytFile)
    
    roadRast=PyRaster.raster()    
    roadRast.readAscii(roadFile)
    
    ytRast=ytRast*corrFactorYtor
    
    utRast=ytRast+roadRast
    utRastName=os.path.join(utDir,subst+"_totTrafficCorr.asc")
    utRastPath=os.path.join(utDir,utRastName)
    utRast.write(utRastPath)
    
    resFid.write(subst+"\tytsumma: "+str(ytRast.sum())+"\tlanksumma: "+str(roadRast.sum())+"\ttotalSumma: "+str(utRast.sum())+"\n")
    
resFid.close()
Both regional and national emission data should be on rasters of the same size, extent and resolution

written by David Segersson, 080205
"""
import PyRaster, os, sys, numpy

#-----------------------------------

regionDefinitionRasterFile= "\\Winfs\data\prod\Smeddata\Geofordelning\geodata\asciiln07.asc" #Raster that defines the region for which emissions are to be replaced
natRastFile="\\Winfs\data\proj\Luftkvalitet\emissioner\2003\emissionsfiler\ascii\simairVedair\ #Raster with national emission data (e.g. SMED)
regRastFile= #Raster with regional emission data( e.g. SLB)
regFormat="airviro" #could also be "XY" or "ArcGIS"
maskRegionCode= [1, 3] #The codes in the region definition raster defining the area for which daa is to be replaced
outputFilePath=
#-----------------------------------
natRast=PyRaster.raster()
regRast=PyRaster.raster()
maskRast=PyRaster.raster()

if not os.path.exists(maskRastFile):
    sys.exit("Mask raster does not exist")
if not os.path.exists(nationalRastFile):
    sys.exit("National raster does not exist")
if not os.path.exists(regRastFile):
    sys.exit("Regional raster does not exist")


regRast.readAscii(regRastFile)
regRast.nodataToZero()
natRast.readAscii(natRastFile)
natRast.nodataToZero()
Пример #5
0
    mask=numpy.array(regionArray==code)
    regKey=mask*data
    regSum=numpy.sum(numpy.sum(regKey))
    #These two are put here to ensure that all threads are executed equally fast.
    onesRast=mask*(numpy.ones(mask.shape)*1.0)
    regMaskSum=numpy.sum(numpy.sum(onesRast))
    if regSum>0:
        result=result+regKey/regSum
    elif regionalTotal>0:
        print "Warning, for code: ",code," the distribution key is zero but the regional total is: ",regionalTotal
        print "The regional total is distributed uniformly over the whole region!"        
        result=result+onesRast/regMaskSum      
    return result

keyRast= PyRaster.raster() # skapar ett initialt tomt raster
regionRast= PyRaster.raster()# skapar ett initialt tomt raster

#Laser in indata efter att ha kontrollerat att filerna finns
if os.path.exists(regionRasterPath):
    print "Reading region raster"
    regionRast.readAscii(regionRasterPath)  
else:
    sys.exit("regionRaster does not exist")

regionalTotals=pyEmissions.RegTotalEmis("dummy")
if os.path.exists(regionalTotalsPath):
    print "Reading regional totals"
    regionalTotals.readAscii(regionalTotalsPath)
else:
    sys.exit("Regional totals file does not exist")
Пример #6
0
import PyRaster
import os, sys, numpy

# maskFile="//Winfs/data/proj/Luftkvalitet/Masker/alla_masker.asc"
maskFile = "//Winfs/data/prod/Smeddata/Geofordelning/2007/regionRasters/Cat_01_2007_kommungrupper.asc"
emisFile = (
    "//Winfs/data/prod/Smeddata/Geofordelning/2007/kvalitetskontroller/ElVarmeverk_u/ElVarmeverk_uCO2tonperyear2007.asc"
)
flippa = 0
# --------------------
outputDir = os.path.dirname(emisFile)
summaryFile = "sumByMask_" + os.path.basename(emisFile)
summaryPath = os.path.join(outputDir, summaryFile)
output = open(summaryPath, "w")

maskRast = PyRaster.raster()
maskRast.readAscii(maskFile)
maskRast.nodataToZero()

emisRast = PyRaster.raster()
emisRast.readAscii(emisFile)
emisRast.nodataToZero()
if flippa:
    emisRast.flip()
print "Finding all mask codes"
maskCodes = maskRast.unique()

if maskRast.nodata in maskCodes:
    maskCodes.remove(maskRast.nodata)
if 0.0 in maskCodes:
    maskCodes.remove(0.0)
Пример #7
0
subst="PM10"
totRastFile="//Winfs/data/proj/Luftkvalitet/emissioner/2007/Arbetsmaterial/SLB/SLB_stockholmuppsala_t07/"+subst+"_tot.txt"
#totRastFile="D:/Simair/SLB/SLB_stockholmuppsala_s05/"+subst+"_tot.txt"
rootDir="//Winfs/data/proj/Luftkvalitet/emissioner/2007/Arbetsmaterial/SLB/SLB_stockholmuppsala_t07"
#rootDir="D:/Simair/SLB/SLB_stockholmuppsala_s05"
navigationRastFile=path.join(rootDir,"navigation","navigation"+subst+"gpers2007reg.asc")
trafficRastFile=path.join(rootDir,"traffic","traffic"+subst+"gpers2007reg.asc")
residentialRastFile=path.join(rootDir,"residential","residential"+subst+"gpers2007reg.asc")
trafficRastPM10Tot=path.join(rootDir,"traffic","traffic"+subst+"Tot"+"gpers2007reg.asc")
outputOtherRast=path.join(rootDir,"other","other"+subst+"gpers2007reg.asc")

subtractResuspForPM10=True                       
#-----------------------------------

if path.exists(totRastFile):
    totRast=PyRaster.raster()
    totRast.readAscii(totRastFile)
    totRast.nodataToZero()
    print "Read total emissions, sum is: ", totRast.sum()
else:
    sys.exit("Total raster does not exist")

if path.exists(navigationRastFile):
    navRast=PyRaster.raster()
    navRast.readAscii(navigationRastFile)
    navRast.nodataToZero()
    print "Read navigation emissions, sum is: ", navRast.sum()
else:
    sys.exit("Navigation raster does not exist")

if subst!="PM10":
Пример #8
0
import PyRaster
import os,sys,numpy

#maskFile="//Winfs/data/proj/Luftkvalitet/Masker/alla_masker.asc"
maskFile="//Winfs/data/prod/Smeddata/Geofordelning/2007/regionRasters/Cat_01_2007_kommungrupper.asc"
emisFile="//Winfs/data/prod/Smeddata/Geofordelning/2007/kvalitetskontroller/ElVarmeverk_u/ElVarmeverk_uCO2tonperyear2007.asc"
flippa=0
#--------------------
outputDir=os.path.dirname(emisFile)
summaryFile="sumByMask_"+os.path.basename(emisFile)
summaryPath=os.path.join(outputDir,summaryFile)
output=open(summaryPath,'w')

maskRast=PyRaster.raster()
maskRast.readAscii(maskFile)
maskRast.nodataToZero()

emisRast=PyRaster.raster()
emisRast.readAscii(emisFile)
emisRast.nodataToZero()
if flippa:
    emisRast.flip()
print "Finding all mask codes"
maskCodes=maskRast.unique()

if maskRast.nodata in maskCodes:
    maskCodes.remove(maskRast.nodata)
if 0.0 in maskCodes:
    maskCodes.remove(0.0)

ortRast=PyRaster.raster()
Пример #9
0
substances=["NH3","NOx","SO2"]
maskDirectory="//Winfs/data/proj/Luftkvalitet/Masker"
maskFileNameList=["mask0.ascii","mask1.ascii","mask2.ascii","mask3.ascii","mask4.ascii","mask5.ascii","mask6.ascii","mask7.ascii"]
outputDirectory="D:/projekt/geo/output/2007/miljoovervakning/masked"
outputFileNameExtension=["_0","_1","_2","_3","_4","_5","_6","_7"]
outputUnits="Gg/year"
inputUnits="g/s"
writeNational=True #Flag write the national emission grids in the given outputUnits

#Se comment in code for specification of file names of national raters
#-----------------------------------------------

print "Reading mask rasters"
maskRasters=[]
for maskFileName in maskFileNameList:
    maskRast=PyRaster.raster()
    maskPath=os.path.join(maskDirectory,maskFileName)
    if not os.path.exists(maskPath):
        sys.exit("Mask raster: "+maskFileName+" does not exist")
    maskRast.readAscii(maskPath)
    maskRast.nodataToZero()
    maskRasters.append(maskRast)

for cat in categories:
    for subst in substances:
        units=inputUnits.split("/")
        nationalRasterFileName=cat+subst+units[0]+"per"+units[1]+"2007.asc" #Adjust this row for other file names
        nationalRasterPath=os.path.join(nationalRasterDir,nationalRasterFileName)
        if not os.path.exists(nationalRasterPath):
            print "Raster "+nationalRasterPath+"not found"
        else:    
Пример #10
0
#rasters into one raster covering the whole country

inputDir="//Winfs/data/proj/Luftkvalitet/Emis/2003/Ascii"
outputDir="//Winfs/data/proj/Luftkvalitet/emissioner/2003/emissionsfiler/ascii/simairVedair"
masks=["ma0b","ma1b","ma2b","ma3b","ma4b","ma5b","ma6b","ma7b"]
cats=["fix","sea","tra"]
substances=["pm","nox","co","benzene"]
outCats=["other_and_residential","navigation","traffic"]
outSubst=["PM10","NOx","CO","Bensen"]


for catInd,cat in enumerate(cats):
    for substInd, subst in enumerate(substances):
        print "category: ", cat, " subst: ", subst

        riksRast=PyRaster.raster()
        riksRast.nodataToZero()
        checkSum=0
        for mask in masks:
            print "mask: ", mask
            fileName="q"+subst+cat+"2003"+mask+".asc"
            maskRast=PyRaster.raster()
            maskRast.readAscii(os.path.join(inputDir,fileName))
            maskRast.nodataToZero()
            
            riksRast=riksRast+maskRast
            print "Mask rast sum: ",maskRast.sum(),"Tot rast Sum: ",riksRast.sum()
            
        outputFileName=outCats[catInd]+outSubst[substInd]+"gpers2003.asc"
        
        riksRast.write(os.path.join(outputDir,outputFileName))
Пример #11
0
             }

orgShipRasters={"NOx":"D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_ships_ascii/S8/NOx_Mg.asc",
             "SOx":"D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_ships_ascii/S8/SOx_Mg.asc",
             "PM2.5":"D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_ships_ascii/S8/PM2.5_Mg.asc",
             "PMcoarse":"D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_ships_ascii/S8/PMcoarse_Mg.asc",
             "NMVOC":"D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_ships_ascii/S8/NMVOC_Mg.asc",
             }

outputDir="D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_ascii/S8"
outputDirShipsReduced20proc="D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_shipsReduced20proc_ascii/S8"
outputDirSweReduced20Proc="D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_SweReduced20proc_ascii/S8"
outputDirEurReduced20Proc="D:/projekt/PRIMES/EMEP_SMA/PrimesBaseline09_2020_EurReduced20proc_ascii/S8"
SECA_mask_file="//winfs/data/proj/Ml-data/users/David/Projekt/2010/PRIMES/seca_mask_emep50km.asc"

SECA_mask=PyRaster.raster()
SECA_mask.readAscii(SECA_mask_file)
SECA_mask.nodataToZero()
nonSECA_mask=SECA_mask.replace([(1,2),(0,1)])
nonSECA_mask=nonSECA_mask.replace([(2,0)])

nonSECA_mask.write("//winfs/data/proj/Ml-data/users/David/Projekt/2010/PRIMES/nonSECA_mask_emep50km.asc")
SECA_mask.write(SECA_mask_file)

SECA_mask_factor=SECA_mask*sulphurFactorSECA + nonSECA_mask
nonSECA_mask_factor=nonSECA_mask*sulphurFactorNonSECA+SECA_mask
print "Read input data"

for substance in shipRasters:
    print "Processing substance %s" %substance
    shipRast=PyRaster.raster()
Пример #12
0
    mask = numpy.array(regionArray == code)
    regKey = mask * data
    regSum = numpy.sum(numpy.sum(regKey))
    # These two are put here to ensure that all threads are executed equally fast.
    onesRast = mask * (numpy.ones(mask.shape) * 1.0)
    regMaskSum = numpy.sum(numpy.sum(onesRast))
    if regSum > 0:
        result = result + regKey / regSum
    elif regionalTotal > 0:
        print "Warning, for code: ", code, " the distribution key is zero but the regional total is: ", regionalTotal
        print "The regional total is distributed uniformly over the whole region!"
        result = result + onesRast / regMaskSum
    return result


keyRast = PyRaster.raster()  # skapar ett initialt tomt raster
regionRast = PyRaster.raster()  # skapar ett initialt tomt raster

# Laser in indata efter att ha kontrollerat att filerna finns
if os.path.exists(regionRasterPath):
    print "Reading region raster"
    regionRast.readAscii(regionRasterPath)
else:
    sys.exit("regionRaster does not exist")

regionalTotals = pyEmissions.RegTotalEmis("dummy")
if os.path.exists(regionalTotalsPath):
    print "Reading regional totals"
    regionalTotals.readAscii(regionalTotalsPath)
else:
    sys.exit("Regional totals file does not exist")
Пример #13
0
import PyRaster, glob, os, string

wdirs=["//Winfs/data/prod/Smeddata/Geofordelning/2006/aggregates/simair/traffic",
    "//Winfs/data/prod/Smeddata/Geofordelning/2006/aggregates/simair/residential",
    "//Winfs/data/prod/Smeddata/Geofordelning/2006/aggregates/simair/navigation",
    "//Winfs/data/prod/Smeddata/Geofordelning/2006/aggregates/simair/other"]
#wdirs=["D:/SMED_temp/Geo_fordelning/data2005/BUM_2010/other_2010",
#    "D:/SMED_temp/Geo_fordelning/data2005/BUM_2020/other_2020"]
logFile="VOC_till_Bensen_konvertering.txt"
scalingFactor=0.0344827586206897
for wd in wdirs:
    logFid=open(os.path.join(wd,logFile),'w')
    logFid.write("summa Bensen och NMVOC raster\n")
    searchStr=os.path.join(wd,"*NMVOC*")
    files=glob.glob(searchStr)
    
    fileName=files[0]
    bensRast=PyRaster.raster()
    bensRast.readAscii(fileName)
    logFid.write("Summa NMVOC: "+str(bensRast.sum())+"g/s\n")
    logFid.write("Skalningsfactor mellan NMVOC och Bensen: "+str(scalingFactor)+"\n")
    bensRast=bensRast*scalingFactor
    logFid.write("Summa Bensen: "+str(bensRast.sum())+"g/s\n")
    logFid.close()
    bensFileName=string.replace(fileName, "NMVOC", "Bensen")
    bensRast.write(bensFileName)
print 'klar'