Ignore:
Timestamp:
May 30, 2023, 5:35:09 PM (21 months ago)
Author:
romain.vande
Message:

Mars util :

Adapt expandstartfi to subslope dimension + small correction regarding depreciated dimension "number_of_advected_field"

RV

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LMDZ.MARS/util/expandstartfi.F90

    r1469 r2975  
    2020integer :: varid ! to store the ID of a variable
    2121integer :: datashape(4) ! to store dimension IDs of a given dataset
    22 integer :: corner(3),edges(3) ! to read data with a time axis
     22integer :: corner(4),edges(4) ! to read data with a time axis
    2323character(len=90) :: varname ! name of a variable
    2424character(len=90) :: varatt ! name of attribute of a variable
     
    2828integer :: subsurface_layers_dimid
    2929integer :: nlayer_plus_1_dimid
     30integer :: nslope_dimid
    3031integer :: number_of_advected_fields_dimid
    3132integer :: time_dimid
     
    3738integer :: subsurface_layers
    3839integer :: nlayer_plus_1
     40integer :: nslope
    3941integer :: number_of_advected_fields
    4042integer :: timelen
    4143real,allocatable :: surf_field(:) ! to store a 1D field of physical_points elements
    4244real,allocatable :: subsurf_field(:,:) ! to store subsurface (2D field)
     45real,allocatable :: subslope_field(:,:)
     46real,allocatable :: nlayer_plus_1_field(:,:)
     47real,allocatable :: subsurf_subslope_field(:,:,:)
    4348
    4449! Output file dimensions:
     
    5156real,allocatable :: out_surf_field(:,:)
    5257real,allocatable :: out_subsurf_field(:,:,:)
     58real,allocatable :: out_subslope_field(:,:,:)
     59real,allocatable :: out_nlayer_plus_1_field(:,:,:)
     60real,allocatable :: out_subsurf_subslope_field(:,:,:,:)
    5361! Output file dimension IDs
    5462integer :: lon_dimid
    5563integer :: lat_dimid
    5664integer :: depth_dimid
     65integer :: nslope_out_dimid
     66integer :: nlayer_plus_1_out_dimid
    5767! IDs of Output file variables
    5868integer :: lon_varid
     
    143153endif
    144154
    145 status=nf90_inq_dimid(inid,"number_of_advected_fields",number_of_advected_fields_dimid)
    146 if (status.ne.nf90_noerr) then
    147   write(*,*)"Failed to find number_of_advected_fields dimension"
    148   write(*,*)trim(nf90_strerror(status))
    149  stop
    150 endif
    151 status=nf90_inquire_dimension(inid,number_of_advected_fields_dimid,len=number_of_advected_fields)
    152 if (status.ne.nf90_noerr) then
    153   write(*,*)"Failed to find number_of_advected_fields value"
     155status=nf90_inq_dimid(inid,"nslope",nslope_dimid)
     156if (status.ne.nf90_noerr) then
     157  write(*,*)"Failed to find slope dimension, old startfi file"
     158  nslope=0
     159  write(*,*)trim(nf90_strerror(status))
     160else
     161status=nf90_inquire_dimension(inid,nslope_dimid,len=nslope)
     162if (status.ne.nf90_noerr) then
     163  write(*,*)"Failed to find nslope value"
    154164  write(*,*)trim(nf90_strerror(status))
    155165 stop
    156166else
    157   write(*,*) " number_of_advected_fields = ",number_of_advected_fields
     167  write(*,*) " nslope = ",nslope
     168endif
    158169endif
    159170
     
    177188allocate(surf_field(physical_points))
    178189allocate(subsurf_field(physical_points,subsurface_layers))
     190allocate(subslope_field(physical_points,nslope))
     191allocate(nlayer_plus_1_field(physical_points,nlayer_plus_1))
     192allocate(subsurf_subslope_field(physical_points,subsurface_layers,nslope))
    179193
    180194! 2.1. Create output file
     
    296310  stop
    297311endif
     312! nslope:
     313status=nf90_def_dim(outid,"nslope",nslope,nslope_out_dimid)
     314if (status.ne.nf90_noerr) then
     315  write(*,*) "Failed creating nslope dimension"
     316  write(*,*)trim(nf90_strerror(status))
     317  stop
     318endif
     319! nslope:
     320status=nf90_def_dim(outid,"nlayer_plus_1",nlayer_plus_1,nlayer_plus_1_out_dimid)
     321if (status.ne.nf90_noerr) then
     322  write(*,*) "Failed creating nslope dimension"
     323  write(*,*)trim(nf90_strerror(status))
     324  stop
     325endif
    298326
    299327!2.3. write variables to output file
     
    379407
    380408allocate(out_surf_field(lonlen,latlen))
    381 
    382 shape(:) = 0
     409allocate(out_subsurf_field(lonlen,latlen,subsurface_layers))
     410allocate(out_subslope_field(lonlen,latlen,nslope))
     411allocate(out_nlayer_plus_1_field(lonlen,latlen,nlayer_plus_1))
     412allocate(out_subsurf_subslope_field(lonlen,latlen,subsurface_layers,nslope))
     413
    383414do ivar=1,nbinvars ! loop on all input variables
     415  shape(:) = 0
    384416  ! find out what dimensions are linked to this variable
    385417  status=nf90_inquire_variable(inid,ivar,name=varname,ndims=nbdim,&
    386418                               dimids=shape,natts=nbatt)
     419
    387420  if (((nbdim==1).and.(shape(1)==physical_points_dimid))&
    388421  .or.((nbdim==2).and.(shape(1)==physical_points_dimid)&
     
    473506    endif
    474507  endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid))
    475 enddo ! of do i=1,nbinvars
    476 
    477 ! 2.4. 3D (subsurface) variables
    478 allocate(out_subsurf_field(lonlen,latlen,subsurface_layers))
    479 
    480 do ivar=1,nbinvars ! loop on all input variables
    481   ! find out what dimensions are linked to this variable
    482   status=nf90_inquire_variable(inid,ivar,name=varname,ndims=nbdim,&
    483                                dimids=shape,natts=nbatt)
    484   if (((nbdim==2).and.(shape(1)==physical_points_dimid)&
    485                  .and.(shape(2)==subsurface_layers_dimid))&
    486   .or.((nbdim==3).and.(shape(1)==physical_points_dimid)&
    487                  .and.(shape(2)==subsurface_layers_dimid)&
    488                  .and.(shape(3)==time_dimid))) then
     508
     509  if ((nbdim==2).and.(shape(1)==physical_points_dimid)&
     510                 .and.(shape(2)==subsurface_layers_dimid)) then
    489511   
    490512    corner(1) = 1
     
    570592    endif
    571593  endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid)...)
     594
     595
     596
     597
     598  if ((nbdim==3).and.(shape(1)==physical_points_dimid)&
     599                 .and.(shape(2)==nslope_dimid)) then
     600   
     601    corner(1) = 1
     602    corner(2) = 1
     603    corner(3) = timelen
     604    edges(1)  = physical_points
     605    edges(2)  = nslope
     606    edges(3)  = 1
     607   
     608    write(*,*) " processing: ",trim(varname)
     609
     610    ! load input data:
     611    status=nf90_inq_varid(inid,varname,invarid)
     612    status=nf90_get_var(inid,invarid,subslope_field,corner,edges)
     613   
     614    ! switch output file to to define mode
     615    status=nf90_redef(outid)
     616    if (status.ne.nf90_noerr) then
     617      write(*,*) "Failed to swich to define mode"
     618      write(*,*)trim(nf90_strerror(status))
     619      stop
     620    endif
     621    !define the variable
     622    status=nf90_def_var(outid,trim(varname),NF90_REAL,&
     623                         (/lon_dimid,lat_dimid,nslope_out_dimid/),varid)
     624    if (status.ne.nf90_noerr) then
     625      write(*,*) "Failed creating variable ",trim(varname)
     626      write(*,*)trim(nf90_strerror(status))
     627      stop
     628    endif
     629
     630    ! variable attributes
     631    do k=1,nbatt
     632      status=nf90_inq_attname(inid,invarid,k,varatt)
     633      if (status.ne.nf90_noerr) then
     634        write(*,*) "Failed getting attribute number",k," for ",trim(varname)
     635        write(*,*)trim(nf90_strerror(status))
     636      stop
     637      endif
     638      status=nf90_get_att(inid,invarid,trim(varatt),varattcontent)
     639      if (status.ne.nf90_noerr) then
     640        write(*,*) "Failed loading attribute ",trim(varatt)
     641        write(*,*)trim(nf90_strerror(status))
     642      stop
     643      endif
     644
     645      ! write the attribute and its value to output
     646      status=nf90_put_att(outid,varid,trim(varatt),trim(varattcontent))
     647      if (status.ne.nf90_noerr) then
     648        write(*,*) "Failed writing attribute ",trim(varatt)
     649        write(*,*)trim(nf90_strerror(status))
     650      stop
     651      endif
     652    enddo ! do k=1,nbatt
     653
     654    ! swich out of NetCDF define mode
     655    status=nf90_enddef(outid)
     656    if (status.ne.nf90_noerr) then
     657      write(*,*) "Failed to swich out of define mode"
     658      write(*,*)trim(nf90_strerror(status))
     659      stop
     660    endif
     661   
     662    ! "convert" from subsurf_field(:,:) to out_subslope_field(:,:,:)
     663    do i=1,lonlen
     664      out_subslope_field(i,1,:)=subslope_field(1,:) ! North Pole
     665      out_subslope_field(i,latlen,:)=subslope_field(physical_points,:) ! South Pole
     666    enddo
     667    do j=2,latlen-1
     668      ig0=1+(j-2)*(lonlen-1)
     669      do i=1,lonlen-1
     670        out_subslope_field(i,j,:)=subslope_field(ig0+i,:)
     671      enddo
     672      out_subslope_field(lonlen,j,:)=out_subslope_field(1,j,:) ! redundant lon=180
     673    enddo
     674   
     675    ! write the variable
     676    status=nf90_put_var(outid,varid,out_subslope_field)
     677    if (status.ne.nf90_noerr) then
     678      write(*,*) "Failed to write ",trim(varname)
     679      write(*,*)trim(nf90_strerror(status))
     680      stop
     681    endif
     682  endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid)...)
     683
     684
     685
     686
     687
     688  if ((nbdim==3).and.(shape(1)==physical_points_dimid)&
     689                 .and.(shape(2)==nlayer_plus_1_dimid)) then
     690   
     691    corner(1) = 1
     692    corner(2) = 1
     693    corner(3) = timelen
     694    edges(1)  = physical_points
     695    edges(2)  = nlayer_plus_1
     696    edges(3)  = 1
     697   
     698    write(*,*) " processing: ",trim(varname)
     699
     700    ! load input data:
     701    status=nf90_inq_varid(inid,varname,invarid)
     702    status=nf90_get_var(inid,invarid,nlayer_plus_1_field,corner,edges)
     703   
     704    ! switch output file to to define mode
     705    status=nf90_redef(outid)
     706    if (status.ne.nf90_noerr) then
     707      write(*,*) "Failed to swich to define mode"
     708      write(*,*)trim(nf90_strerror(status))
     709      stop
     710    endif
     711    !define the variable
     712    status=nf90_def_var(outid,trim(varname),NF90_REAL,&
     713                         (/lon_dimid,lat_dimid,nlayer_plus_1_out_dimid/),varid)
     714    if (status.ne.nf90_noerr) then
     715      write(*,*) "Failed creating variable ",trim(varname)
     716      write(*,*)trim(nf90_strerror(status))
     717      stop
     718    endif
     719
     720    ! variable attributes
     721    do k=1,nbatt
     722      status=nf90_inq_attname(inid,invarid,k,varatt)
     723      if (status.ne.nf90_noerr) then
     724        write(*,*) "Failed getting attribute number",k," for ",trim(varname)
     725        write(*,*)trim(nf90_strerror(status))
     726      stop
     727      endif
     728      status=nf90_get_att(inid,invarid,trim(varatt),varattcontent)
     729      if (status.ne.nf90_noerr) then
     730        write(*,*) "Failed loading attribute ",trim(varatt)
     731        write(*,*)trim(nf90_strerror(status))
     732      stop
     733      endif
     734
     735      ! write the attribute and its value to output
     736      status=nf90_put_att(outid,varid,trim(varatt),trim(varattcontent))
     737      if (status.ne.nf90_noerr) then
     738        write(*,*) "Failed writing attribute ",trim(varatt)
     739        write(*,*)trim(nf90_strerror(status))
     740      stop
     741      endif
     742    enddo ! do k=1,nbatt
     743
     744    ! swich out of NetCDF define mode
     745    status=nf90_enddef(outid)
     746    if (status.ne.nf90_noerr) then
     747      write(*,*) "Failed to swich out of define mode"
     748      write(*,*)trim(nf90_strerror(status))
     749      stop
     750    endif
     751   
     752    ! "convert" from subsurf_field(:,:) to out_nlayer_plus_1_field(:,:,:)
     753    do i=1,lonlen
     754      out_nlayer_plus_1_field(i,1,:)=nlayer_plus_1_field(1,:) ! North Pole
     755      out_nlayer_plus_1_field(i,latlen,:)=nlayer_plus_1_field(physical_points,:) ! South Pole
     756    enddo
     757    do j=2,latlen-1
     758      ig0=1+(j-2)*(lonlen-1)
     759      do i=1,lonlen-1
     760        out_nlayer_plus_1_field(i,j,:)=nlayer_plus_1_field(ig0+i,:)
     761      enddo
     762      out_nlayer_plus_1_field(lonlen,j,:)=out_nlayer_plus_1_field(1,j,:) ! redundant lon=180
     763    enddo
     764   
     765    ! write the variable
     766    status=nf90_put_var(outid,varid,out_nlayer_plus_1_field)
     767    if (status.ne.nf90_noerr) then
     768      write(*,*) "Failed to write ",trim(varname)
     769      write(*,*)trim(nf90_strerror(status))
     770      stop
     771    endif
     772  endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid)...)
     773
     774
     775
     776
     777  if (nbdim==4) then
     778   
     779    corner(1) = 1
     780    corner(2) = 1
     781    corner(3) = 1
     782    corner(4) = timelen
     783    edges(1)  = physical_points
     784    edges(2)  = subsurface_layers
     785    edges(3)  = nslope
     786    edges(4)  = 1
     787   
     788    write(*,*) " processing: ",trim(varname)
     789
     790    ! load input data:
     791    status=nf90_inq_varid(inid,varname,invarid)
     792    status=nf90_get_var(inid,invarid,subsurf_subslope_field,corner,edges)
     793   
     794    ! switch output file to to define mode
     795    status=nf90_redef(outid)
     796    if (status.ne.nf90_noerr) then
     797      write(*,*) "Failed to swich to define mode"
     798      write(*,*)trim(nf90_strerror(status))
     799      stop
     800    endif
     801    !define the variable
     802    print *, "subsurface_layers_dimid", subsurface_layers_dimid
     803    print *, "nslope_out_dimid", nslope_out_dimid
     804    status=nf90_def_var(outid,trim(varname),NF90_REAL,&
     805                         (/lon_dimid,lat_dimid,depth_dimid,nslope_out_dimid/),varid)
     806    if (status.ne.nf90_noerr) then
     807      write(*,*) "Failed creating variable ",trim(varname)
     808      write(*,*)trim(nf90_strerror(status))
     809      stop
     810    endif
     811
     812    ! variable attributes
     813    do k=1,nbatt
     814      status=nf90_inq_attname(inid,invarid,k,varatt)
     815      if (status.ne.nf90_noerr) then
     816        write(*,*) "Failed getting attribute number",k," for ",trim(varname)
     817        write(*,*)trim(nf90_strerror(status))
     818      stop
     819      endif
     820      status=nf90_get_att(inid,invarid,trim(varatt),varattcontent)
     821      if (status.ne.nf90_noerr) then
     822        write(*,*) "Failed loading attribute ",trim(varatt)
     823        write(*,*)trim(nf90_strerror(status))
     824      stop
     825      endif
     826
     827      ! write the attribute and its value to output
     828      status=nf90_put_att(outid,varid,trim(varatt),trim(varattcontent))
     829      if (status.ne.nf90_noerr) then
     830        write(*,*) "Failed writing attribute ",trim(varatt)
     831        write(*,*)trim(nf90_strerror(status))
     832      stop
     833      endif
     834    enddo ! do k=1,nbatt
     835
     836    ! swich out of NetCDF define mode
     837    status=nf90_enddef(outid)
     838    if (status.ne.nf90_noerr) then
     839      write(*,*) "Failed to swich out of define mode"
     840      write(*,*)trim(nf90_strerror(status))
     841      stop
     842    endif
     843   
     844    ! "convert" from subsurf_field(:,:) to out_subsurf_subslope_field(:,:,:)
     845    do i=1,lonlen
     846      out_subsurf_subslope_field(i,1,:,:)=subsurf_subslope_field(1,:,:) ! North Pole
     847      out_subsurf_subslope_field(i,latlen,:,:)=subsurf_subslope_field(physical_points,:,:) ! South Pole
     848    enddo
     849    do j=2,latlen-1
     850      ig0=1+(j-2)*(lonlen-1)
     851      do i=1,lonlen-1
     852        out_subsurf_subslope_field(i,j,:,:)=subsurf_subslope_field(ig0+i,:,:)
     853      enddo
     854      out_subsurf_subslope_field(lonlen,j,:,:)=out_subsurf_subslope_field(1,j,:,:) ! redundant lon=180
     855    enddo
     856   
     857    ! write the variable
     858    status=nf90_put_var(outid,varid,out_subsurf_subslope_field)
     859    if (status.ne.nf90_noerr) then
     860      write(*,*) "Failed to write ",trim(varname)
     861      write(*,*)trim(nf90_strerror(status))
     862      stop
     863    endif
     864  endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid)...)
     865
     866
     867
    572868enddo ! of do i=1,nbinvars
    573 
    574869
    575870! 3. Finish things and cleanup
     
    582877endif
    583878
     879  write(*,*)"Done writing file ",trim(outfile)
     880
    584881end program expandstartfi
Note: See TracChangeset for help on using the changeset viewer.