program warp

integer,parameter                :: nx=360,ny=180
integer,dimension(nx,ny)         :: mask
real,dimension(nx,ny)            :: indat,newgrid,garea,c,p,v,fa
character(len=90)                :: fin,fout,line


fin='v1120_bpot.dat'
open(14,file=fin,status='old')

!do ix=1,nx
do iy=1,ny
do ix=1,nx  !* correct way
    read(14,*) indat(ix,iy)
    if(indat(ix,iy).lt.0.0) indat(ix,iy)=0.0
end do
end do
close(14)




fin='/raid1/gh/matt/glm/cellarea/cellarea.1deg.nohead.txt'
open(14,file=fin,status='old')

do iy=1,ny
    read(14,*) garea(:nx,iy)
end do
close(14)
garea=garea*1000.0*1000.0  !* convert km2 to m2


fin='/raid1/gh/matt/glm/processed/land_a/1deg_grids/gcrop.1700.txt'
open(14,file=fin,status='old')

do iy=1,6
  read(14,*) line
end do
do iy=1,ny
    read(14,*) c(:nx,iy)
end do
close(14)

fin='/raid1/gh/matt/glm/processed/land_a/1deg_grids/gpast.1700.txt'
open(14,file=fin,status='old')

do iy=1,6
  read(14,*) line
end do
do iy=1,ny
    read(14,*) p(:nx,iy)
end do
close(14)

fin='/raid1/gh/matt/glm/processed/land_a/1deg_grids/gothr.1700.txt'
open(14,file=fin,status='old')

do iy=1,6
  read(14,*) line
end do
do iy=1,ny
    read(14,*) v(:nx,iy) 
end do
close(14)

fin='/raid1/gh/matt/glm/glm.landmsk.131.txt'
open(14,file=fin,status='old')

do iy=1,ny
    read(14,*) mask(:nx,iy)
end do
close(14)


fa=c+p+v
print *, sum(indat*garea*fa,indat*0.75>2) 

!print *, sum(indat*fa*garea*mask)
stop

!* output of new carbon grid 360x180

fout='miami_1deg.txt'
open(15,file=fout,status='unknown')

do iy=1,ny 
  write(15,'(360(f8.2,1x))') indat(:nx,iy)
end do
close(15)

stop
end
